JavaScript缩放图表功能

问题描述 投票:-1回答:1

我编写了此JavaScript代码来跟踪折线图,完成后,我需要修改代码以允许通过鼠标选择缩放和平移画布。

function zoom(){
    startindex = document.getElementById("startpoint").selectedIndex;
    endindex= document.getElementById("endpoint").selectedIndex;
    var newlabels=oldlabels.slice(startindex, endindex);
    var newdata=oldDatas.slice(startindex,endindex);
    data.labels= newlabels;
    data.datasets.data=newdata;
    myChart = new Chart(context).Line(data);
 }

the link to my chart

javascript jquery html5-canvas zoom linechart
1个回答
0
投票

了解context.translate和context.scale方法。

了解这些内容即可平移和缩放。

变焦的基本原理是:

  • 转换为您想要放大的点
  • 将画布缩放到所需的缩放级别
  • 擦除画布
  • 在-chartWidth / 2和-chartHeight / 2重新绘制图表。

潘甚至更容易:

  • 根据您要平移的x距离进行翻译
  • 擦除画布
  • 在0,0重新绘制图表
© www.soinside.com 2019 - 2024. All rights reserved.