如何确定图表中出现的每个列系列的宽度 [英] how to determine the width of every column series present in the chart- highhcarts

查看:93
本文介绍了如何确定图表中出现的每个列系列的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在x轴上添加了stackLabel,添加了格式化程序功能,该功能将截断x轴标签以适合图表大小:

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>`;
            },
          }

以上内容将始终截断字符串,而不管序列列的大小如何.如果有更宽的序列可用,它仍将截断为3.

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/

相反,我想展示这个 https://jsfiddle.net/f2bv35gy/

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

是否有任何方法可以根据图表点的宽度将子字符串的char号动态传递给字符串?

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

推荐答案

尝试使用dataLabels代替stackLabels,并使用以下配置:

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)
          }
        })
      })
    }
  }

演示: 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

这篇关于如何确定图表中出现的每个列系列的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