forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChartLegendInteractive.tsx
More file actions
153 lines (143 loc) · 4.51 KB
/
ChartLegendInteractive.tsx
File metadata and controls
153 lines (143 loc) · 4.51 KB
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
145
146
147
148
149
150
151
152
153
import {
Chart,
ChartArea,
ChartAxis,
ChartGroup,
ChartLegend,
ChartLegendTooltip,
ChartScatter,
ChartThemeColor,
createContainer,
getInteractiveLegendEvents,
getInteractiveLegendItemStyles
} from '@patternfly/react-charts/victory';
import { getResizeObserver } from '@patternfly/react-core';
export const ChartLegendInteractive: React.FunctionComponent = () => {
const containerRef = React.useRef(null);
const [hiddenSeries, setHiddenSeries] = React.useState(new Set());
const [width, setWidth] = React.useState(0);
const series = [
{
datapoints: [
{ x: '2015', y: 3 },
{ x: '2016', y: 4 },
{ x: '2017', y: 8 },
{ x: '2018', y: 6 }
],
legendItem: { name: 'Cats' }
},
{
datapoints: [
{ x: '2015', y: 2 },
{ x: '2016', y: 3 },
{ x: '2017', y: 4 },
{ x: '2018', y: 5 },
{ x: '2019', y: 6 }
],
legendItem: { name: 'Dogs' }
},
{
datapoints: [
{ x: '2015', y: 1 },
{ x: '2016', y: 2 },
{ x: '2017', y: 3 },
{ x: '2018', y: 2 },
{ x: '2019', y: 4 }
],
legendItem: { name: 'Birds' }
}
];
// Returns groups of chart names associated with each data series
const getChartNames = () => series.map((_, index) => [`area-${index}`, `scatter-${index}`]);
// Handles legend click to toggle visibility of data series
const handleLegendClick = (props) => {
setHiddenSeries((prev) => {
const newHidden = new Set(prev);
if (!newHidden.delete(props.index)) {
newHidden.add(props.index);
}
return newHidden;
});
};
// Returns legend data styled per hiddenSeries
const getLegendData = () =>
series.map((s, index) => ({
childName: `area-${index}`,
...s.legendItem,
...getInteractiveLegendItemStyles(hiddenSeries.has(index))
}));
// Returns true if data series is hidden
const isHidden = (index) => hiddenSeries.has(index);
// Checks if any data series is visible
const isDataAvailable = () => hiddenSeries.size !== series.length;
// Set chart width per current window size
useEffect(() => {
const observer = getResizeObserver(containerRef.current, () => {
if (containerRef.current?.clientWidth) {
setWidth(containerRef.current.clientWidth);
}
});
return () => observer();
}, []);
// Note: Container order is important
const CursorVoronoiContainer = createContainer('voronoi', 'cursor');
const container = (
<CursorVoronoiContainer
cursorDimension="x"
labels={({ datum }) => (datum.childName.includes('area-') && datum.y !== null ? `${datum.y}` : null)}
labelComponent={<ChartLegendTooltip legendData={getLegendData()} title={(datum) => datum.x} />}
mouseFollowTooltips
voronoiDimension="x"
voronoiPadding={50}
disable={!isDataAvailable()}
/>
);
return (
<div ref={containerRef}>
<div className="area-chart-legend-bottom-responsive">
<Chart
ariaDesc="Average number of pets"
ariaTitle="Area chart example"
containerComponent={container}
events={getInteractiveLegendEvents({
chartNames: getChartNames(),
isHidden,
legendName: 'chart5-ChartLegend',
onLegendClick: handleLegendClick
})}
height={225}
legendComponent={<ChartLegend name={'chart5-ChartLegend'} data={getLegendData()} />}
legendPosition="bottom-left"
name="chart5"
padding={{ bottom: 75, left: 50, right: 50, top: 50 }}
maxDomain={{ y: 9 }}
themeColor={ChartThemeColor.multiUnordered}
width={width}
>
<ChartAxis tickValues={['2015', '2016', '2017', '2018']} />
<ChartAxis dependentAxis showGrid />
<ChartGroup>
{series.map((s, index) => (
<ChartScatter
key={`scatter-${index}`}
name={`scatter-${index}`}
data={!isHidden(index) ? s.datapoints : [{ y: null }]}
size={({ active }) => (active ? 5 : 3)}
/>
))}
</ChartGroup>
<ChartGroup>
{series.map((s, index) => (
<ChartArea
key={`area-${index}`}
name={`area-${index}`}
data={!isHidden(index) ? s.datapoints : [{ y: null }]}
interpolation="monotoneX"
/>
))}
</ChartGroup>
</Chart>
</div>
</div>
);
};