gpt4 book ai didi

chart.js - 数据标签未显示在 Chart.js 上

转载 作者:行者123 更新时间:2023-12-05 01:59:52 25 4
gpt4 key购买 nike

我试图在我的 chart.js 顶部显示总金额;我正在尝试使用数据标签插件,但我不确定为什么它没有显示标签,我没有收到任何错误,这是我的代码:

<script src="https://cdn.jsdelivr.net/npm/chart.js@3.0.0/dist/chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@2.0.0-rc"></script>
<canvas id="myChart" width="850" height="520"></canvas>
<script>
var ctx = document.getElementById('myChart');
Chart.register(ChartDataLabels);
Chart.defaults.set('plugins.datalabels', {
color: '#FE777B'
});
var myChart = new Chart(ctx, {
type: 'bar',
plugins: [ChartDataLabels],
data: {
datasets: [{
data: <?php print_r($barData); ?>,
label: 'Advisor Closed MTD',
backgroundColor: 'rgb(192,111,94)',
barThickness: 25,
datalabels: {
color: '#FFCE56'
}

}],
},
options: {
responsive: false,
plugins: {
datalabels: {
color: 'blue'
}
}
}
});
</script>

图表显示了正确的信息,但标签根本没有显示。

最佳答案

标签正在显示,它们在栏本身中,要在栏顶部显示它们,您需要像这样配置它:

options: {
responsive: false,
plugins: {
datalabels: {
anchor: 'end',
align: 'end',
labels: {
value: {
color: 'blue'
}
}

}
}
}

例子:

<script src="https://cdn.jsdelivr.net/npm/chart.js@3.0.0/dist/chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@2.0.0-rc"></script>
<canvas id="myChart" width="850" height="520"></canvas>
<script>
var ctx = document.getElementById('myChart');
Chart.register(ChartDataLabels);
Chart.defaults.set('plugins.datalabels', {
color: '#FE777B'
});
var myChart = new Chart(ctx, {
type: 'bar',
plugins: [ChartDataLabels],
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
data: [12, 19, 3, 5, 2, 3],
label: 'Advisor Closed MTD',
backgroundColor: 'rgb(192,111,94)',
barThickness: 25,
datalabels: {
color: '#FFCE56'
}

}],
},
options: {
responsive: false,
plugins: {
datalabels: {
anchor: 'end',
align: 'end',
labels: {
value: {
color: 'blue'
}
}

}
}
}
});

</script>

关于chart.js - 数据标签未显示在 Chart.js 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67592548/

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