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