Stacked 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 { curveCardinal } from 'd3-shape';
import React from 'react';
import StackedAreaMockData from './mock';
const formatDate = (type: 'axis' | 'tooltip') => (value: any) => {
const options =
type === 'axis'
? {
month: 'short' as const,
day: 'numeric' as const,
}
: {
year: 'numeric' as const,
month: 'long' as const,
day: 'numeric' as const,
};
return new Intl.DateTimeFormat('en', options).format(value);
};
const Demo = () => {
return (
<Chart.Area
data={data}
plotWidth={500}
plotHeight={200}
groupKey='time'
tooltipValueFormatter={formatDate('tooltip')}
axisXValueFormatter={formatDate('axis')}
stacked={true}
curve={curveCardinal}
aria-label='Stacked area chart'
/>
);
};
const data = StackedAreaMockData.Default;
export default Demo;
Stacked area
If you need to display a part-to-whole ratio – use the <StackedArea/> and <StackedArea.Area/> components.
tsx
import { Flex, Box } from '@semcore/ui/base-components';
import { Plot, XAxis, YAxis, minMax, StackedArea, HoverLine } from '@semcore/ui/d3-chart';
import { Text } from '@semcore/ui/typography';
import { scaleLinear } from 'd3-scale';
import { curveCardinal } from 'd3-shape';
import React from 'react';
import StackedAreaMockData 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, 15]);
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>
<HoverLine.Tooltip x='time' wMin={100}>
{({ xIndex }) => {
return {
children: (
<>
<HoverLine.Tooltip.Title>
{formatDate(data[xIndex].time, {
year: 'numeric',
month: 'long',
day: 'numeric',
})}
</HoverLine.Tooltip.Title>
<Flex justifyContent='space-between'>
<HoverLine.Tooltip.Dot mr={4}>Stack 1</HoverLine.Tooltip.Dot>
<Text bold>{data[xIndex].stack1}</Text>
</Flex>
<Flex mt={2} justifyContent='space-between'>
<HoverLine.Tooltip.Dot mr={4}>Stack 2</HoverLine.Tooltip.Dot>
<Text bold>{data[xIndex].stack2}</Text>
</Flex>
<Flex mt={2} justifyContent='space-between'>
<HoverLine.Tooltip.Dot mr={4}>Stack 3</HoverLine.Tooltip.Dot>
<Text bold>{data[xIndex].stack3}</Text>
</Flex>
<Flex mt={2} justifyContent='space-between'>
<Box mr={4}>Total</Box>
<Text bold>
{data[xIndex].stack1 + data[xIndex].stack2 + data[xIndex].stack3}
</Text>
</Flex>
</>
),
};
}}
</HoverLine.Tooltip>
<StackedArea x='time'>
<StackedArea.Area y='stack1' curve={curveCardinal}>
<StackedArea.Area.Dots />
</StackedArea.Area>
<StackedArea.Area y='stack2' curve={curveCardinal}>
<StackedArea.Area.Dots />
</StackedArea.Area>
<StackedArea.Area y='stack3' curve={curveCardinal}>
<StackedArea.Area.Dots />
</StackedArea.Area>
</StackedArea>
</Plot>
);
};
const data = StackedAreaMockData.Default;
export default Demo;
Edge cases
- If a part of the chart has no data – use a dashed line to draw that period.
- If the data has only one value – display it as a dot.
- Two consecutively known values will automatically be displayed as the
StackedAreacomponent.
tsx
import { Flex } from '@semcore/ui/base-components';
import { Plot, XAxis, YAxis, minMax, StackedArea, HoverLine } from '@semcore/ui/d3-chart';
import { Text } from '@semcore/ui/typography';
import { scaleLinear } from 'd3-scale';
import React from 'react';
import StackAreaMockData from './mock';
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, 15]);
return (
<Plot data={data} scale={[xScale, yScale]} width={width} height={height} patterns>
<YAxis>
<YAxis.Ticks />
<YAxis.Grid />
</YAxis>
<XAxis>
<XAxis.Ticks ticks={data.map((d) => +d.time)} />
</XAxis>
<HoverLine.Tooltip x='time' wMin={100}>
{({ xIndex }) => {
return {
children: (
<>
<HoverLine.Tooltip.Title>{data[xIndex].time}</HoverLine.Tooltip.Title>
<Flex justifyContent='space-between'>
<HoverLine.Tooltip.Dot mr={4}>Stack 1</HoverLine.Tooltip.Dot>
<Text bold>{data[xIndex].stack1 ?? 'n/a'}</Text>
</Flex>
<Flex mt={2} justifyContent='space-between'>
<HoverLine.Tooltip.Dot mr={4}>Stack 2</HoverLine.Tooltip.Dot>
<Text bold>{data[xIndex].stack2 ?? 'n/a'}</Text>
</Flex>
<Flex mt={2} justifyContent='space-between'>
<HoverLine.Tooltip.Dot mr={4}>Stack 3</HoverLine.Tooltip.Dot>
<Text bold>{data[xIndex].stack3 ?? 'n/a'}</Text>
</Flex>
</>
),
};
}}
</HoverLine.Tooltip>
<StackedArea x='time'>
<StackedArea.Area y='stack1'>
<StackedArea.Area.Null />
<StackedArea.Area.Dots />
</StackedArea.Area>
<StackedArea.Area y='stack2'>
<StackedArea.Area.Null />
<StackedArea.Area.Dots />
</StackedArea.Area>
<StackedArea.Area y='stack3'>
<StackedArea.Area.Null />
<StackedArea.Area.Dots />
</StackedArea.Area>
</StackedArea>
</Plot>
);
};
const data = StackAreaMockData.EdgeCase;
export default Demo;
Legend and pattern fill
Note that for ChartLegend patterns property works only with default shape={'Checkbox'}.
tsx
import { Flex, Box } from '@semcore/ui/base-components';
import {
Plot,
XAxis,
YAxis,
minMax,
StackedArea,
HoverLine,
makeDataHintsContainer,
ChartLegend,
} from '@semcore/ui/d3-chart';
import { Text } from '@semcore/ui/typography';
import { scaleLinear } from 'd3-scale';
import { curveCardinal } from 'd3-shape';
import React from 'react';
import StackedAreaMockData from './mock';
function formatDate(value: any, options: any) {
return new Intl.DateTimeFormat('en', options).format(value);
}
const lineColors: Record<string, string> = {
stack1: '--blue-300',
stack2: '--green-200',
stack3: '--orange-400',
};
const dataHints = makeDataHintsContainer();
type DataItem = typeof StackedAreaMockData.Default[0];
const getDegaultLegendItems = () => {
return Object.keys(data[0])
.filter((name) => name !== 'time')
.map((item, index) => {
return {
id: item,
label: `Stack ${index + 1}`,
checked: true,
color: lineColors[item],
};
});
};
const Demo = () => {
const [legendItems, setLegendItems] = React.useState(getDegaultLegendItems);
const handleChangeVisible = React.useCallback((id: string, isVisible: boolean) => {
setLegendItems((prevItems) => {
const newItems = prevItems.map((item) => {
if (item.id === id) {
item.checked = isVisible;
}
return item;
});
return newItems;
});
}, []);
const MARGIN = 28;
const width = 500;
const height = 260;
const xScale = scaleLinear()
.range([MARGIN, width - MARGIN])
.domain(minMax(data, 'time'));
const yScale = scaleLinear()
.range([height - MARGIN, MARGIN])
.domain([0, 15]);
return (
<>
<ChartLegend
dataHints={dataHints}
items={legendItems}
shape='Checkbox'
patterns
aria-label='Stacked area chart legend'
onChangeVisibleItem={handleChangeVisible}
/>
<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>
<HoverLine.Tooltip x='time' wMin={100}>
{({ xIndex }) => {
return {
children: (
<>
<HoverLine.Tooltip.Title>
{formatDate(data[xIndex].time, {
year: 'numeric',
month: 'long',
day: 'numeric',
})}
</HoverLine.Tooltip.Title>
{legendItems.map((item) => {
return (
<Flex key={item.id} justifyContent='space-between'>
<HoverLine.Tooltip.Dot mr={4} color={lineColors[item.id]}>
{item.label}
</HoverLine.Tooltip.Dot>
{/* @ts-ignore */}
<Text bold>{data[xIndex][item.id as keyof DataItem]}</Text>
</Flex>
);
})}
<Flex mt={2} justifyContent='space-between'>
<Box mr={4}>Total</Box>
<Text bold>
{data[xIndex].stack1 + data[xIndex].stack2 + data[xIndex].stack3}
</Text>
</Flex>
</>
),
};
}}
</HoverLine.Tooltip>
<StackedArea x='time'>
{legendItems.map((item) =>
item.checked
? (
<StackedArea.Area
key={item.id}
y={item.id}
fill={`chart-palette-order-${item.id}`}
color={lineColors[item.id]}
curve={curveCardinal}
>
<StackedArea.Area.Dots />
</StackedArea.Area>
)
: null,
)}
</StackedArea>
</Plot>
</>
);
};
const data = StackedAreaMockData.Default;
export default Demo;