Ad

Use An Array As Input Data In D3.js

- 1 answer

I have built a custom D3 chart that used an old REST api, and I am looking for help on how to adapt the D3 input to the new one. At http://codepen.io/anon/pen/bEgMdV I use the dataset 'dataset' and I have tried to use the 'data' which has 'genes' and 'dataset' in one.

So far, I have tried changing

var gs = svg.selectAll("g")
.data(d3.values(dataset)).enter().append("g");
gs.selectAll("path").data(function(d) {
        return pie(d.data);

to

var gs = svg.selectAll("g")
.data(d3.values(data.values)).enter().append("g");
gs.selectAll("path").data(function(d) {
        return pie(d[0]);

In order to get the numbers of the array in 'values'. I feel I am somewhat close to getting this. Can someone help?

Ad

Answer

One of the ways is to convert the new data into the old data structure, so that you don't need to change the code.

This for loop will change you new data to the expected old data structure.

genes = [];

dataset = [{data:[], gene:"LPAR"}];
data.values.forEach(function(s){
  genes = [];

dataset = [{data:[], gene:"LPAR"}];
data.values.forEach(function(s){
  genes.push(s[0]);//iterate and add the gene
  dataset[0].data.push(s[1])//iterate and add the values.
});

Working code here

Hope this helps!

Ad
source: stackoverflow.com
Ad