|
| 1 | +import './StackedAreaChart.less'; |
| 2 | +import * as d3 from 'd3'; |
| 3 | +import { stackedArea } from 'britecharts'; |
| 4 | +import { useEffect, useMemo } from 'react'; |
| 5 | +import { useSelector, useDispatch } from 'react-redux'; |
| 6 | +import * as colors from '../../../constants/colors'; |
| 7 | +import { |
| 8 | + getLastDate, |
| 9 | + pruneIncompleteStackedAreaInterval, |
| 10 | + isStackedAreaDataEmpty, |
| 11 | +} from '../../../utils/chart'; |
| 12 | +import { updateTrendsTooltip } from '../../../actions/trends'; |
| 13 | +import { debounce } from '../../../utils'; |
| 14 | +import { |
| 15 | + selectTrendsResultsDateRangeArea, |
| 16 | + selectTrendsColorMap, |
| 17 | +} from '../../../reducers/trends/selectors'; |
| 18 | +import { |
| 19 | + selectQueryDateReceivedMin, |
| 20 | + selectQueryDateReceivedMax, |
| 21 | + selectQueryDateInterval, |
| 22 | + selectQueryLens, |
| 23 | +} from '../../../reducers/query/selectors'; |
| 24 | +import { selectViewIsPrintMode } from '../../../reducers/view/selectors'; |
| 25 | +import { ChartWrapper } from '../ChartWrapper/ChartWrapper'; |
| 26 | + |
| 27 | +export const StackedAreaChart = () => { |
| 28 | + const dispatch = useDispatch(); |
| 29 | + |
| 30 | + const colorMap = useSelector(selectTrendsColorMap); |
| 31 | + const data = useSelector(selectTrendsResultsDateRangeArea); |
| 32 | + const from = useSelector(selectQueryDateReceivedMin); |
| 33 | + const to = useSelector(selectQueryDateReceivedMax); |
| 34 | + const lens = useSelector(selectQueryLens); |
| 35 | + const interval = useSelector(selectQueryDateInterval); |
| 36 | + const isPrintMode = useSelector(selectViewIsPrintMode); |
| 37 | + |
| 38 | + const showTooltip = lens !== 'Overview'; |
| 39 | + |
| 40 | + const filteredData = useMemo(() => { |
| 41 | + const dateRange = { from, to }; |
| 42 | + return pruneIncompleteStackedAreaInterval(data, dateRange, interval); |
| 43 | + }, [data, from, to, interval]); |
| 44 | + |
| 45 | + const isDataEmpty = isStackedAreaDataEmpty(filteredData); |
| 46 | + |
| 47 | + useEffect(() => { |
| 48 | + const dateRange = { from, to }; |
| 49 | + const chartID = '#stacked-area-chart'; |
| 50 | + const chartSelector = chartID + ' .stacked-area'; |
| 51 | + const container = d3.select(chartID); |
| 52 | + |
| 53 | + if (!container.node() || isDataEmpty) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + const tooltipUpdated = (selectedState) => { |
| 58 | + dispatch(updateTrendsTooltip(selectedState)); |
| 59 | + }; |
| 60 | + |
| 61 | + const updateTooltip = (point) => { |
| 62 | + tooltipUpdated({ |
| 63 | + date: point.date, |
| 64 | + dateRange, |
| 65 | + interval, |
| 66 | + values: point.values, |
| 67 | + }); |
| 68 | + }; |
| 69 | + |
| 70 | + d3.select(chartSelector).remove(); |
| 71 | + |
| 72 | + const width = isPrintMode |
| 73 | + ? 550 |
| 74 | + : container.node().getBoundingClientRect().width; |
| 75 | + |
| 76 | + const colorData = filteredData.filter((item) => item.name !== 'Other'); |
| 77 | + const colorScheme = [...new Set(colorData.map((item) => item.name))].map( |
| 78 | + (obj) => colorMap[obj], |
| 79 | + ); |
| 80 | + colorScheme.push(colors.DataLens[10]); |
| 81 | + |
| 82 | + const stackedAreaChart = stackedArea(); |
| 83 | + |
| 84 | + stackedAreaChart |
| 85 | + .margin({ left: 70, right: 10, top: 10, bottom: 40 }) |
| 86 | + .areaCurve('linear') |
| 87 | + .initializeVerticalMarker(true) |
| 88 | + .isAnimated(false) |
| 89 | + .tooltipThreshold(1) |
| 90 | + .grid('horizontal') |
| 91 | + .aspectRatio(0.5) |
| 92 | + .width(width) |
| 93 | + .dateLabel('date') |
| 94 | + .colorSchema(colorScheme) |
| 95 | + .on('customMouseMove', debounce(updateTooltip, 200)); |
| 96 | + |
| 97 | + container.datum(filteredData).call(stackedAreaChart); |
| 98 | + |
| 99 | + const config = { |
| 100 | + dateRange, |
| 101 | + interval, |
| 102 | + }; |
| 103 | + |
| 104 | + tooltipUpdated(getLastDate(filteredData, config)); |
| 105 | + |
| 106 | + return () => { |
| 107 | + d3.select(chartSelector).remove(); |
| 108 | + container.datum([]); |
| 109 | + }; |
| 110 | + }, [ |
| 111 | + colorMap, |
| 112 | + from, |
| 113 | + to, |
| 114 | + dispatch, |
| 115 | + filteredData, |
| 116 | + interval, |
| 117 | + isPrintMode, |
| 118 | + isDataEmpty, |
| 119 | + ]); |
| 120 | + |
| 121 | + return ( |
| 122 | + <ChartWrapper |
| 123 | + hasKey={showTooltip} |
| 124 | + domId="stacked-area-chart" |
| 125 | + isEmpty={isDataEmpty} |
| 126 | + /> |
| 127 | + ); |
| 128 | +}; |
0 commit comments