lauhub
lauhub

Reputation: 920

CSV cannot display two-axis in Dygraphs without hardcoding

I would like to display a chart from a CSV data with two different axis.

I succeed to do this, but with some limitations.

Here is a sample of my CSV file, but the number of columns can vary and also the column labels:

"date","Tch","HyR","CO2"
2012/12/23-10:41,22.2,43,2379.52
2012/12/23-10:42,22.2,44,2319.36
2012/12/23-10:43,22.2,44,2319.36

I can display it using this code:

<div id="div_g" style="width:900px; height:400px; border: 1px solid black"></div> 
<script type="text/javascript" src="http://www.dygraphs.com/dygraph-combined.js"></script>

<script> 

        var g = new Dygraph(
        document.getElementById("div_g"),
        "enregistrement-subset0.csv",
        {
            labels:["date","Tch","HyR","CO2"], //the line I'd like to remove
            colors:["red", "blue", "green"],
            "CO2": { axis: {
                yAxisLabelFormatter: function (v) { return v.toPrecision(2); },
                yValueFormatter: function (v) { return v.toPrecision(2); }
            }},
            axes: {
                y: {
                    valueFormatter: function(y) {
                        return y.toPrecision(3);
                    },
                    axisLabelFormatter: function(y) {
                        return y;
                    }
                }
            },
        });
</script>

I tried to remove the "labels:" line, but doing this, the second axis disappears.

The jsFiddle for this is here http://jsfiddle.net/lauhub/jY2m6/

Also, keeping this line makes warning being generated while displaying the chart.

I also would like to dynamically load labels from the CSV file, since the number of columns and names can vary.

Is it possible to do this programmatically with dygraphs ? How ?

Thanks

Upvotes: 0

Views: 1310

Answers (1)

lauhub
lauhub

Reputation: 920

Here is an answer posted by Konigsberg from Dygraphs google group: The simple answer is that your CSV doesn't think column 0 is labeled [date], it thinks it's labeled ["date"] (with the quotes.)

Remove the quotes around the CSV and you'll be fine:

date,Tch,HyR,CO2
2012/12/23-10:41,22.2,43,2379.52

See http://jsfiddle.net/KUdsY/2 for this in action.

Also, I recommend using the new way to specify axes and series using the procedure specified in http://blog.dygraphs.com/2012/12/the-new-and-better-way-to-specify.html, which makes your solution look like http://jsfiddle.net/KUdsY/4/.

And I post the code below (copied from the last link):

var csv = 'date,Tch,HyR,CO2\n' +
    '2012/12/23-10:41,22.2,43,2379.52\n' +
    '2012/12/23-10:42,22.2,44,2319.36\n' +
    '2012/12/23-10:43,22.2,44,2319.36\n';
var g = new Dygraph(
document.getElementById("graph"),
csv, {
  //  labels: ["date", "Tch", "HyR", "CO2"], //the line I'd like to remove
    colors: ["red", "blue", "green"],
    series : {
        "CO2": {
            axis : 'y2'
        }
    },
    axes: {
        y: {
            valueFormatter: function (y) {
                return y.toPrecision(3);
            },
            axisLabelFormatter: function (y) {
                return y;
            }
        },
        y2: {
            yAxisLabelFormatter: function (v) {
                return v.toPrecision(2);
            },
            yValueFormatter: function (v) {
                return v.toPrecision(2);
            }
        }
    }
});

Upvotes: 1

Related Questions