Reputation: 548
I want to create a stacked bar chart.
I saw sample and documentation from chart.js but i didn't find example using dynamic dataset and tooltip.
What i want to achieve is like a picture below.
So the first stacked bar is consisted of two datasheet; 15 and 30, sum to 45. When user hovering this first stacked bar it will show tooltip; "15 (1-3)" for bottom bar and "30 (3-6)" for hovering top bar. The problem is i don't know how to attach dynamic dataset (for one single date it will contain 1 to 10 datasets) and show the corresponding tooltip. I can generate line/bar chart with dynamic datasets but stacked bar is new for me.
My json data source is like this:
[
{
"date":"2020-02-07",
"range":"1-3",
"off":15
},
{
"date":"2020-02-07",
"range":"3-6",
"off":30
},
{
"date":"2020-02-08",
"range":"1-4",
"off":25
},
{
"date":"2020-02-08",
"range":"4-5",
"off":15
},
{
"date":"2020-02-08",
"range":"5-6",
"off":15
}
]
It should genereate the following stacked bar.
Could you provide me some examples or maybe i miss some part in documentation that already show what i want?
Thank you.
Upvotes: 0
Views: 1954
Reputation: 26190
Please have a look at https://stackoverflow.com/a/59658062/2358409 to understand how it basically works.
Starting from this information, some data processing together with the use of chartjs-plugin-datalabels leads you to the following sample code.
const data = [
{
"date":"2020-02-07",
"range":"1-3",
"off":15
},
{
"date":"2020-02-07",
"range":"3-6",
"off":30
},
{
"date":"2020-02-08",
"range":"1-4",
"off":25
},
{
"date":"2020-02-08",
"range":"4-5",
"off":15
},
{
"date":"2020-02-08",
"range":"5-6",
"off":15
}
];
const distinctDates = Array.from(new Set(data.map(o => o.date)));
const dataPerDate = distinctDates.map(d => data.filter(o => o.date == d));
const numberOfDatasets = Math.max.apply(null, dataPerDate.map(data => data.length));
const dataSets = [];
for (let i = 0; i < numberOfDatasets; i++) {
dataSets.push({
data: dataPerDate.map(data => i < data.length ? data[i].off : 0),
ranges: dataPerDate.map(data => i < data.length ? data[i].range : ''),
backgroundColor: distinctDates.map(d =>
"rgba(" + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + ", 0.5)"),
categoryPercentage: 1,
barPercentage: 1
});
}
new Chart(document.getElementById("myChart"), {
type: "bar",
data: {
labels: distinctDates,
datasets: dataSets
},
options: {
plugins: {
datalabels: {
display: context => context.dataset.data[context.dataIndex] > 0,
formatter: (value, context) => value + ' (' + context.dataset.ranges[context.dataIndex] + ')'
}
},
legend: {
display: false
},
tooltips: {
enabled: false
},
scales: {
xAxes: [{
stacked: true
}],
yAxes: [{
stacked: true
}]
}
}
});
canvas {
max-width: 300px
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels"></script>
<canvas id="myChart" width="100" height="100"></canvas>
Upvotes: 3