Add Stripline Dynamically on Clicking over the Chart and allow Dragging it | CanvasJS JavaScript Charts
Add Stripline Dynamically on Clicking over the Chart and allow Dragging it | 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 selected = -1;
var chart = new CanvasJS.Chart("chartContainer", {
theme: "light2",
title: {
text: "Stacked Column Chart with Draggable stripLines"
},
axisX: [{
stripLines: [{
value: 15,
showOnTop: true,
thickness: 2,
color: "#9999fd",
lineDashType: "dash"
}]
}],
data: [{
type: "stackedColumn",
color: "#343434",
dataPoints: [
{ x: 10, y: 71 },
{ x: 20, y: 55 }
]
},{
type: "stackedColumn",
color: "#006699",
dataPoints: [
{ x: 10, y: 68 },
{ x: 20, y: 50 }
]
},{
type: "stackedColumn",
color: "#099",
dataPoints: [
{ x: 10, y: 35 },
{ x: 20, y: 95 }
]
}]
});
chart.render();
$(".canvasjs-chart-canvas").last().on("mousedown", function(e) {
// Get the selected stripLine & change the cursor
var parentOffset = $(this).parent().offset();
var relX = e.pageX - parentOffset.left;
var relY = e.pageY - parentOffset.top;
var snapDistance = 5;
for(var i = 0; i < chart.options.axisX[0].stripLines.length; i++) {
if(relX > chart.axisX[0].stripLines[i].get("bounds").x1 - snapDistance && relX < chart.axisX[0].stripLines[i].get("bounds").x2 + snapDistance && relY > chart.axisX[0].stripLines[i].get("bounds").y1 && relY < chart.axisX[0].stripLines[i].get("bounds").y2) {
selected = i;
$(this).css("cursor","pointer");
}
}
});
$(".canvasjs-chart-canvas").last().on("mousemove", function(e) {
var parentOffset = $(this).parent().offset();
var relX = e.pageX - parentOffset.left;
var relY = e.pageY - parentOffset.top;
var snapDistance = 5;
// Move the selected stripLine
if(selected !== -1) {
chart.options.axisX[0].stripLines[selected].value = chart.axisX[0].convertPixelToValue(relX);
chart.render();
}
for(var i = 0; i < chart.options.axisX[0].stripLines.length; i++) {
if(relX > chart.axisX[0].stripLines[i].get("bounds").x1 - snapDistance && relX <...