Show interlacedcolor for selected dataPoint (Workaround using stripline) - CanvasJS JavaScript Charts
Show interlacedcolor for selected dataPoint (Workaround using stripline) - 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", {
axisX: {
stripLines: [{color: "#C4C4C4"}]
},
data: [
{
type: "column",
dataPoints: [
{ x: 10, y: 71 },
{ x: 20, y: 55 },
{ x: 30, y: 50 },
{ x: 40, y: 65 },
{ x: 50, y: 95 },
{ x: 60, y: 68 },
{ x: 70, y: 28 },
{ x: 80, y: 34 },
{ x: 90, y: 14 }
]
}
]
});
chart.render();
var x, y;
var parentOffset = $(chart.container).offset();
jQuery(chart.container).on({
mousemove: function(e) {
var dps = [];
for(var i = 0; i < chart.data[0].dataPoints.length; i++) {
dps.push(chart.data[0].dataPoints[i].x)
}
x = e.clientX - parentOffset.left;
y = e.clientY - parentOffset.top;
var nearestX = closest(chart.axisX[0].convertPixelToValue(x), dps);
var dpsDiff = dps[1] - dps[0];
if(x > chart.plotArea.x1 && x < chart.plotArea.x2 && y > chart.plotArea.y1 && y < chart.plotArea.y2){
chart.options.axisX.stripLines[0].startValue = nearestX - dpsDiff / 2;
chart.options.axisX.stripLines[0].endValue = nearestX + dpsDiff / 2;
}else{
chart.options.axisX.stripLines[0].startValue = chart.options.axisX.stripLines[0].endValue = null;
}
chart.render();
},
mouseout: function(e){
chart.options.axisX.stripLines[0].startValue = chart.options.axisX.stripLines[0].endValue = null;
chart.render();
}
});
function closest (num, arr) {
var curr = arr[0];
var diff = Math.abs (num - curr);
for (var val = 0; val < arr.length; val++) {
var newdiff = Math.abs (num - arr[val]);
if (newdiff < diff) {
diff = newdiff;
curr = arr[val];
}
}
return curr;
}