Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display legend items in two columns highcharts

In highcharts is it possible to display the legend in two columns, stacked vertically?

I'm trying to work out the best way of displaying the legend items, at the moment we have the legend items all stacked on top of each other.

There will be a maximum of 4 series on the chart.

I'm not really sure how to approach this, I see the option of useHTML but then I can't seem to find any examples of what to do with the HTML.

http://jsbin.com/oyicuc/9/edit

Any advice would be very helpful, thanks.

like image 717
hcharge Avatar asked Mar 18 '13 10:03

hcharge


People also ask

What is the legend of a chart?

The legend is a box containing a symbol and name for each series item or point item in the chart. Each series (or points in case of pie charts) is represented by a symbol and its name in the legend. It is possible to override the symbol creator function and create custom legend symbols. Accessibility options for the legend.

What is a legend in highmaps?

The legend is a box containing a symbol and name for each series item or point item in the chart. Each series (or points in case of pie charts) is represented by a symbol and its name in the legend. It is possible to override the symbol creator function and create custom legend symbols. A Highmaps legend by default contains one legend item per...

How to align the legend box within the chart area?

The horizontal alignment of the legend box within the chart area. Valid values are left, center and right. In the case that the legend is aligned in a corner position, the layout option will determine whether to place it above/below or on the side of the plot area.

How do I display the axis in a highmaps legend?

A Highmaps legend by default contains one legend item per series, but if a colorAxis is defined, the axis will be displayed in the legend. Either as a gradient, or as multiple legend items for dataClasses.


2 Answers

Have you tried to use itemWidth parameter?

Please take look at

http://jsfiddle.net/B9L2b/1266/

 legend: {
    width: 200,
    itemWidth: 100
},

http://api.highcharts.com/highcharts#legend.itemWidth

EDIT:

http://jsbin.com/oyicuc/31/

width:600,
        itemWidth:300,
        itemStyle: {
          width:280
        }

http://api.highcharts.com/highstock#legend.itemStyle

like image 60
Sebastian Bochan Avatar answered Dec 03 '22 12:12

Sebastian Bochan


function renderElements() {
      if (this.legend) {
       this.legend.destroy();
      }
      //distance between 2 elements
      let itemDistance = this.legend.options.itemDistance;
      //the biggest element
      let maxItemWidth = this.legend.maxItemWidth;
      //make the width of the legend in the size of 2 largest elements + 
      //distance  
      let nextLegendWidth = (maxItemWidth * 2) + itemDistance;
      //container width
      let boxWidth = this.plotBox.width;
      //if the length of the 2 largest elements + the distance between them 
      //is less than the width of           container, we make 1 row, else 
      //set legend width 2 max elements + distance between
    if (boxWidth < nextLegendWidth) {
     this.legend.options.width = maxItemWidth;
    } else {
     this.legend.options.width = nextLegendWidth;
  }

  this.render()   
}

chart: {
    events: {
      load: renderElements,
      redraw: renderElements
  }
}

https://jsfiddle.net/jecrovb7/38/

like image 26
Юрій Деревенко Avatar answered Dec 03 '22 14:12

Юрій Деревенко