条形图在QuickChart中带有两套标签
我正在尝试创建一个条形图,以跟踪使用 quickchart 。我希望该图表在栏上有一组标签(剩余%),每个栏内的第二组标签(剩余$)。
我能够在酒吧顶部找出标签,但是需要帮助,并在酒吧内添加第二组标签。条形内的标签将需要基于第二个数据数组。
我已经发布了我的代码,可以在实时编辑器
{
type: 'bar',
data: {
labels: ['Food', 'Gas', 'Other'],
datasets: [{
label: 'Category',
data: [.5, .7, .8]
}]
},
options: {
plugins: {
datalabels: {
formatter: function(value) {
return Math.round(value*100) + '%';},
anchor: 'end',
align: 'end',
color: 'black',
labels: {
percent: {
font: {
weight: 'bold',
size:26,
}
}
}
}
},
scales: {
yAxes: [ {
type: 'linear',
ticks: {
beginAtZero: true,
suggestedMin : 0,
suggestedMax : 1,
}
}]
}
}
}
I am trying to create a bar chart for tracking a budget with quickchart. I want the chart to have one set of labels on top of the bars (for % remaining), and a second set of labels inside each bar (for $'s remaining).
I was able to figure out the labels on top of the bars, but need help adding a second set of labels within the bars. The labels within the bars will need to be based on a second array of data.
I have posted my code, which can be tweaked in the live editor here. Any help would be greatly appreciated!
{
type: 'bar',
data: {
labels: ['Food', 'Gas', 'Other'],
datasets: [{
label: 'Category',
data: [.5, .7, .8]
}]
},
options: {
plugins: {
datalabels: {
formatter: function(value) {
return Math.round(value*100) + '%';},
anchor: 'end',
align: 'end',
color: 'black',
labels: {
percent: {
font: {
weight: 'bold',
size:26,
}
}
}
}
},
scales: {
yAxes: [ {
type: 'linear',
ticks: {
beginAtZero: true,
suggestedMin : 0,
suggestedMax : 1,
}
}]
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了
datalabels.labels.percent
,您还可以定义datalabels.labels.value
,均具有特定的form> form> form> formatter
和定位。无需定义第二个数据集
,只需在现有数据集中定义amountremaining
,然后在相应的form> formatter
中引用它。看起来如下...
Beside
datalabels.labels.percent
, you could also definedatalabels.labels.value
, both with specificformatter
and positioning. No need to define a seconddataset
, simply defineamountRemaining
inside the existing dataset and reference it in the correspondingformatter
.This could look as follows...