[英]Chart js grouped bar chart with individual label
您应该声明并配置另一个 x 轴。 在以下受您的数据启发的示例中,我使用options.scales.x.ticks.callback
(请参阅标记轴 | Chart.js ),它允许我注入新标签。
const ctx = document.getElementById('myChart'); new Chart(ctx, { type: 'bar', data: { labels: ['Category 1', 'Category 2', 'Category 3', 'Category 4'], datasets: [{ label: 'Dataset 1', data: [3, 4, 4, 2], stack: 'Stack 0' }, { label: 'Dataset 2', data: [5, 3, 4, 7], stack: 'Stack 0' }, { label: 'Dataset 3', data: [3, 0, 4, 4], stack: 'Stack 1' }, { label: 'Dataset 4', data: [2, 5, 6, 2], stack: 'Stack 1' }] }, options: { scales: { x: { stacked: true, ticks: { font: { weight: 'bold' }, callback: () => 'Label 1 | Label 2' } }, x2: { border: { display: false }, grid: { display: false } }, y: { stacked: true, beginAtZero: true } }, plugins: { legend: { display: false } } } });
.chart-container { position: relative; width: 600px; }
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.2.0"></script> <div class="chart-container"> <canvas id="myChart"></canvas> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.