首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Angular中使用chartjs-plugin-annotation

如何在Angular中使用chartjs-plugin-annotation
EN

Stack Overflow用户
提问于 2019-11-14 14:42:07
回答 1查看 1.7K关注 0票数 0

我在Angular项目中的版本是:"chart.js":"^2.9.2","chartjs-plugin-annotation":"^0.5.7",

在键入import语句时:

代码语言:javascript
复制
import * as ChartAnnotation from 'chartjs-plugin-annotation';

我把这个放在它上面

代码语言:javascript
复制
Could not find a declaration file for module 'chartjs-plugin-annotation'. 'c:/XRay/xray/node_modules/chartjs-plugin-annotation/src/index.js' implicitly has an 'any' type.
  Try `npm install @types/chartjs-plugin-annotation` if it exists or add a new declaration (.d.ts) file containing `declare module 'chartjs-plugin-annotation';`ts(7016)

我正试着用这个在我的散点图上画一条线

代码语言:javascript
复制
this.scatterChart = new Chart('myChart', {

        type: 'scatter',
        //plugins: [ChartAnnotation],
        data: {
            datasets: [{
                label: 'Scatter Dataset',
                pointBackgroundColor: ['yellow', 'blue', 'red', 'green', 'orange', 'indigo'],

                data: [
                  { x: -10, y: 0 }, 
                    { x: 0, y: 10 }, 
                    { x: 3, y: 8 }, 
                    { x: 1, y: 4 }, 
                    { x: 9, y: 1 }, 
                    { x: -1, y: 5 }
                ]
            }]
        },
        options: {
          legend: {
            display: false
          },

            scales: {
                xAxes: [{
                    type: 'linear',
                    position: 'bottom'
                }]
            },
            annotation:{
              drawTime: 'afterDatasetsDraw',
              annotations:[{
              type: 'line',
              id: 'vline',
              mode: 'horizontal',
              yscaleID: 'y-axis-0',
              value: 5,
              borderColor: 'green',
              borderWidth: 1,
              label: {
                enabled: false,
                content: 'Test label'
              }
            }]
          }
        } as ChartOptions


    });
EN

回答 1

Stack Overflow用户

发布于 2019-11-18 17:58:41

在我将import语句更改为:

代码语言:javascript
复制
var ChartAnnotation=require('chartjs-plugin-annotation');

并将scaleID从x轴0更改为x轴1:

代码语言:javascript
复制
annotation: {
      annotations: [
          {
              type: 'line',
              mode: 'vertical',
              scaleID: 'x-axis-1',
              value: avgXValue,
              borderColor: 'red',
              borderWidth: 2,
          },
          {
            type: 'line',
              mode: 'horizontal',
              scaleID: 'y-axis-1',
              value: avgYValue,
              borderColor: 'red',
              borderWidth: 2,
          }
      ]
  } 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58850841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档