Ray M'Ann
Ray M'Ann

Reputation: 13

Using hardcoded data instead of CSV file in D3 graphs

Although I'm quite familiar with javasript. For the first time I'm using D3 and example svg graph: http://bl.ocks.org/bbest/2de0e25d4840c68f2db1

in the example above, CSV file is used as data. (the file from example is aster_data.csv)

I want to use data array defined within the script instead of the file.

Instead of:

d3.csv('aster_data.csv', function(error, data) {
...

I've added my array-

var myData= [
      {"id":"ABC","order":1,"score":50,"weight":1,"color":"#9E0041","label": "Line1"},
      {"id":"CDE","order":2,"score":60,"weight": 1,"color":"#C32F4B", "label": "Line2"}
];
d3.selection(myData, function(error, data) {
...

However it doesn't work, not I can't see any errors in console.

Upvotes: 1

Views: 645

Answers (1)

Peter
Peter

Reputation: 14947

No selection. The csv function fetches the csv, then calls the lambda with the corresponding data. Just call the body of that lambda with your data in the data variable.

var data= [
  {"id":"ABC","order":1,"score":50,"weight":1,"color":"#9E0041","label": "Line1"},
  {"id":"CDE","order":2,"score":60,"weight": 1,"color":"#C32F4B", "label": "Line2"}
];

data.forEach(function(d) {
    d.id     =  d.id;
    d.order  = +d.order;
    d.color  =  d.color;
    d.weight = +d.weight;
    d.score  = +d.score;
    d.width  = +d.weight;
    d.label  =  d.label;
  });
// etc

Upvotes: 3

Related Questions