@cdc/chart 4.23.8 → 4.23.10

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.
Files changed (34) hide show
  1. package/dist/cdcchart.js +44114 -44410
  2. package/examples/feature/__data__/area-chart-date-apple.json +1 -5073
  3. package/examples/feature/area/area-chart-date-apple.json +73 -10316
  4. package/examples/feature/area/area-chart-date-city-temperature.json +204 -80
  5. package/examples/{private/confidence_interval_test.json → feature/area/area-chart-stacked.json} +65 -74
  6. package/examples/feature/filters/bar-filter.json +5027 -0
  7. package/examples/feature/legend-highlights/highlights.json +567 -0
  8. package/index.html +28 -7
  9. package/package.json +3 -2
  10. package/src/{CdcChart.jsx → CdcChart.tsx} +77 -71
  11. package/src/components/AreaChart.Stacked.jsx +73 -0
  12. package/src/components/AreaChart.jsx +24 -26
  13. package/src/components/BarChart.StackedVertical.jsx +2 -0
  14. package/src/components/DeviationBar.jsx +67 -13
  15. package/src/components/EditorPanel.jsx +493 -454
  16. package/src/components/Forecasting.jsx +5 -5
  17. package/src/components/Legend.jsx +17 -8
  18. package/src/components/LineChart.Circle.tsx +108 -0
  19. package/src/components/{LineChart.jsx → LineChart.tsx} +10 -42
  20. package/src/components/LinearChart.jsx +460 -443
  21. package/src/components/PieChart.jsx +54 -25
  22. package/src/components/Series.jsx +63 -17
  23. package/src/components/SparkLine.jsx +7 -19
  24. package/src/data/initial-state.js +10 -1
  25. package/src/hooks/useEditorPermissions.js +87 -24
  26. package/src/hooks/useLegendClasses.js +14 -11
  27. package/src/hooks/useReduceData.js +6 -1
  28. package/src/hooks/useScales.js +2 -2
  29. package/src/hooks/useTooltip.jsx +21 -8
  30. package/src/scss/legend.scss +206 -0
  31. package/src/scss/main.scss +25 -24
  32. package/examples/private/tooltip-issue.json +0 -45275
  33. package/src/components/DataTable.jsx +0 -374
  34. /package/src/{components → hooks}/useIntersectionObserver.jsx +0 -0
@@ -1,18 +1,17 @@
1
1
  import { Line } from '@visx/shape'
2
2
  import { Group } from '@visx/group'
3
- import { useContext, useEffect } from 'react'
3
+ import { useContext, useEffect, useRef, useState } from 'react'
4
4
  import ConfigContext from '../ConfigContext'
5
5
  import { Text } from '@visx/text'
6
6
  import ErrorBoundary from '@cdc/core/components/ErrorBoundary'
7
7
  import chroma from 'chroma-js'
8
+ import useIntersectionObserver from '../hooks/useIntersectionObserver'
8
9
 
