Javascript 对于我们这些通过ChartKick和Rails(或任何其他API包装器)使用Chart.js的人来说,您不能将函数设置为上述选项(因为它们只呈现为字符串,而不是可调用函数)。但是,您只需在页面加载后访问客户端上的图表,并通过访问Chartkick.char
Javascript 对于我们这些通过ChartKick和Rails(或任何其他API包装器)使用Chart.js的人来说,您不能将函数设置为上述选项(因为它们只呈现为字符串,而不是可调用函数)。但是,您只需在页面加载后访问客户端上的图表,并通过访问Chartkick.char,javascript,chart.js,chart.js2,Javascript,Chart.js,Chart.js2,对于我们这些通过ChartKick和Rails(或任何其他API包装器)使用Chart.js的人来说,您不能将函数设置为上述选项(因为它们只呈现为字符串,而不是可调用函数)。但是,您只需在页面加载后访问客户端上的图表,并通过访问Chartkick.charts[''].getChartObject().tooltip.\u options.callbacks覆盖其中的标签和标题函数即可标签和标题是可以从那里设置的子属性。简单易行Y轴上的数据也可能有“%”吗? <html> <
对于我们这些通过ChartKick和Rails(或任何其他API包装器)使用Chart.js的人来说,您不能将函数设置为上述选项(因为它们只呈现为字符串,而不是可调用函数)。但是,您只需在页面加载后访问客户端上的图表,并通过访问
Chartkick.charts[''].getChartObject().tooltip.\u options.callbacks
覆盖其中的标签和标题函数即可<代码>标签和标题
是可以从那里设置的子属性。简单易行Y轴上的数据也可能有“%”吗?
<html>
<head>
<title>Doughnut Chart</title>
<script src="../dist/Chart.bundle.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<style>
canvas {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
</style>
</head>
<body>
<div id="canvas-holder" style="width:75%">
<canvas id="chart-area" />
</div>
<script>
var randomScalingFactor = function() {
return Math.round(Math.random() * 100);
};
var randomColorFactor = function() {
return Math.round(Math.random() * 255);
};
var randomColor = function(opacity) {
return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity || '.3') + ')';
};
var config = {
type: 'doughnut',
data: {
datasets: [{
data: [
486.5,
501.5,
139.3,
162,
263.7,
],
backgroundColor: [
"#F7464A",
"#46BFBD",
"#FDB45C",
"#949FB1",
"#4D5360",
],
label: 'Expenditures'
}],
labels: [
"Hospitals: $486.5 billion",
"Physicians & Professional Services: $501.5 billion",
"Long Term Care: $139.3 billion",
"Prescription Drugs: $162 billion",
"Other Expenditures: $263.7 billion"
]
},
options: {
responsive: true,
legend: {
position: 'bottom',
},
title: {
display: false,
text: 'Chart.js Doughnut Chart'
},
animation: {
animateScale: true,
animateRotate: true
}
}
};
window.onload = function() {
var ctx = document.getElementById("chart-area").getContext("2d");
window.myDoughnut = new Chart(ctx, config);{
}
};
</script>
</body>
</html>
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 percentage = Math.floor(((currentValue/total) * 100)+0.5);
return percentage + "%";
}
}
}
tooltips: {
callbacks: {
label: function(tooltipItem, data) {
var dataset = data.datasets[tooltipItem.datasetIndex];
var meta = dataset._meta[Object.keys(dataset._meta)[0]];
var total = meta.total;
var currentValue = dataset.data[tooltipItem.index];
var percentage = parseFloat((currentValue/total*100).toFixed(1));
return currentValue + ' (' + percentage + '%)';
},
title: function(tooltipItem, data) {
return data.labels[tooltipItem[0].index];
}
}
},
tooltips: {
callbacks: {
label: function(tooltipItem, data) {
var index = tooltipItem.index;
var currentValue = data.datasets[tooltipItem.datasetIndex].data[index];
var total = 0;
data.datasets.forEach(function(el){
total = total + el.data[index];
});
var percentage = parseFloat((currentValue/total*100).toFixed(1));
return currentValue + ' (' + percentage + '%)';
},
title: function(tooltipItem, data) {
return data.datasets[tooltipItem[0].datasetIndex].label;
}
}
}