| id | Area chart | ||||||
|---|---|---|---|---|---|---|---|
| section | charts | ||||||
| propComponents |
|
||||||
| hideDarkMode | true |
import { createRef } from 'react'; import { Chart, ChartArea, ChartAxis, ChartGroup, ChartThreshold, ChartThemeColor, ChartLegendTooltip, ChartVoronoiContainer, createContainer } from '@patternfly/react-charts/victory'; import { getResizeObserver } from '@patternfly/react-core';
Note: PatternFly React charts live in its own package at @patternfly/react-charts!
The examples below are based on the Victory chart library, along with additional functionality, custom components, and theming for PatternFly. This provides a collection of React based components you can use to build PatternFly patterns with consistent markup, styling, and behavior.
import { Chart, ChartArea, ChartAxis, ChartGroup, ChartVoronoiContainer } from '@patternfly/react-charts/victory';
// import '@patternfly/patternfly/patternfly-charts.css'; // Required for mix-blend-mode CSS property
<div style={{ height: '200px', width: '800px' }}>
<Chart
ariaDesc="Average number of pets"
ariaTitle="Area chart example"
containerComponent={<ChartVoronoiContainer labels={({ datum }) => `${datum.name}: ${datum.y}`} constrainToVisibleArea />}
legendData={[{ name: 'Cats' }, { name: 'Dogs' }, { name: 'Birds' }]}
legendOrientation="vertical"
legendPosition="right"
height={200}
maxDomain={{y: 9}}
name="chart1"
padding={{
bottom: 50,
left: 50,
right: 200, // Adjusted to accommodate legend
top: 50
}}
width={800}
>
<ChartAxis />
<ChartAxis dependentAxis showGrid/>
<ChartGroup>
<ChartArea
data={[
{ name: 'Cats', x: '2015', y: 3 },
{ name: 'Cats', x: '2016', y: 4 },
{ name: 'Cats', x: '2017', y: 8 },
{ name: 'Cats', x: '2018', y: 6 }
]}
interpolation="monotoneX"
/>
<ChartArea
data={[
{ name: 'Dogs', x: '2015', y: 2 },
{ name: 'Dogs', x: '2016', y: 3 },
{ name: 'Dogs', x: '2017', y: 4 },
{ name: 'Dogs', x: '2018', y: 5 },
{ name: 'Dogs', x: '2019', y: 6 }
]}
interpolation="monotoneX"
/>
<ChartArea
data={[
{ name: 'Birds', x: '2015', y: 1 },
{ name: 'Birds', x: '2016', y: 2 },
{ name: 'Birds', x: '2017', y: 3 },
{ name: 'Birds', x: '2018', y: 2 },
{ name: 'Birds', x: '2019', y: 4 }
]}
interpolation="monotoneX"
/>
</ChartGroup>
</Chart>
</div>This demonstrates how to combine cursor and voronoi containers to display tooltips along with a cursor.
import { Chart, ChartArea, ChartAxis, ChartGroup, ChartThemeColor, ChartLegendTooltip, createContainer } from '@patternfly/react-charts/victory';
// import '@patternfly/patternfly/patternfly-charts.css'; // Required for mix-blend-mode CSS property
class BottomAlignedLegend extends React.Component {
render() {
// Note: Container order is important
const CursorVoronoiContainer = createContainer("voronoi", "cursor");
const legendData = [{ childName: 'cats', name: 'Cats' }, { childName: 'dogs', name: 'Dogs' }, { childName: 'birds', name: 'Birds' }];
return (
<div style={{ height: '250px', width: '650px' }}>
<Chart
ariaDesc="Average number of pets"
ariaTitle="Area chart example"
containerComponent={
<CursorVoronoiContainer
cursorDimension="x"
labels={({ datum }) => `${datum.y}`}
labelComponent={<ChartLegendTooltip legendData={legendData} title={(datum) => datum.x}/>}
mouseFollowTooltips
voronoiDimension="x"
voronoiPadding={50}
/>
}
legendData={legendData}
legendPosition="bottom"
height={250}
name="chart2"
padding={{
bottom: 100, // Adjusted to accommodate legend
left: 50,
right: 50,
top: 50,
}}
maxDomain={{y: 9}}
themeColor={ChartThemeColor.teal}
width={650}
>
<ChartAxis label="Years" fixAxisLabelHeight />
<ChartAxis dependentAxis showGrid/>
<ChartGroup>
<ChartArea
data={[
{ x: '2015', y: 3 },
{ x: '2016', y: 4 },
{ x: '2017', y: 8 },
{ x: '2018', y: 6 }
]}
interpolation="monotoneX"
name="cats"
/>
<ChartArea
data={[
{ x: '2015', y: 2 },
{ x: '2016', y: 3 },
{ x: '2017', y: 4 },
{ x: '2018', y: 5 },
{ x: '2019', y: 6 }
]}
interpolation="monotoneX"
name="dogs"
/>
<ChartArea
data={[
{ x: '2015', y: 1 },
{ x: '2016', y: 2 },
{ x: '2017', y: 3 },
{ x: '2018', y: 2 },
{ x: '2019', y: 4 }
]}
interpolation="monotoneX"
name="birds"
/>
</ChartGroup>
</Chart>
</div>
);
}
}import { Chart, ChartArea, ChartAxis, ChartGroup, ChartThemeColor, ChartVoronoiContainer } from '@patternfly/react-charts/victory';
import { getResizeObserver } from '@patternfly/react-core';
// import '@patternfly/patternfly/patternfly-charts.css'; // Required for mix-blend-mode CSS property
class MultiColorChart extends React.Component {
constructor(props) {
super(props);
this.containerRef = createRef();
this.observer = () => {};
this.state = {
width: 0
};
this.handleResize = () => {
if (this.containerRef.current && this.containerRef.current.clientWidth) {
this.setState({ width: this.containerRef.current.clientWidth });
}
};
}
componentDidMount() {
this.observer = getResizeObserver(this.containerRef.current, this.handleResize);
this.handleResize();
}
componentWillUnmount() {
this.observer();
}
render() {
const { width } = this.state;
return (
<div ref={this.containerRef} style={{ height: '225px' }}>
<Chart
ariaDesc="Average number of pets"
ariaTitle="Area chart example"
containerComponent={<ChartVoronoiContainer labels={({ datum }) => `${datum.name}: ${datum.y}`} constrainToVisibleArea />}
legendData={[{ name: 'Cats' }, { name: 'Dogs' }, { name: 'Birds' }]}
legendPosition="bottom-left"
height={225}
name="chart3"
padding={{
bottom: 75, // Adjusted to accommodate legend
left: 50,
right: 50,
top: 50,
}}
maxDomain={{y: 9}}
themeColor={ChartThemeColor.multiUnordered}
width={width}
>
<ChartAxis />
<ChartAxis dependentAxis showGrid />
<ChartGroup>
<ChartArea
data={[
{ name: 'Cats', x: '2015', y: 3 },
{ name: 'Cats', x: '2016', y: 4 },
{ name: 'Cats', x: '2017', y: 8 },
{ name: 'Cats', x: '2018', y: 6 }
]}
interpolation="monotoneX"
/>
<ChartArea
data={[
{ name: 'Dogs', x: '2015', y: 2 },
{ name: 'Dogs', x: '2016', y: 3 },
{ name: 'Dogs', x: '2017', y: 4 },
{ name: 'Dogs', x: '2018', y: 5 },
{ name: 'Dogs', x: '2019', y: 6 }
]}
interpolation="monotoneX"
/>
<ChartArea
data={[
{ name: 'Birds', x: '2015', y: 1 },
{ name: 'Birds', x: '2016', y: 2 },
{ name: 'Birds', x: '2017', y: 3 },
{ name: 'Birds', x: '2018', y: 2 },
{ name: 'Birds', x: '2019', y: 4 }
]}
interpolation="monotoneX"
/>
</ChartGroup>
</Chart>
</div>
);
}
}- See Victory's FAQ
- For single data points or zero values, you may want to set the
domainprop ChartLegendmay be used as a standalone component, instead of usinglegendData- The
themeandthemeColorprops should be applied at the most top level component - Use
ChartGroupto apply theme color scales and other properties to multiple components
Currently, the generated documentation below is not able to resolve type definitions from Victory imports. For the components used in the examples above, Victory pass-thru props are also documented here:
- For
Chartprops, see VictoryChart - For
ChartAreaprops, see VictoryArea - For
ChartAxisprops, see VictoryAxis - For
ChartGroupprops, see VictoryGroup - For
ChartVoronoiContainerprops, see VictoryVoronoiContainer