how to determine the width of every column series present in the chart- highhcarts

150 views Asked by At

I have stackLabels that I have added in the x- axis, I have added a formatter function that will truncate the x-axis labels in order to fit into the chart size: something like this:

https://jsfiddle.net/jqdf7nap/

however as the data and the width of the series column chart changes I would also like to change the truncated number of chars on the string.currently I have this:

stackLabels: {
            enabled: true,
            verticalAlign: 'bottom',
            //y:160,
            style: {
              fontWeight: 'bold',
              color: 'gray'
            },
            formatter: function () {
             let label = this.stack || '';
              let truncatedLabel = label.length <= 3
              ? label  : `${label.substring(0, 3)}...`;
                  
             return `<span>${truncatedLabel}</span>`;
            },
          }

the above will always truncate the string regardless of the size of the series column.ex if a wider series is available it'll still truncate to 3. like this

https://jsfiddle.net/ka9uhx10/

Instead I would like to show this https://jsfiddle.net/f2bv35gy/

is there any way to dynamically pass the substring char number to the string based on the chart point width?

1

There are 1 answers

0
Sebastian Wędzel On BEST ANSWER

Try to use the dataLabels instead the stackLabels and use the below config:

  events: {
    render() {
      const chart = this;

      chart.series.forEach(s => {
        s.points.forEach(p => {
          if (p.dataLabel && p.dataLabel.width > p.pointWidth || p.dataLabel && p.dataLabel.text.styles.textOverflow) {
            let dataLabel = p.dataLabel;
            dataLabel.css({
              width: p.pointWidth,
              textOverflow: 'ellipsis'
            })

            dataLabel.translate(p.barX - 5, chart.plotHeight)
          }
        })
      })
    }
  }

Demo: https://jsfiddle.net/BlackLabel/47e3msuk/

API: https://api.highcharts.com/class-reference/Highcharts.CSSObject#textOverflow

API: https://api.highcharts.com/highcharts/series.line.dataLabels.overflow

API: https://api.highcharts.com/highcharts/chart.events.render