Area chart
TIP
For core principles, concept description, API and changelog, refer to the D3 chart.
Basic usage
tsx
import { Chart } from '@semcore/ui/d3-chart';
import React from 'react';
import AreaMockData from './mock';
function formatDate(value: any) {
const options = {
month: 'short' as const,
day: 'numeric' as const,
};
return new Intl.DateTimeFormat('en', options).format(value);
}
const Demo = () => {
return (
<Chart.Area
groupKey='time'
data={data}
plotWidth={500}
plotHeight={200}
tooltipValueFormatter={formatDate}
aria-label='Area chart'
/>
);
};
const data = AreaMockData.Default;
export default Demo;
Area
- You can draw a chart with areas using the
Areacomponent. Dotsare the dots on the line chart.- As with the Line chart, you can draw a polyline or a smoothed chart by passing the required method to the curve property.
tsx
import { Plot, XAxis, YAxis, minMax, Area } from '@semcore/ui/d3-chart';
import { scaleLinear } from 'd3-scale';
import { curveCardinal } from 'd3-shape';
import React from 'react';
import AreaMockData from './mock';
function formatDate(value: any, options: any) {
return new Intl.DateTimeFormat('en', options).format(value);
}
const Demo = () => {
const MARGIN = 40;
const width = 500;
const height = 300;
const xScale = scaleLinear()
.range([MARGIN, width - MARGIN])
.domain(minMax(data, 'time'));
const yScale = scaleLinear()
.range([height - MARGIN, MARGIN])
.domain([0, 10]);
return (
<Plot data={data} scale={[xScale, yScale]} width={width} height={height}>
<YAxis>
<YAxis.Ticks />
<YAxis.Grid />
</YAxis>
<XAxis>
<XAxis.Ticks ticks={data.map((d) => +d.time)}>
{({ value }) => ({
children: formatDate(value, {
month: 'short',
day: 'numeric',
}),
})}
</XAxis.Ticks>
</XAxis>
<Area x='time' y='line' curve={curveCardinal}>
<Area.Dots display />
</Area>
</Plot>
);
};
const data = AreaMockData.Default;
export default Demo;
Edge cases
- If a part of the chart has no data – use a dashed line to draw the period.
- If the data has only one value – display it as a dot.
- Two consecutively known values will automatically be displayed as the
Areacomponent.
tsx
import { Flex } from '@semcore/ui/base-components';
import { Plot, XAxis, YAxis, minMax, HoverLine, Area } from '@semcore/ui/d3-chart';
import { Text } from '@semcore/ui/typography';
import { scaleLinear } from 'd3-scale';
import React from 'react';
import AreaMockData from './mock';
const Demo = () => {
const MARGIN = 40;
const width = 500;
const height = 300;
const xScale = scaleLinear()
.range([MARGIN, width - MARGIN])
.domain(minMax(data, 'x'));
const yScale = scaleLinear()
.range([height - MARGIN, MARGIN])
.domain([0, 10]);
return (
<Plot data={data} scale={[xScale, yScale]} width={width} height={height}>
<YAxis>
<YAxis.Ticks />
<YAxis.Grid />
</YAxis>
<XAxis>
<XAxis.Ticks />
</XAxis>
<HoverLine.Tooltip x='x' wMin={100}>
{({ xIndex }) => {
return {
children: (
<>
<HoverLine.Tooltip.Title>{data[xIndex].x}</HoverLine.Tooltip.Title>
<Flex justifyContent='space-between'>
<HoverLine.Tooltip.Dot mr={4}>Line</HoverLine.Tooltip.Dot>
<Text bold>{data[xIndex].y ?? 'n/a'}</Text>
</Flex>
</>
),
};
}}
</HoverLine.Tooltip>
<Area x='x' y='y'>
<Area.Null />
<Area.Dots />
</Area>
</Plot>
);
};
const data = AreaMockData.XYEdgeCase;
export default Demo;
Custom line
tsx
import { Area, minMax, Plot, XAxis, YAxis } from '@semcore/ui/d3-chart';
import { scaleLinear } from 'd3-scale';
import { curveCardinal } from 'd3-shape';
import React from 'react';
import AreaMockData from './mock';
const customLineStyles = { strokeWidth: 4, stroke: 'pink' };
const Demo = () => {
const MARGIN = 40;
const width = 500;
const height = 300;
const xScale = scaleLinear()
.range([MARGIN, width - MARGIN])
.domain(minMax(data, 'x'));
const yScale = scaleLinear()
.range([height - MARGIN, MARGIN])
.domain([0, 10]);
return (
<Plot data={data} scale={[xScale, yScale]} width={width} height={height}>
<YAxis>
<YAxis.Ticks />
</YAxis>
<XAxis>
<XAxis.Ticks />
</XAxis>
<Area x='x' y='y' curve={curveCardinal}>
<Area.Line style={customLineStyles} />
</Area>
</Plot>
);
};
const data = AreaMockData.XY;
export default Demo;
Interpolation
If exact values of specific point is not available, you can pass interpolateValue and value will be automatically interpolated.
WARNING
🚨 Interpolation doesn't works with StackedArea.
tsx
import { Plot, XAxis, YAxis, minMax, Area } from '@semcore/ui/d3-chart';
import { scaleLinear } from 'd3-scale';
import { curveCardinal } from 'd3-shape';
import React from 'react';
import AreaMockData from './mock';
function formatDate(value: any, options: any) {
return new Intl.DateTimeFormat('en', options).format(value);
}
const Demo = () => {
const MARGIN = 40;
const width = 500;
const height = 300;
const xScale = scaleLinear()
.range([MARGIN, width - MARGIN])
.domain(minMax(data, 'time'));
const yScale = scaleLinear()
.range([height - MARGIN, MARGIN])
.domain([0, 10]);
return (
<Plot data={data} scale={[xScale, yScale]} width={width} height={height}>
<YAxis>
<YAxis.Ticks />
<YAxis.Grid />
</YAxis>
<XAxis>
<XAxis.Ticks ticks={data.map((d) => +d.time)}>
{({ value }) => ({
children: formatDate(value, {
month: 'short',
day: 'numeric',
}),
})}
</XAxis.Ticks>
</XAxis>
<Area x='time' y='line1' curve={curveCardinal}>
<Area.Dots display />
</Area>
<Area x='time' y='line2' curve={curveCardinal}>
<Area.Dots display />
</Area>
</Plot>
);
};
const data = AreaMockData.Interpolation;
export default Demo;
Legend and pattern fill
To make data available without relying only on colors (for example, for different kinds of colorblind and high-contrast modes), use the patterns property.
Note that for ChartLegend patterns property works only with default shape={'Checkbox'}.
tsx
import {
Plot,
XAxis,
YAxis,
minMax,
Area,
ChartLegend,
makeDataHintsContainer,
} from '@semcore/ui/d3-chart';
import { scaleLinear } from 'd3-scale';
import { curveCardinal } from 'd3-shape';
import React from 'react';
import AreaMockData from './mock';
function formatDate(value: any, options: any) {
return new Intl.DateTimeFormat('en', options).format(value);
}
const dataHints = makeDataHintsContainer();
const Demo = () => {
const MARGIN = 40;
const width = 500;
const height = 300;
const xScale = scaleLinear()
.range([MARGIN, width - MARGIN])
.domain(minMax(data, 'time'));
const yScale = scaleLinear()
.range([height - MARGIN, MARGIN])
.domain([0, 10]);
const [legendItems, setLegendItems] = React.useState(
Object.keys(data[0])
.filter((name) => name !== 'time')
.map((item, index) => {
return {
id: item,
label: `Line ${index + 1}`,
checked: true,
color: `chart-palette-order-${index + 1}`,
};
}),
);
const handleChangeVisible = React.useCallback((id: string, isVisible: boolean) => {
setLegendItems((prevItems) => {
return prevItems.map((item) => {
if (item.id === id) {
item.checked = isVisible;
}
return item;
});
});
}, []);
return (
<>
<ChartLegend
dataHints={dataHints}
items={legendItems}
onChangeVisibleItem={handleChangeVisible}
patterns
aria-label='Area chart legend'
/>
<Plot
data={data}
scale={[xScale, yScale]}
width={width}
height={height}
dataHints={dataHints}
patterns={true}
>
<YAxis>
<YAxis.Ticks />
<YAxis.Grid />
</YAxis>
<XAxis>
<XAxis.Ticks ticks={data.map((d) => +d.time)}>
{({ value }) => ({
children: formatDate(value, {
month: 'short',
day: 'numeric',
}),
})}
</XAxis.Ticks>
</XAxis>
{legendItems.map((item) => {
return (
item.checked && (
<Area key={item.id} x='time' y={item.id} curve={curveCardinal} color={item.color}>
<Area.Dots display />
</Area>
)
);
})}
</Plot>
</>
);
};
const data = AreaMockData.Interpolation;
export default Demo;