与Chart.js的堆叠条图表中仅在顶级栏上显示标签

发布于 2025-02-13 20:34:17 字数 2580 浏览 1 评论 0原文

我正在使用JS和Chart.js在HTML中制作堆叠的条形图。对于此图表,我不需要轴或多个堆叠的条(仅一个)。我想出了如何完成所有这些和自定义标签的方法,但是我只希望标签在顶级栏中显示(此示例中的红色栏)。我该如何仅在顶级栏中显示标签?我不太确定格式化的事情是如何工作的,我只是复制并粘贴了它,它显示了我想要的信息。

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>

<div class="col-8 offset-2 my-5">
        <div class="card">
            <div class="card-body">
                Stacked Bar Chart in Chart JS
                <hr>
                <canvas id="barchart"></canvas>
            </div>
        </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>

<script>
const ctx = document.getElementById('barchart').getContext('2d');
const myChart = new Chart(ctx, {
  plugins: [ChartDataLabels],
  type: 'bar',
  data: {
      labels: [1],
      datasets: [{
          label: 'Hypo',
          data: [12],
          backgroundColor: [
              'red',
          ],
          borderColor: [
              'red',
          ],
          borderWidth: 1
      }, {
          label: 'Hyper',
          data: [12],
          backgroundColor: [
              'orange',
          ],
          borderColor: [
              'orange',
          ],
          borderWidth: 1
      }]
  },
  options: {
      plugins: {
          legend: {
              display: false
          },
          datalabels: {
              color: 'black', 
              labels: {
                  title: {
                      font: {
                          family: "sans-serif",
                          size: 100,
                          weight: 'bold'
                      }
                  }
              },
              formatter: function(value, context) {
                  return context.chart.data.labels[context.dataIndex];
              }
          }
      },
      scales: {
          x: {
              stacked: true,
              display: false,
          },
          y: {
              beginAtZero: true,
              stacked: true,
              display: false,
          },
      }
  }
});
</script>

I am working on making a stacked bar chart in HTML using JS and Chart.js. For this chart, I don't want axes or multiple stacked bars (just one). I figured out how to do all of that and the custom labels, but I only want the label to show in the top bar (red bar in this example). How can I go about only displaying the label in the top bar? I'm not really sure how the formatter thing works, I just copy and pasted it in and it displays the info that I was looking for.

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>

<div class="col-8 offset-2 my-5">
        <div class="card">
            <div class="card-body">
                Stacked Bar Chart in Chart JS
                <hr>
                <canvas id="barchart"></canvas>
            </div>
        </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>

<script>
const ctx = document.getElementById('barchart').getContext('2d');
const myChart = new Chart(ctx, {
  plugins: [ChartDataLabels],
  type: 'bar',
  data: {
      labels: [1],
      datasets: [{
          label: 'Hypo',
          data: [12],
          backgroundColor: [
              'red',
          ],
          borderColor: [
              'red',
          ],
          borderWidth: 1
      }, {
          label: 'Hyper',
          data: [12],
          backgroundColor: [
              'orange',
          ],
          borderColor: [
              'orange',
          ],
          borderWidth: 1
      }]
  },
  options: {
      plugins: {
          legend: {
              display: false
          },
          datalabels: {
              color: 'black', 
              labels: {
                  title: {
                      font: {
                          family: "sans-serif",
                          size: 100,
                          weight: 'bold'
                      }
                  }
              },
              formatter: function(value, context) {
                  return context.chart.data.labels[context.dataIndex];
              }
          }
      },
      scales: {
          x: {
              stacked: true,
              display: false,
          },
          y: {
              beginAtZero: true,
              stacked: true,
              display: false,
          },
      }
  }
});
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还在原地等你 2025-02-20 20:34:18

我最终自己弄清楚了这个答案。我更改了格式化函数:

formatter: function(value, context) {
   return context.datasetIndex == 1 ? context.chart.data.labels[context.dataIndex] : ' ';
}

I ended up figuring out the answer to this myself. I changed the formatter function to this:

formatter: function(value, context) {
   return context.datasetIndex == 1 ? context.chart.data.labels[context.dataIndex] : ' ';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文