0
我有一個Chart.JS條形圖,我需要看到條形圖上的值,而不是將鼠標懸停在每個條形圖上。無法在條形圖上看到值
這是我的圖表腳本,我通過AJAX得到PHP數據並顯示出來:
$.ajax
({
url: 'stat_income_2.php',
type: 'POST',
data: {current_year: y},
dataType: 'JSON',
success:function(resp)
{
var costData = [];
$.each(resp, function(key, row)
{
costData.push(row['cost']);
});
var areaChartData = {
labels: ["January", "February", "March", "April", "May", "June", "July", "August"
, "September", "October", "November", "December"],
datasets: [
{
label: "Cash Income",
strokeColor: "rgba(210, 214, 222, 1)",
pointColor: "rgba(210, 214, 222, 1)",
pointStrokeColor: "#c1c7d1",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: costData
}
]
};
var barChartCanvas = $("#barChart2").get(0).getContext("2d");
var barChart = new Chart(barChartCanvas);
var barChartData = areaChartData;
barChartData.datasets[0].fillColor = "#00c0ef";
barChartData.datasets[0].strokeColor = "#00c0ef";
barChartData.datasets[0].pointColor = "#00a65a";
var barChartOptions = {
//Boolean - Whether the scale should start at zero, or an order of magnitude down from the lowest value
scaleBeginAtZero: true,
//Boolean - Whether grid lines are shown across the chart
scaleShowGridLines: true,
//String - Colour of the grid lines
scaleGridLineColor: "rgba(0,0,0,.05)",
//Number - Width of the grid lines
scaleGridLineWidth: 1,
//Boolean - Whether to show horizontal lines (except X axis)
scaleShowHorizontalLines: true,
//Boolean - Whether to show vertical lines (except Y axis)
scaleShowVerticalLines: true,
//Boolean - If there is a stroke on each bar
barShowStroke: true,
//Number - Pixel width of the bar stroke
barStrokeWidth: 2,
//Number - Spacing between each of the X value sets
barValueSpacing: 5,
//Number - Spacing between data sets within X values
barDatasetSpacing: 1,
//String - A legend template
legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].fillColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>",
//Boolean - whether to make the chart responsive
responsive: true,
maintainAspectRatio: true,
// onAnimationComplete: function() {
// var barChartData = this.chart.barChartData;
// barChartData.font = this.scale.font;
// barChartData.fillStyle = this.scale.textColor
// barChartData.textAlign = "center";
// barChartData.textBaseline = "bottom";
// this.datasets.forEach(function (dataset) {
// dataset.bars.forEach(function (bar) {
// barChartData.fillText(bar.value, bar.x, bar.y - 5);
// });
// })
//}
};
barChartOptions.datasetFill = false;
barChart.Bar(barChartData, barChartOptions);
}
});
我試圖the answer from this question,我添加了下面的腳本到現有的一個:
var myLine = new Chart(ctx).Line(chartData, {
showTooltips: false,
onAnimationComplete: function() {
var ctx = this.chart.ctx;
ctx.font = this.scale.font;
ctx.fillStyle = this.scale.textColor
ctx.textAlign = "center";
ctx.textBaseline = "bottom";
this.datasets.forEach(function (dataset) {
dataset.points.forEach(function (points) {
ctx.fillText(points.value, points.x, points.y - 10);
});
})
}
});
但什麼也沒有顯示,我將滾動每個欄以查看值。任何幫助表示讚賞。
它的工作原理,但當我誤把鼠標放在酒吧上的數字消失,我需要刷新,所以我可以再次看到他們 – androidnation
我認爲錯誤來自工具提示。如果您刪除它(showTooltips:在選項中爲false),值將保留。我使用Chart.JS的最新版本,並提供了一些示例,包括在圖表中顯示值。按照提供的示例,我沒有任何問題。也許你應該嘗試升級到Chart.JS V2,但你需要調整你的代碼,因爲圖表創建有點不同。 –