Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highcarts custom legend

Tags:

highcharts

How can I customize the legends

From

custom

To this

desired

like image 272
Miftah Farid Avatar asked Aug 21 '17 02:08

Miftah Farid


Video Answer


2 Answers

check highcharts legend api options.You can further customize with desired css. use proper svg image (background color is taken from chart itself)

legend: {
  align: 'right',
  verticalAlign: 'top',
  layout: 'vertical',
  x: -50,
  y: 120,
  symbolPadding: 0,
  symbolWidth: 0.1,
  symbolHeight: 0.1,
  symbolRadius: 0,
  useHTML: true,
  symbolWidth: 0,
  labelFormatter: function() {
    if(this.name=="Microsoft Internet Explorer"){
         return '<div style="width:200px;"><span style="float:left; margin-left:10px"><img src = "http://cdn.onlinewebfonts.com/svg/img_508736.svg" width = "40px" height = "40px" style="background-color:' + this.color + ';"></span><span style="float:right;padding:9px">' + this.percentage.toFixed(2) + " " + this.y + '%</span></div>';
    }
   if(this.name=="Chrome"){
   return '<div style="width:200px;"><span style="float:left; margin-left:10px"><img src = "http://cdn.onlinewebfonts.com/svg/img_159842.svg" width = "40px" height = "40px" style="background-color:' + this.color + ';"></span><span style="float:right;padding:9px">' + this.percentage.toFixed(2) + " " + this.y + '%</span></div>';
   }
   if(this.name=="Firefox"){
   return '<div style="width:200px;"><span style="float:left; margin-left:10px"><img src = "http://cdn.onlinewebfonts.com/svg/img_261106.svg" width = "40px" height = "40px" style="background-color:' + this.color + ';"></span><span style="float:right;padding:9px">' + this.percentage.toFixed(2) + " " + this.y + '%</span></div>';
   }

  },
  itemStyle: {
    color: '#ffffff',
    fontWeight: 'bold',
    fontSize: '19px'
  }
},

Fiddle demo

like image 127
Deep 3015 Avatar answered Oct 03 '22 12:10

Deep 3015


You can modify legend. To display custom icons in place of legend symbols, you need to set legend.useHTML with true value, hide symbol with:

.highcharts-legend-item rect {
  visibility: hidden;
}

and use labelFormatter. Take a look at the example posted below.

API Reference:
http://api.highcharts.com/highcharts/legend

Example:
http://jsfiddle.net/2trc1gv9/

like image 27
pawel_d Avatar answered Oct 03 '22 12:10

pawel_d