Combining line and column chart without reducing dataPointWidth of Column Chart- CanvasJS JavaScript Charts
Combining line and column chart without reducing dataPointWidth of Column Chart- CanvasJS JavaScript Charts
by canvasjs
HTML
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<br/><!-- Just so that JSFiddle's Result label doesn't overlap the Chart -->
<div id="chartContainer" style="height: 360px; width: 100%;"></div>
JavaScript
var chart = new CanvasJS.Chart("chartContainer", {
title:{
text: "combined data"
},
data: [
{
type: "column",
dataPoints: [
{ label: "apple", y: 10 },
{ label: "orange", y: 15 },
{ label: "banana", y: 25 },
{ label: "mango", y: 30 },
{ label: "grape", y: 28 }
]
},
{
type: "line",
dataPoints: [
{ x: 0, y: 15 },
{ x: 0.25, y: 15.5 },
{ x: 0.5, y: 17 },
{ x: 0.75, y: 18.5 },
{ x: 1, y: 21 },
{ x: 2, y: 30 },
{ x: 3, y: 33 },
{ x: 4, y: 15 },
]
}
]
});
chart.render();
resizeColumnWidth();
window.addEventListener('resize', resizeColumnWidth, true);
function resizeColumnWidth() {
var secondDataSeries = chart.options.data[chart.options.data.length - 1];
chart.set("dataPointWidth", null, false);
chart.options.data.splice(-1, 1);
chart.render();
var maxWidth = chart.get("dataPointWidth");
chart.options.data.push(secondDataSeries);
chart.axisX[0].set("minimum", chart.axisX[0].get("minimum"), false);
chart.axisX[0].set("maximum", chart.axisX[0].get("maximum"), false);
chart.set("dataPointWidth", maxWidth);
}