@@ -24,310 +24,32 @@ The examples below are based on the [Victory](https://formidable.com/open-source
2424
2525## Examples
2626### Basic with right aligned legend
27- ``` js
28- import { Chart , ChartAxis , ChartBoxPlot , ChartThemeColor } from ' @patternfly/react-charts/victory' ;
27+ ``` ts file = "ChartBoxPlotRightAlignedLegend.tsx"
2928
30- < div style= {{ height: ' 300px' , width: ' 750px' }}>
31- < Chart
32- ariaDesc= " Average number of pets"
33- ariaTitle= " Bar chart example"
34- domain= {{y: [0 , 12 ]}}
35- domainPadding= {{ x: [30 , 25 ] }}
36- legendData= {[{ name: ' Cats' }]}
37- legendOrientation= " vertical"
38- legendPosition= " right"
39- height= {300 }
40- name= " chart3"
41- padding= {{
42- bottom: 50 ,
43- left: 50 ,
44- right: 200 , // Adjusted to accommodate legend
45- top: 50
46- }}
47- themeColor= {ChartThemeColor .blue }
48- width= {750 }
49- >
50- < ChartAxis / >
51- < ChartAxis dependentAxis showGrid / >
52- < ChartBoxPlot
53- data= {[
54- { name: ' Cats' , x: ' 2015' , y: [1 , 2 , 3 , 5 ] },
55- { name: ' Cats' , x: ' 2016' , y: [3 , 2 , 8 , 10 ] },
56- { name: ' Cats' , x: ' 2017' , y: [2 , 8 , 6 , 5 ] },
57- { name: ' Cats' , x: ' 2018' , y: [1 , 3 , 2 , 9 ] }
58- ]}
59- / >
60- < / Chart>
61- < / div>
6229```
6330
6431### Labels with bottom aligned legend
6532
6633This demonstrates how to display labels.
6734
68- ``` js
69- import { Chart , ChartAxis , ChartBoxPlot , ChartThemeColor } from ' @patternfly/react-charts/victory' ;
35+ ``` ts file= "ChartBoxPlotLabelsLegend.tsx"
7036
71- < div style= {{ height: ' 300px' , width: ' 600px' }}>
72- < Chart
73- ariaDesc= " Average number of pets"
74- ariaTitle= " Bar chart example"
75- domain= {{y: [0 , 12 ]}}
76- domainPadding= {{ x: [30 , 25 ] }}
77- legendData= {[{ name: ' Cats' }]}
78- legendPosition= " bottom"
79- height= {300 }
80- name= " chart2"
81- padding= {{
82- bottom: 75 , // Adjusted to accommodate legend
83- left: 50 ,
84- right: 50 ,
85- top: 50
86- }}
87- themeColor= {ChartThemeColor .yellow }
88- width= {600 }
89- >
90- < ChartAxis / >
91- < ChartAxis dependentAxis showGrid / >
92- < ChartBoxPlot
93- data= {[
94- { name: ' Cats' , x: ' 2015' , y: [1 , 2 , 3 , 5 ] },
95- { name: ' Cats' , x: ' 2016' , y: [3 , 2 , 8 , 10 ] },
96- { name: ' Cats' , x: ' 2017' , y: [2 , 8 , 6 , 5 ] },
97- { name: ' Cats' , x: ' 2018' , y: [1 , 3 , 2 , 9 ] }
98- ]}
99- labelOrientation= {{
100- min: " right" ,
101- max: " right" ,
102- }}
103- maxLabels
104- minLabels
105- / >
106- < / Chart>
107- < / div>
10837```
10938
11039### Embedded legend
11140
11241This demonstrates how to embed a legend within a tooltip. Combining cursor and voronoi containers is required to display tooltips with a vertical cursor.
11342
114- ``` js
115- import { Chart , ChartAxis , ChartBoxPlot , ChartLegendTooltip , ChartThemeColor , ChartThreshold , createContainer } from ' @patternfly/react-charts/victory' ;
116- import chart_color_orange_300 from ' @patternfly/react-tokens/dist/esm/chart_color_orange_300' ;
43+ ``` ts file = "ChartBoxPlotEmbeddedLegend.tsx"
11744
118- class EmbeddedLegend extends React .Component {
119- render () {
120- // Note: Container order is important
121- const CursorVoronoiContainer = createContainer (" voronoi" , " cursor" );
122- const legendData = [
123- {
124- childName: ' limit' ,
125- name: ' Limit' ,
126- symbol: { fill: chart_color_orange_300 .var , type: ' threshold' }
127- },
128- { childName: ' cats' , name: ' Cats' },
129- // Force extra space below for line wrapping
130- {
131- childName: ' cats' ,
132- name: ' ' ,
133- symbol: { fill: ' none' }
134- },
135- {
136- childName: ' cats' ,
137- name: ' ' ,
138- symbol: { fill: ' none' }
139- },
140- ];
141- const labelFormatter = (datum ) => {
142- // With box plot data, datum.y will also be an array
143- if (datum && (datum ._min || datum ._median || datum ._max || datum ._q1 || datum ._q3 )) {
144- return ` Min: ${ datum ._min } , Max: ${ datum ._max } \n Median: ${ datum ._median } \n Q1: ${ datum ._q1 } , Q3: ${ datum ._q3 } ` ;
145- }
146- const yVal = Array .isArray (datum .y ) ? datum .y [0 ] : datum .y ;
147- return yVal !== null ? yVal : ' no data' ;
148- }
149- return (
150- < div style= {{ height: ' 350px' , width: ' 600px' }}>
151- < Chart
152- ariaDesc= " Average number of pets - possibly more information to summarize the data in the chart."
153- ariaTitle= " Embedded legend example chart title"
154- containerComponent= {
155- < CursorVoronoiContainer
156- cursorDimension= " x"
157- labels= {({ datum }) => labelFormatter (datum)}
158- labelComponent= {< ChartLegendTooltip legendData= {legendData} title= {(datum ) => datum .x } / > }
159- mouseFollowTooltips
160- voronoiDimension= " x"
161- voronoiPadding= {50 }
162- / >
163- }
164- domain= {{y: [0 , 13 ]}}
165- domainPadding= {{ x: [30 , 25 ] }}
166- legendData= {legendData}
167- legendPosition= " bottom"
168- height= {350 }
169- name= " chart5"
170- padding= {{
171- bottom: 75 , // Adjusted to accommodate legend
172- left: 50 ,
173- right: 50 ,
174- top: 50
175- }}
176- themeColor= {ChartThemeColor .green }
177- width= {600 }
178- >
179- < ChartAxis / >
180- < ChartAxis dependentAxis showGrid / >
181- < ChartThreshold
182- data= {[
183- { name: ' Limit' , x: ' 2015' , y: 12 },
184- { name: ' Limit' , x: ' 2016' , y: 12 },
185- { name: ' Limit' , x: ' 2017' , y: 12 },
186- { name: ' Limit' , x: ' 2018' , y: 12 }
187- ]}
188- name= " limit"
189- style= {{
190- data: {
191- stroke: chart_color_orange_300 .var
192- }
193- }}
194- / >
195- < ChartBoxPlot
196- data= {[
197- { name: ' Cats' , x: ' 2015' , y: [null ] },
198- { name: ' Cats' , x: ' 2016' , y: [3 , 2 , 8 , 10 ] },
199- { name: ' Cats' , x: ' 2017' , y: [2 , 8 , 6 , 5 ] },
200- { name: ' Cats' , x: ' 2018' , y: [1 , 3 , 2 , 9 ] }
201- ]}
202- name= " cats"
203- / >
204- < / Chart>
205- < / div>
206- );
207- }
208- }
20945```
21046
21147### Embedded HTML
21248
21349This demonstrates how to embed HTML within a tooltip. Combining cursor and voronoi containers is required to display tooltips with a vertical cursor.
21450
215- ``` js
216- import { Chart , ChartAxis , ChartBoxPlot , ChartCursorTooltip , ChartThemeColor , createContainer } from ' @patternfly/react-charts/victory' ;
217-
218- class EmbeddedHtml extends React .Component {
219- constructor (props ) {
220- super (props);
221- this .baseStyles = {
222- color: ' #f0f0f0' ,
223- fontFamily: ' "Red Hat Text", "RedHatText", "Noto Sans Arabic", "Noto Sans Hebrew", "Noto Sans JP", "Noto Sans KR", "Noto Sans Malayalam", "Noto Sans SC", "Noto Sans TC", "Noto Sans Thai", Helvetica, Arial, sans-serif' ,
224- fontSize: ' 14px'
225- };
226- this .leftColumn = {
227- paddingLeft: ' 10px' ,
228- width: ' 50%'
229- }
230- this .rightColumn = {
231- paddingRight: ' 20px' ,
232- textAlign: ' right' ,
233- width: ' 50%'
234- }
235- }
236-
237- render () {
238- // Note: Container order is important
239- const CursorVoronoiContainer = createContainer (" voronoi" , " cursor" );
240- const legendData = [{ name: ' Cats' }];
241-
242- // Custom HTML component to create a legend layout
243- const HtmlLegendContent = ({datum, text, title, x, y, ... rest}) => (
244- < g>
245- < foreignObject height= " 100%" width= " 100%" x= {x - 30 } y= {y - 62 } >
246- < table>
247- < thead>
248- < tr>
249- < th colSpan= {2 } style= {{... this .baseStyles , ... this .leftColumn , fontWeight: 700 }}> {title (datum)}< / th>
250- < / tr>
251- < / thead>
252- < tbody>
253- < tr style= {this .baseStyles }>
254- < td style= {this .leftColumn }> Max< / td>
255- < td style= {this .rightColumn }> {datum ._max }< / td>
256- < / tr>
257- < tr style= {this .baseStyles }>
258- < td style= {this .leftColumn }> Median< / td>
259- < td style= {this .rightColumn }> {datum ._median }< / td>
260- < / tr>
261- < tr style= {this .baseStyles }>
262- < td style= {this .leftColumn }> Min< / td>
263- < td style= {this .rightColumn }> {datum ._min }< / td>
264- < / tr>
265- < tr style= {this .baseStyles }>
266- < td style= {this .leftColumn }> Q1 < / td>
267- < td style= {this .rightColumn }> {datum ._q1 }< / td>
268- < / tr>
269- < tr style= {this .baseStyles }>
270- < td style= {this .leftColumn }> Q3 < / td>
271- < td style= {this .rightColumn }> {datum ._q3 }< / td>
272- < / tr>
273- < / tbody>
274- < / table>
275- < / foreignObject>
276- < / g>
277- );
51+ ``` ts file = "ChartBoxPlotEmbeddedHtml.tsx"
27852
279- return (
280- < div style= {{ height: ' 300px' , width: ' 600px' }}>
281- < Chart
282- ariaDesc= " Average number of pets - possibly more information to summarize the data in the chart."
283- ariaTitle= " Embedded html example chart title"
284- containerComponent= {
285- < CursorVoronoiContainer
286- cursorDimension= " x"
287- labels= {({ datum }) => ` ${ datum .y } ` }
288- labelComponent= {
289- < ChartCursorTooltip
290- flyoutHeight= {145 }
291- flyoutWidth= {110 }
292- labelComponent= {< HtmlLegendContent title= {(datum ) => datum .x } / > }
293- / >
294- }
295- mouseFollowTooltips
296- voronoiDimension= " x"
297- voronoiPadding= {50 }
298- / >
299- }
300- domain= {{y: [0 , 12 ]}}
301- domainPadding= {{ x: [30 , 25 ] }}
302- legendData= {legendData}
303- legendPosition= " bottom"
304- height= {300 }
305- name= " chart4"
306- padding= {{
307- bottom: 75 , // Adjusted to accommodate legend
308- left: 50 ,
309- right: 50 ,
310- top: 50 ,
311- }}
312- maxDomain= {{y: 9 }}
313- themeColor= {ChartThemeColor .orange }
314- width= {600 }
315- >
316- < ChartAxis / >
317- < ChartAxis dependentAxis showGrid / >
318- < ChartBoxPlot
319- data= {[
320- { name: ' Cats' , x: ' 2015' , y: [1 , 2 , 3 , 5 ] },
321- { name: ' Cats' , x: ' 2016' , y: [3 , 2 , 8 , 10 ] },
322- { name: ' Cats' , x: ' 2017' , y: [2 , 8 , 6 , 5 ] },
323- { name: ' Cats' , x: ' 2018' , y: [1 , 3 , 2 , 9 ] }
324- ]}
325- / >
326- < / Chart>
327- < / div>
328- );
329- }
330- }
33153```
33254
33355## Documentation
0 commit comments