Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions highcharts-api/highcharts/06-stacked-bar/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">

<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>

</head>

<body>
<div id="container"></div>
<div id="container"></div>
</body>

<script src="main.js"></script>
<script src="main.js"></script>
142 changes: 142 additions & 0 deletions highcharts-api/highcharts/06-stacked-bar/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
const buttonAttrs = {
height: 6,
zIndex: 10,
stroke: 'blue',
};
const buttonCSS = { 'font-size': '13px' };
const labelCSS = { 'font-size': '10px' };

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to set such attribs where you create elements with renderer, no need for such an abstraction here


const chartEvents = {
render: function() {
const chart = this;

if (chart.svgElements) {
chart.svgElements.forEach(e => e.destroy());

@dqmrf dqmrf Jun 24, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to update coords instead. Should fix this

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed it's better :)

}
chart.svgElements = [];

const buttons = addButtons(chart);
chart.svgElements.push(...buttons);

const labels = addLabels(chart);
chart.svgElements.push(...labels);
}
}

Highcharts.chart('container', {
dataTable: {
columns: {
Categories: ['Data', 'Emails', 'Duplicates', 'Support'],
Orange: [100, 130, 35, 30],
Green: [15, 0, 15, 10],
Blue: [110, 110, 30],
Red: [15, 0, 10, 5]
}
},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you using dataTable here? feels like an overkill

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know. It was just interesting how it works

chart: {
type: 'bar',
events: chartEvents,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that you like to keep the config small and define all helpers outside, which is a common pattern, but not here - with such small demos we tend to keep all the logic just in the chart config, here under relevant chart events - can you refactor your solution to follow that pattern, please?

marginTop: 20
},
title: {
text: null
},
legend: {
enabled: false
},
xAxis: {
type: 'category',
lineWidth: 0,
gridLineWidth: 1
},
yAxis: {
title: { text: 'Amount' },
softMax: 400,
gridLineWidth: 0,
stackLabels: {
enabled: true,
formatter: function() {
return this.total + ' K';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, but actually why do we need formatter here? There is a closely related, simpler and more performant way ;)

}
}
},
plotOptions: {
series: {
dataMapping: {
name: 'Categories'
},
stacking: 'normal'
}
},
series: [{
dataMapping: {
y: 'Orange'
}
}, {
dataMapping: {
y: 'Green'
}
}, {
dataMapping: {
y: 'Blue'
}
}, {
dataMapping: {
y: 'Red'
}
}]
});

function addLabels(chart) {
const labelY = -5;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are hardcoding coordinates, and it work, but have you seen this useful api option, which we tend you use for such cases? https://api.highcharts.com/class-reference/Highcharts.Axis#toPixels

const label1X = 3,
label2X = chart.plotLeft - 3,
label3X = chart.plotWidth;

const label1 = chart.renderer
.label('Issue', label1X, labelY)
.css(labelCSS)
.add();

const label2 = chart.renderer
.label('Record Count', label2X, labelY)
.css(labelCSS)
.add();

const label3 = chart.renderer
.label('Action', label3X, labelY)
.css(labelCSS)
.add();

return [label1, label2, label3];
}

function addButtons(chart) {
const buttonX = chart.plotWidth;
const buttons = [];

chart.series[0].points.forEach((point, i) => {
const pointX = chart.plotTop + point.shapeArgs.x;
const pointWidth = point.shapeArgs.width;

const button = chart.renderer
.button(
'How to fix',
buttonX,
pointX,
function () {},
buttonAttrs
)
.css(buttonCSS)
.add();

buttons.push(button);

// You can only get correct button height after it was rendered.
button.attr({
y: pointX + pointWidth / 2 - button.height / 2
});
});

return buttons;
}