Search code examples
javascriptcssdygraphs

How to reverse the order of legend Items when using dygraphs?


I have a series of 5 area plots to be drawn in a single figure. I am using dygraphs for this.

I want the legend to be displayed in reverse order, but I don't want to change my .csv file. I didn't find anything to solve this issue in the dygraphs documentation page.

Kindly help me with the way out.

The code looks like this:

 <script type="text/javascript"  src="dygraph.js"></script>
 <link rel="stylesheet" src="dygraph.css" />
 <div id="graphdiv3"  style="width:1000px; height:300px;"></div>
 <div id="status", class="status"></div>
 <script>
  var g3 = new Dygraph(
     document.getElementById("graphdiv3"),
     "data/area.csv",
      {labelsDiv: document.getElementById('status'),
      labelsSeparateLines: true,
      labelsKMB: true,
      legend: 'always',
      xlabel: 'Date',
      ylabel: 'Area',
      drawGrid: false,
      rollPeriod: 10,
      showRoller: true,
      fillGraph: true,
      fillAlpha: 1.0,
      showRangeSelector: true,
      interactionModel: Dygraph.defaultInteractionModel,
   }
)


Solution

  • You might use flex and reverse flex-direction:

    var csv = btoa(`Year-month,class1,class2,class3,class5,class6
    1982-01-01,0.617100372,2.669144981,6.43866171,17.15241636,30.49070632
    1982-02-01,0.081784387,1.278810409,3.955390335,12.29739777,24.58736059
    1982-03-01,0.104089219,0.996282528,3.568773234,11.98513011,21.81412639
    1982-04-01,0.022304833,0.505576208,2.453531599,9.56133829,19.97769517
    1982-05-01,0.215613383,2.066914498,7.152416357,18.47583643,31.21189591
    1982-06-01,0.133828996,1.144981413,4.29739777,15.53159851,29.40520446
    1982-07-01,3.910780669,8.505576208,16.69144981,35.10037175,48.4535316
    1982-08-01,0.609665428,3.353159851,8.698884758,21.0260223,31.81412639
    1982-09-01,2.579925651,6.059479554,12.95910781,29.91821561,43.04089219
    1982-10-01,4.661710037,10.73605948,20.92193309,39.18215613,52.46096654
    1982-11-01,0.713754647,2.750929368,7.420074349,17.23420074,27.67286245
    1982-12-01,0.795539033,2.788104089,7.31598513,18.04460967,29.76951673`);
    
    var g3 = new Dygraph(
      document.getElementById("graphdiv3"),
      'data:application/text+csv;base64,' + csv, {
        labelsDiv: document.getElementById('status'),
        labelsSeparateLines: true,
        labelsKMB: true,
        legend: 'always',
        xlabel: 'Date',
        ylabel: 'Area',
        drawGrid: false,
        rollPeriod: 10,
        showRoller: true,
        fillGraph: true,
        fillAlpha: 1.0,
        showRangeSelector: true,
        interactionModel: Dygraph.defaultInteractionModel,
      }
    )
    #status {display:flex; flex-direction:column-reverse}
    #status span {order:-1}
    <script src="https://cdnjs.cloudflare.com/ajax/libs/dygraph/2.1.0/dygraph.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/dygraph/2.1.0/dygraph.min.css" rel="stylesheet" />
    
    <div id="graphdiv3" style="width:1000px; height:300px;"></div>
    <div id="status" class="status"></div>