[英]Highcharts: Make the legend symbol a square or rectangle
我正在尝试将图例符号设置为线图的正方形或矩形。 例
这条线很好。 我不想改变线宽。 HTML:
<script src="http://code.highcharts.com/highcharts.js"></script>
<div id="container" style="height: 400px"></div>
使用Javascript:
$(function () {
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'line',
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
plotOptions: {
series: {
marker: {
enabled: false
}
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
symbolHeight:100,
borderWidth: 0
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
});
});
我尝试在图例中添加symbolHeight。 但它不起作用。
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
symbolHeight:100,
borderWidth: 0
},
如何增加线符号的高度使其成为矩形或方形?
可以通过配置实现方形图例符号。 只需将legend.symbolRadius
值设置为0
。
JSFiddle演示: https ://jsfiddle.net/9bzy2qzq/
这是问题的副本: 如何访问legendSymbols并在HighCharts中更改其形状
它有一个类似的答案和另外两个:
第一选择:
Highcharts.seriesTypes.line.prototype.drawLegendSymbol =
Highcharts.seriesTypes.area.prototype.drawLegendSymbol;
第二种选择:
您可以更改path元素上的stroke-width
属性。
我们可以为Highcharts提供在绘制图表时绘制的函数。 由于在第一个图纸上没有调用redraw
,因此需要load
事件
chart: {
events: {
load: function () {
$(".highcharts-legend-item path").attr('stroke-width', 10);
},
redraw: function () {
$(".highcharts-legend-item path").attr('stroke-width', 10);
}
}
},
您可以按如下方式制作假系列,并为其提供提供者标记。
$(function () {
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'line',
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
plotOptions: {
series: {
marker: {
enabled: true
}
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
//borderWidth: 0
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4],
showInLegend : false,
marker:{enabled:false}
},{
name : "testing",
data : {},
marker : {symbol : 'square',radius : 12 }
}
]
});
});
工作演示: DEMO
所有的答案都是正确的,但我找到了一个非常黑客的方法。 用方形替换图例矩形符号:
legend: {
symbolHeight: 12,
symbolWidth: 12,
symbolRadius: 6
}
由于Highcharts 5引入了样式模式 ,您可以轻松找到符号元素,并更改其属性。 要使符号方形:
$(".highcharts-legend-item path").attr('stroke-width',16);
如果你想要一个圆形符号,你可以使用它: http : //jsfiddle.net/kL5sghrx/3/
chart: { events: { load: function(){ $( ".highcharts-legend-item path" ).each(function( index ) { $(this) .attr('d','M10.1,15.3L10.1,15.3c-3,0-5.5-2.5-5.5-5.5v0c0-3,2.5-5.5,5.5-5.5h0c3,0,5.5,2.5,5.5,5.5v0 C15.6,12.8,13.2,15.3,10.1,15.3z') .attr('fill', $( this ).attr('stroke')) .attr('stroke-dasharray','0,0'); }); }, redraw: function(){ $( ".highcharts-legend-item path" ).each(function( index ) { $(this) .attr('d','M10.1,15.3L10.1,15.3c-3,0-5.5-2.5-5.5-5.5v0c0-3,2.5-5.5,5.5-5.5h0c3,0,5.5,2.5,5.5,5.5v0 C15.6,12.8,13.2,15.3,10.1,15.3z') .attr('fill', $( this ).attr('stroke')) .attr('stroke-dasharray','0,0'); }); } }, }
对于矩形图例,我们需要设置squareSymbol: false
。
Highcharts.chart('container', { chart: { type: 'column' }, title: { text: 'Round legend symbols' }, xAxis: { categories: ['Jan', 'Feb', 'Mar', 'Apr'] }, legend: { symbolWidth: 16, symbolRadius: 0, squareSymbol: false }, series: [{ data: [1, 3, 2, 4] }, { data: [6, 4, 5, 3] }, { data: [2, 7, 6, 5] }] });
<script src="https://code.highcharts.com/highcharts.js"></script> <div id="container" style="height: 400px"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.