如何在高图表中包装图例项目?

发布于 2024-11-19 01:57:29 字数 2007 浏览 2 评论 0原文

我在使用 highcharts 时遇到了一个大问题,因为我已经尝试了几个小时来包装图例项目(如果它们很长)。

我尝试设置图例和图例项宽度,但我的文本仍然从图例中出来。我发现的唯一一件事就是更改 highcharts.src.js 但我认为这不是解决这个问题的方法。

这是我的代码:

<script type="text/javascript">
  var chart;
  $(document).ready(function() {
    chart = new Highcharts.Chart({
      chart: {
        renderTo: 'graph_container',
        defaultSeriesType: 'line',
        zoomType: 'y',
        marginRight: 130,
        marginBottom: $ {
          marginBottom
        }
      },
      title: {
        x: -10,
        text: null
      },
      xAxis: {
        title: {
          text: '<fmt:message key="html.time" bundle="${msg}"/>',
          align: 'high'
        },
        categories: [$ {
          years
        }]
      },
      yAxis: {
        title: {
          text: '<fmt:message key="html.value" bundle="${msg}"/>',
          align: 'high'
        },
        plotLines: [{
          value: 0,
          width: 1,
          color: '#808080'
        }]
      },
      tooltip: {
        style: {
          fontSize: '9pt',
          width: '400px', //,
          wrap: 'hard'
        },
        formatter: function() {
          return '<b>' + this.series.name + '<br>' +
            +this.x + ': ' + this.y + '</b>';
        }
      },
      legend: {
        layout: 'vertical',
        width: 600,
        floating: true,
        align: 'center',
        verticalAlign: 'bottom',
        borderWidth: 1,
        itemWidth: '500px'

      },
      credits: {
        enabled: false
      },
      exporting: {
        enabled: false
      },
      series: [ <
        c: forEach items = "${graphValues}"
        var = "value"
        varStatus = "counter" >
        <
        c: if test = "${counter.count != 1}" > , < /c:if> {
          name: '${value.name}',
          data: $ {
            value.jsonData
          }
        } <
        /c:forEach>
      ]
    });


  });
</script>

I have a big problem using highcharts, because I have been trying for hours to wrap legend items if they very long.

I have tried to set legend and legend item width, but my text still get out from a legend. Only thing that I found is to change highcharts.src.js but I think that's not a way to solve this problem.

Here my code:

<script type="text/javascript">
  var chart;
  $(document).ready(function() {
    chart = new Highcharts.Chart({
      chart: {
        renderTo: 'graph_container',
        defaultSeriesType: 'line',
        zoomType: 'y',
        marginRight: 130,
        marginBottom: $ {
          marginBottom
        }
      },
      title: {
        x: -10,
        text: null
      },
      xAxis: {
        title: {
          text: '<fmt:message key="html.time" bundle="${msg}"/>',
          align: 'high'
        },
        categories: [$ {
          years
        }]
      },
      yAxis: {
        title: {
          text: '<fmt:message key="html.value" bundle="${msg}"/>',
          align: 'high'
        },
        plotLines: [{
          value: 0,
          width: 1,
          color: '#808080'
        }]
      },
      tooltip: {
        style: {
          fontSize: '9pt',
          width: '400px', //,
          wrap: 'hard'
        },
        formatter: function() {
          return '<b>' + this.series.name + '<br>' +
            +this.x + ': ' + this.y + '</b>';
        }
      },
      legend: {
        layout: 'vertical',
        width: 600,
        floating: true,
        align: 'center',
        verticalAlign: 'bottom',
        borderWidth: 1,
        itemWidth: '500px'

      },
      credits: {
        enabled: false
      },
      exporting: {
        enabled: false
      },
      series: [ <
        c: forEach items = "${graphValues}"
        var = "value"
        varStatus = "counter" >
        <
        c: if test = "${counter.count != 1}" > , < /c:if> {
          name: '${value.name}',
          data: $ {
            value.jsonData
          }
        } <
        /c:forEach>
      ]
    });


  });
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

罗罗贝儿 2024-11-26 01:57:29

您可以使用:

legend: {
    itemStyle: {
        width: 90 // or whatever
    },
}

Highcharts 将为您包装项目。

You can use:

legend: {
    itemStyle: {
        width: 90 // or whatever
    },
}

And Highcharts will wrap the items for you.

凉薄对峙 2024-11-26 01:57:29

请注意,2017 年您可以使用 textOverflow 选项

legend.itemStyle

每个图例项的 CSS 样式。仅支持 CSS 的一个子集,
特别是那些与文本相关的选项。 默认textOverflow
属性会使长文本被截断。将其设置为 null 以换行文本
相反。

as a note, in 2017 you can use textOverflow option

legend.itemStyle

CSS styles for each legend item. Only a subset of CSS is supported,
notably those options related to text. The default textOverflow
property makes long texts truncate. Set it to null to wrap text
instead.

