MCPcopy
hub / github.com/timqian/chart.xkcd / constructor

Method constructor

src/XY.js:22–81  ·  view source on GitHub ↗
(svg, {
    title, xLabel, yLabel, data: { datasets }, options,
  })

Source from the content-addressed store, hash-verified

20
21class XY {
22 constructor(svg, {
23 title, xLabel, yLabel, data: { datasets }, options,
24 }) {
25 this.options = {
26 unxkcdify: false,
27 dotSize: 1,
28 showLine: false,
29 timeFormat: '',
30 xTickCount: 3,
31 yTickCount: 3,
32 legendPosition: config.positionType.upLeft,
33 dataColors: colors,
34 fontFamily: 'xkcd',
35 strokeColor: 'black',
36 backgroundColor: 'white',
37 showLegend: true,
38 ...options,
39 };
40 // TODO: extract a function?
41 if (title) {
42 this.title = title;
43 margin.top = 60;
44 }
45 if (xLabel) {
46 this.xLabel = xLabel;
47 margin.bottom = 50;
48 }
49 if (yLabel) {
50 this.yLabel = yLabel;
51 margin.left = 70;
52 }
53 this.data = {
54 datasets,
55 };
56
57 this.filter = 'url(#xkcdify)';
58 this.fontFamily = this.options.fontFamily || 'xkcd';
59 if (this.options.unxkcdify) {
60 this.filter = null;
61 this.fontFamily = '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif';
62 }
63
64 this.svgEl = select(svg)
65 .style('stroke-width', 3)
66 .style('font-family', this.fontFamily)
67 .style('background', this.options.backgroundColor)
68 .attr('width', svg.parentElement.clientWidth)
69 .attr('height', Math.min((svg.parentElement.clientWidth * 2) / 3, window.innerHeight));
70 this.svgEl.selectAll('*').remove();
71
72 this.chart = this.svgEl.append('g')
73 .attr('transform',
74 `translate(${margin.left},${margin.top})`);
75
76 this.width = this.svgEl.attr('width') - margin.left - margin.right;
77 this.height = this.svgEl.attr('height') - margin.top - margin.bottom;
78 addFont(this.svgEl);
79 addFilter(this.svgEl);

Callers

nothing calls this directly

Calls 3

renderMethod · 0.95
addFontFunction · 0.85
addFilterFunction · 0.85

Tested by

no test coverage detected