@cdc/chart 4.22.10 → 4.23.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/dist/495.js +3 -0
- package/dist/703.js +1 -0
- package/dist/cdcchart.js +723 -6
- package/examples/age-adjusted-rates.json +1486 -1218
- package/examples/box-plot-data.json +71 -0
- package/examples/box-plot.csv +5 -0
- package/examples/{private/yaxis-test.json → box-plot.json} +46 -54
- package/examples/case-rate-example-config.json +1 -1
- package/examples/covid-confidence-example-config.json +33 -33
- package/examples/covid-example-config.json +34 -34
- package/examples/covid-example-data-confidence.json +30 -30
- package/examples/covid-example-data.json +20 -20
- package/examples/cutoff-example-config.json +36 -36
- package/examples/cutoff-example-data.json +36 -36
- package/examples/date-exclusions-config.json +1 -1
- package/examples/dynamic-legends.json +124 -124
- package/examples/gallery/bar-chart-horizontal/horizontal-bar-chart-with-numbers-on-bar.json +191 -197
- package/examples/gallery/bar-chart-horizontal/horizontal-bar-chart.json +230 -240
- package/examples/gallery/bar-chart-horizontal/horizontal-stacked.json +239 -247
- package/examples/gallery/bar-chart-vertical/combo-line-chart.json +138 -136
- package/examples/gallery/bar-chart-vertical/vertical-bar-chart-categorical.json +79 -79
- package/examples/gallery/bar-chart-vertical/vertical-bar-chart-stacked.json +80 -80
- package/examples/gallery/bar-chart-vertical/vertical-bar-chart-with-confidence.json +67 -67
- package/examples/gallery/bar-chart-vertical/vertical-bar-chart.json +179 -110
- package/examples/gallery/lollipop/lollipop-style-horizontal.json +215 -219
- package/examples/gallery/paired-bar/paired-bar-chart.json +195 -195
- package/examples/horizontal-chart.json +35 -35
- package/examples/horizontal-stacked-bar-chart.json +34 -34
- package/examples/line-chart.json +75 -75
- package/examples/new-data.csv +17 -0
- package/examples/newdata.json +90 -0
- package/examples/paired-bar-data.json +16 -14
- package/examples/paired-bar-example.json +48 -48
- package/examples/paired-bar-formatted.json +36 -36
- package/examples/planet-chart-horizontal-example-config.json +33 -33
- package/examples/planet-combo-example-config.json +34 -31
- package/examples/planet-example-config.json +35 -33
- package/examples/planet-example-data.json +56 -56
- package/examples/planet-pie-example-config.json +28 -28
- package/examples/stacked-vertical-bar-example.json +1 -1
- package/examples/temp-example-config.json +61 -54
- package/examples/temp-example-data.json +1 -1
- package/package.json +3 -2
- package/src/CdcChart.tsx +449 -434
- package/src/components/BarChart.tsx +383 -497
- package/src/components/BoxPlot.js +92 -0
- package/src/components/DataTable.tsx +182 -197
- package/src/components/EditorPanel.js +1068 -722
- package/src/components/Filters.js +131 -0
- package/src/components/Legend.js +286 -329
- package/src/components/LineChart.tsx +143 -81
- package/src/components/LinearChart.tsx +432 -451
- package/src/components/PairedBarChart.tsx +197 -213
- package/src/components/PieChart.tsx +105 -151
- package/src/components/SparkLine.js +179 -201
- package/src/components/useIntersectionObserver.tsx +19 -20
- package/src/context.tsx +3 -3
- package/src/data/initial-state.js +44 -17
- package/src/hooks/useActiveElement.js +13 -13
- package/src/hooks/useChartClasses.js +34 -28
- package/src/hooks/useColorPalette.ts +56 -63
- package/src/hooks/useLegendClasses.js +18 -10
- package/src/hooks/useReduceData.ts +64 -77
- package/src/hooks/useRightAxis.js +25 -0
- package/src/hooks/useTopAxis.js +6 -0
- package/src/index.html +19 -19
- package/src/index.tsx +13 -16
- package/src/scss/DataTable.scss +6 -5
- package/src/scss/editor-panel.scss +71 -69
- package/src/scss/main.scss +188 -114
- package/src/scss/variables.scss +1 -1
- package/examples/private/line-test-data.json +0 -22
- package/examples/private/line-test-two.json +0 -216
- package/examples/private/line-test.json +0 -102
- package/examples/private/newtest.csv +0 -101
- package/examples/private/shawn.json +0 -1296
- package/examples/private/test.json +0 -10124
- package/examples/private/yaxis-testing.csv +0 -27
- package/examples/private/yaxis.json +0 -28
|
@@ -1,241 +1,225 @@
|
|
|
1
|
-
import React, { useContext } from 'react'
|
|
2
|
-
import { Group } from '@visx/group'
|
|
3
|
-
import { Bar } from '@visx/shape'
|
|
4
|
-
import { scaleLinear, scaleBand } from '@visx/scale'
|
|
5
|
-
import { Text } from '@visx/text'
|
|
6
|
-
|
|
7
|
-
import Context from '../context';
|
|
8
|
-
import chroma from 'chroma-js';
|
|
1
|
+
import React, { useContext } from 'react'
|
|
2
|
+
import { Group } from '@visx/group'
|
|
3
|
+
import { Bar } from '@visx/shape'
|
|
4
|
+
import { scaleLinear, scaleBand } from '@visx/scale'
|
|
5
|
+
import { Text } from '@visx/text'
|
|
9
6
|
|
|
7
|
+
import Context from '../context'
|
|
8
|
+
import chroma from 'chroma-js'
|
|
10
9
|
|
|
11
10
|
interface PairedBarChartProps {
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
width: number
|
|
12
|
+
height: number
|
|
14
13
|
}
|
|
15
14
|
|
|
16
15
|
const PairedBarChart: React.FC<PairedBarChartProps> = ({ width, height }) => {
|
|
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
|
-
|
|
16
|
+
const { config, colorScale, transformedData, formatNumber, seriesHighlight } = useContext<any>(Context)
|
|
17
|
+
|
|
18
|
+
if (!config || config?.series?.length < 2) return
|
|
19
|
+
|
|
20
|
+
const data = transformedData
|
|
21
|
+
const adjustedWidth = width
|
|
22
|
+
const adjustedHeight = height
|
|
23
|
+
const halfWidth = adjustedWidth / 2
|
|
24
|
+
|
|
25
|
+
const groupOne = {
|
|
26
|
+
parentKey: config.dataDescription.seriesKey,
|
|
27
|
+
dataKey: config.series[0].dataKey,
|
|
28
|
+
color: colorScale(config.runtime.seriesLabels[config.series[0].dataKey]),
|
|
29
|
+
max: Math.max.apply(
|
|
30
|
+
Math,
|
|
31
|
+
data.map(item => item[config.series[0].dataKey])
|
|
32
|
+
),
|
|
33
|
+
labelColor: ''
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const groupTwo = {
|
|
37
|
+
parentKey: config.dataDescription.seriesKey,
|
|
38
|
+
dataKey: config.series[1].dataKey,
|
|
39
|
+
color: colorScale(config.runtime.seriesLabels[config.series[1].dataKey]),
|
|
40
|
+
max: Math.max.apply(
|
|
41
|
+
Math,
|
|
42
|
+
data.map(item => item[config.series[1].dataKey])
|
|
43
|
+
),
|
|
44
|
+
labelColor: ''
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const xScale = scaleLinear({
|
|
48
|
+
domain: [0, Math.max(groupOne.max, groupTwo.max)],
|
|
49
|
+
range: [0, halfWidth]
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
const yScale = scaleBand({
|
|
53
|
+
range: [0, adjustedHeight],
|
|
54
|
+
domain: data.map(d => d[config.dataDescription.xKey])
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
// Set label color
|
|
58
|
+
let labelColor = '#000000'
|
|
59
|
+
|
|
60
|
+
if (chroma.contrast(labelColor, groupOne.color) < 4.9) {
|
|
61
|
+
groupOne.labelColor = '#FFFFFF'
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (chroma.contrast(labelColor, groupTwo.color) < 4.9) {
|
|
65
|
+
groupTwo.labelColor = '#FFFFFF'
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const dataTipOne = d => {
|
|
69
|
+
return `<p>
|
|
68
70
|
${config.dataDescription.seriesKey}: ${groupOne.dataKey}<br/>
|
|
69
71
|
${config.xAxis.dataKey}: ${d[config.xAxis.dataKey]}<br/>
|
|
70
72
|
${config.dataDescription.valueKey}: ${formatNumber(d[groupOne.dataKey])}
|
|
71
73
|
</p>`
|
|
72
|
-
|
|
73
|
-
}
|
|
74
|
+
}
|
|
74
75
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
`<p>
|
|
76
|
+
const dataTipTwo = d => {
|
|
77
|
+
return `<p>
|
|
78
78
|
${config.dataDescription.seriesKey}: ${groupTwo.dataKey}<br/>
|
|
79
79
|
${config.xAxis.dataKey}: ${d[config.xAxis.dataKey]}<br/>
|
|
80
80
|
${config.dataDescription.valueKey}: ${formatNumber(d[groupTwo.dataKey])}
|
|
81
81
|
</p>`
|
|
82
|
-
|
|
83
|
-
}
|
|
82
|
+
}
|
|
84
83
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
84
|
+
const isLabelBelowBar = config.yAxis.labelPlacement === 'Below Bar'
|
|
85
|
+
const isLabelOnYAxis = config.yAxis.labelPlacement === 'On Date/Category Axis'
|
|
86
|
+
const isLabelMissing = !config.yAxis.labelPlacement
|
|
88
87
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
88
|
+
return (
|
|
89
|
+
width > 0 && (
|
|
90
|
+
<>
|
|
91
|
+
<style>
|
|
92
|
+
{`
|
|
93
93
|
#cdc-visualization__paired-bar-chart,
|
|
94
94
|
#cdc-visualization__paired-bar-chart > .visx-group {
|
|
95
95
|
transform-origin: center
|
|
96
96
|
}
|
|
97
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
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
config.barPadding = barPadding;
|
|
188
|
-
}
|
|
189
|
-
} else {
|
|
190
|
-
config.barPadding = barPadding / 2;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
return(
|
|
195
|
-
<>
|
|
196
|
-
<style>
|
|
197
|
-
{`
|
|
98
|
+
</style>
|
|
99
|
+
<svg id='cdc-visualization__paired-bar-chart' width={width} height={height} viewBox={`0 0 ${width} ${height}`} role='img' tabIndex={0}>
|
|
100
|
+
<Group top={0} left={Number(config.xAxis.size)}>
|
|
101
|
+
{data
|
|
102
|
+
.filter(item => config.series[0].dataKey === groupOne.dataKey)
|
|
103
|
+
.map((d, index) => {
|
|
104
|
+
let transparentBar = config.legend.behavior === 'highlight' && seriesHighlight.length > 0 && seriesHighlight.indexOf(config.series[0].dataKey) === -1
|
|
105
|
+
let displayBar = config.legend.behavior === 'highlight' || seriesHighlight.length === 0 || seriesHighlight.indexOf(config.series[0].dataKey) !== -1
|
|
106
|
+
let barWidth = xScale(d[config.series[0].dataKey])
|
|
107
|
+
let barHeight = Number(config.barHeight) ? Number(config.barHeight) : 25
|
|
108
|
+
let barPadding = barHeight
|
|
109
|
+
config.barHeight = Number(config.barHeight) ? Number(config.barHeight) : 25
|
|
110
|
+
config.barPadding = config.barHeight
|
|
111
|
+
|
|
112
|
+
if (config.orientation === 'horizontal') {
|
|
113
|
+
if (isLabelBelowBar || isLabelMissing || isLabelOnYAxis) {
|
|
114
|
+
if (barHeight < 40) {
|
|
115
|
+
config.barPadding = 40
|
|
116
|
+
} else {
|
|
117
|
+
config.barPadding = barPadding
|
|
118
|
+
}
|
|
119
|
+
} else {
|
|
120
|
+
config.barPadding = barPadding / 2
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
config.height = Number(barHeight) * data.length + config.barPadding * data.length
|
|
125
|
+
|
|
126
|
+
let y = yScale([d[config.dataDescription.xKey]]) + config.barHeight / 1.5
|
|
127
|
+
y = Number(config.barPadding) > 20 ? (y += Number(config.barPadding / 3.5) - config.barHeight / 2) : (y += 0)
|
|
128
|
+
|
|
129
|
+
return (
|
|
130
|
+
<>
|
|
131
|
+
<Group key={`group-${groupOne.dataKey}-${d[config.xAxis.dataKey]}`} className='horizontal'>
|
|
132
|
+
<Bar
|
|
133
|
+
id={`bar-${groupOne.dataKey}-${d[config.dataDescription.xKey]}`}
|
|
134
|
+
className='bar group-1'
|
|
135
|
+
key={`bar-${groupOne.dataKey}-${d[config.dataDescription.xKey]}`}
|
|
136
|
+
x={halfWidth - barWidth}
|
|
137
|
+
y={y}
|
|
138
|
+
width={xScale(d[config.series[0].dataKey])}
|
|
139
|
+
height={barHeight}
|
|
140
|
+
fill={groupOne.color}
|
|
141
|
+
data-tip={dataTipOne(d)}
|
|
142
|
+
data-for={`cdc-open-viz-tooltip-${config.runtime.uniqueId}`}
|
|
143
|
+
stroke='#333'
|
|
144
|
+
strokeWidth={config.barBorderThickness || 1}
|
|
145
|
+
opacity={transparentBar ? 0.5 : 1}
|
|
146
|
+
display={displayBar ? 'block' : 'none'}
|
|
147
|
+
/>
|
|
148
|
+
{config.yAxis.displayNumbersOnBar && displayBar && (
|
|
149
|
+
<Text textAnchor={barWidth < 100 ? 'end' : 'start'} verticalAnchor='middle' x={halfWidth - (barWidth < 100 ? barWidth + 10 : barWidth - 5)} y={y + config.barHeight / 2} fill={barWidth > 100 ? groupOne.labelColor : '#000'}>
|
|
150
|
+
{formatNumber(d[groupOne.dataKey])}
|
|
151
|
+
</Text>
|
|
152
|
+
)}
|
|
153
|
+
</Group>
|
|
154
|
+
</>
|
|
155
|
+
)
|
|
156
|
+
})}
|
|
157
|
+
{data
|
|
158
|
+
.filter(item => config.series[1].dataKey === groupTwo.dataKey)
|
|
159
|
+
.map(d => {
|
|
160
|
+
let barWidth = xScale(d[config.series[1].dataKey])
|
|
161
|
+
let transparentBar = config.legend.behavior === 'highlight' && seriesHighlight.length > 0 && seriesHighlight.indexOf(config.series[1].dataKey) === -1
|
|
162
|
+
let displayBar = config.legend.behavior === 'highlight' || seriesHighlight.length === 0 || seriesHighlight.indexOf(config.series[1].dataKey) !== -1
|
|
163
|
+
|
|
164
|
+
let barHeight = config.barHeight ? config.barHeight : 25
|
|
165
|
+
let barPadding = barHeight
|
|
166
|
+
config.barHeight = Number(config.barHeight)
|
|
167
|
+
|
|
168
|
+
let y = yScale([d[config.dataDescription.xKey]]) + config.barHeight / 1.5
|
|
169
|
+
y = Number(config.barPadding) > 20 ? (y += Number(config.barPadding / 3.5) - config.barHeight / 2) : (y += 0)
|
|
170
|
+
|
|
171
|
+
if (config.orientation === 'horizontal') {
|
|
172
|
+
if (isLabelBelowBar || isLabelMissing || isLabelOnYAxis) {
|
|
173
|
+
if (barHeight < 40) {
|
|
174
|
+
config.barPadding = 40
|
|
175
|
+
} else {
|
|
176
|
+
config.barPadding = barPadding
|
|
177
|
+
}
|
|
178
|
+
} else {
|
|
179
|
+
config.barPadding = barPadding / 2
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return (
|
|
184
|
+
<>
|
|
185
|
+
<style>
|
|
186
|
+
{`
|
|
198
187
|
.bar-${groupTwo.dataKey}-${d[config.xAxis.dataKey]} {
|
|
199
188
|
transform-origin: ${halfWidth}px ${y}px
|
|
200
189
|
}
|
|
201
190
|
`}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
)}
|
|
235
|
-
</Group>
|
|
236
|
-
</svg>
|
|
237
|
-
</>
|
|
238
|
-
);
|
|
191
|
+
</style>
|
|
192
|
+
<Group key={`group-${groupTwo.dataKey}-${d[config.dataDescription.xKey]}`} className='horizontal'>
|
|
193
|
+
<Bar
|
|
194
|
+
id={`bar-${groupTwo.dataKey}-${d[config.dataDescription.xKey]}`}
|
|
195
|
+
className='bar group-2'
|
|
196
|
+
key={`bar-${groupTwo.dataKey}-${d[config.dataDescription.xKey]}`}
|
|
197
|
+
x={halfWidth}
|
|
198
|
+
y={y}
|
|
199
|
+
width={xScale(d[config.series[1].dataKey])}
|
|
200
|
+
height={barHeight}
|
|
201
|
+
fill={groupTwo.color}
|
|
202
|
+
data-tip={dataTipTwo(d)}
|
|
203
|
+
data-for={`cdc-open-viz-tooltip-${config.runtime.uniqueId}`}
|
|
204
|
+
strokeWidth={config.barBorderThickness || 1}
|
|
205
|
+
stroke='#333'
|
|
206
|
+
opacity={transparentBar ? 0.5 : 1}
|
|
207
|
+
display={displayBar ? 'block' : 'none'}
|
|
208
|
+
/>
|
|
209
|
+
{config.yAxis.displayNumbersOnBar && displayBar && (
|
|
210
|
+
<Text textAnchor={barWidth < 100 ? 'start' : 'end'} verticalAnchor='middle' x={halfWidth + (barWidth < 100 ? barWidth + 10 : barWidth - 10)} y={y + config.barHeight / 2} fill={barWidth > 100 ? groupTwo.labelColor : '#000'}>
|
|
211
|
+
{formatNumber(d[groupTwo.dataKey])}
|
|
212
|
+
</Text>
|
|
213
|
+
)}
|
|
214
|
+
</Group>
|
|
215
|
+
</>
|
|
216
|
+
)
|
|
217
|
+
})}
|
|
218
|
+
</Group>
|
|
219
|
+
</svg>
|
|
220
|
+
</>
|
|
221
|
+
)
|
|
222
|
+
)
|
|
239
223
|
}
|
|
240
224
|
|
|
241
|
-
export default PairedBarChart
|
|
225
|
+
export default PairedBarChart
|