0

I am making a diagram that should show states in a timeline like diagram. I figured that the best fit for it would be horizontal bar diagram, I added data like this:

series: [{
    data: [?],
    name: 'state1'
}, {
    data: [?],
    name: 'state2'
}, {
    data: [?],
    name: 'state3'
}, {
    data: [?],
    name: 'state1'
}, {
    ....
}, {
    data: [?],
    name: 'state1'
}]

Here's what I'v got: Stacked bar chart

The chart looks like what I need, but I need to somehow group legends, for example this case there should be only 3 legends: state1, state2, state3.

How can I achieve something like this?

Community
  • 1
  • 1
antanas_sepikas
  • 5,644
  • 4
  • 36
  • 67

3 Answers3

0
var legends = {};
series.forEach(function(bar){
  legends[bar.name] = bar.data;
});
matsjoyce
  • 5,744
  • 6
  • 31
  • 38
StAlex
  • 91
  • 2
0

Basing on the answer of @StAlex, I would recommend a similar solution, but without the ECMAScript 5 specific forEach:

var legend = {};
for (var i = 0; i < series.length; i++) {
    var bar = series[i];
    if (!legend[bar.name]) {
        legend[bar.name] = 0;
    }
    legend[bar.name] += bar.data;
}

I assumed that data is a number. If this is not so, add a comment.

meskobalazs
  • 15,741
  • 2
  • 40
  • 63
0

I would suggest a different approach, using the column range series type.

This way you can do this with only three series, with a data point for each time slot, rather than a series for each time slot. This saves from having to manipulate the legend or fake other series level actions.

This is an example I did for an earlier question that demonstrates the method (with only two states):

can easily be adapted to use a datetime axis as well, which it seems you might be going for.

jlbriggs
  • 17,612
  • 4
  • 35
  • 56