首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图表注释未在角度2中显示

图表注释未在角度2中显示
EN

Stack Overflow用户
提问于 2018-03-06 05:30:35
回答 2查看 1.3K关注 0票数 2

我正在尝试将chartjs- plugin -annotation插件与Chart.js和Angular结合使用。

我基于chartjs-plugin-annotation示例代码构建了一个非常精简的图表,它使用简单的HTML和JavaScript很好地工作,显示了几个点和一条水平线:

代码语言:javascript
复制
<!doctype html>
<html>

<head>
    <script src="./Chart.bundle.js"></script>
    <script src="./chartjs-plugin-annotation.js"></script>
</head>

<body>
    <div style="width:75%">
        <div>
            <canvas id="canvas"></canvas>
        </div>
    </div>
    <script>
        var scatterChartData = {
          datasets: [{
            data: [
              { x:0, y:0 },
              { x:10, y:10 },
              { x:5, y:10 }
            ]
          }]
        };
        window.onload = function() {
            var ctx = document.getElementById("canvas").getContext("2d");
            window.myScatter = Chart.Scatter(ctx, {
                data: scatterChartData,
                options: {
                    scales: {
                        xAxes: [{
                        }],
                        yAxes: [{
                        }]
                    },
                    annotation: {
                        annotations: [{
                            type: 'line',
                            mode: 'horizontal',
                            scaleID: 'y-axis-1',
                            value: 5,
                            borderColor: 'rgba(255, 0, 0, 0.5)',
                            borderWidth: 4,
                        }]
                    }
                }
            });
        };
    </script>
</body>

</html>

但是,当我尝试在Angular中做同样的事情时,图表注释并没有出现,尽管图表是按预期显示的:

代码语言:javascript
复制
import { Component, OnInit, ViewChild } from '@angular/core';
import { Chart } from 'chart.js';
import * as annotation from 'chartjs-plugin-annotation';

@Component({
  selector: 'app-graphtest',
  template: `
     <div style="width:75%">
       <div>
         <canvas #canvas></canvas>
       </div>
     </div>`
})
export class GraphTest implements OnInit {

  constructor() {}

  ngOnInit() {
    let ctx = this.canvas.nativeElement.getContext('2d');
    this.myScatter = Chart.Scatter(
            ctx,
            {
                data: this.scatterChartData,
                options: {
                    scales: {
                        xAxes: [{
                        }],
                        yAxes: [{
                        }]
                    },
                    annotation: {
                        annotations: [{
                            type: 'line',
                            mode: 'horizontal',
                            scaleID: 'y-axis-1',
                            value: 5,
                            borderColor: 'rgba(255, 0, 0, 0.5)',
                            borderWidth: 4,
                        }]
                    }
                }
            }
        );
  }

  @ViewChild('canvas') canvas;

  myScatter;

  scatterChartData = {
    datasets: [{
      data: [
        { x:0, y:0 },
        { x:10, y:10 },
        { x:5, y:10 }
      ]
    }]
  };

}

有人知道我做错了什么吗?

EN

回答 2

Stack Overflow用户

发布于 2018-07-20 16:15:50

在App.module.ts的导入区域中,粘贴以下行import 'chartjs-plugin-annotation';

票数 2
EN

Stack Overflow用户

发布于 2018-09-05 04:17:42

添加以下行:

代码语言:javascript
复制
Chart.pluginService.register(annotation);

在您的ngInit()函数的顶部,应该可以做到这一点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49119805

复制
相关文章

相似问题

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