Position axisY to center of Chart Workaround - CanvasJS JavaScript Charts
Position axisY to center of Chart Workaround - CanvasJS JavaScript Charts
by canvasjs
HTML
<script src="http://canvasjs.com/assets/script/canvasjs.min.js"></script>
<br/><!-- Just so that JSFiddle's Result label doesn't overlap the Chart -->
<div id="chartContainer1" style="height: 360px; width: 100%;"></div>
<div id="chartContainer2" style="height: 360px; width: 40px;"></div>
CSS
#chartContainer1 {
position: absolute;
z-index: 1;
}
JavaScript
var chart1 = new CanvasJS.Chart("chartContainer1", {
title: {
text: "Position axisY to center of Chart"
},
zoomEnabled: true,
zoomType: "xy",
rangeChanged: rangeChanged,
backgroundColor: "transparent",
axisY: {
lineThickness: 0,
tickLength: 0,
gridThickness: 0,
labelFormatter: function(e) {
return ""
}
},
axisX: {
title: "Axis X"
},
data: [
{
type: "spline",
dataPoints: [
{ x: -40, y: 71 },
{ x: -30, y: 55 },
{ x: -20, y: 50 },
{ x: -10, y: 65 },
{ x: 10, y: 68 },
{ x: 20, y: 28 },
{ x: 30, y: 34 },
{ x: 40, y: 14 }
]
}
]
});
chart1.render();
var chart2 = new CanvasJS.Chart("chartContainer2", {
title: {
text: ".",
fontColor: "transparent"
},
backgroundColor: "transparent",
axisY: {
gridThickness: 0
},
data: [
{
type: "line"
}
]
});
chart2.render();
chart2.axisY[0].set("viewportMinimum", chart1.axisY[0].get("viewportMinimum"), false);
chart2.axisY[0].set("viewportMaximum", chart1.axisY[0].get("viewportMaximum"), false);
chart2.axisY[0].set("labelFontSize", chart1.axisY[0].get("labelFontSize"), false);
chart2.axisY[0].set("labelMaxWidth", chart1.axisY[0].get("labelMaxWidth"), false);
chart2.axisX[0].set("margin", chart1.axisX[0].bounds.height - chart1.axisX[0].bounds.x1, false);
if(chart1.title.text)
chart2.title.set("fontSize", chart1.title.get("fontSize"));
else
chart2.title.set("fontSize", 0);
$("#chartContainer2").css("margin-left", chart1.axisX[0].convertValueToPixel(0) - chart2.axisY[0].bounds.x2 + "px");
$(window).resize(function() {
chart2.axisX[0].set("margin", chart1.axisX[0].bounds.height - chart1.axisX[0].bounds.x1);
$("#chartContainer2").css("margin-left", chart1.axisX[0].convertValueToPixel(0) - chart2.axisY[0].bounds.x2 + "px");
});
function rangeChanged(e) {
chart2.options.axisY.viewportMinimum = e.chart.axisY[0].viewportMinimum;
...