-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
vertical-bar-chart.js
144 lines (125 loc) · 4.69 KB
/
vertical-bar-chart.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
import { max } from 'd3-array';
// eslint-disable-next-line no-unused-vars
import { select, selectAll, node } from 'd3-selection';
import { axisLeft, axisBottom } from 'd3-axis';
import { scaleLinear, scaleBand } from 'd3-scale';
import { stack } from 'd3-shape';
import {
GREY,
LIGHT_AND_DARK_BLUE,
SVG_DIMENSIONS,
TRANSLATE,
formatNumbers,
} from '../../utils/chart-helpers';
/**
* @module VerticalBarChart
* VerticalBarChart components are used to display stacked data in a vertical bar chart with accompanying tooltip
*
* @example
* ```js
* <VerticalBarChart @dataset={dataset} @chartLegend={chartLegend} />
* ```
* @param {array} dataset - dataset for the chart, must be an array of flattened objects
* @param {array} chartLegend - array of objects with key names 'key' and 'label' so data can be stacked
* @param {string} xKey - string denoting key for x-axis data (data[xKey]) of dataset
* @param {string} yKey - string denoting key for y-axis data (data[yKey]) of dataset
*/
export default class VerticalBarChart extends Component {
@tracked tooltipTarget = '';
@tracked tooltipTotal = '';
@tracked entityClients = '';
@tracked nonEntityClients = '';
get chartLegend() {
return this.args.chartLegend;
}
get xKey() {
return this.args.xKey || 'month';
}
get yKey() {
return this.args.yKey || 'clients';
}
@action
registerListener(element, args) {
let dataset = args[0];
let stackFunction = stack().keys(this.chartLegend.map((l) => l.key));
let stackedData = stackFunction(dataset);
let chartSvg = select(element);
chartSvg.attr('viewBox', `-50 20 600 ${SVG_DIMENSIONS.height}`); // set svg dimensions
// DEFINE DATA BAR SCALES
let yScale = scaleLinear()
.domain([0, max(dataset.map((d) => d[this.yKey]))])
.range([0, 100])
.nice();
let xScale = scaleBand()
.domain(dataset.map((d) => d[this.xKey]))
.range([0, SVG_DIMENSIONS.width]) // set width to fix number of pixels
.paddingInner(0.85);
let dataBars = chartSvg
.selectAll('g')
.data(stackedData)
.enter()
.append('g')
.style('fill', (d, i) => LIGHT_AND_DARK_BLUE[i]);
dataBars
.selectAll('rect')
.data((stackedData) => stackedData)
.enter()
.append('rect')
.attr('width', '7px')
.attr('class', 'data-bar')
.attr('height', (stackedData) => `${yScale(stackedData[1] - stackedData[0])}%`)
.attr('x', ({ data }) => xScale(data[this.xKey])) // uses destructuring because was data.data.month
.attr('y', (data) => `${100 - yScale(data[1])}%`); // subtract higher than 100% to give space for x axis ticks
// MAKE AXES //
let yAxisScale = scaleLinear()
.domain([0, max(dataset.map((d) => d[this.yKey]))])
.range([`${SVG_DIMENSIONS.height}`, 0])
.nice();
let yAxis = axisLeft(yAxisScale)
.ticks(4)
.tickPadding(10)
.tickSizeInner(-SVG_DIMENSIONS.width)
.tickFormat(formatNumbers);
let xAxis = axisBottom(xScale).tickSize(0);
yAxis(chartSvg.append('g'));
xAxis(chartSvg.append('g').attr('transform', `translate(0, ${SVG_DIMENSIONS.height + 10})`));
chartSvg.selectAll('.domain').remove(); // remove domain lines
// WIDER SELECTION AREA FOR TOOLTIP HOVER
let greyBars = chartSvg
.append('g')
.attr('transform', `translate(${TRANSLATE.left})`)
.style('fill', `${GREY}`)
.style('opacity', '0')
.style('mix-blend-mode', 'multiply');
let tooltipRect = greyBars
.selectAll('rect')
.data(dataset)
.enter()
.append('rect')
.style('cursor', 'pointer')
.attr('class', 'tooltip-rect')
.attr('height', '100%')
.attr('width', '30px') // three times width
.attr('y', '0') // start at bottom
.attr('x', (data) => xScale(data[this.xKey])); // not data.data because this is not stacked data
// MOUSE EVENT FOR TOOLTIP
tooltipRect.on('mouseover', (data) => {
let hoveredMonth = data[this.xKey];
this.tooltipTotal = `${data[this.yKey]} ${data.new_clients ? 'total' : 'new'} clients`;
this.entityClients = `${data.entity_clients} entity clients`;
this.nonEntityClients = `${data.non_entity_clients} non-entity clients`;
let node = chartSvg
.selectAll('rect.data-bar')
// filter for the top data bar (so y-coord !== 0) with matching month
.filter((data) => data[0] !== 0 && data.data.month === hoveredMonth)
.node();
this.tooltipTarget = node; // grab the node from the list of rects
});
}
@action removeTooltip() {
this.tooltipTarget = null;
}
}