@@ -11,7 +11,7 @@ propComponents: [
1111hideDarkMode : true
1212---
1313
14- import { createRef } from 'react';
14+ import { useEffect, useRef, useState } from 'react';
1515import { Chart, ChartAxis, ChartGroup, ChartLine, ChartThemeColor, ChartLegendTooltip, ChartVoronoiContainer, createContainer } from '@patternfly/react-charts /victory';
1616import { getResizeObserver } from '@patternfly/react-core ';
1717import { VictoryZoomContainer } from 'victory-zoom-container';
@@ -23,274 +23,24 @@ The examples below are based on the [Victory](https://formidable.com/open-source
2323
2424## Examples
2525### Basic with right aligned legend
26- ``` js
27- import { Chart , ChartAxis , ChartGroup , ChartLine , ChartVoronoiContainer } from ' @patternfly/react-charts/victory' ;
26+ ``` ts file = "ChartLineBasicRightLegend.tsx"
2827
29- < div style= {{ height: ' 250px' , width: ' 600px' }}>
30- < Chart
31- ariaDesc= " Average number of pets"
32- ariaTitle= " Line chart example"
33- containerComponent= {< ChartVoronoiContainer labels= {({ datum }) => ` ${ datum .name } : ${ datum .y } ` } constrainToVisibleArea / > }
34- legendData= {[{ name: ' Cats' }, { name: ' Dogs' , symbol: { type: ' dash' } }, { name: ' Birds' }, { name: ' Mice' }]}
35- legendOrientation= " vertical"
36- legendPosition= " right"
37- height= {250 }
38- maxDomain= {{y: 10 }}
39- minDomain= {{y: 0 }}
40- name= " chart1"
41- padding= {{
42- bottom: 50 ,
43- left: 50 ,
44- right: 200 , // Adjusted to accommodate legend
45- top: 50
46- }}
47- width= {600 }
48- >
49- < ChartAxis tickValues= {[2 , 3 , 4 ]} / >
50- < ChartAxis dependentAxis showGrid tickValues= {[2 , 5 , 8 ]} / >
51- < ChartGroup>
52- < ChartLine
53- data= {[
54- { name: ' Cats' , x: ' 2015' , y: 1 },
55- { name: ' Cats' , x: ' 2016' , y: 2 },
56- { name: ' Cats' , x: ' 2017' , y: 5 },
57- { name: ' Cats' , x: ' 2018' , y: 3 }
58- ]}
59- / >
60- < ChartLine
61- data= {[
62- { name: ' Dogs' , x: ' 2015' , y: 2 },
63- { name: ' Dogs' , x: ' 2016' , y: 1 },
64- { name: ' Dogs' , x: ' 2017' , y: 7 },
65- { name: ' Dogs' , x: ' 2018' , y: 4 }
66- ]}
67- style= {{
68- data: {
69- strokeDasharray: ' 3,3'
70- }
71- }}
72- / >
73- < ChartLine
74- data= {[
75- { name: ' Birds' , x: ' 2015' , y: 3 },
76- { name: ' Birds' , x: ' 2016' , y: 4 },
77- { name: ' Birds' , x: ' 2017' , y: 9 },
78- { name: ' Birds' , x: ' 2018' , y: 5 }
79- ]}
80- / >
81- < ChartLine
82- data= {[
83- { name: ' Mice' , x: ' 2015' , y: 3 },
84- { name: ' Mice' , x: ' 2016' , y: 3 },
85- { name: ' Mice' , x: ' 2017' , y: 8 },
86- { name: ' Mice' , x: ' 2018' , y: 7 }
87- ]}
88- / >
89- < / ChartGroup>
90- < / Chart>
91- < / div>
9228```
9329
9430### Green with bottom aligned legend
9531
9632This demonstrates how to combine cursor and voronoi containers to display tooltips along with a cursor.
9733
98- ``` js
99- import { Chart , ChartAxis , ChartGroup , ChartLine , ChartThemeColor , ChartLegendTooltip , createContainer } from ' @patternfly/react-charts/victory' ;
34+ ``` ts file = "ChartLineGreenBottomLegend.tsx"
10035
101- class BottomAlignedLegend extends React .Component {
102- render () {
103- // Note: Container order is important
104- const CursorVoronoiContainer = createContainer (" voronoi" , " cursor" );
105- const legendData = [{ childName: ' cats' , name: ' Cats' }, { childName: ' dogs' , name: ' Dogs' , symbol: { type: ' dash' }}, { childName: ' birds' , name: ' Birds' }, { childName: ' mice' , name: ' Mice' }];
106-
107- return (
108- < div style= {{ height: ' 275px' , width: ' 450px' }}>
109- < Chart
110- ariaDesc= " Average number of pets"
111- ariaTitle= " Line chart example"
112- containerComponent= {
113- < CursorVoronoiContainer
114- cursorDimension= " x"
115- labels= {({ datum }) => ` ${ datum .y } ` }
116- labelComponent= {< ChartLegendTooltip legendData= {legendData} title= {(datum ) => datum .x }/ > }
117- mouseFollowTooltips
118- voronoiDimension= " x"
119- voronoiPadding= {50 }
120- / >
121- }
122- legendData= {legendData}
123- legendPosition= " bottom"
124- height= {275 }
125- maxDomain= {{y: 10 }}
126- minDomain= {{y: 0 }}
127- name= " chart2"
128- padding= {{
129- bottom: 75 , // Adjusted to accommodate legend
130- left: 50 ,
131- right: 50 ,
132- top: 50
133- }}
134- themeColor= {ChartThemeColor .green }
135- width= {450 }
136- >
137- < ChartAxis tickValues= {[2 , 3 , 4 ]} / >
138- < ChartAxis dependentAxis showGrid tickValues= {[2 , 5 , 8 ]} / >
139- < ChartGroup>
140- < ChartLine
141- data= {[
142- { x: ' 2015' , y: 1 },
143- { x: ' 2016' , y: 2 },
144- { x: ' 2017' , y: 5 },
145- { x: ' 2018' , y: 3 }
146- ]}
147- name= " cats"
148- / >
149- < ChartLine
150- data= {[
151- { x: ' 2015' , y: 2 },
152- { x: ' 2016' , y: 1 },
153- { x: ' 2017' , y: 7 },
154- { x: ' 2018' , y: 4 }
155- ]}
156- name= " dogs"
157- style= {{
158- data: {
159- strokeDasharray: ' 3,3'
160- }
161- }}
162- / >
163- < ChartLine
164- data= {[
165- { x: ' 2015' , y: 3 },
166- { x: ' 2016' , y: 4 },
167- { x: ' 2017' , y: 9 },
168- { x: ' 2018' , y: 5 }
169- ]}
170- name= " birds"
171- / >
172- < ChartLine
173- data= {[
174- { x: ' 2015' , y: 3 },
175- { x: ' 2016' , y: 3 },
176- { x: ' 2017' , y: 8 },
177- { x: ' 2018' , y: 7 }
178- ]}
179- name= " mice"
180- / >
181- < / ChartGroup>
182- < / Chart>
183- < / div>
184- );
185- }
186- }
18736```
18837
18938### Multi-color (unordered) with responsive container
19039
19140This demonstrates zoom for the x axis only.
19241
193- ``` js
194- import { Chart , ChartAxis , ChartGroup , ChartLine , ChartThemeColor } from ' @patternfly/react-charts/victory' ;
195- import { getResizeObserver } from ' @patternfly/react-core' ;
196- import { VictoryZoomContainer } from ' victory-zoom-container' ;
197-
198- class MultiColorChart extends React .Component {
199- constructor (props ) {
200- super (props);
201- this .containerRef = createRef ();
202- this .observer = () => {};
203- this .state = {
204- width: 0
205- };
206- this .handleResize = () => {
207- if (this .containerRef .current && this .containerRef .current .clientWidth ) {
208- this .setState ({ width: this .containerRef .current .clientWidth });
209- }
210- };
211- }
212-
213- componentDidMount () {
214- this .observer = getResizeObserver (this .containerRef .current , this .handleResize );
215- this .handleResize ();
216- }
217-
218- componentWillUnmount () {
219- this .observer ();
220- }
42+ ``` ts file = "ChartLineMultiColor.tsx"
22143
222- render () {
223- const { width } = this .state ;
224-
225- return (
226- < div ref= {this .containerRef }>
227- < div style= {{ height: ' 275px' }}>
228- < Chart
229- ariaDesc= " Average number of pets"
230- ariaTitle= " Line chart example"
231- containerComponent= {< VictoryZoomContainer zoomDimension= " x" / > }
232- legendData= {[{ name: ' Cats' }, { name: ' Dogs' , symbol: { type: ' dash' } }, { name: ' Birds' }, { name: ' Mice' }]}
233- legendPosition= " bottom-left"
234- height= {275 }
235- maxDomain= {{y: 10 }}
236- minDomain= {{y: 0 }}
237- name= " chart3"
238- padding= {{
239- bottom: 75 , // Adjusted to accommodate legend
240- left: 50 ,
241- right: 50 ,
242- top: 50
243- }}
244- themeColor= {ChartThemeColor .multiUnordered }
245- width= {width}
246- >
247- < ChartAxis tickValues= {[2 , 3 , 4 ]} / >
248- < ChartAxis dependentAxis showGrid tickValues= {[2 , 5 , 8 ]} / >
249- < ChartGroup>
250- < ChartLine
251- data= {[
252- { name: ' Cats' , x: ' 2015' , y: 1 },
253- { name: ' Cats' , x: ' 2016' , y: 2 },
254- { name: ' Cats' , x: ' 2017' , y: 5 },
255- { name: ' Cats' , x: ' 2018' , y: 3 }
256- ]}
257- / >
258- < ChartLine
259- data= {[
260- { name: ' Dogs' , x: ' 2015' , y: 2 },
261- { name: ' Dogs' , x: ' 2016' , y: 1 },
262- { name: ' Dogs' , x: ' 2017' , y: 7 },
263- { name: ' Dogs' , x: ' 2018' , y: 4 }
264- ]}
265- style= {{
266- data: {
267- strokeDasharray: ' 3,3'
268- }
269- }}
270- / >
271- < ChartLine
272- data= {[
273- { name: ' Birds' , x: ' 2015' , y: 3 },
274- { name: ' Birds' , x: ' 2016' , y: 4 },
275- { name: ' Birds' , x: ' 2017' , y: 9 },
276- { name: ' Birds' , x: ' 2018' , y: 5 }
277- ]}
278- / >
279- < ChartLine
280- data= {[
281- { name: ' Mice' , x: ' 2015' , y: 3 },
282- { name: ' Mice' , x: ' 2016' , y: 3 },
283- { name: ' Mice' , x: ' 2017' , y: 8 },
284- { name: ' Mice' , x: ' 2018' , y: 7 }
285- ]}
286- / >
287- < / ChartGroup>
288- < / Chart>
289- < / div>
290- < / div>
291- );
292- }
293- }
29444```
29545
29646## Documentation
0 commit comments