gpt4 book ai didi

javascript - 将鼠标悬停在图表上时如何显示百分比?

转载 作者:行者123 更新时间:2023-11-30 11:36:46 24 4
gpt4 key购买 nike

情况:

我希望在将鼠标悬停在图表上时获得的数字工具提示旁边添加一个百分比。我怎样才能做到这一点?例如,我想在 83.33 旁边添加一个 % 符号。

enter image description here


错误:

ERROR TypeError: Cannot read property '0' of undefined
at i.label (eval at <anonymous> (http://localhost:3000/js/app/bundle.js:1564:1), <anonymous>:37:63)

代码:

// Pie
public pieChartLabels:string[] = [];
public pieChartData:number[] = [];
public pieChartType:string = 'pie';
public pieChartOptions:any = {};

ngOnInit() {
var result1 = parseFloat(((this.poll.counter1/(this.poll.counter2+this.poll.counter1))*100).toFixed(2));
var result2 = parseFloat(((this.poll.counter2/(this.poll.counter2+this.poll.counter1))*100).toFixed(2));
this.pieChartData = [result1, result2];
this.pieChartLabels = [this.poll.choice1, this.poll.choice2];
this.pieChartType = 'pie';
this.pieChartOptions = {
tooltips: {
callbacks: {
label: function (tooltipItems, data) {
return data.datasets[tooltipItems.datasetIndex].label + ': ' +
tooltipItems.pieChartLabels[tooltipItems.datasetIndex].replace(/(\d)(?=(\d{3})+\.)/g, '$1,');
}
}
}

}

// events
public chartClicked(e:any):void {

}

public chartHovered(e:any):void {

}

最佳答案

您可以在 chart.js 中使用工具提示回调来更改工具提示后缀。这是一个关于如何添加 % 的例子。我通过搜索和查找其他示例将其整合在一起。

https://jsfiddle.net/nt50dzb7/

  options: {
tooltips: {
enabled: true,
mode: 'single',
callbacks: {
label: function(tooltipItem, data) {
var allData = data.datasets[tooltipItem.datasetIndex].data;
var tooltipLabel = data.labels[tooltipItem.index];
var tooltipData = allData[tooltipItem.index];
return tooltipLabel + ": " + tooltipData + "%";
}
}
}
}
}

关于javascript - 将鼠标悬停在图表上时如何显示百分比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44121189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com