Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

highcharts/3-stacked-bar #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions highcharts-api/highcharts/3-stacked-bar/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

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

<style>
.highcharts-button-box {
stroke: blue !important;
stroke-width: 2px !important;
}
jszuminski marked this conversation as resolved.
Show resolved Hide resolved
</style>
</head>

<body>
Expand Down
130 changes: 130 additions & 0 deletions highcharts-api/highcharts/3-stacked-bar/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
Highcharts.chart('container', {
chart: {
type: 'bar',
marginTop: 50,

events: {
load() {
const chart = this;

//Rendering additional texts
chart.renderer.text('Issue', 65, 35).add();
chart.renderer.text('Record count', chart.plotLeft + 15, 35).add();

chart.actionsText = chart.renderer.text('Actions', chart.plotWidth, 35).add();

//Rendering 'How to fix buttons'
const axisHeight = chart.xAxis[0].toPixels(1) - chart.xAxis[0].toPixels(0);
jszuminski marked this conversation as resolved.
Show resolved Hide resolved

chart.buttons = Array.from({ length: chart.yAxis[0].series.length }, () => 0);
chart.yAxis[0].series.forEach((_, ind) => {
chart.buttons[ind] = chart.renderer.button('How to fix', chart.plotWidth, chart.plotTop + axisHeight * (ind * 4 + 1) / 4,
() => console.log('Button clicked!')).add();
});

Choose a reason for hiding this comment

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

Wouldn't it be possible to assign buttons to category objects? And try using forEach to refer to them using the first parameter instead of the index, I think it would be clearer and it would take less code. What catches my eye is the _ in forEach((_, ind)


//Creating space for buttons
chart.yAxis[0].update({
max: chart.yAxis[0].dataMax * 1.75
});
jszuminski marked this conversation as resolved.
Show resolved Hide resolved

//Calculating sums for each category in xAxis
let sumTable = Array.from({ length: chart.yAxis[0].series.length }, () => 0);
chart.yAxis[0].series.forEach(serie => {
serie.yData.forEach((value, index) => sumTable[index] += value);
});

//Adding dataLabels based on sumTable
chart.xAxis[0].series[0].data.forEach((serie, index) => serie.update({
dataLabels: [{
inside: 'false',
align: 'right',
x: 50,
format: `${Math.floor(sumTable[index] / 1000)} K`
}]
}));
jszuminski marked this conversation as resolved.
Show resolved Hide resolved
},

render() {
const chart = this;

const axisHeight = chart.xAxis[0].toPixels(1) - chart.xAxis[0].toPixels(0);
jszuminski marked this conversation as resolved.
Show resolved Hide resolved

if(chart.actionsText) {
chart.actionsText.attr({
x: chart.plotWidth
});
}

chart.yAxis[0].series.forEach((_, ind) => {
if(chart.buttons[ind]) {
chart.buttons[ind].attr({
x: chart.plotWidth,
y: chart.plotTop + axisHeight * (ind * 4 + 1) / 4
});
}
});
}
}
},

title: {
text: ''
},

xAxis: {
categories: ['Data', 'Emails', 'Duplicates', 'Support'],

tickWidth: 1,
tickLength: 100,
tickColor: 'gray',
gridLineColor: 'gray',
gridLineWidth: 1,

lineWidth: 0,
lineColor: 'transparent'
},

yAxis: {
title: {
text: ''
},

labels: {
formatter() {
return this.value / 1000;
}
},

gridLineWidth: 0,
},

legend: {
enabled: false,
},

plotOptions: {
series: {
stacking: 'normal',
dataLabels: {
enabled: 'true',
formatter() {
return typeof this === 'object' ? '' : this;
}
}
jszuminski marked this conversation as resolved.
Show resolved Hide resolved
},
},

series: [{
name: 'Tokyo',
data: [4000, 4590, 6580, 15000],
}, {
name: 'Warsaw',
data: [5325, 3421, 12321, 6321]
}, {
name: 'Budapest',
data: [5555, 1555, 8900, 5321]
}, {
name: 'London',
data: [3355, 15321, 8321, 9321]
}]
});