here is how i fixed using both touchscreen or mouse event x,y coordinates for the excellent d3 example above by wrapping event screen coordinates in a more "generic" x,y object.

(probably d3 has something similar to handle both types of events but lot of reading to find out..)

//get an class of {points: [{x, y},], type: event.type} clicked or touched
function geteventpoints(event)
    var retval = {point: [], type: event.type};

    //get x,y of mouse point or touch event
    if (event.type.startswith("touch")) {
        //return x,y of one or more touches
        //note 'changedtouches' has missing iterators and can not be iterated with foreach 
        for (var i = 0; i < event.changedtouches.length; i++) {
            var touch = event.changedtouches.item(i);
            retval.point.push({ x: touch.clientx, y: touch.clienty })
    else if (event.type.startswith("mouse")) {
        //return x,y of mouse event
        retval.point.push({ x: event.layerx, y: event.layery })

    return retval;

.. and here is how i would use it in the above d3 example to store the initial grab point y. and works for both mouse and touch.

check the fiddle

here how i solved the problem with using d3 and wanting to drag the document on mobile or touch screens. somehow with the d3 event subscription all chart area events where already blocked from bubbling up the dom.

was not able to figure out if d3 could be configured to pass canvas events on without touching them. so in a protest i just eliminated d3 as it was not much involved other than subscribing events.

not being a javascript master this is some fun code that subscribes the events the old way. to prevent chart touches from dragging the screen only when a chart point is grabed each of the handlers just have to return true and the event.preventdefault() is called to keep the event to your self.

//chartjs event handler attaching events to chart canvas
const charteventhandler = {

    //call init with a chartjs chart instance to apply mouse and touch events to its canvas.
    init(chartinstance) {

        //event handler for event types subscribed
        var evthandler =
        function myeventhandler(evt) {
            var cancel = false; 
            switch (evt.type) {
            case "mousedown":
            case "touchstart":
                cancel = begindrag(evt);
            case "mousemove":
            case "touchmove":
                cancel = duringdrag(evt);
            case "mouseup":
            case "touchend":
                cancel = enddrag(evt);

            if (cancel) {
                //prevent the event e from bubbling up the dom
                if (evt.cancelable) {
                    if (evt.preventdefault) {
                    if (evt.cancelbubble != null) {
                        evt.cancelbubble = true;

        //events to subscribe to
        var events = ['mousedown', 'touchstart', 'mousemove', 'touchmove', 'mouseup', 'touchend'];

        //subscribe events
        events.foreach(function (evtname) {
            chartinstance.canvas.addeventlistener(evtname, evthandler);


the handler above is initiated like this with an existing chart.js object:


the begindrag(evt), duringdrag(evt) and enddrag(evt) have the same basic function as in the d3 example above. just returns true when wanting to consume the event and not pasing it on for document panning and similar.

try it in this fiddle using a touch screen. unless you touch close to select a chart point the rest of the chart will be transparent to touch/mouse events and allow panning the page.


update: my previous answer got deleted because it only featured a link to a plugin solving the issue, however here comes the explanation to what it does:

the general procedure on how to achieve the desired behaviour is to

  1. intercept a mousedown (and check if it's a dragging gesture) on a given chart
  2. check if the mousedown was over a data point using the getelementatevent function
  3. on mousemove, translate the new y-pixel value into a data coordinate using the axis.getvalueforpixel function
  4. synchronously update the chart data using chart.update(0)

as pointed out in this chart.js issue.

in order to intercept the mousedown, mousemove and mouseup events (the dragging gesture), event listeners for said events need to be created. in order to simplify the creation of the listeners one may use the d3 library in this case as follows:
    .on('start', getelement)
    .on('drag', updatedata)
    .on('end', callback)

on mousedown (the 'start' event here), a function (getelement) may be called thatfetches the closest chart element to the pointers location and gets the id of the y-scale

function getelement () {
    var e = d3.event.sourceevent
    element = chartinstance.getelementatevent(e)[0]
    scale = element['_yscale'].id

on mousemove ('drag'), the chart data is supposed to be updated according to the current y-pixel value of the pointer. we can therefore create an updatedata function that gets the position of the clicked data point in the charts data array and the according dataset like this

function updatedata () {
  var e = d3.event.sourceevent
  var datasetindex = element['_datasetindex']
  var index = element['_index']
  var value = chartinstance.scales[scale].getvalueforpixel(e.clienty)[datasetindex].data[index] = value

and that's it! if you need to store the resulting value after dragging, you may also specify a callback function like this

function callback () {
  var datasetindex = element['_datasetindex']
  var index = element['_index']
  var value =[datasetindex].data[index]
  // e.g. store value in database

here is a working fiddle of the above code. the functionality is also the core of the chart.js plugin dragdata, which may be easier to implement in many cases.


in case anyone is looking for a solution that doesn't require the use of plugins, it's pretty straightforward to do it in vanilla chart.js.

here's a simple working example - just click and drag a data point

// some data to be plotted
var x_data = [1500,1600,1700,1750,1800,1850,1900,1950,1999,2050];
var y_data_1 = [86,114,106,106,107,111,133,221,783,2478];
var y_data_2 = [2000,700,200,100,100,100,100,50,25,0];

// globals
var activepoint = null;
var canvas = null;

// draw a line chart on the canvas context
window.onload = function () {

    // draw a line chart with two data sets
    var ctx = document.getelementbyid("canvas").getcontext("2d");
    canvas = document.getelementbyid("canvas");
    window.mychart = chart.line(ctx, {
        data: {
            labels: x_data,
            datasets: [
                    data: y_data_1,
                    label: "data 1",
                    bordercolor: "#3e95cd",
                    fill: false
                    data: y_data_2,
                    label: "data 2",
                    bordercolor: "#cd953e",
                    fill: false
        options: {
            animation: {
                duration: 0
            tooltips: {
                mode: 'nearest'

    // set pointer event handlers for canvas element
    canvas.onpointerdown = down_handler;
    canvas.onpointerup = up_handler;
    canvas.onpointermove = null;

function down_handler(event) {
    // check for data point near event location
    const points = window.mychart.getelementatevent(event, {intersect: false});
    if (points.length > 0) {
        // grab nearest point, start dragging
        activepoint = points[0];
        canvas.onpointermove = move_handler;

function up_handler(event) {
    // release grabbed point, stop dragging
    activepoint = null;
    canvas.onpointermove = null;

function move_handler(event)
    // locate grabbed point in chart data
    if (activepoint != null) {
        var data =;
        var datasetindex = activepoint._datasetindex;

        // read mouse position
        const helpers = chart.helpers;
        var position = helpers.getrelativeposition(event, mychart);

        // convert mouse position to chart y axis value 
        var chartarea = window.mychart.chartarea;
        var yaxis = window.mychart.scales["y-axis-0"];
        var yvalue = map(position.y, chartarea.bottom,, yaxis.min, yaxis.max);

        // update y value of active data point
        data.datasets[datasetindex].data[activepoint._index] = yvalue;

// map value to other coordinate system
function map(value, start1, stop1, start2, stop2) {
    return start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1))
body {
  font-family: helvetica neue, arial, sans-serif;
  text-align: center;

.wrapper {
  max-width: 800px;
  margin: 50px auto;

h1 {
  font-weight: 200;
  font-size: 3em;
  margin: 0 0 0.1em 0;

h2 {
  font-weight: 200;
  font-size: 0.9em;
  margin: 0 0 50px;
  color: #555;

a {
  margin-top: 50px;
  display: block;
  color: #3e95cd;
<!doctype html>

  <!-- head element: load the stylesheet and the chart.js library -->
    <title>draggable points</title>
    <script src=""></script>
    <link rel="stylesheet" type="text/css" href="style.css">

  <!-- body element: create a canvas and render a chart on it -->

    <!-- canvas element in a container -->
    <div class="wrapper">
      <canvas id="canvas" width="1600" height="900"></canvas>

    <!-- call external script to create and render a chart on the canvas -->
    <script src="script.js"></script>


Related Query

More Query from same tag