如何在高图表中包装图例项目?
我在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以使用:
Highcharts 将为您包装项目。
You can use:
And Highcharts will wrap the items for you.
请注意,2017 年您可以使用
textOverflow
选项as a note, in 2017 you can use
textOverflow
option编辑:实际上设置项目宽度确实有效,可能是更好的解决方案。
设置 itemWidth 对我不起作用,但是您可以执行以下操作:请参阅 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:See http://jsfiddle.net/ArmRM/13520/ for an example.
包裹长图例名称的方法
Way to wrap long legend name
在图例上设置 itemStyle 有一个错误。没有空格的长标签仍然不会换行。
这是一个标签渲染器函数,它包装到特定数量的字符(我硬编码了 20 个字符),并将强制中断比这更长的单词:
它可以像这样使用:
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:
It can be used like:
如果您希望所有项目都在自己的行上,您可以使用
If you want all items on their own lines, you can use
使用
您可以在标签中 ,也可以在格式化程序中添加 html 标签。在这种情况下,您可以添加
标签来手动换行。请参阅:
http://www.highcharts.com/ref/#legend--labelFormatter
You can use
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
对于使用
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 defaulttextOverflow: "ellipsis"
setting messing with your wrapping.As teran noted above, setting
textOverflow: null
insideitemStyle
will instantly fix your wrapping whenuseHTML
is enabled and you're trying to wrap custom HTML you've written insidelegendFormatter()
.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.