9
10
  export default function DeviationBar({ height, xScale }) {
10
- const { transformedData: data, config, formatNumber, twoColorPalette, getTextWidth, updateConfig, parseDate, formatDate } = useContext(ConfigContext)
11
-
12
- if (!config || config?.series?.length !== 1 || config.orientation !== 'horizontal') return
13
-
11
+ const { transformedData: data, config, formatNumber, twoColorPalette, getTextWidth, updateConfig, parseDate, formatDate, currentViewport } = useContext(ConfigContext)
14
12
  const { barStyle, tipRounding, roundingStyle, twoColor } = config
15
-
13
+ const barRefs = useRef([])
14
+ const [windowWidth, setWindowWidth] = useState(window.innerWidth)
16
15
  const radius = roundingStyle === 'standard' ? '8px' : roundingStyle === 'shallow' ? '5px' : roundingStyle === 'finger' ? '15px' : '0px'
17
16
  const fontSize = { small: 16, medium: 18, large: 20 }
18
17
  const isRounded = config.barStyle === 'rounded'
@@ -76,14 +75,57 @@ export default function DeviationBar({ height, xScale }) {
76
75
  }
77
76
  targetLabel.calculate()
78
77
 
78
+ const targetRef = useRef(null)
79
+
80
+ const entry = useIntersectionObserver(targetRef, {})
81
+
79
82
  useEffect(() => {
80
- if (config.barStyle === 'lollipop' && !config.isLollipopChart) {
81
- updateConfig({ ...config, isLollipopChart: true })
83
+ const handleResize = () => {
84
+ setWindowWidth(window.innerWidth)
85
+ barRefs.current.forEach(bar => {
86
+ bar.style.transition = 'none'
87
+ bar.style.transform = 'translate(0) scale(1)'
88
+ })
82
89
  }
83
- if (isRounded || config.barStyle === 'flat') {
84
- updateConfig({ ...config, isLollipopChart: false })
90
+ window.addEventListener('resize', handleResize)
91
+
92
+ return () => {
93
+ window.removeEventListener('resize', handleResize)
85
94
  }
86
- }, [config.barStyle])
95
+ }, [])
96
+ const [animatedChart, setAnimatedChart] = useState(false)
97
+
98
+ useEffect(() => {
99
+ if (entry?.isIntersecting) {
100
+ setTimeout(() => {
101
+ setAnimatedChart(true)
102
+ }, 100)
103
+ }
104
+ }, [entry?.isIntersecting, config.animate]) // eslint-disable-line
105
+
106
+ useEffect(() => {
107
+ barRefs.current.forEach((bar, i) => {
108
+ if (config.animate) {
109
+ const normalizedTarget = (target / maxVal) * 100
110
+ bar.style.opacity = '0'
111
+ bar.style.transform = `translate(${normalizedTarget / 1.07}%) scale(0, 1)`
112
+ setTimeout(() => {
113
+ bar.style.opacity = '1'
114
+ bar.style.transform = 'translate(0) scale(1)'
115
+ bar.style.transition = 'transform 0.5s ease'
116
+ }, 100)
117
+ } else {
118
+ bar.style.transition = 'none'
119
+ bar.style.opacity = '0'
120
+ }
121
+ if (!config.animate) {
122
+ bar.style.transition = 'none'
123
+ bar.style.opacity = '1'
124
+ }
125
+ })
126
+ }, [config.animate, config, animatedChart])
127
+
128
+ if (!config || config?.series?.length !== 1) return <></>
87
129
 
88
130
  return (
89
131
  <ErrorBoundary component='Deviation Bar'>
@@ -122,7 +164,6 @@ export default function DeviationBar({ height, xScale }) {
122
164
  const fill = isBarColorDark ? '#FFFFFF' : '#000000'
123
165
 
124
166
  let textProps = getTextProps(config.isLollipopChart, textFits, lollipopShapeSize, fill)
125
-
126
167
  // tooltips
127
168
  const xAxisValue = formatNumber(barValue, 'left')
128
169
  const yAxisValue = config.runtime.yAxis.type === 'date' ? formatDate(parseDate(data[index][config.runtime.originalXAxis.dataKey])) : data[index][config.runtime.originalXAxis.dataKey]
@@ -135,7 +176,19 @@ export default function DeviationBar({ height, xScale }) {
135
176
 
136
177
  return (
137
178
  <Group key={`deviation-bar-${config.orientation}-${seriesKey}-${index}`}>
138
- <foreignObject x={barX} y={barY} width={barWidth} height={barHeight} style={{ border: `${borderWidth}px solid #333`, backgroundColor: barColor[barPosition], ...borderRadius }} data-tooltip-html={tooltip} data-tooltip-id={`cdc-open-viz-tooltip-${config.runtime.uniqueId}`} />
179
+ <foreignObject
180
+ ref={el => {
181
+ // targetRef.current = el
182
+ barRefs.current[index] = el
183
+ }}
184
+ x={barX}
185
+ y={barY}
186
+ width={barWidth}
187
+ height={barHeight}
188
+ style={{ border: `${borderWidth}px solid #333`, backgroundColor: barColor[barPosition], ...borderRadius }}
189
+ data-tooltip-html={tooltip}
190
+ data-tooltip-id={`cdc-open-viz-tooltip-${config.runtime.uniqueId}`}
191
+ />
139
192
  {config.yAxis.displayNumbersOnBar && (
140
193
  <Text verticalAnchor='middle' x={textX} y={textY} {...textProps[barPosition]}>
141
194
  {formatNumber(d[seriesKey], 'left')}
@@ -155,6 +208,7 @@ export default function DeviationBar({ height, xScale }) {
155
208
 
156
209
  {shouldShowTargetLine && <Line from={{ x: targetX, y: 0 }} to={{ x: targetX, y: height }} stroke='#333' strokeWidth={2} />}
157
210
  </Group>
211
+ <foreignObject y={height / 2} ref={targetRef}></foreignObject>
158
212
  </ErrorBoundary>
159
213
  )
160
214
  }