Skip to content

Commit

Permalink
Banded Chart example (#296)
Browse files Browse the repository at this point in the history
* Banded Chart example

* Banded Chart example: Legend to exclude entry for band data

* converted class to function component; added link to private codesandbox with the example

* fixed: export should be default not named
  • Loading branch information
skyboyer authored Jun 27, 2024
1 parent 5887c5c commit b67bc66
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
90 changes: 90 additions & 0 deletions src/docs/exampleComponents/ComposedChart/BandedChart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import React from 'react';
import {
ComposedChart,
Line,
Area,
XAxis,
YAxis,
CartesianGrid,
Tooltip,
Legend,
DefaultLegendContent,
ResponsiveContainer,
} from 'recharts';

const data = [
{
name: "Page A",
a: [0, 0],
b: 0,
},
{
name: "Page B",
a: [50, 300],
b: 106,
},
{
name: "Page C",
a: [150, 423],
},
{
name: "Page D",
b: 312,
},
{
name: "Page E",
a: [367, 678],
b: 451,
},
{
name: "Page F",
a: [305, 821],
b: 623,
},
];

export default function Example() {
const renderTooltipWithoutRange = ({ payload, content, ...rest }) => {
const newPayload = payload.filter((x) => x.dataKey !== "a");
return <Tooltip payload={newPayload} {...rest} />;
}

const renderLegendWithoutRange = ({ payload, content, ...rest }) => {
const newPayload = payload.filter((x) => x.dataKey !== "a");
return <DefaultLegendContent payload={newPayload} {...rest} />;
}

return (
<ResponsiveContainer width="100%" height="100%">
<ComposedChart
width={500}
height={400}
data={data}
margin={{
top: 10,
right: 30,
left: 0,
bottom: 0,
}}
>
<CartesianGrid strokeDasharray="3 3" />
<XAxis dataKey="name" />
<YAxis />
<Tooltip content={renderTooltipWithoutRange} />
<Area
type="monotone"
dataKey="a"
stroke="none"
fill="#cccccc"
connectNulls
dot={false}
activeDot={false}
/>
<Line type="natural" dataKey="b" stroke="#ff00ff" connectNulls />
<Legend content={renderLegendWithoutRange} />
</ComposedChart>
</ResponsiveContainer>
);
}

Example.demoUrl = 'https://codesandbox.io/p/sandbox/simple-area-chart-forked-hncq3r';
2 changes: 2 additions & 0 deletions src/docs/exampleComponents/ComposedChart/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import LineBarAreaComposedChart from './LineBarAreaComposedChart';
import SameDataComposedChart from './SameDataComposedChart';
import VerticalComposedChart from './VerticalComposedChart';
import ScatterAndLineOfBestFit from './ScatterAndLineOfBestFit';
import BandedChart from './BandedChart'

export default {
LineBarAreaComposedChart,
SameDataComposedChart,
VerticalComposedChart,
ComposedChartWithAxisLabels,
ScatterAndLineOfBestFit,
BandedChart,
};

0 comments on commit b67bc66

Please sign in to comment.