score:0
I came across this issue when trying to find out how to stack specific datasets of a line
diagram in Chart.js
v2.x.
Chart.js
v3.x supports the stack
attribute for line diagrams but in Chart.js
v2.x:
- you have to use multiple y-axis
- to align the values you need to calculate the maximal value yourself and set
ticks.max
orticks.suggestedMax
in the axis configuration.
false console:
let yMax = 3000; // Calculate value beforehand.
var config = {
"type": "line",
"data": {
"labels": ["Mar-2016","Apr-2016","May-2016"],
"datasets": [ {
"label": "Crossville",
"backgroundColor": "#009900",
"data": ["2268.44","2268","31"]
}, {
"label": "test",
"borderColor": "#ffff00",
"data": ["181.92","181.92", "1000"],
"yAxis": 'A'
},{
"label": "benchmark",
"backgroundColor": "#ff3300",
"data": ["2632.29","2632.29","500"]
}]
},
"options": {
"scales": {
"yAxes": [
{
"id": "A",
ticks: {
beginAtZero: true,
display: true,
suggestedMax: yMax
},
},
{
"stacked": true,
"ticks": {
beginAtZero: true,
display: true, // Set this to false so that only one axis is shown!!!
suggestedMax: yMax
},
},
],
"xAxes": [{
"position": "bottom",
"stacked": true,
"display": true
}]
}
}
};
var chartElement = document.getElementById('chart');
var chart = new Chart(chartElement, config);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>
<div style="width:75%;">
<canvas id="chart"></canvas>
</div>
score:3
It's odd that your code doesn't work, if you change the non-stacked dataSet to a line graph you see the data! Maybe worth posting an issue on the chartjs github.
But...
There was a feature released in 2.5.0
which will allow for this sort of scenario. More on what it is can be read here, https://github.com/chartjs/Chart.js/issues/2643.
Basically, you can add a new property stack
on each of your dataSets to denote which stack you would like it to go on to, so for example with your data you could remove the extra scales and have 2 stacks
var config = {
"type": "bar",
"data": {
"labels": ["Mar-2016","Apr-2016"],
"datasets": [ {
"label": "Crossville",
"backgroundColor": "#009900",
"data": ["2268.44","2268"],
stack: 2,
}, {
"label": "test",
"backgroundColor": "#ffff00",
"data": ["181.92","181.92"],
stack: 2
},{
"label": "benchmark",
"type": "bar",
"backgroundColor": "#ff3300",
"data": ["2632.29","2632.29"],
stack: 1
}]
},
"options": {
"scales": {
"yAxes": [{
"id": "stacked_testY",
"type": 'linear',
"position": "left",
"stacked": true,
"display": true
}],
"xAxes": [{
"position": "bottom",
"stacked": true,
"display": true
}]
}
}
};
var chartElement = document.getElementById('chart');
var chart = new Chart(chartElement, config);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
<div style="width:75%;">
<canvas id="chart"></canvas>
</div>
Source: stackoverflow.com
Related Query
- How to show stacked and unstacked bar graphs together in Chart.js
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- How to show data values in top of bar chart and line chart in chart.js 3
- angular-chart.js : how to show numbers in each bar of stacked bar chart
- Chart.js v2 - combined stacked bar chart and 2 unstacked lines
- how can i show labels and value in both on bar chart
- How can I get my Chart.JS bar chart to stack two data values together on each bar, and print a calculated value on each bar?
- How to show symbols after the label and before the numeric value using chart.js Bar chart in Angular
- How to add second Y-axis for Bar and Line chart in Chart.js?
- How to create stacked bar chart using react-chartjs-2?
- How to display inline values in a stacked bar chart with Chart.js?
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- Remove the label and show only value in tooltips of a bar chart
- Showing Percentage and Total In stacked Bar Chart of chart.js
- PrimeNg bar chart how to show a label for the y-axis
- How can I datalabels and Sum display in the same time on a stacked bar
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- In Stacked horizontal bar chart how to remove the vertical line in Chart.js?
- How to show gradient vertically on chart js grouped bar chart?
- How to show different product name for every bar in chart js
- Chart.js: How to get x-axis labels to show on top of bars in bar chart
- Chart.Js how to hide datapoints with "0" value in Stacked Bar Chart
- How to plot chart from external JSON and format X-AXIS to show time using Chart.JS?
- How do I create a stacked horizontal bar chart with Chart.js in React?
- How to show the chartjs bar chart data values labels as text?
- how to reduce list chart to one and use select dropdown to show selection without refresh page?
- how to show information of hidden bar graphs also in a Tooltip using chart.js in angular?
- How can I create a stacked bar chart with Charts.JS that displays relative rather than absolute values?
- ChartJs - stacked bar chart with groups - how to create second x-axis with stack id
- Chart.js how to display multiple labels on multi bar stacked chart
More Query from same tag
- functional component not re-rendering after setState update hook
- Type 'Chart' is missing the following properties
- what is `dart.js` in chartjs.dart example
- JavaScript implementation doesnt work (Chart.js)
- What is the correct format for time data in chartjs?
- Angular-chart.js - Make line chart does not curve
- How can I get chart.js to automatically add colours for dynamic labels?
- Chart.js, Can you override the legend label box color?
- how to add color to each data in chartjs scatter plot
- Display Json from Node.js in Chart.js with React
- ChartJS multiple tooltip callbacks not working
- Created an onclick function to remove data from a line chart in ChartsJs, but getting "Cannot read property 'data' of undefined" error
- chartjs: How to remove specific label
- I am using chartjs node canvas to render a chart however the graph options are being ignored
- How to set equal width between ticks - chartjs
- Why is the data from my API not displaying on my chart.js chart in React?
- Vue ChartJS is not updating
- Chartjs-node installation failing
- chart.js Multiple barchart in time scale won't display all
- Is it possible to maintain two different font size in Chart.JS based on screen size?
- How to get the database data into ChartJS using codeigniter
- How do I add time sourced from an external source as an X axis to a ChartJS graph?
- Is it possible to have a canvas next to another canvas without space on the bottom?
- How to display "%" sign on mouse hover in Pie-Chart
- Trying to insert another label for a piechart in ChartsJS
- How to draw multiple line on y axis for same x axis in chartjs v2?
- Problems trying to render some data on chartjs
- Returning fetched JSON correctly but can't get it into Chart.JS
- Split assoc array by keys
- Is it possible to avoid the shrinking of Chart.js pie charts when accompanied by labels?