score:3
you can set the stacked property on bar datasets, so lines don't stack. try something like this:
data: {
datasets: [{
label: 'line 1',
data: [],
backgroundcolor: '#3788d8cc',
bordercolor: '#3788d8cc',
fill: false
}, {
label: 'stacked bar 1',
data: [],
backgroundcolor: '#e51c23',
bordercolor: '#e51c23',
type: 'bar',
stack: 'bar-stacked'
}, {
label: 'line 2',
data: [],
backgroundcolor: '#ff9800',
bordercolor: '#ff9800',
fill: false
},
{
label: 'stacked bar 2',
data: [],
type: 'bar',
backgroundcolor: '#3f51b5',
bordercolor: '#3f51b5',
stack: 'bar-stacked'
}],
labels: [],
},
options: {
maintainaspectratio: false,
legend: {
display: true
},
scales: {
xaxes: [{
display: true
}],
yaxes: [{
display: true
}]
}
}
score:4
another answer here - you can set a 'stack' identifier for the data you want to be cumulative (i.e. just the bars), and turn off 'fill' on the line graphs to draw just as a line rather than mountain range
score:21
you can get this functionality with a combination of setting a different yaxisid
(e.g. yaxisid: "bar-stacked"
) to each of your datasets, and then adding a second options.scales.yaxes
object. so you would have this as a dataset:
{
label: 'promoters',
backgroundcolor: "#aad700",
yaxisid: "bar-stacked",
data: [
50, 44, 52, 62, 48, 58, 59, 50, 51, 52, 53, 54
]
}
and then you would add an additional yaxes (the first one will be the collection of your line datasets [no yaxisid
in the example below], the second will be all of the bars you want stacked):
yaxes: [{
stacked: false,
ticks: {
beginatzero: true,
min: 0,
max: 100
}
}, {
id: "bar-stacked",
stacked: true,
display: false, //optional if both yaxes use the same scale
ticks: {
beginatzero: true,
min: 0,
max: 100
},
type: 'linear'
}]
full example is as follows:
<!doctype html>
<html>
<head>
<title>stacked bar chart</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.1.3/chart.bundle.js"></script>
<style>
canvas {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
</style>
</head>
<body>
<div style="width: 100%">
<canvas id="canvas"></canvas>
</div>
<script>
var barchartdata = {
labels: ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"],
datasets: [{
data: [
50, 30, 60, 70, 80, 90, 95, 70, 90, 20, 60, 95
],
type: 'line',
label: 'this year',
fill: false,
backgroundcolor: "#fff",
bordercolor: "#70cbf4",
bordercapstyle: 'butt',
borderdash: [],
borderdashoffset: 0.0,
borderjoinstyle: 'miter',
linetension: 0.3,
pointbackgroundcolor: "#fff",
pointbordercolor: "#70cbf4",
pointborderwidth: 1,
pointhoverradius: 5,
pointhoverbackgroundcolor: "#70cbf4",
pointhoverbordercolor: "#70cbf4",
pointhoverborderwidth: 2,
pointradius: 4,
pointhitradius: 10
}, {
data: [
25, 40, 30, 70, 60, 50, 40, 70, 40, 80, 30, 90
],
type: 'line',
label: 'last year',
fill: false,
backgroundcolor: "#fff",
bordercolor: "#737373",
bordercapstyle: 'butt',
borderdash: [10, 10],
borderdashoffset: 0.0,
borderjoinstyle: 'miter',
linetension: .3,
pointbackgroundcolor: "#fff",
pointbordercolor: "#737373",
pointborderwidth: 1,
pointhoverradius: 5,
pointhoverbackgroundcolor: "#737373",
pointhoverbordercolor: "#737373",
pointhoverborderwidth: 2,
pointradius: 4,
pointhitradius: 10
}, {
label: 'promoters',
backgroundcolor: "#aad700",
yaxisid: "bar-y-axis",
data: [
50, 44, 52, 62, 48, 58, 59, 50, 51, 52, 53, 54
]
}, {
label: 'passives',
backgroundcolor: "#ffe100",
yaxisid: "bar-y-axis",
data: [
20, 21, 24, 25, 26, 17, 28, 19, 20, 11, 22, 33
]
}, {
label: 'detractors',
backgroundcolor: "#ef0000",
yaxisid: "bar-y-axis",
data: [
30, 35, 24, 13, 26, 25, 13, 31, 29, 37, 25, 13
]
}]
};
window.onload = function() {
var ctx = document.getelementbyid("canvas").getcontext("2d");
window.mybar = new chart(ctx, {
type: 'bar',
data: barchartdata,
options: {
title: {
display: true,
text: "chart.js bar chart - stacked"
},
tooltips: {
mode: 'label'
},
responsive: true,
scales: {
xaxes: [{
stacked: true,
}],
yaxes: [{
stacked: false,
ticks: {
beginatzero: true,
min: 0,
max: 100
}
}, {
id: "bar-y-axis",
stacked: true,
display: false, //optional
ticks: {
beginatzero: true,
min: 0,
max: 100
},
type: 'linear'
}]
}
}
});
};
</script>
</body>
</html>
Source: stackoverflow.com
Related Query
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- Draw stacked horizontal bar chart with Average line using ChartJS
- Chart.js Mixed Bar and Line chart with different scales
- Show X axis on top and bottom in line chart rendered with Chart.js 2.4.0
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- Chart.js with dual axis on bar and line graph
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- Adding line over stacked line chart with ChartJS
- Chartjs 3.x - How to duplicate X axis on both sides of horizontal bar chart with only 1 dataset?
- Chart.js v2 - combined stacked bar chart and 2 unstacked lines
- chart js - bar chart with time scale on Y axis in Hours and Minutes
- Bar chart with min height and zero values - ChartJs
- Stacked bar chart with chartjs with included values
- Chart with Time axis only displaying first grid line and tick label (unitStepSize)
- Chart.js combined line and bar chart with differing data points
- ChartJs - stacked bar chart with groups - how to create second x-axis with stack id
- Chart.js bar chart with time on X axis and category on Y axis is not rendered
- ChartJS - would like to create a mixed chart with horizontal Bar and a dot to represent the answer from the current user
- Google Charts, HighCharts or ChartJS Dual Axis Gantt Chart and Line Chart Visualization
- Chart.js Draw a Stacked Bar Chart with Limit Line
- ChartJs line chart time cartesian axis number of ticks and wierd offset
- ChartJS 2.7.3 stacked bar chart with overlap
- Chartjs v3 overlap stacked bar chart with groups
- Stacked Bar Chart With Line Chart In Charts.js
- Multiple axis line chart with Chart.js and JSON data from Google Sheet
- ChartJS: Is this horizontal stacked bar and line graph multiaxis chart even possible?
- Chart.js with line chart and bar chart - bar chart not rendered although the max value of it is shown
- How to add vertical line in bar chart using chartJs and ng2-charts?
- Chartjs v2.0: stacked bar chart
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
More Query from same tag
- How could I put a string for the points on the x-axis?
- chart.js d3js chart in time scale, data from CSV file
- Redraw Chart.js with PHP JSON
- How to show stacked and unstacked bar graphs together in Chart.js
- Add dynamic dataset to chart.js
- ChartJS charts not generating within tabs
- Specify varying thickness of each bar in Chart.js bar chart
- Chart JS Displays Only Bigger Dataset But Has Both Datasets?
- JavaScript Chart.JS - issue keeping charts in two rows, instead everything in stacked into one column
- how to show only each 15th x-labels in line chart chart.js in angular 6?
- How to set the size of the arc in ChartJS?
- react-chartjs state update error
- Problem for display a chart with Chart.js and Angular
- Chart.js dynamic bar width
- react-rchartjs-2 Radar - dataset with less points than lables
- charts.js Stacked Bar Graphs - selecting row and dataset selected
- Pass data to Chart Js Laravel
- Render pie chart datasets out of 100% -- Chart.JS
- JSON cyclic object value when posting data in ChartJS
- Limit data in Chart.js with Flask/SQLAlchemy in python
- Pushing API Data into Chart.js
- chart.js plugins.register function with outer data
- How to create two x-axes label using chart.js
- Chart.js addData is undefined when using SignalR
- hereChartJS Line Chart with Time Axis
- Using associative PHP array in JS to later on use in chart generated via chart.js
- (Chart.js) Is there a way to compare one chart with another so as not to have this inconsistent effect of small values being as big as big values?
- Chart.js loading data colors from PHP
- Chart.js find visible data points following zoom
- highlightFill for radar charts in chartJS