Skip to content

Commit ea9a812

Browse files
authored
chore(chart tooltip): convert to typescript part two (#11897)
* chore(chart tooltip): convert to typescript part two * minor changes * minor changes * minor changes * removed unused imports
1 parent f632014 commit ea9a812

File tree

7 files changed

+405
-322
lines changed

7 files changed

+405
-322
lines changed

packages/react-charts/src/victory/components/ChartTooltip/examples/ChartTooltip.md

Lines changed: 13 additions & 322 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ propComponents: ['ChartTooltip']
55
hideDarkMode: true
66
---
77

8-
import { createRef } from 'react';
8+
import { useRef, useState, useEffect } from 'react';
99
import {
1010
Chart,
1111
ChartArea,
@@ -88,355 +88,46 @@ This demonstrates how to embed a legend within a tooltip, but with a custom font
8888

8989
This demonstrates an alternate way of applying tooltips using data labels.
9090

91-
```js
92-
import { Chart, ChartAxis, ChartBar, ChartStack, ChartThemeColor, ChartTooltip } from '@patternfly/react-charts/victory';
93-
94-
<div style={{ height: '275px', width: '450px' }}>
95-
<Chart
96-
ariaDesc="Average number of pets - possibly more information to summarize the data in the chart."
97-
ariaTitle="Data label example chart title"
98-
domainPadding={{ x: [30, 25] }}
99-
legendData={[{ name: 'Cats' }, { name: 'Dogs' }, { name: 'Birds' }, { name: 'Mice' }]}
100-
legendPosition="bottom-left"
101-
height={275}
102-
name="chart6"
103-
padding={{
104-
bottom: 75, // Adjusted to accommodate legend
105-
left: 50,
106-
right: 50,
107-
top: 50
108-
}}
109-
themeColor={ChartThemeColor.multiOrdered}
110-
width={450}
111-
>
112-
<ChartAxis />
113-
<ChartAxis dependentAxis showGrid />
114-
<ChartStack horizontal>
115-
<ChartBar
116-
data={[
117-
{ name: 'Cats', x: '2015', y: 1, label: 'Cats: 1' },
118-
{ name: 'Cats', x: '2016', y: 2, label: 'Cats: 2' },
119-
{ name: 'Cats', x: '2017', y: 5, label: 'Cats: 5' },
120-
{ name: 'Cats', x: '2018', y: 3, label: 'Cats: 3' }
121-
]}
122-
labelComponent={<ChartTooltip constrainToVisibleArea />}
123-
/>
124-
<ChartBar
125-
data={[
126-
{ name: 'Dogs', x: '2015', y: 2, label: 'Dogs: 2' },
127-
{ name: 'Dogs', x: '2016', y: 1, label: 'Dogs: 1' },
128-
{ name: 'Dogs', x: '2017', y: 7, label: 'Dogs: 7' },
129-
{ name: 'Dogs', x: '2018', y: 4, label: 'Dogs: 4' }
130-
]}
131-
labelComponent={<ChartTooltip constrainToVisibleArea />}
132-
/>
133-
<ChartBar
134-
data={[
135-
{ name: 'Birds', x: '2015', y: 4, label: 'Birds: 4' },
136-
{ name: 'Birds', x: '2016', y: 4, label: 'Birds: 4' },
137-
{ name: 'Birds', x: '2017', y: 9, label: 'Birds: 9' },
138-
{ name: 'Birds', x: '2018', y: 7, label: 'Birds: 7' }
139-
]}
140-
labelComponent={<ChartTooltip constrainToVisibleArea />}
141-
/>
142-
<ChartBar
143-
data={[
144-
{ name: 'Mice', x: '2015', y: 3, label: 'Mice: 3' },
145-
{ name: 'Mice', x: '2016', y: 3, label: 'Mice: 3' },
146-
{ name: 'Mice', x: '2017', y: 8, label: 'Mice: 8' },
147-
{ name: 'Mice', x: '2018', y: 5, label: 'Mice: 5' }
148-
]}
149-
labelComponent={<ChartTooltip constrainToVisibleArea />}
150-
/>
151-
</ChartStack>
152-
</Chart>
153-
</div>
91+
```ts file = "ChartTooltipDataLabel.tsx"
92+
15493
```
15594

15695
### Fixed tooltip
15796
This demonstrates how to adjust the tooltip position and label radius
158-
```js
159-
import { ChartDonut, ChartThemeColor, ChartTooltip } from '@patternfly/react-charts/victory';
160-
161-
<div style={{ height: '150px', width: '150px' }}>
162-
<ChartDonut
163-
ariaDesc="Average number of pets"
164-
ariaTitle="Donut chart example"
165-
constrainToVisibleArea
166-
data={[{ x: 'Cats', y: 35 }, { x: 'Dogs', y: 55 }, { x: 'Birds', y: 10 }]}
167-
height={150}
168-
labelComponent={<ChartTooltip center={{ x: 75, y: 0 }} />}
169-
labelRadius={46}
170-
labels={({ datum }) => `${datum.x}: ${datum.y}%`}
171-
name="chart5"
172-
subTitle="Pets"
173-
title="100"
174-
themeColor={ChartThemeColor.teal}
175-
width={150}
176-
/>
177-
</div>
97+
```ts file = "ChartTooltipFixed.tsx"
98+
17899
```
179100

180101
### Legend
181102

182103
This demonstrates an approach for applying tooltips to a legend using a custom legend label component.
183104

184-
```js
185-
import { ChartLabel, ChartLegend, ChartPie, ChartThemeColor } from '@patternfly/react-charts/victory';
186-
import { Tooltip } from '@patternfly/react-core';
187-
188-
class TooltipPieChart extends React.Component {
189-
constructor(props) {
190-
super(props);
191-
192-
// Custom legend label component
193-
// Note: Tooltip wraps children with a div tag, so we add a reference to ChartLabel instead
194-
this.LegendLabel = ({datum, ...rest}) => {
195-
const ref = createRef();
196-
return (
197-
<g ref={ref}>
198-
<ChartLabel {...rest} />
199-
<Tooltip content={datum.name} enableFlip reference={ref} />
200-
</g>
201-
);
202-
}
203-
204-
// Custom legend component
205-
this.getLegend = (legendData) => (
206-
<ChartLegend
207-
data={legendData}
208-
labelComponent={<this.LegendLabel />}
209-
/>
210-
);
211-
}
212-
213-
render() {
214-
return (
215-
<div style={{ height: '275px', width: '300px' }}>
216-
<ChartPie
217-
ariaDesc="Average number of pets - possibly more information to summarize the data in the chart."
218-
ariaTitle="Legend example chart title"
219-
constrainToVisibleArea
220-
data={[{ x: 'Cats', y: 35 }, { x: 'Dogs', y: 55 }, { x: 'Birds', y: 10 }]}
221-
height={275}
222-
labels={({ datum }) => `${datum.x}: ${datum.y}`}
223-
legendComponent={this.getLegend([
224-
{ name: 'Cats: 35' },
225-
{ name: 'Dogs: 55' },
226-
{ name: 'Birds: 10' }
227-
])}
228-
legendPosition="bottom"
229-
name="chart7"
230-
padding={{
231-
bottom: 65,
232-
left: 20,
233-
right: 20,
234-
top: 20
235-
}}
236-
themeColor={ChartThemeColor.multiOrdered}
237-
width={300}
238-
/>
239-
</div>
240-
);
241-
}
242-
}
105+
```ts file = "ChartTooltipLegend.tsx"
106+
243107
```
244108

245109
### Left aligned
246110

247111
This demonstrates how to customize tooltip label alignment using theme properties.
248112

249-
```js
250-
import { Chart, ChartAxis, ChartGroup, ChartLine, ChartThemeColor, ChartVoronoiContainer, getCustomTheme } from '@patternfly/react-charts/victory';
251-
252-
class TooltipThemeChart extends React.Component {
253-
constructor(props) {
254-
super(props);
255-
256-
// Victory theme properties only
257-
this.themeProps = {
258-
voronoi: {
259-
style: {
260-
labels: {
261-
textAnchor: 'start' // Align tooltip labels left
262-
}
263-
}
264-
}
265-
};
266-
267-
// Applies theme color and variant to base theme
268-
this.myCustomTheme = getCustomTheme(
269-
ChartThemeColor.default,
270-
this.themeProps
271-
);
272-
}
273-
274-
render() {
275-
return (
276-
<div style={{ height: '250px', width: '600px' }}>
277-
<Chart
278-
ariaDesc="Average number of pets - possibly more information to summarize the data in the chart."
279-
ariaTitle="Left aligned example chart title"
280-
containerComponent={<ChartVoronoiContainer labels={({ datum }) => `${datum.name}: ${datum.y}`} constrainToVisibleArea voronoiDimension="x"/>}
281-
legendData={[{ name: 'Cats' }, { name: 'Dogs', symbol: { type: 'dash' } }, { name: 'Birds' }, { name: 'Mice' }]}
282-
legendOrientation="vertical"
283-
legendPosition="right"
284-
height={250}
285-
maxDomain={{y: 10}}
286-
minDomain={{y: 0}}
287-
name="chart8"
288-
padding={{
289-
bottom: 50,
290-
left: 50,
291-
right: 200, // Adjusted to accommodate legend
292-
top: 50
293-
}}
294-
theme={this.myCustomTheme}
295-
width={600}
296-
>
297-
<ChartAxis tickValues={[2, 3, 4]} />
298-
<ChartAxis dependentAxis showGrid tickValues={[2, 5, 8]} />
299-
<ChartGroup>
300-
<ChartLine
301-
data={[
302-
{ name: 'Cats', x: '2015', y: 1 },
303-
{ name: 'Cats', x: '2016', y: 2 },
304-
{ name: 'Cats', x: '2017', y: 5 },
305-
{ name: 'Cats', x: '2018', y: 3 }
306-
]}
307-
/>
308-
<ChartLine
309-
data={[
310-
{ name: 'Dogs', x: '2015', y: 2 },
311-
{ name: 'Dogs', x: '2016', y: 1 },
312-
{ name: 'Dogs', x: '2017', y: 7 },
313-
{ name: 'Dogs', x: '2018', y: 4 }
314-
]}
315-
style={{
316-
data: {
317-
strokeDasharray: '3,3'
318-
}
319-
}}
320-
/>
321-
<ChartLine
322-
data={[
323-
{ name: 'Birds', x: '2015', y: 3 },
324-
{ name: 'Birds', x: '2016', y: 4 },
325-
{ name: 'Birds', x: '2017', y: 9 },
326-
{ name: 'Birds', x: '2018', y: 5 }
327-
]}
328-
/>
329-
<ChartLine
330-
data={[
331-
{ name: 'Mice', x: '2015', y: 3 },
332-
{ name: 'Mice', x: '2016', y: 3 },
333-
{ name: 'Mice', x: '2017', y: 8 },
334-
{ name: 'Mice', x: '2018', y: 7 }
335-
]}
336-
/>
337-
</ChartGroup>
338-
</Chart>
339-
</div>
340-
);
341-
}
342-
}
113+
```ts file = "ChartTooltipLeftAligned.tsx"
114+
343115
```
344116

345117
### CSS overflow
346118

347119
This demonstrates an alternate way of applying tooltips using CSS overflow instead of <code>constrainToVisibleArea</code>.
348120

349-
```js
350-
import { ChartArea, ChartGroup, ChartLabel, ChartThemeColor, ChartVoronoiContainer } from '@patternfly/react-charts/victory';
351-
352-
// Workaround for documentation-framework issue https://github.com/patternfly/patternfly-react/issues/11455
353-
const sheet = (() => {
354-
var style = document.createElement("style");
355-
document.head.appendChild(style);
356-
return style.sheet;
357-
})();
358-
359-
sheet.insertRule(".ws-react-charts-tooltip-overflow { margin-left: 50px; margin-top: 50px; height: 135px; }", sheet.cssRules.length);
360-
sheet.insertRule(".ws-react-charts-tooltip-overflow svg { overflow: visible; }", sheet.cssRules.length);
361-
362-
<div className="ws-react-charts-tooltip-overflow">
363-
<div style={{ height: '100px', width: '400px' }}>
364-
<ChartGroup
365-
ariaDesc="Average number of pets - possibly more information to summarize the data in the chart."
366-
ariaTitle="CSS overflow example chart title"
367-
containerComponent={<ChartVoronoiContainer labels={({ datum }) => `${datum.name}: ${datum.y}`} />}
368-
height={100}
369-
maxDomain={{y: 9}}
370-
name="chart9"
371-
padding={0}
372-
themeColor={ChartThemeColor.green}
373-
width={400}
374-
>
375-
<ChartArea
376-
data={[
377-
{ name: 'Cats', x: '2015', y: 3 },
378-
{ name: 'Cats', x: '2016', y: 4 },
379-
{ name: 'Cats', x: '2017', y: 8 },
380-
{ name: 'Cats', x: '2018', y: 6 }
381-
]}
382-
/>
383-
<ChartLabel text="CPU utilization" dy={120}/>
384-
</ChartGroup>
385-
</div>
386-
</div>
121+
```ts file = "ChartTooltipCssOverflow.tsx"
122+
387123
```
388124

389125
### Wrapped chart
390126

391127
This demonstrates an alternate way of applying tooltips by wrapping charts with the Tooltip component.
392128

393-
```js
394-
import { ChartDonutThreshold, ChartDonutUtilization } from '@patternfly/react-charts/victory';
395-
import { Button, Tooltip, TooltipPosition } from '@patternfly/react-core';
396-
397-
class TooltipChart extends React.Component {
398-
constructor(props) {
399-
super(props);
400-
this.state = {
401-
isVisible: false
402-
};
403-
this.showTooltip = () => {
404-
this.setState({ isVisible: !this.state.isVisible });
405-
};
406-
}
407-
408-
render() {
409-
const { isVisible } = this.state;
410-
411-
return (
412-
<div>
413-
<div style={{ height: '230px', width: '230px' }}>
414-
<Tooltip content={<div>My custom tooltip</div>} isVisible={isVisible} position={TooltipPosition.right} trigger="manual">
415-
<ChartDonutThreshold
416-
allowTooltip={false}
417-
ariaDesc="Storage capacity - possibly more information to summarize the data in the chart."
418-
ariaTitle="Wrapped example chart title"
419-
data={[{ x: 'Warning at 60%', y: 60 }, { x: 'Danger at 90%', y: 90 }]}
420-
labels={() => null}
421-
name="chart10"
422-
>
423-
<ChartDonutUtilization
424-
allowTooltip={false}
425-
data={{ x: 'Storage capacity', y: 45 }}
426-
labels={() => null}
427-
subTitle="of 100 GBps"
428-
title="45%"
429-
/>
430-
</ChartDonutThreshold>
431-
</Tooltip>
432-
</div>
433-
<div style={{ width: '230px', textAlign: 'center' }}>
434-
<Button onClick={this.showTooltip}>Show Tooltip</Button>
435-
</div>
436-
</div>
437-
);
438-
}
439-
}
129+
```ts file = "ChartTooltipWrappedChart.tsx"
130+
440131
```
441132

442133
## Documentation

0 commit comments

Comments
 (0)