Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in Dimple how do you change the order of the series ina legend?

enter image description here

I have a dimple graph with th ecode

var svg = dimple.newSvg("#chartContainer", 800, 600);

var myChart = new dimple.chart(svg, data);
myChart.setBounds(120, 30, 750, 550);
var x = myChart.addCategoryAxis("x", "time");
x.showGridlines = true;
x.addOrderRule("time");

var y = myChart.addMeasureAxis("y", "height", null);
//.overrideMax=(max);
y.overrideMin = 0;
y.overrideMax = 1000;
y.tickFormat = "d";

var s = myChart.addSeries("row", dimple.plot.line);
s.lineWeight = 1;
s.lineMarkers = true;
s.addOrderRule(function(n){
    console.log('n:', n);
    return n.row;
});


myChart.addLegend(0, 0, 900, 100, "left", s);
myChart.draw();

and the legend order is wonky.

the data is in the form

[
   },
{
    "row": "1",
    "height": -1,
    "time": 607
},
{
    "row": "1",
    "height": -11,
    "time": 709
},
{
    "row": "1",
    "height": -22,
    "time": 809
},
{
    "row": "1",
    "height": -32,
    "time": 910
},
{
    "row": "1",
    "height": -42,
    "time": 1011
},
{
    "row": "1",
    "height": -52,
    "time": 1113...

].

I'd like the row series to be in order in the legend.

Thanks.

like image 974
Dave Edelhart Avatar asked Dec 11 '22 06:12

Dave Edelhart


1 Answers

Another, possibly less fragile way of overriding the _getEntries method leverages the original method using function.apply()

// first, store a copy of the original _getEntries method.
legend._getEntries_old = legend._getEntries;

// now override the method
legend._getEntries = function()
{
    // but call the original version,
    // then sort the returned array before returning it.
    return legend._getEntries_old.apply(this, arguments).reverse();
}

Incidentally: Thanks, Dave, for your original answer - it was driving me crazy that the legend in an area chart was reversed from the stacking order of the area shapes.

like image 107
Christopher Rider Avatar answered Feb 16 '23 03:02

Christopher Rider