知足的幸福 2024-11-26 01:57:29

编辑:实际上设置项目宽度确实有效,可能是更好的解决方案。

设置 itemWidth 对我不起作用,但是您可以执行以下操作:

labelFormatter: function() {
    var words = this.name.split(/[\s]+/);
    var numWordsPerLine = 4;
    var str = [];

    for (var word in words) {
        if (word > 0 && word % numWordsPerLine == 0)
            str.push('<br>');

        str.push(words[word]);
    }

    return str.join(' ');
}

请参阅 http://jsfiddle .net/ArmRM/13520/ 为例。

Edit: Actually setting the item width did work, probably a better solution.

Setting the itemWidth doesn't work for me, however you can do something like this:

labelFormatter: function() {
    var words = this.name.split(/[\s]+/);
    var numWordsPerLine = 4;
    var str = [];

    for (var word in words) {
        if (word > 0 && word % numWordsPerLine == 0)
            str.push('<br>');

        str.push(words[word]);
    }

    return str.join(' ');
}

See http://jsfiddle.net/ArmRM/13520/ for an example.

何以笙箫默 2024-11-26 01:57:29

包裹长图例名称的方法

legend: {
    enabled: true,
    width:555,
    itemWidth:500,
    itemStyle: {
        width:500
    }
}

Way to wrap long legend name

legend: {
    enabled: true,
    width:555,
    itemWidth:500,
    itemStyle: {
        width:500
    }
}
以为你会在 2024-11-26 01:57:29

在图例上设置 itemStyle 有一个错误。没有空格的长标签仍然不会换行。

这是一个标签渲染器函数,它包装到特定数量的字符(我硬编码了 20 个字符),并将强制中断比这更长的单词:

function hcLabelRender(){
    var s = this.name;
    var r = "";
    var lastAppended = 0;
    var lastSpace = -1;
    for (var i = 0; i < s.length; i++) {
        if (s.charAt(i) == ' ') lastSpace = i;
        if (i - lastAppended > 20) {
            if (lastSpace == -1) lastSpace = i;
            r += s.substring(lastAppended, lastSpace);
            lastAppended = lastSpace;
            lastSpace = -1;
            r += "<br>";
        }
    }
    r += s.substring(lastAppended, s.length);
    return r;
}

它可以像这样使用:

legend:{
    labelFormatter: hcLabelRender
}

Setting the itemStyle on the legend has a bug. Long labels with no spaces still don't wrap.

Here is a label renderer function that wraps to a specific number of characters (I hard coded 20 into it) and will force a break in words that are longer than that:

function hcLabelRender(){
    var s = this.name;
    var r = "";
    var lastAppended = 0;
    var lastSpace = -1;
    for (var i = 0; i < s.length; i++) {
        if (s.charAt(i) == ' ') lastSpace = i;
        if (i - lastAppended > 20) {
            if (lastSpace == -1) lastSpace = i;
            r += s.substring(lastAppended, lastSpace);
            lastAppended = lastSpace;
            lastSpace = -1;
            r += "<br>";
        }
    }
    r += s.substring(lastAppended, s.length);
    return r;
}

It can be used like:

legend:{
    labelFormatter: hcLabelRender
}
猫九 2024-11-26 01:57:29

如果您希望所有项目都在自己的行上,您可以使用

legend: {
    layout: "vertical"
}

If you want all items on their own lines, you can use

legend: {
    layout: "vertical"
}
2024-11-26 01:57:29

使用

labelFormatter: function() {
    return this.name; // insert your formatter function here
}

您可以在标签中 ,也可以在格式化程序中添加 html 标签。在这种情况下,您可以添加
标签来手动换行。

请参阅:
http://www.highcharts.com/ref/#legend--labelFormatter

You can use

labelFormatter: function() {
    return this.name; // insert your formatter function here
}

in your label and you can add html tags in the formatter. In this case you can add <br> tags to manually break lines.

Please see:
http://www.highcharts.com/ref/#legend--labelFormatter

忘东忘西忘不掉你 2024-11-26 01:57:29

对于使用 useHTML 选项的其他人,您将遇到默认 textOverflow: "ellipsis" 设置扰乱换行的问题。

如上所述,在启用 useHTML 且您尝试包装自定义 HTML 时,在 itemStyle 内设置 textOverflow: null 将立即修复您的包装问题您已在 legendFormatter() 中编写。

如果没有这个,默认截断不会应用于您的 HTML(例如不是字符串),并且就像您设置了 overflow: hide 一样。

For anyone else using the useHTML option, you'll run into an issue with the default textOverflow: "ellipsis" setting messing with your wrapping.

As teran noted above, setting textOverflow: null inside itemStyle will instantly fix your wrapping when useHTML is enabled and you're trying to wrap custom HTML you've written inside legendFormatter().

Without this, the default truncation doesn't apply to your HTML (e.g. not a string) and just acts like you have overflow: hidden set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文