Anu*_*tik 14 legend tooltip highcharts
我使用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>
Run Code Online (Sandbox Code Playgroud)
Wil*_*oft 41
您可以使用:
legend: {
itemStyle: {
width: 90 // or whatever
},
}
Run Code Online (Sandbox Code Playgroud)
而Highcharts将为您包装物品.
请注意,在 2017 年,您可以使用textOverflow选项
legend.itemStyle每个图例项的 CSS 样式。仅支持 CSS 的一个子集,尤其是那些与文本相关的选项。默认
textOverflow属性会截断长文本。将其设置null为换行文本。
小智 7
编辑:实际设置项目宽度确实有效,可能是一个更好的解决方案.
设置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(' ');
}
Run Code Online (Sandbox Code Playgroud)
有关示例,请参见http://jsfiddle.net/ArmRM/13520/.