Stacked horizontal bar chart
TIP
For core principles, concept description, API and changelog, refer to the D3 chart.
Horizontal bar
You can rotate a chart using the <HorizontalBar/> component by swapping scaleBand and scaleLinear. See more about scaleBand and scaleLiner in the Bar chart example.
Horizontal stacked bar
To draw a horizontal stacked chart, use the <StackBar.HorizontalBar/> component.
tsx
import { Flex, Box } from '@semcore/ui/base-components';
import { Plot, StackBar, YAxis, XAxis, HoverRect } from '@semcore/ui/d3-chart';
import { Text } from '@semcore/ui/typography';
import { scaleLinear, scaleBand } from 'd3-scale';
import React from 'react';
import StackedBarMockData from './mock';
const Demo = () => {
const MARGIN = 40;
const width = 500;
const height = 300;
const xScale = scaleLinear()
.range([MARGIN * 2, width - MARGIN])
.domain([0, 20]);
const yScale = scaleBand()
.range([height - MARGIN, MARGIN])
.domain(data.map((d) => d.bar))
.paddingInner(0.4)
.paddingOuter(0.2);
return (
<Plot data={data} scale={[xScale, yScale]} width={width} height={height}>
<YAxis hide={false}>
<YAxis.Ticks />
</YAxis>
<XAxis>
<XAxis.Ticks />
<XAxis.Grid />
</XAxis>
<HoverRect.Tooltip y='bar' wMin={100}>
{({ yIndex }) => {
return {
children: (
<>
<HoverRect.Tooltip.Title>{data[yIndex].bar}</HoverRect.Tooltip.Title>
<Flex justifyContent='space-between'>
<HoverRect.Tooltip.Dot mr={4}>Category 1</HoverRect.Tooltip.Dot>
<Text bold>{data[yIndex].Category1}</Text>
</Flex>
<Flex mt={2} justifyContent='space-between'>
<HoverRect.Tooltip.Dot mr={4}>Category 2</HoverRect.Tooltip.Dot>
<Text bold>{data[yIndex].Category2}</Text>
</Flex>
<Flex mt={2} justifyContent='space-between'>
<Box mr={4}>Total</Box>
<Text bold>{data[yIndex].Category1 + data[yIndex].Category2}</Text>
</Flex>
</>
),
};
}}
</HoverRect.Tooltip>
<StackBar y='bar'>
<StackBar.HorizontalBar x='Category1' />
<StackBar.HorizontalBar x='Category2' />
</StackBar>
</Plot>
);
};
const data = StackedBarMockData.Default;
export default Demo;
Legend and pattern fill
Note that for ChartLegend patterns property works only with default shape={'Checkbox'}.
tsx
import { Flex } from '@semcore/ui/base-components';
import Card from '@semcore/ui/card';
import {
Plot,
StackBar,
YAxis,
XAxis,
ChartLegend,
makeDataHintsContainer,
} from '@semcore/ui/d3-chart';
import { scaleLinear, scaleBand } from 'd3-scale';
import React from 'react';
import StackedBarMockData from './mock';
const dataHints = makeDataHintsContainer();
const Demo = () => {
const MARGIN = 40;
const width = 500;
const height = 300;
const xScale = scaleLinear()
.range([MARGIN * 2, width - MARGIN])
.domain([0, 20]);
const yScale = scaleBand()
.range([height - MARGIN, MARGIN])
.domain(data.map((d) => d.bar))
.paddingInner(0.4)
.paddingOuter(0.2);
const [legendItems, setLegendItems] = React.useState(
Object.keys(data[0])
.filter((name) => name !== 'bar')
.map((item, index) => {
return {
id: item,
label: `Category ${index + 1}`,
checked: true,
color: `chart-palette-order-${index + 1}`,
};
}),
);
const [highlightedLine, setHighlightedLine] = React.useState(-1);
const handleChangeVisible = React.useCallback((id: string, isVisible: boolean) => {
setLegendItems((prevItems) => {
return prevItems.map((item) => {
if (item.id === id) {
item.checked = isVisible;
}
return item;
});
});
}, []);
const handleMouseEnter = React.useCallback((id: string) => {
setHighlightedLine(legendItems.findIndex((line) => line.id === id));
}, []);
const handleMouseLeave = React.useCallback(() => {
setHighlightedLine(-1);
}, []);
return (
<Card w='550px'>
<Card.Header pt={4}>
<Card.Title tag='h4' m={0} inline={true}>
Chart legend
</Card.Title>
</Card.Header>
<Card.Body tag={Flex} direction='column'>
<ChartLegend
items={legendItems}
onChangeVisibleItem={handleChangeVisible}
onMouseEnterItem={handleMouseEnter}
onMouseLeaveItem={handleMouseLeave}
dataHints={dataHints}
patterns
aria-label='Stacked horizontal bar chart legend'
/>
<Plot
data={data}
scale={[xScale, yScale]}
width={width}
height={height}
dataHints={dataHints}
patterns
>
<YAxis>
<YAxis.Ticks />
<YAxis.Grid />
</YAxis>
<XAxis>
<XAxis.Ticks />
</XAxis>
<StackBar y='bar'>
{legendItems.map((stack, index) => {
return (
stack.checked && (
<StackBar.HorizontalBar
x={stack.id}
key={stack.id}
color={stack.color}
transparent={highlightedLine !== -1 && highlightedLine !== index}
/>
)
);
})}
</StackBar>
</Plot>
</Card.Body>
</Card>
);
};
const data = StackedBarMockData.ThreeBars;
export default Demo;
Last updated: