0
私はChart.JS棒グラフを持っていますので、棒グラフ上に値を表示する必要があります。棒グラフの値を見ることができません
これは私がAJAXを通じてPHPからデータを取得し、それらを表示する場所を私のチャートスクリプトです:私は既存のものに次のスクリプトを追加したところ、私はthe answer from this questionを試してみました
$.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);
}
});
:
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にアップグレードする必要がありますが、チャートの作成が少し異なるため、コードを調整する必要があります。 –