Gopal Rao
Gopal Rao

Reputation: 161

Array elements representation in data table

I have a data set where some of the field values are arrays and I'd like to use crossfilter and d3.js or dc.js to display this data table.
I have tried doing this with this code:

var data = [
{"Name":"ajay","interests":[{"games":"vally"},{ "games":"cricket"}]},
]
var assetDataCF = crossfilter(data );
var dimByHostName = assetDataCF.dimension(function(d){return d.Name; });

var apSecAdvConfig = dc.dataTable('#apSecAdvConfig');
        apSecAdvConfig
            .dimension(dimByHostName)
            .group(function(d){ return "" })
            .transitionDuration(1000)
            .columns([ 
                  function(d) { return d.Name;},
                  function(d) { var s=d.interests,
                                  a=[];
                        for(var i=0;i<s.length;i++)
                        {
                             a.push(d.s[i].games);

                        } return a;}
                 }];

It displays the table as follows:

-------------------------------
name   ---   games
-----------------------------
 ajay   ---   vally,cricket
------------------------------

...but I want the result to be:


   name   ---   games
 ------------------------
   ajay   ---   vally
  -----------------------
   ajay   ----   cricket
  ------------------------
**********************************************

Anyone has any ideas how to achieve that?

Upvotes: 0

Views: 137

Answers (1)

Cyril Cherian
Cyril Cherian

Reputation: 32327

We can change the dataset

    var data = [
    {"Name":"ajay","interests":[{"games":"vally"},{ "games":"cricket"}]},
    {"Name":"cyril","interests":[{"games":"football"}]},
    ];
var newData = [];
data.forEach(function (person) {
    person.interests.forEach(function (interest) {
        newData.push({
            Name: person.Name,
            interest: interest.games
        })
    })
})

The above function will make the newdataset like this:

    [{"Name":"ajay","interest":"vally"},
{"Name":"ajay","interest":"cricket"},
{"Name":"cyril","interest":"football"}]

Post this your code can work on the newData set created above something like this:

var assetDataCF = crossfilter(newData );
//oldcode...

Upvotes: 1

Related Questions