是否可以显示每个传说与图表中的每个栏对齐。

发布于 2025-01-24 07:24:31 字数 357 浏览 2 评论 0原文

我使用Chart.js制作了水平条形图。在默认设置中,所有传说都像

它们之间没有差距。我想显示每个传说与每个栏都像这样

​是否可以在Chart.js中的每个栏对齐传奇和中心?

我正在使用Chart.js(v.3.7)

I made a horizontal bar chart using Chart.js. In default setting all the legends are shown all together like this

enter image description here

there is no gap among them. I want to show each legend aligned with each bar like this

enter image description here

I tried so many solutions but none of them worked. Is it possible to show legends right and center aligned with each bar in chart.js?

I am using Chart.js (v.3.7)

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

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

发布评论

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

评论(1

情丝乱 2025-01-31 07:24:31

您正在寻找的行为可以通过在传奇标签配置并试图找到正确的值。
这看起来可能是这样的:

const chart = new Chart(ctx, {
    type: 'bar',
    data: data,
    options: {
        plugins: {
            legend: {
                display: true,
                labels: {
                    padding: 15 // change according to testing
                }
            }
        }
    }
});
 

您通常不会为此使用传奇,而是直接按照下面的链接进行标记。

请查看文档: httpps:htttps:///www.chartjs。 org/docs/最新/轴/笛卡尔/#tick-configuration

它提供了一个有关如何使用类似轴标签的示例:

// config
const config = {
  type: 'bar',
  data,
  options: {
    indexAxis: 'y',
    scales: {
      y: {
        ticks: {
          crossAlign: 'far',
        }
      }
    }
  }
};

// setup
const labels = ["Label 1", "Label 2", "Label 3"]
const data = {
  labels: labels,
  datasets: [{
    label: 'My dataset',
    borderWidth: 1,
    data: [65, 59, 80],
  }]
};

The behavior you are looking for can possibly be achieved by using the padding option in the Legend Label Configuration and trying to find the correct value.
This could look something like this:

const chart = new Chart(ctx, {
    type: 'bar',
    data: data,
    options: {
        plugins: {
            legend: {
                display: true,
                labels: {
                    padding: 15 // change according to testing
                }
            }
        }
    }
});
 

You would usually not use a legend for this but instead label the axis directly as described in the link below.

Have a look at the documentation: https://www.chartjs.org/docs/latest/axes/cartesian/#tick-configuration

It provides an example on how to use axis labels like that:

// config
const config = {
  type: 'bar',
  data,
  options: {
    indexAxis: 'y',
    scales: {
      y: {
        ticks: {
          crossAlign: 'far',
        }
      }
    }
  }
};

// setup
const labels = ["Label 1", "Label 2", "Label 3"]
const data = {
  labels: labels,
  datasets: [{
    label: 'My dataset',
    borderWidth: 1,
    data: [65, 59, 80],
  }]
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文