Angular 2 ng2-charts 甜甜圈添加文本
Angular 2 ng2-charts donut add text
我正在使用基于 chart.js 的 ng2-charts。我正在尝试在圆环图中间添加文本,但我无法设置文本或注册插件。谁能帮忙?到目前为止,我在 javascript 中找到了几个不完整的答案,但我正在努力解决 angular 2 和 chart.js.
的打字稿实现
我的模板文件:
<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(){
}
}
已将您的代码复制到 Plunker,工作正常:
plunker: https://plnkr.co/edit/UGH3BKiWmgFqFWf78ZUE?p=preview
我认为以这种方式添加脚本是行不通的,因为 angular 会对其进行清理。尝试在 <script>
页面上的 chart.js 脚本之后添加脚本,或者最好在它自己的文件中添加脚本。
我正在使用基于 chart.js 的 ng2-charts。我正在尝试在圆环图中间添加文本,但我无法设置文本或注册插件。谁能帮忙?到目前为止,我在 javascript 中找到了几个不完整的答案,但我正在努力解决 angular 2 和 chart.js.
的打字稿实现我的模板文件:
<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(){
}
}
已将您的代码复制到 Plunker,工作正常:
plunker: https://plnkr.co/edit/UGH3BKiWmgFqFWf78ZUE?p=preview
我认为以这种方式添加脚本是行不通的,因为 angular 会对其进行清理。尝试在 <script>
页面上的 chart.js 脚本之后添加脚本,或者最好在它自己的文件中添加脚本。