带有标签和其他文字的水平巴尔查特
我想创建一个水平条形图。 数据范围为 0 到 20 或 0 到 -20。 数据标签应始终位于条形图的另一侧。 我还需要在左侧和右侧添加额外的文字。
编辑:
这是我使用 Chart.js 的方法:
var ctx = document.getElementById("myChart");
var chartStatus = Chart.getChart("myChart");
if (chartStatus != undefined) {
chartStatus.destroy();
}
var myChart = new Chart(ctx, {
type: "bar",
data: {
labels: ["Cats", "Dogs", "Dugs", "Mice", "Hamster", "Tiger"],
datasets: [
{
label: false,
data: [-9, 1, 9, -9, 1, 9],
// data: [12, 19, -3],
backgroundColor: [
"rgba(39, 152, 228, 0.7)",
"rgba(233, 85, 59, 0.7)",
"rgba(152, 140, 202, 0.7)",
"rgba(39, 152, 228, 0.7)",
"rgba(233, 85, 59, 0.7)",
"rgba(152, 140, 202, 0.7)"
],
borderColor: [
"rgba(39, 152, 228,1)",
"rgba(233, 85, 59, 1)",
"rgba(152, 140, 202, 1)",
"rgba(39, 152, 228,1)",
"rgba(233, 85, 59, 1)",
"rgba(152, 140, 202, 1)"
],
borderWidth: 6
}
]
},
options: {
responsive: true,
indexAxis: "y",
plugins: {
gridLines: {
display: false
},
title: {
display: true,
text: "Pets",
font: {
size: 22
}
},
legend: {
display: false
},
tooltip: {
enabled: false
}
},
scales: {
x: {
min: -20,
max: 20,
gridLines: {
display: false
},
ticks: {
color: "#d6d6d9",
font: {
size: 22
}
}
},
y: {
labels: ["Text1", "Text2", "Text3", "Text4", "Text5", "Text6"],
gridLines: {
display: false
},
barPercentage: 1,
gridLines: {
display: false
},
ticks: {
color: "#d6d6d9",
font: {
size: 22
}
}
},
gridLines: {
display: false
},
secondY: {
labels: ["Text7", "Text8", "Text9", "Text10", "Text11", "Text12"],
position: "right",
ticks: {
color: "#d6d6d9",
font: {
size: 22
}
}
}
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.min.js" integrity="sha512-QSkVNOCYLtj73J4hbmVoOV6KVZuMluZlioC+trLpewV8qMjsWqlIQvkn1KGX2StWvPMdWGBqim1xlC8krl1EKQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<canvas id="myChart"></canvas>
<img style="display:none" id="url" />
我可以使用相应轴上的标签来解决左侧和右侧的不同文本。
但是如何将标签(猫、狗等)放入图表中,以便它们始终位于条形图的另一侧?
I would like to create a horizontal BarChart.
The data range is from 0 to 20 or from 0 to -20.
The labels of the data should always be on the opposite side of the bar.
I also need additional text on the left and right.
The picture should make it clear:
I would like to do it with Chart.js.
edit:
This is my approach with Chart.js:
var ctx = document.getElementById("myChart");
var chartStatus = Chart.getChart("myChart");
if (chartStatus != undefined) {
chartStatus.destroy();
}
var myChart = new Chart(ctx, {
type: "bar",
data: {
labels: ["Cats", "Dogs", "Dugs", "Mice", "Hamster", "Tiger"],
datasets: [
{
label: false,
data: [-9, 1, 9, -9, 1, 9],
// data: [12, 19, -3],
backgroundColor: [
"rgba(39, 152, 228, 0.7)",
"rgba(233, 85, 59, 0.7)",
"rgba(152, 140, 202, 0.7)",
"rgba(39, 152, 228, 0.7)",
"rgba(233, 85, 59, 0.7)",
"rgba(152, 140, 202, 0.7)"
],
borderColor: [
"rgba(39, 152, 228,1)",
"rgba(233, 85, 59, 1)",
"rgba(152, 140, 202, 1)",
"rgba(39, 152, 228,1)",
"rgba(233, 85, 59, 1)",
"rgba(152, 140, 202, 1)"
],
borderWidth: 6
}
]
},
options: {
responsive: true,
indexAxis: "y",
plugins: {
gridLines: {
display: false
},
title: {
display: true,
text: "Pets",
font: {
size: 22
}
},
legend: {
display: false
},
tooltip: {
enabled: false
}
},
scales: {
x: {
min: -20,
max: 20,
gridLines: {
display: false
},
ticks: {
color: "#d6d6d9",
font: {
size: 22
}
}
},
y: {
labels: ["Text1", "Text2", "Text3", "Text4", "Text5", "Text6"],
gridLines: {
display: false
},
barPercentage: 1,
gridLines: {
display: false
},
ticks: {
color: "#d6d6d9",
font: {
size: 22
}
}
},
gridLines: {
display: false
},
secondY: {
labels: ["Text7", "Text8", "Text9", "Text10", "Text11", "Text12"],
position: "right",
ticks: {
color: "#d6d6d9",
font: {
size: 22
}
}
}
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.min.js" integrity="sha512-QSkVNOCYLtj73J4hbmVoOV6KVZuMluZlioC+trLpewV8qMjsWqlIQvkn1KGX2StWvPMdWGBqim1xlC8krl1EKQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<canvas id="myChart"></canvas>
<img style="display:none" id="url" />
I could solve the different text for the left and the right side with the labels
on the respective axes.
But how do I get the labels (cat, dog etc) into the graph so that they are always on the opposite side of the bars?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案:
Solution: