Question

Consider the example : http://dygraphs.com/tests/two-axes.html

 g = new Dygraph(    
              document.getElementById("demodiv"),    
              data,    
          {   
            labels: [ 'Date', 'Y1', 'Y2', 'Y3', 'Y4' ],   
            'Y3': { axis: {  }  },  
            'Y4': {   
              axis: 'Y3'  // use the same y-axis as series Y3  
            },  
            axes: {  
              y2: {  
                // set axis-related properties here  
                labelsKMB: true  
              }  
            },  
            ylabel: 'Primary y-axis',  
            y2label: 'Secondary y-axis',  
            yAxisLabelWidth: 60  
          }  
      );

The above code creates a Dygraph with Y3 as secondary axis.
My question is, can we pass 'Y3' as a variable instead of hardcoding it .
I am trying to make it dynamic.
I tried to pass it as a string variable/array/json obj . But it didn't work.
Kindly suggest solution to pass'Y3' as a variable.

Was it helpful?

Solution

This isn't specific to dygraphs, it's a general JS question. You can set a field in an object using [] notation:

var series = 'Y3';
var opts = { ... };
opts[series] = { axis: {} };

You may also want to use the new syntax for secondary axes, which is a bit more straightforward.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top