user3309434
user3309434

Reputation: 21

split display legend in highcharts

I have combination chart with highcharts, there are spline, pie and stacked. Is it possible to split legend or use vertical and horizontal layout together?

current layout legend is horizontal

*series 1 *series 2 *series 3 *series 4 *series 5 *series 6 *series 7 *series 8

I want the legend display to be

*series 1 *series 2
*series 3 *series 4 *series 5
*series 6 *series 7 *series 8

is it possible?

thanx

Upvotes: 0

Views: 1140

Answers (2)

Kamil Kulig
Kamil Kulig

Reputation: 5826

It cannot be done just by using Highcharts constructor options. You can achieve that kind of look and behavior by wrapping some core functions:

(function(H) {
  var merge = H.merge;

  H.wrap(H.Legend.prototype, 'getAllItems', function() {
    var allItems = [],
      chart = this.chart,
      options = this.options,
      legendID = options.legendID;

    H.each(chart.series, function(series) {
      if (series) {
        var seriesOptions = series.options;

        // use points or series for the legend item depending on legendType
        if (!isNaN(legendID) && (seriesOptions.legendID === legendID)) {
          allItems = allItems.concat(
            series.legendItems ||
            (seriesOptions.legendType === 'point' ?
              series.data :
              series)
          );
        }
      }
    });

    return allItems;
  });

  H.wrap(H.Chart.prototype, 'render', function(p) {
    var chart = this,
      chartOptions = chart.options;

    chart.firstLegend = new H.Legend(chart, merge(chartOptions.legend, chartOptions.firstLegend, {
      legendID: 0
    }));

    chart.secondLegend = new H.Legend(chart, merge(chartOptions.legend, chartOptions.secondLegend, {
      legendID: 1
    }));

    p.call(this);
  });

  H.wrap(H.Chart.prototype, 'redraw', function(p, r, a) {
    var chart = this;

    p.call(chart, r, a);

    chart.firstLegend.render();
    chart.secondLegend.render();
  });

  H.wrap(H.Legend.prototype, 'positionItem', function(p, item) {
    p.call(this, item);
  });
})(Highcharts);

Highcharts options:

Highcharts.chart('container', {

  chart: {
    marginRight: 350 // create some space for the legend
  },

  legend: {
    verticalAlign: 'middle',
    width: 300,
    align: 'right'
  },
  firstLegend: {
    y: -25
  },
  secondLegend: {
    y: 25
  },

  series: [{
    data: [5, 6, 7],
    legendID: 0,
  }, {
    data: [2, 3, 1],
    legendID: 0,
  },
  (...)
  {
    data: [1, 8, 2],
    legendID: 1
  }, {
    data: [3, 2],
    legendID: 1
  },
  (...)
  ]
});

Live demo: http://jsfiddle.net/kkulig/r70fwasr/

This code can be optimized so that it works for more than 2 legends.

Upvotes: 1

Ronniel Sembrano
Ronniel Sembrano

Reputation: 96

You can use legend's itemWidth property. Here's the link

Highcharts.chart('container', {
chart: {
    width: 500
},

title: {
    text: 'Legend <em>itemWidth</em> option'
},

legend: {
    itemWidth: 100
},

series: [{
    data: [6, 4, 2],
    name: 'First'
}, {
    data: [7, 3, 2],
    name: 'Second'
}, {
    data: [9, 4, 8],
    name: 'Third'
}, {
    data: [1, 2, 6],
    name: 'Fourth'
}, {
    data: [4, 6, 4],
    name: 'Fifth'
}, {
    data: [1, 2, 7],
    name: 'Sixth'
}, {
    data: [4, 2, 5],
    name: 'Seventh'
}, {
    data: [8, 3, 2],
    name: 'Eighth'
}, {
    data: [4, 5, 6],
    name: 'Ninth'
}]

});

http://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/legend/itemwidth-80/

Upvotes: 1

Related Questions