Chartjs打字稿定义缺少某些字段

时间:2020-07-12 15:10:33

标签: javascript angular typescript chart.js

我正在尝试使用chart.js / angular / typescript组合实现某种行为(我试图在图表单击时显示一条垂直线)。我可以使用以下代码执行此操作:

Chart.plugins.register({
    afterDatasetsDraw: (chart) => {
      if (chart.tooltip._active && chart.tooltip._active.length) {
          var activePoint = chart.tooltip._active[0],
             ctx = chart.ctx,
             y_axis = chart.scales['y-axis-0'],
             x = activePoint.tooltipPosition().x,
             topY = y_axis.top,
             bottomY = y_axis.bottom;
          ctx.save();
          ctx.beginPath();
          ctx.moveTo(x, topY);
          ctx.lineTo(x, bottomY);
          ctx.lineWidth = 2;
          ctx.strokeStyle = '#07C';
          ctx.stroke();
          ctx.restore();
       }
    }
});

问题是,JavaScript Chart API中可用的某些字段在打字稿定义文件中不可用(例如chart.tooltip和chart.scales)。因此,在没有打字稿的情况下运行时代码运行良好,但是在打字稿编译时却失败了(对于定义,我使用的是DefinitelyTyped:https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/chart.js/index.d.ts)。我想知道打字稿定义文件中是否故意省略了其中某些字段(例如,由于封装),还是背后有其他原因?

0 个答案:

没有答案