2

I am using chartjs for making a small project. I am getting some confusion and hard luck in editing the points.

Is there any function in this library I can bind an onclick event to which will show me a pop up and I can remove the point?

Here is the summary what I want:

  1. Click on the point and a popup appear
  2. After clicking on the remove button it removed the point and redraw the point. Right now i am only using simple line chart this is my jsFiddle

I am using chartjs 2.6

fat potato
  • 503
  • 1
  • 9
  • 29

1 Answers1

5

You can use the onclick event in the option to show the popup. Then you can check whether a point was clicked with getElementsAtEvent and if so remove it from the options and update the chart. I've updated your jsfiddle.

var option = {
    showLines: true,
    onClick: function(evt) {   
      var element = myLineChart.getElementAtEvent(evt);
      if(element.length > 0)
      {
        var ind = element[0]._index;
        if(confirm('Do you want to remove this point?')){
          data.datasets[0].data.splice(ind, 1);
          data.labels.splice(ind, 1);
          myLineChart.update(data);
        }
      }
    }
};
Shiffty
  • 2,086
  • 2
  • 26
  • 31
  • @Shiftfty Thank you for your response mate. I did not understand the pop up point . I have to include the pop up in the event? Can i use the modal pop up in this ? – fat potato Nov 16 '17 at 15:09
  • @fatpotato You don't have to include the initialisation of the popup in the event but you have to call its show method. If your modal has a button you can move the code to remove the point and update the chart to the on click event of that button. – Shiffty Nov 17 '17 at 06:03
  • let me test and get back to you – fat potato Nov 17 '17 at 07:08
  • @fatpotato were you able to test this? – Shiffty Nov 20 '17 at 14:12
  • i am really sorry for not testing it. give me a day to test this. applogies – fat potato Nov 20 '17 at 14:57
  • 1
    @Sgiffty i am really sorry for not accepting it. This works fine as you have said. Thank you :) – fat potato Jan 11 '18 at 07:33