ChartJS-线图 - 数据标签格式功能无法正常工作
我有以下html/js代码,如下所示:
- 标签
- 值
并将其转换为带有标签上标签的线图:
<div style = "position:relative; bottom:140px; z-index: -2;">
<canvas id="lineChart" width = 600 height = 200></canvas>
<script>
Chart.register(ChartDataLabels);
var ctx = document.getElementById("lineChart").getContext("2d");
var linechart = new Chart(ctx, {
type: "line",
data: {
labels: {{ labels | safe}},
datasets: [
{
label: "Data points",
data: {{ values | safe}},
fill: false,
borderColor: "rgb(75, 192, 192)",
lineTension: 0.5
}
]
},
options: {
responsive: true,
layout: {
padding: {
top: 100
}
},
scales: {
x: {
grid: {
display: false
}
},
y: {
display: false,
}
},
plugins: {
legend: {
display: false
},
datalabels: {
anchor: 'end',
align: 'top',
formatter: Math.round,
font: {
weight: 'bold',
size: 15
}
}
}
}
});
</script>
这很好:
但是,我希望标签到达标签 时,请使用'£'
符号,然后'm'
,
在每次尝试使用格式化函数
formatter: function(value){
return value + 'm';
例如:或任何其他组合,图表都无法返回。我完全困惑为什么。
有什么建议吗?
I have the following HTML/JS code as follows which takes two lists:
- labels
- values
And converts it into a line chart with labels on the points:
<div style = "position:relative; bottom:140px; z-index: -2;">
<canvas id="lineChart" width = 600 height = 200></canvas>
<script>
Chart.register(ChartDataLabels);
var ctx = document.getElementById("lineChart").getContext("2d");
var linechart = new Chart(ctx, {
type: "line",
data: {
labels: {{ labels | safe}},
datasets: [
{
label: "Data points",
data: {{ values | safe}},
fill: false,
borderColor: "rgb(75, 192, 192)",
lineTension: 0.5
}
]
},
options: {
responsive: true,
layout: {
padding: {
top: 100
}
},
scales: {
x: {
grid: {
display: false
}
},
y: {
display: false,
}
},
plugins: {
legend: {
display: false
},
datalabels: {
anchor: 'end',
align: 'top',
formatter: Math.round,
font: {
weight: 'bold',
size: 15
}
}
}
}
});
</script>
This is working fine:
However, I want the labels to have the '£'
symbol before and 'm'
after
Every time I try to use the formatter function like:
formatter: function(value){
return value + 'm';
Or any other combination, the chart fails to return. I am completely perplexed why.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下
格式
对我来说很好。请在下面查看您的修订和可运行的代码。
The following
formatter
works fine for me.Please take a look at your amended and runnable code below.