Setting y value of a dataPoint by select and drag - CanvasJS HTML-JavaScript chart
Setting y value of a dataPoint by select and drag - CanvasJS HTML-JavaScript chart
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>
CSS
.canvasjs-chart-toolbar {
display: none !important;
}
JavaScript
var chart = new CanvasJS.Chart("chartContainer",
{
title:{
text:"Set y value of a dataPoint by select and drag"
},
zoomEnabled: true,
zoomType:"y",
rangeChanging: getViewport, //We are usig rangeChanging for knowing new y value after dragging
data: [
{
type: "line",
mouseover: mouseoverHandler,
mouseout: mouseoutHandler,
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}
]
},
{
//It's a dummy invisible dataseries only for the purpose of increasing y value even upto samll values
toolTipContent:null,
color:"transparent",
dataPoints:[{y:.1},{y:.2}]
}
]
});
chart.render();
document.getElementById("chartContainer").onmousedown = function(){
chart.isMouseDown = true;
};
function mouseoutHandler(e){
if(!e.chart.isMouseDown)
e.chart.draggingDataPoint = null;
}
function mouseoverHandler(e){
e.chart.draggingDataPoint = e.dataPoint;
}
//Function for getting viewport and set corrosponding y value
function getViewport(e){
if(e.chart.isMouseDown && e.chart.draggingDataPoint){
//For updating the y value
e.chart.draggingDataPoint.y = (e.axisY[0].viewportMaximum-e.chart.draggingDataPoint.y) >
(e.chart.draggingDataPoint.y - e.axisY[0].viewportMinimum) ?
e.axisY[0].viewportMaximum : e.axisY[0].viewportMinimum;
}
if(!e.chart.options.axisY)
e.chart.options.axisY = {};
e.chart.options.axisY.viewportMinimum = e.chart.options.axisY.viewportMaximum = null;
e.chart.isMouseDown = false;
e.chart.draggingDataPoint = null;
}