Add peak circles using the draw events
A plugin adds a circle at the peak of every bar, sized by its value.
src/main.ts
import 'chartist/styles.css';
import { barChart, getMultiValue, node, type ChartPlugin } from 'chartist';
// Adds a circle at the peak of every bar, its radius depending on the value.
const peakCircles: ChartPlugin = {
name: 'peak-circles',
install(chart) {
// A series comes through `compose` after its bars, so we rebuild its
// children with a circle after each bar.
chart.on('compose', series => {
if (series.role !== 'series') {
return;
}
return series.children.reduce((result, child) => {
const withChild = result.append(child);
if (child.role !== 'bar') {
return withChild;
}
// The circle sits at the bar peak's coordinates
const { x2, y2 } = child.attrs;
return withChild.append(
node(
'circle',
{
cx: x2,
cy: y2,
r: Math.abs(Number(getMultiValue(child.ctx.value))) * 2 + 5
},
'ct-slice-pie'
)
);
}, series.empty());
});
}
};
// Create a simple bi-polar bar chart
barChart(
'#chart',
{
labels: ['W1', 'W2', 'W3', 'W4', 'W5', 'W6', 'W7', 'W8', 'W9', 'W10'],
series: [[1, 2, 4, 8, 6, -2, -1, -4, -6, -2]]
},
{
plugins: [peakCircles],
high: 10,
low: -10,
axisX: {
labelInterpolationFnc: (value, index) => (index % 2 === 0 ? value : null)
}
}
);import 'chartist/styles.css';
import { BarChart, Svg, getMultiValue } from 'chartist';
import { compat } from 'chartist/compat';
// Create a simple bi-polar bar chart
const chart = new BarChart(
'#chart',
{
labels: ['W1', 'W2', 'W3', 'W4', 'W5', 'W6', 'W7', 'W8', 'W9', 'W10'],
series: [[1, 2, 4, 8, 6, -2, -1, -4, -6, -2]]
},
{
// 1.x draw events come from the compat plugin in v2.
plugins: [compat],
high: 10,
low: -10,
axisX: {
labelInterpolationFnc: (value, index) => (index % 2 === 0 ? value : null)
}
}
);
// Listen for draw events on the bar chart
chart.on('draw', data => {
// If this draw event is of type bar we can use the data to create additional content
if (data.type === 'bar') {
// We use the group element of the current series to append a simple circle with the bar peek coordinates and a circle radius that is depending on the value
data.group.append(
new Svg(
'circle',
{
cx: data.x2,
cy: data.y2,
r: Math.abs(Number(getMultiValue(data.value))) * 2 + 5
},
'ct-slice-pie'
)
);
}
}); Live
Open in the playgroundView as MarkdownSwitch to 1.x above the code to see the same chart written with the Chartist 1.x classes.