角度2 ng2图表甜甜圈添加文本

时间:2017-04-28 19:38:49

标签: angular chart.js ng2-charts

我正在使用基于chart.js的ng2-charts。我想在圆环图中间添加文本,但我无法设置文本或注册插件。有人可以帮忙吗?到目前为止,我发现了几个不完整的答案如何在javascript中执行此操作,但我正在努力使用chart.js的angular 2和typescript实现。

我的模板文件:

    <div style="display: block" class="donut">
  <canvas #mycanvas baseChart
          [data]="doughnutChartData"
          [labels]="doughnutChartLabels"
          [chartType]="doughnutChartType"
          [options]="doughnutChartOptions"
          [datasets]="doughnutChartDatasets"
          (chartHover)="chartHovered($event)"
          (chartClick)="chartClicked($event)"></canvas>
</div>
<script>
  Chart.pluginService.register({
    afterDraw: function (chart) {
      if (chart.config.options.elements.center) {
        var helpers = Chart.helpers;
        var centerX = (chart.chartArea.left + chart.chartArea.right) / 2;
        var centerY = (chart.chartArea.top + chart.chartArea.bottom) / 2;

        var ctx = chart.chart.ctx;
        ctx.save();
        var fontSize = helpers.getValueOrDefault(chart.config.options.elements.center.fontSize, Chart.defaults.global.defaultFontSize);
        var fontStyle = helpers.getValueOrDefault(chart.config.options.elements.center.fontStyle, Chart.defaults.global.defaultFontStyle);
        var fontFamily = helpers.getValueOrDefault(chart.config.options.elements.center.fontFamily, Chart.defaults.global.defaultFontFamily);
        var font = helpers.fontString(fontSize, fontStyle, fontFamily);
        ctx.font = font;
        ctx.fillStyle = helpers.getValueOrDefault(chart.config.options.elements.center.fontColor, Chart.defaults.global.defaultFontColor);
        ctx.textAlign = 'center';
        ctx.textBaseline = 'middle';
        ctx.fillText(chart.config.options.elements.center.text, centerX, centerY);
        ctx.restore();
      }
    },
  })
</script>

和ts文件:app.component.ts

 import { Component } from '@angular/core';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app works!';

  colors = {
    indigo: '#14143e',
    pink: '#fd1c49',
    orange: '#ff6e00',
    yellow: '#f0c800',
    mint: '#00efab',
    cyan: '#05d1ff',
    purple: '#841386',
    white: '#fff'
  };


  // Doughnut
  public doughnutChartLabels:string[] = ['Drive Score', ''];
  public doughnutChartData:number[] = [85, 15];
  public doughnutChartType:string = 'doughnut';

  public doughnutChartOptions: any = {
    cutoutPercentage: 80,
    elements: {
      center: {
        text: 'Hello',
        fontColor: '#000',
        fontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif",
        fontSize: 24,
        fontStyle: 'normal'
      }
    }
  };
  public doughnutChartDatasets: any[] = [
    {
      data: [300, 50, 100],
      options: this.doughnutChartOptions,
      backgroundColor: [
        "#FF6384",
        "#36A2EB",
        "#FFCE56"
      ],
      hoverBackgroundColor: [
        "#FF6384",
        "#36A2EB",
        "#FFCE56"
      ]
    }];

  // events
  public chartClicked(e:any):void {
    console.log(e);
  }

  public chartHovered(e:any):void {
    console.log(e);
  }

  constructor(){

  }




}

1 个答案:

答案 0 :(得分:1)

将您的代码复制到Plunker,效果很好:

  

plunker:https://plnkr.co/edit/UGH3BKiWmgFqFWf78ZUE?p=preview

我认为以这种方式添加脚本是行不通的,因为angular会对它进行清理。尝试在<script>页面上的chart.js脚本之后添加脚本,或者最好在其自己的文件中添加脚本。