简体   繁体   English

为 Canvas 独立于 ChartJS 中的图例调整图表大小 下载

[英]Resize Chart Independently From Legend in ChartJS for Canvas Download

Some of the charts created and downloaded by my application have the potential to have very large legends.我的应用程序创建和下载的一些图表可能具有非常大的图例。 When this is the case, the idea is to create (ideally via ChartJs - as is the case with the associated chart) and download a dedicated canvas/image showing the full legend for the chart.在这种情况下,想法是创建(理想情况下通过 ChartJs - 与关联图表一样)并下载显示图表完整图例的专用画布/图像。

So far, I have been able to "hide" the chart by setting the scales to display: false eg到目前为止,我已经能够通过将比例设置为显示来“隐藏”图表:false 例如

options: {
  scales: {
    x: {
      display: false
    },
    y: {
      display: false
    }

This successfully prevents the chart from showing, but the canvas still seems to allocate half the real-estate for the chart eg这成功地阻止了图表显示,但 canvas 似乎仍然为图表分配了一半的空间,例如

在此处输入图像描述

Is there any way I can make the space for the chart smaller (or even disable it from showing at all?) allowing more space for the legend?有什么办法可以使图表的空间更小(甚至根本无法显示?)为图例留出更多空间?

Playground here: https://jsfiddle.net/ShallDev2018/7bdnjsw9/16/这里的游乐场: https://jsfiddle.net/ShallDev2018/7bdnjsw9/16/

NOTE: HtmlLegend is not an option here as the sole purpose of this canvas is for downloads.注意:此处没有 HtmlLegend 选项,因为此 canvas 的唯一目的是用于下载。

After some more tinkering, I have found the solution to the problem.经过更多的修补,我找到了解决问题的方法。

As it happens, ChartJs sets a default max-width for the legend (although it doesn't specify this in the documentation).碰巧的是,ChartJs 为图例设置了默认的最大宽度(尽管它没有在文档中指定)。 By setting legend max width to a high value, the legend can assume the whole canvas eg通过将图例最大宽度设置为高值,图例可以假设整个 canvas 例如

legend: {
  position: 'left',
  align: 'start',
  fullSize: false,
  labels: {
    font: {
      size: 12
    },
    padding: 10
  },
  maxWidth: 9999
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM