angular - 如何使用 ng2 图表在圆环图中显示标签?

标签 angular typescript chart.js ng2-charts

我正在使用 ng2 圆环图来显示一些项目,根据圆环图文档,它一切正常,但我需要更改的是,每个切片的标签显示仅在悬停时显示,但我需要的是它应该在每个切片中都是默认的。

Dashboard.component.html:

<canvas #mycanvas baseChart
    [data]="doughnutChartData"
    [labels]="doughnutChartLabels"
    [chartType]="doughnutChartType"
    [colors]="colors"
    [options]="options"
    (chartHover)="chartHovered($event)"
    (chartClick)="chartClicked($event)">
</canvas>

Dashboard.component.ts:

public doughnutChartLabels:string[] = ['Running', 'Stop', 'Idle'];
public doughnutChartData:number[] = [1, 4, 5];
public doughnutChartType:string = 'doughnut';
public colors: any[] = [{ backgroundColor: ["#008000", "#FF0000", "#FFA500"] }];

我需要的东西和图片一模一样

enter image description here

每个切片中的标签应该是默认的,并且只需要使用纯 typescript 而不是 javascript 来实现。请帮助我达到预期的结果。

最佳答案

您可以通过使用名为 - Chart.PieceLabel.js 的 Chart.js 插件来实现。 .

首先..通过 npm 安装它:

npm install chart.piecelabel.js --save

然后..将其导入您的仪表板组件:

import 'chart.piecelabel.js';

最后..在图表选项配置中设置以下选项:

pieceLabel: {
   render: 'label'
}

注意:这是显示标签需要设置的最低选项, 您可以找到更多选项 here .

参见 - working example

关于angular - 如何使用 ng2 图表在圆环图中显示标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46659986/

相关文章:

javascript - Angular Testing 模块未正确设置 : Can't resolve all parameters (ngrx)

c# - WebSocket 握手错误 : Unexpected response code 400

angular - 在 IIS 直接 url 上托管 Angular2 应用程序后无法正常工作

angular - 使组件可用于嵌套模块

javascript - AngularJS/ChartJS : Get the current series

angular - 未捕获( promise ): Error: Template parse errors: 'ion-col' is not a known element

typescript - 在启用 noImplicitAny 标志的情况下编译 typescript 时,如何防止错误 "Index signature of object type implicitly has an ' any' type"?

javascript - 按键过滤 map

javascript - Chartjs 的时间刻度格式问题,大多数是unitStepSize

javascript - 是否可以更新 Chart.js 中图表的宽度?