chartjs-plugin-datalabels不显示chartjs上的值

9wbgstp7  于 2022-11-06  发布在  Chart.js
关注(0)|答案(1)|浏览(246)

我正在使用以下版本:

  • Angular :14.0.6
  • 类型脚本:4.7.4
  • Chart.js:^3.8.2
  • chartjs插件注解:^1.4.0版本
  • 图表插件数据标签:^2.0.0版本
  • ng 2-图表:^4.0.0

我尝试用虚拟数据创建一个简单的演示Doughnat图表。问题是,无论我尝试什么,这些值都不会显示在图表上。我用that作为例子帮助自己,但我发现新版本的这种方法不起作用。以下是我迄今为止尝试的方法。

我的应用程序.模块.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HeaderComponent } from './header/header.component';
import { FoodManagementModule } from './food-management/food-management.module';
import { NgChartsModule } from 'ng2-charts';

@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    FoodManagementModule,
    NgChartsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

我的模板如下所示:

<div class="container">
        <div class="row">
          <div class="col-sm-6">
            <canvas
              baseChart
              [datasets]="doughnutChartData"
              [labels]="doughnutChartLabels"
              [options]="doughnutChartOptions"
              [plugins]="doughnutChartPlugins"
              [legend]="doughnutChartLegend"
              [type]="doughnutChartType"
            >
            </canvas>
          </div>
        </div>
      </div>

在TS文件中,我导入了pluginDataLabels以及其他类型,如下所示:

import {
  ChartConfiguration,
  ChartDataset,
  ChartOptions,
  ChartType,
} from "chart.js";
import * as pluginDataLabels from "chartjs-plugin-datalabels";

"然后我试着这样“

public doughnutChartOptions: ChartConfiguration<'doughnut'>['options'] = {
    responsive: true,
    plugins: {
      datalabels: {
        display: true,
        anchor: 'center',
        align: 'center',
        font: {
          size: 20,
        },
        color: "#000000"
      },
      tooltip: {
        enabled: true,
      },
      title: {
        display: true,
        text: 'Macros'
      },
      legend: {
        display: true,
      },
    },
  };

  // public doughnutChartOptions: ChartOptions = {
  //   responsive: true,
  //   plugins: {
  //     datalabels: {
  //       color: "#36A2EB",
  //       display: true,
  //       anchor: "end",
  //       align: "end",
  //       font: {
  //         size: 20,
  //       },
  //     },
  //   },

  public doughnutChartLabels: string[] = ["Proteins", "Carbohydrates", "Fats"];
  public doughnutChartType: ChartType = "doughnut";
  public doughnutChartLegend: boolean = true;
  public doughnutChartPlugins = [pluginDataLabels];
  public doughnutChartData: ChartDataset[] = [
    {
      label: "Example",
      data: [33, 44, 55],
      datalabels: {
        color: "#36A2EB",
        display: true,
      },
    },
  ];

  constructor() {}

  ngOnInit(): void { }

但似乎没有什么工作..当我使用旧版本,并按照上面的例子,所有的工作,但我想这样做与较新的版本..任何建议,我做错了什么?

chhkpiq4

chhkpiq41#

您没有按照文档中的说明注册插件

import {Chart} from 'chart.js';
import ChartDataLabels from 'chartjs-plugin-datalabels';

Chart.register(ChartDataLabels);

相关问题