Angular 2 ng2-charts donut add text

帅比萌擦擦* 提交于 2019-12-12 23:23:11

问题


I am using ng2-charts based on chart.js. I am trying to add text in the middle of donut chart but I am not able to set text or register plugin. Can anyone help? So far I found several incomplete answers how to do this in javascript but I am struggling with angular 2 and typescript implementation of chart.js.

my template file:

    <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>

and ts file: 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:


Copied your code to a Plunker, works just fine:

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

I think that adding the script this way will not work because angular would sanitize it. Try adding the script after the chart.js script on your page in <script> or preferably in it's own file.



来源:https://stackoverflow.com/questions/43687757/angular-2-ng2-charts-donut-add-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!