[英]Chart with tool tip values
我真的很困惑如何在angular js中创建图表。 使用angular-chart.js,我可以创建一个饼图和条形图。 但无法添加工具提示。 我正好在寻找的东西像这样的显示比例为工具提示。
javascript
$scope.labels = ["Test1", "Test2", "Test3", "Test4"];
$scope.data = [8000, 6000, 22000, 500];
$scope.color = ['#90EE90', '#FF6600', '#8080FF'];
$scope.options = {
legend: {
display: true
},
responsive: true, // set to false to remove responsiveness. Default responsive value is true.
tooltipEvents: [],
showTooltips: true,
tooltipCaretSize: 0,
onAnimationComplete: function() {
this.showTooltip(this.segments, true);
}
}
html
<canvas id="doughnut" class="chart chart-doughnut" chart-data="data" chart-labels="labels" chart-colors="color" chart-options="options">
</canvas>
我能够创建图表。 但是,如何仅使用值设置工具提示或将值转换为百分比? 我尝试了另一个也不起作用的示例 。 我尝试在angular 1.4版本中使用该代码 ,但给出错误,表明未定义值。 谁能帮助我创建工具提示上带有百分比值的图表?
您可以使用jtblin / angular-chart.js,因为示例中的库未显示百分比。
还添加了工具提示回调
tooltips: {
callbacks: {
label: function(tooltipItem, data) {
//get the concerned dataset
var dataset = data.datasets[tooltipItem.datasetIndex];
//calculate the total of this data set
var total = dataset.data.reduce(function(previousValue, currentValue, currentIndex, array) {
return previousValue + currentValue;
});
//get the current items value
var currentValue = dataset.data[tooltipItem.index];
//calculate the precentage based on the total and current item, also this does a rough rounding to give a whole number
var precentage = Math.floor(((currentValue / total) * 100) + 0.5);
return precentage + "%";
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.