简体   繁体   English

无法在 Highcharts 中设置图例的最小高度

[英]Unable to set Minimum Height for Legends in Highcharts

I am using sunbursts in highcharts where I needed to customize my legends.我在 highcharts 中使用 sunbursts,我需要自定义我的图例。 As part of this, I have legends starting from 0 items ( an empty chart) to any where near 15 legend items.作为其中的一部分,我有从 0 个项目(一个空图表)到接近 15 个图例项目的任何地方的图例。

I see that legends provide a maxHeight property to set max height, but it doesnt provide an option like minHeight.我看到图例提供了一个 maxHeight 属性来设置最大高度,但它没有提供像 minHeight 这样的选项。 I tried giving a custom class to legends, and give height directly to it but it didnt work out well.我尝试为图例提供自定义 class,并直接为其提供高度,但效果不佳。 Its not even honoring it.它甚至不尊重它。

If i give maxheight, when only 1 legend item is available, chart is resizing.如果我给出 maxheight,当只有 1 个图例项可用时,图表正在调整大小。 I dont want to give a specific height to my chart as it will be driven by parent element.我不想为我的图表指定特定高度,因为它将由父元素驱动。

If anyone has any idea of how to tackle this, it would be greatful.如果有人知道如何解决这个问题,那就太好了。 Thanks in advance.提前致谢。

This is what I have tried so far.到目前为止,这是我尝试过的。 Below is the sample link以下是示例链接
https://codesandbox.io/s/highcharts-react-demo-forked-p1okt5?file=/demo.jsx https://codesandbox.io/s/highcharts-react-demo-forked-p1okt5?file=/demo.jsx

A workaround might be to set spacingBottom which will give you the option to reserve space for the legend and set maxHeight .一种解决方法可能是设置spacingBottom这将使您可以选择为图例保留空间并设置maxHeight

  chart: {
    spacingBottom: 100,
  },
  legend: {
    maxHeight: 40,
  },

Demo: https://jsfiddle.net/BlackLabel/hL9ofjks/1/演示: https://jsfiddle.net/BlackLabel/hL9ofjks/1/

You can set all this for dynamic changes when loading the chart in chart.events.load , below is an example of how you can write functions to calculate new maxHeight.您可以在chart.events.load中加载图表时为动态更改设置所有这些,下面是一个示例,说明如何编写函数来计算新的 maxHeight。

  chart: {
    spacingBottom: 100,
    events: {
      load: function() {
        let legend = this.legend,
          itemHeight = this.legend.allItems[0].legendGroup.element.getBBox().height,
          navHeight = this.legend.nav.element.children[2].getBBox().height,
          avHeight = legend.options.maxHeight - navHeight,
          totalHeight = itemHeight,
          numberOfRows = 0;

        while (totalHeight < avHeight) {
          totalHeight += itemHeight;
          numberOfRows++;
        }

        legend.update({
          maxHeight: (numberOfRows * itemHeight) + navHeight
        });
      }
    }
  },

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

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