我可以绑定一个onclick事件并在chartjs中编辑一个点(Can I bind an onclick event and edit a point in chartjs)

编程入门 行业动态 更新时间:2024-10-21 09:23:06
我可以绑定一个onclick事件并在chartjs中编辑一个点(Can I bind an onclick event and edit a point in chartjs)

我正在使用chartjs来制作一个小型项目。 我在编辑要点时遇到了一些困惑和运气。

有没有这个库中的任何功能,我可以绑定一个onclick事件,它会显示我弹出,我可以删除点?

以下是我想要的摘要:

单击该点,将出现一个弹出窗口 单击“删除”按钮后,它将删除该点并重绘该点。 现在我只使用简单的折线图这是我的jsFiddle

我正在使用chartjs 2.6

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:

Click on the point and a popup appear 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

最满意答案

您可以使用选项中的onclick事件来显示弹出窗口。 然后,您可以使用getElementsAtEvent检查点是否被点击,如果是这样,请将其从选项中删除并更新图表。 我更新了你的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); } } } };

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); } } } };

更多推荐

本文发布于:2023-08-02 03:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1368393.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并在   绑定   编辑   事件   onclick

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!