简体   繁体   English

如何在chartjs canvas中绑定touchmove?

[英]How to bind touchmove in chartjs canvas?

I'm using chartjs to draw line charts and can successfully bind onHover but unable to bind touchmove.我正在使用chartjs绘制折线图,可以成功绑定onHover但无法绑定touchmove。 From chartjs documentation ( https://www.chartjs.org/docs/latest/general/interactions/events.html#event-option ) i can't find a way to bind touchmove.从chartjs文档( https://www.chartjs.org/docs/latest/general/interactions/events.html#event-option )我找不到绑定touchmove的方法。

const ctx = document.getElementById("myChart");
    const myChart = new Chart(ctx, {
      type: "line",
      data,
      legend: { display: false },
      options: {
        tooltips: {
          enabled: false
        }
        onHover: function(event, array = []) {
          if (array?.length) {
            var activeElement = array[0];
            if ("_index" in activeElement) {
              //console.log(activeElement);
              const index = activeElement["_index"];
              const model = activeElement["_model"];
              const xPosition = model["x"];
            }
          }
        }

I tried binding mousemove to canvas element(addlistener to canvas dom element) but it desnt contain relevant information(array/events etc) so of no use to me.我尝试将 mousemove 绑定到 canvas 元素(将侦听器添加到 canvas dom 元素),但它不包含相关信息(数组/事件等),所以对我没有用。 Any guidance on how to bind touchmove to chartjs canvas element?有关如何将 touchmove 绑定到 chartjs canvas 元素的任何指导?

If you bind the mousemove event to the canvas itself you can get the elements/datasets with the build in methods of chart.js by passing it the event itself.如果将 mousemove 事件绑定到 canvas 本身,则可以通过将事件本身传递给 chart.js 的内置方法来获取元素/数据集。

https://www.chartjs.org/docs/latest/developers/api.html#getelementatevente https://www.chartjs.org/docs/latest/developers/api.html#getelementateevente

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM