Javascript Highcharts:使图例符号为正方形或矩形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27510810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:14:50  来源:igfitidea点击:

Highcharts: Make the legend symbol a square or rectangle

javascriptjquerycsshighcharts

提问by Manu K Mohan

I am trying to Make the legend symbol a square or rectangle for a line graph. Example

我正在尝试将图例符号设为折线图的正方形或矩形。例子

enter image description here

在此处输入图片说明

The line is fine. I dont want change the line width. HTML:

线路没问题。我不想改变线宽。HTML:

<script src="http://code.highcharts.com/highcharts.js"></script>

<div id="container" style="height: 400px"></div>

Javascript:

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

I tried adding symbolHeight in legend. But its not working.

我尝试在图例中添加符号高度。但它不起作用。

legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'middle',
            symbolHeight:100,
            borderWidth: 0
        },

How to increase the height of line symbol to make it rectangle or square?

如何增加线符号的高度以使其成为矩形或方形?

采纳答案by Anchika Agarwal

You can make a fake series as follows and provider marker to it.

您可以按如下方式制作一个假系列并为其添加提供者标记。

$(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 }
    }
            ]
});
});

Working demo : DEMO

工作演示:DEMO

回答by Roman Pavlov

It's possible to achieve square legend symbols via configuration. Just set legend.symbolRadiusvalue to 0.

可以通过配置实现方形图例符号。只需将legend.symbolRadius值设置为0.

JSFiddle demo: https://jsfiddle.net/9bzy2qzq/

JSFiddle 演示:https://jsfiddle.net/9bzy2qzq/

回答by Shwaydogg

This is a copy of the question: How to access legendSymbols and change their shape in HighCharts

这是问题的副本:How to access legendSymbols and change their shape in HighCharts

It has a similar answer and two others:

它有一个类似的答案和另外两个:

First Option:

第一个选项:

Highcharts.seriesTypes.line.prototype.drawLegendSymbol = 
Highcharts.seriesTypes.area.prototype.drawLegendSymbol;

Second Option:

第二种选择:

You can change the stroke-widthattribute on the path element.

您可以更改stroke-width路径元素上的属性。

We can provide functions to Highcharts that will be drawn whenever the chart is drawn. Since redrawis not called on the first drawing the loadevent is needed

我们可以为 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);
        }
    }
},

回答by RockOnGom

All answers are correct but I found a pretty hack method. Replacing legend rectangle symbol with square:

所有答案都是正确的,但我发现了一个非常巧妙的方法。用正方形替换图例矩形符号:

legend: {
   symbolHeight: 12,
   symbolWidth: 12,
   symbolRadius: 6
}

JsFiddle

提琴手

回答by iddo

Since Highcharts 5 introduced styled mode, you can easily find the symbol elements, and change their attributes. To make the symbol square:

由于 Highcharts 5 引入了样式模式,您可以轻松找到符号元素并更改其属性。要使符号方形:

$(".highcharts-legend-item path").attr('stroke-width',16);

http://jsfiddle.net/n3h2totc/23/

http://jsfiddle.net/n3h2totc/23/

回答by Bill_VA

If you'd like a round symbol, you can use this: http://jsfiddle.net/kL5sghrx/3/

如果你想要一个圆形符号,你可以使用这个: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'); 
  });
        }
    },
 }

回答by Ramyani

For rectangular legend we need to set squareSymbol: false.

对于矩形图例,我们需要设置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>