Chart.js图例长标签被裁剪

huangapple go评论98阅读模式
英文:

Chart.js legend long labels clipped

问题

我有一个使用Chart.js创建的图表,图例上有长标签,它们被截断了,像这样:

Chart.js图例长标签被裁剪

Codepen链接:https://codepen.io/eimrek/pen/yLQbNdX

我希望图表尽可能地被挤在一起,以使标签适应。有没有办法做到这一点?

我尝试过使用options.layout.padding.right: 100,但这并不起作用:

Chart.js图例长标签被裁剪

英文:

I have a chart.js chart with a legend that has long labels and they get clipped, like this:

Chart.js图例长标签被裁剪

Codepen: https://codepen.io/eimrek/pen/yLQbNdX

I would prefer that the chart gets squeezed together as much as possible to make the labels fit. Is there any way to do this?

I tried with options.layout.padding.right: 100, but this doesn't work:

Chart.js图例长标签被裁剪

答案1

得分: 1

你应该设置选项 options.plugins.legend.maxWidth,参见文档

你可以设置一个较大的数字,如 maxWidth: 1000,但由于它是可脚本化的,你可以使用一个函数来确保留一些宽度给图表本身。对于图表宽度的 90% 作为 maxWidth,你可以使用:

{
    options: {
        // 其他选项...
        plugins: {
            // 其他插件...
            legend: {
                 // 其他图例选项...
                 maxWidth: ({chart}) => chart.width * 0.9
             }
        }
    }
}

codepen 分支

英文:

You should set the option options.plugins.legend.maxWidth, see the docs

You may set a large number, like maxWidth: 1000, but since it is scriptable, you may use a function to make sure you leave some width to the chart itself. For a maxWidth of 90% of the chart width, you may use

{
    options: {
        // other options .....
        plugins: {
            // other plugins .....
            legend: {
                 // other legend options .....
                 maxWidth: ({chart}) => chart.width * 0.9
             }
        }
    }
}

codepen fork

huangapple
  • 本文由 发表于 2023年6月30日 04:51:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76584541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定