javascript - 如何为图表中的多个数据集共享相同的背景颜色?
问题描述
我正在尝试使用Chart.js创建条形图。我希望我的图表看起来像这个示例,其中每个数据集在图表上具有相同的颜色。
但是,就我而言,颜色显示错误。我希望每个类别在图表中共享相同的颜色。
此外,有没有办法将标签(即Category 1
,Category 2
...)放置在日期的每个条下方?
这是我所做的
document.addEventListener('DOMContentLoaded', function () {
const options = {
"type":"bar",
"data":{
"labels":[
"10/25/2021",
"11/1/2021",
"11/8/2021",
"11/15/2021"
],
"datasets":[
{
"label":"Category 1",
"data":[
{
"x":"11/1/2021",
"y":3.25
},
{
"x":"11/8/2021",
"y":3.66
},
{
"x":"11/15/2021",
"y":4.00
}
],
"borderWidth":0,
"backgroundColor":[
"red",
"blue",
"black",
"silver"
]
},
{
"label":"Category 2",
"data":[
{
"x":"11/1/2021",
"y":3.00
},
{
"x":"11/8/2021",
"y":4.00
},
{
"x":"11/15/2021",
"y":5.00
}
],
"borderWidth":0,
"backgroundColor":[
"red",
"blue",
"black",
"silver"
]
},
{
"label":"Category 3",
"data":[
{
"x":"11/1/2021",
"y":2.00
},
{
"x":"11/8/2021",
"y":4.33
},
{
"x":"11/15/2021",
"y":5.00
}
],
"borderWidth":0,
"backgroundColor":[
"red",
"blue",
"black",
"silver"
]
},
{
"label":"Category 4",
"data":[
{
"x":"11/1/2021",
"y":3.25
},
{
"x":"11/8/2021",
"y":4.33
},
{
"x":"11/15/2021",
"y":4.00
}
],
"borderWidth":0,
"backgroundColor":[
"red",
"blue",
"black",
"silver"
]
},
{
"label":"Category 5",
"data":[
{
"x":"11/1/2021",
"y":3.25
},
{
"x":"11/8/2021",
"y":4.00
},
{
"x":"11/15/2021",
"y":5.00
}
],
"borderWidth":0,
"backgroundColor":[
"red",
"blue",
"black",
"silver"
]
},
{
"label":"Category 6",
"data":[
{
"x":"11/1/2021",
"y":3.00
},
{
"x":"11/8/2021",
"y":4.66
},
{
"x":"11/15/2021",
"y":4.00
}
],
"borderWidth":0,
"backgroundColor":[
"red",
"blue",
"black",
"silver"
]
}
]
},
"options":{
"responsive":false,
"plugins":{
"legend":{
"position":"top"
},
"title":{
"display":true,
"text":"Category Overview"
}
}
}
};
let canvas = document.getElementById('chart');
new Chart(canvas.getContext('2d'), options);
});
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.6.0/dist/chart.min.js"></script>
<canvas width="400" height="400" id="chart"></canvas>
解决方案
这是因为您将背景颜色作为一个数组提供,这使得 chart.js 在您希望这些颜色为该数据集旋转时对其进行插值。如果您希望数据集像您链接的示例一样为单一颜色,则只需传递具有单一颜色的字符串或具有单个条目的数组。
document.addEventListener('DOMContentLoaded', function() {
const options = {
"type": "bar",
"data": {
"labels": [
"10/25/2021",
"11/1/2021",
"11/8/2021",
"11/15/2021"
],
"datasets": [{
"label": "Category 1",
"data": [{
"x": "11/1/2021",
"y": 3.25
},
{
"x": "11/8/2021",
"y": 3.66
},
{
"x": "11/15/2021",
"y": 4.00
}
],
"borderWidth": 0,
"backgroundColor": "red"
},
{
"label": "Category 2",
"data": [{
"x": "11/1/2021",
"y": 3.00
},
{
"x": "11/8/2021",
"y": 4.00
},
{
"x": "11/15/2021",
"y": 5.00
}
],
"borderWidth": 0,
"backgroundColor": "blue"
},
{
"label": "Category 3",
"data": [{
"x": "11/1/2021",
"y": 2.00
},
{
"x": "11/8/2021",
"y": 4.33
},
{
"x": "11/15/2021",
"y": 5.00
}
],
"borderWidth": 0,
"backgroundColor": "black"
},
{
"label": "Category 4",
"data": [{
"x": "11/1/2021",
"y": 3.25
},
{
"x": "11/8/2021",
"y": 4.33
},
{
"x": "11/15/2021",
"y": 4.00
}
],
"borderWidth": 0,
"backgroundColor": "silver"
},
{
"label": "Category 5",
"data": [{
"x": "11/1/2021",
"y": 3.25
},
{
"x": "11/8/2021",
"y": 4.00
},
{
"x": "11/15/2021",
"y": 5.00
}
],
"borderWidth": 0,
"backgroundColor": "pink"
},
{
"label": "Category 6",
"data": [{
"x": "11/1/2021",
"y": 3.00
},
{
"x": "11/8/2021",
"y": 4.66
},
{
"x": "11/15/2021",
"y": 4.00
}
],
"borderWidth": 0,
"backgroundColor": "orange"
}
]
},
"options": {
"responsive": false,
"plugins": {
"legend": {
"position": "top"
},
"title": {
"display": true,
"text": "Category Overview"
}
}
}
};
let canvas = document.getElementById('chart');
new Chart(canvas.getContext('2d'), options);
});
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.6.0/dist/chart.min.js"></script>
<canvas width="400" height="400" id="chart"></canvas>
推荐阅读
- php - 如何根据 Laravel 中的选定选项在 mysql 数据库的文本框中显示数据
- c++ - 运算符重载:用二进制“-”减法没有找到采用 _____ 类型的全局运算符(或没有可接受的转换)
- python - Python 重力模拟
- python - 如何将随机二进制信息添加到当前的“坐标”中?(Python)
- mysql - 如何获取MySQL表中列值相同的所有行?
- html - google fonts - 字体系列在页面后加载,如何解决?
- c++ - 指向具有指针成员的对象的指针数组的析构函数
- python - 模型并行不适用于增加 tensorflow1.12 中的模型大小?
- spring-boot - 运行 Azure DevOps Pipeline 时如何跳过构建测试(Spring Boot Maven 项目)
- python - 如何使用标签将 txt 文件数据集分类为两个数据集?