gpt4 book ai didi

javascript - 从 Chart.js 中条形图的 x 轴删除 0(零)

转载 作者:行者123 更新时间:2023-11-28 17:11:49 25 4
gpt4 key购买 nike

对于 x 轴(1 月 - 5 月)上的零值,我只想显示 0 以外的任何内容。下面的代码用于情节。任何帮助/建议表示赞赏

$scope.showChart = function (finalLabels, finalDatasets, finalTitle, plotType,dateRange) {

var ctx = document.getElementById("chart").getContext('2d');
var plotConfig = {

type: plotType,
data: {
labels: finalLabels,
datasets: finalDatasets
},
options: {
responsive: true,
maintainAspectRatio: false,
layout: {
padding: {left: 0, right: 0, top: 10, bottom: 0}
},
plugins: {
labels: {
render: 'value',
fontSize: 10,

}
},
legend: {
display: false,
labels: {
fontSize: 10,
boxWidth: 35
}
},
scales: {
xAxes: [{
ticks: { autoSkip: false,fontSize:10
}
}],
yAxes: [{
scaleLabel: {
display: true,
labelString: 'Testing',
fontSize:10
},
ticks: {
beginAtZero: true,
fontSize:10
}
}]
},

/* title: {
display: true,
text: [finalTitle.toUpperCase(),dateRange],
fontSize:10,
padding:0
}*/
}
};
if ($scope.chart) {
$scope.chart.destroy();
}

$scope.chart = new Chart(ctx, plotConfig);
};

enter image description here

最佳答案

如果您使用 DataLabels插件,这里是你如何做 scriptable display option .

在插件选项中,您可以使用函数来指示 DataLabels 是否显示标签。因此,在本例中,只需检查该值是否大于 0。

var ctx = document.getElementById("myChart");
debugger;
var data = {
labels: ["Jan 18", "Feb 18", "Mar18", "Apr 18", "May 18", "Jun 18", "Jul 18"],
datasets: [{
label: "# of Votes",
data: [0, 0, 0, 0, 10, 12, 24]
}]
}
var myChart = new Chart(ctx, {
type: 'bar',
data: data,
legend: {
"display": false
},
plugins: [ChartDataLabels],
options: {
plugins: {
// Change options for ALL labels of THIS CHART
datalabels: {
color: "#36A2EB",
anchor: "end",
display: function(context) {
var index = context.dataIndex;
var value = context.dataset.data[index];
return value > 0; // display labels with a value greater than 0
}
}
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@0.5.0/dist/chartjs-plugin-datalabels.min.js"></script>
<canvas id="myChart" width="100" height="100"></canvas>

关于javascript - 从 Chart.js 中条形图的 x 轴删除 0(零),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54186169/

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