Skip to content

chore(chart scatter): convert to typescript #11889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ propComponents: [
hideDarkMode: true
---

import { createRef } from 'react';
import {
Chart,
ChartArea,
Expand All @@ -25,6 +24,7 @@ ChartThemeColor,
ChartVoronoiContainer,
} from '@patternfly/react-charts/victory';
import { getResizeObserver } from '@patternfly/react-core';
import { useEffect, useRef, useState } from 'react';

## Introduction
Note: PatternFly React charts live in its own package at [@patternfly/react-charts](https://www.npmjs.com/package/@patternfly/react-charts)!
Expand All @@ -33,296 +33,24 @@ The examples below are based on the [Victory](https://formidable.com/open-source

## Examples
### Basic
```js
import { Chart, ChartAxis, ChartGroup, ChartScatter, ChartVoronoiContainer } from '@patternfly/react-charts/victory';
```ts file = "ChartScatterBasic.tsx"

<div style={{ height: '275px', width: '450px' }}>
<Chart
ariaDesc="Average number of pets"
ariaTitle="Scatter chart example"
containerComponent={
<ChartVoronoiContainer
labels={({ datum }) => `${datum.name}: ${datum.y}`}
constrainToVisibleArea
/>
}
height={275}
maxDomain={{y: 8}}
minDomain={{y: 0}}
name="chart1"
width={450}
>
<ChartAxis />
<ChartAxis dependentAxis showGrid tickValues={[2, 4, 6]} />
<ChartGroup>
<ChartScatter
data={[
{ name: 'Cats', x: '2015', y: 1 },
{ name: 'Cats', x: '2016', y: 2 },
{ name: 'Cats', x: '2017', y: 5 },
{ name: 'Cats', x: '2018', y: 4 }
]}
/>
</ChartGroup>
</Chart>
</div>
```

### Line chart

This demonstrates how to add interactive data points to a line chart.

```js
import { Chart, ChartAxis, ChartGroup, ChartLine, ChartScatter, ChartThemeColor, ChartVoronoiContainer } from '@patternfly/react-charts/victory';
import { getResizeObserver } from '@patternfly/react-core';

class ScatterLineChart 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 });
}
};

this.series = [
{
datapoints: [
{ name: 'Cats', x: '2015', y: 1 },
{ name: 'Cats', x: '2016', y: 2 },
{ name: 'Cats', x: '2017', y: 5 },
{ name: 'Cats', x: '2018', y: 3 }
],
legendItem: { name: 'Cats' }
},
{
datapoints: [
{ name: 'Dogs', x: '2015', y: 2 },
{ name: 'Dogs', x: '2016', y: 1 },
{ name: 'Dogs', x: '2017', y: 7 },
{ name: 'Dogs', x: '2018', y: 4 }
],
legendItem: { name: 'Dogs' },
style: {
data: {
strokeDasharray: '3,3'
}
}
},
{
datapoints: [
{ name: 'Birds', x: '2015', y: 3 },
{ name: 'Birds', x: '2016', y: 4 },
{ name: 'Birds', x: '2017', y: 9 },
{ name: 'Birds', x: '2018', y: 5 }
],
legendItem: { name: 'Birds' }
},
{
datapoints: [
{ name: 'Mice', x: '2015', y: 3 },
{ name: 'Mice', x: '2016', y: 3 },
{ name: 'Mice', x: '2017', y: 8 },
{ name: 'Mice', x: '2018', y: 7 }
],
legendItem: { name: 'Mice' }
}];
}

componentDidMount() {
this.observer = getResizeObserver(this.containerRef.current, this.handleResize);
this.handleResize();
}

componentWillUnmount() {
this.observer();
}

render() {
const { width } = this.state;
```ts file = "ChartScatterLineChart.tsx"

return (
<div ref={this.containerRef}>
<div style={{ height: '275px' }}>
<Chart
ariaDesc="Average number of pets"
ariaTitle="Line chart example"
containerComponent={
<ChartVoronoiContainer
labels={({ datum }) => datum.childName.includes('line-') ? `${datum.name}: ${datum.y}` : null}
constrainToVisibleArea
/>
}
legendData={this.series.map(s => s.legendItem)}
legendPosition="bottom-left"
height={275}
maxDomain={{y: 10}}
minDomain={{y: 0}}
name="chart2"
padding={{
bottom: 75, // Adjusted to accommodate legend
left: 50,
right: 50,
top: 50
}}
themeColor={ChartThemeColor.orange}
width={width}
>
<ChartAxis tickValues={[2, 3, 4]} />
<ChartAxis dependentAxis showGrid tickValues={[2, 5, 8]} />
<ChartGroup>
{this.series.map((s, idx) => {
return (
<ChartScatter
data={s.datapoints}
key={'scatter-' + idx}
name={'scatter-' + idx}
/>
);
})}
</ChartGroup>
<ChartGroup>
{this.series.map((s, idx) => {
return (
<ChartLine
key={'line-' + idx}
name={'line-' + idx}
data={s.datapoints}
/>
);
})}
</ChartGroup>
</Chart>
</div>
</div>
);
}
}
```

### Area chart

This demonstrates how to add interactive data points to an area chart.

```js
import { Chart, ChartArea, ChartAxis, ChartGroup, ChartScatter, ChartThemeColor, ChartVoronoiContainer } from '@patternfly/react-charts/victory';
import { getResizeObserver } from '@patternfly/react-core';
// import '@patternfly/patternfly/patternfly-charts.css'; // For mixed blend mode

class ScatterAreaChart 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 });
}
};

this.series = [
{
datapoints: [
{ name: 'Cats', x: '2015', y: 3 },
{ name: 'Cats', x: '2016', y: 4 },
{ name: 'Cats', x: '2017', y: 8 },
{ name: 'Cats', x: '2018', y: 6 }
],
legendItem: { name: 'Cats' }
},
{
datapoints: [
{ 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 }
],
legendItem: { name: 'Dogs' }
},
{
datapoints: [
{ 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 }
],
legendItem: { name: 'Birds' }
}];
}

componentDidMount() {
this.observer = getResizeObserver(this.containerRef.current, this.handleResize);
this.handleResize();
}

componentWillUnmount() {
this.observer();
}

render() {
const { width } = this.state;
```ts file = "ChartScatterAreaChart.tsx"

return (
<div ref={this.containerRef}>
<div style={{ height: '250px' }}>
<Chart
ariaDesc="Average number of pets"
ariaTitle="Area chart example"
containerComponent={
<ChartVoronoiContainer
labels={({ datum }) => datum.childName.includes('area-') ? `${datum.name}: ${datum.y}` : null}
constrainToVisibleArea
/>
}
height={225}
legendData={this.series.map(s => s.legendItem)}
legendPosition="bottom-left"
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>
{this.series.map((s, idx) => {
return (
<ChartScatter data={s.datapoints} key={'scatter-' + idx} name={'scatter-' + idx} />
);
})}
</ChartGroup>
<ChartGroup>
{this.series.map((s, idx) => {
return (
<ChartArea
interpolation="monotoneX"
key={'area-' + idx} name={'area-' + idx} data={s.datapoints}
/>
);
})}
</ChartGroup>
</Chart>
</div>
</div>
);
}
}
```

## Documentation
Expand Down
Loading
Loading