1

I want to create a timeseries by passing a dictionary in the function. The code from the examples is this:

d3.csv("data.csv", function(error, data) {
  data.forEach(function(d) {
    d.date = parseDate(d.date);
    d.close = +d.close;
  });

  x.domain(d3.extent(data, function(d) { return d.date; }));
  y.domain(d3.extent(data, function(d) { return d.close; }));
  //etc...

What I'm trying to do is to convert this into a function which takes an object with date and close rows (data) and produces the chart from this, i.e.

    function makeGraph(timeseriesdata){
        // create chart above from data
        // what format??
    }
Katie Hurley
  • 179
  • 9
RParadox
  • 6,393
  • 4
  • 23
  • 33

2 Answers2

0

d3.csv does most of the hard work for you. It takes a csv and coverts it to an array of objects, each corresponding to a row of the csv.

https://github.com/mbostock/d3/wiki/CSV has some examples.

After calling d3.csv and cleaning up the data with parseDate, you can pass data to makeGraph.

d3.csv("data.csv", function(error, data) {
  data.forEach(function(d) {
   d.date = parseDate(d.date);
   d.close = +d.close;
  });
  makeGraph(data);
});

function makeGraph(timeseriesdata){

}
Adam Pearce
  • 9,243
  • 2
  • 38
  • 35
0

To create a line path given a dictionary make a list with a dict, such as:

var parseDate = d3.time.format("%Y-%m-%d").parse;   

 var lineData = [ { "date": "2013-04-01",   "close": 5},  { "date": "2013-03-28",  "close": 20},
                  { "date": "2013-03-27",  "close": 10}, { "date": "2013-03-26",  "close": 40},
                  { "date": "2013-03-25",  "close": 5},  { "date": "2013-03-24", "close": 60}];

  lineData.forEach(function(d,i) {     
    d.date = parseDate(d.date);
    d.close = +d.close;    
  });

makeGraph(lineData);
RParadox
  • 6,393
  • 4
  • 23
  • 33