| 33 | |
| 34 | class Combined { |
| 35 | constructor(svg, { |
| 36 | title, xLabel, yLabel, data: { labels, datasets }, options, |
| 37 | }) { |
| 38 | this.options = { |
| 39 | unxkcdify: false, |
| 40 | yTickCount: 3, |
| 41 | legendPosition: config.positionType.upLeft, |
| 42 | dataColors: colors, |
| 43 | fontFamily: 'xkcd', |
| 44 | strokeColor: 'black', |
| 45 | backgroundColor: 'white', |
| 46 | showLegend: true, |
| 47 | ...options, |
| 48 | }; |
| 49 | if (title) { |
| 50 | this.title = title; |
| 51 | margin.top = 60; |
| 52 | } |
| 53 | if (xLabel) { |
| 54 | this.xLabel = xLabel; |
| 55 | margin.bottom = 50; |
| 56 | } |
| 57 | if (yLabel) { |
| 58 | this.yLabel = yLabel; |
| 59 | margin.left = 70; |
| 60 | } |
| 61 | this.data = { |
| 62 | labels, |
| 63 | datasets, |
| 64 | }; |
| 65 | this.filter = 'url(#xkcdify)'; |
| 66 | this.fontFamily = this.options.fontFamily || 'xkcd'; |
| 67 | if (this.options.unxkcdify) { |
| 68 | this.filter = null; |
| 69 | this.fontFamily = '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif'; |
| 70 | } |
| 71 | |
| 72 | this.svgEl = select(svg) |
| 73 | .style('stroke-width', '3') |
| 74 | .style('font-family', this.fontFamily) |
| 75 | .style('background', this.options.backgroundColor) |
| 76 | .attr('width', svg.parentElement.clientWidth) |
| 77 | .attr('height', Math.min((svg.parentElement.clientWidth * 2) / 3, window.innerHeight)); |
| 78 | this.svgEl.selectAll('*').remove(); |
| 79 | |
| 80 | this.chart = this.svgEl.append('g') |
| 81 | .attr('transform', |
| 82 | `translate(${margin.left},${margin.top})`); |
| 83 | this.width = this.svgEl.attr('width') - margin.left - margin.right; |
| 84 | this.height = this.svgEl.attr('height') - margin.top - margin.bottom; |
| 85 | |
| 86 | addFont(this.svgEl); |
| 87 | addFilter(this.svgEl); |
| 88 | this.render(); |
| 89 | } |
| 90 | |
| 91 | render() { |
| 92 | if (this.title) addLabels.title(this.svgEl, this.title, this.options.strokeColor); |