0

I have a dataset like this

[[[{"2001-1":{"cow":2.64,"dog":2.90,"Lemur":0.0,"Lion":2.77,"Tiger":2.31,"bird":3.21}}]],
[[{"2002-3":{"cow":2.54,"dog":2.67,"Lemur":0.0,"Lion":2.93,"Tiger":0.0,"bird":0.0,}}]]] 

The first array are X values and the second set are Y values. How can I generate a graph of where each key is represented as a line. More specifically how do I get the data into an acceptable format that D3 will be able to work with?

I've tried this

var w=400,h=200;
var x=d3.scale.linear().domain([0,dataset.length]).range([0,4]);
var y=d3.scale.linear().domain([0,d3.max(dataset)]).rangeRound([0,h]);
var entries=d3.entries(dataset);
var colors = d3.scale.category20().domain(d3.keys(dataset));
var line=d3.svg.line().interpolate("basis")
        .x(function(d){return x(d.range);})
        .y(function(d){return x(d.range);});

var svg=d3.select("#p-graph")
    .append("svg:svg")
    .attr("width", w)
    .attr("height", h);

        svg.selectAll(".line")
            .data(entries)
            .enter().append("path")
            .attr("class", "line")
            .attr("d",function(d){return line(d.value);})
            .attr("stroke",function(d){return colors(d.key);});

but I nothing shows up in the output. What am I not doing properly?

Update

Each animal should represent a single line on the graph.

Community
  • 1
  • 1
user1819688
  • 53
  • 1
  • 6

0 Answers0