Angular + ChartJS 添加 resetZoom 按钮

问题描述

添加了 chartjs 缩放插件,现在我想添加一个按钮来重置缩放。 我看到的所有教程都使用了一个函数,该函数在仅使用 chartjs 时工作正常。但是由于我的图表在一个 Angular 组件中并且在一个类中,我必须尝试 afaik 来访问一个方法。 所以认为这将是与 Class 一起工作的方式

  myChart!: Chart;
...
this.myChart = new Chart('myChart',{
...
        zoom:{
             
              pan: {
                enabled: true,modifierKey:'alt',mode: 'xy',},zoom :{                  
                drag:{
                  enabled:true,threshold: 1,pinch: {
                  enabled: true
                },mode:'x',}
            }
          },onClick(e) {
            
            console.log('onClick e'+e.native)
            console.log(e.type);
          }
...
     resetChart()
      {
        this.myChart.resetZoom();
      }

我的结果是,即使没有按下按钮,它也会触发 resetChart。这意味着我不能再缩放,也不能平移/捏合/拖动 所以我试着像这样通过 ViewChild 调用

ParentComponent.html

<button (click)="resetChart.resetChart()" id='resetZoomChart' value="reset">Reset Zoom</button>

parentComponent.ts

  @ViewChild("resetChart",{ static: false }) resetChart!: ViewChartComponent;

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)