0

I'm currently working with dc.js, crossfilter.js, and d3.js to render some visualizations. I've come across a post pretty similar to what I was looking for here Crossfilter query and here d3 csv data loading.

What i'm aiming to do is to parse my csv, and for a certain column, parse it as an array within each object row. for instance, i have:

my data currently comes out as:

[
{FY: 2011, proj_name: "project 1", category: "fruit", subcategory: "strawberry, mango", total: 1510},
{FY: 2011, proj_name: "project 2", category: "fruit", subcategory: "orange, mango", total: 100},
{FY: 2011, proj_name: "project 2", category: "vegetable", subcategory: "celery", total: 100}
]

but i'm looking to format the data as so:

[
{FY: 2011, proj_name: "project 1", category: "fruit", subcategory: ["strawberry", "mango"], total: 1510},
{FY: 2011, proj_name: "project 1", category: "fruit", subcategory: ["orange", "mango"], total: 100},
{FY: 2011, proj_name: "project 1", category: "vegetable", subcategory: ["celery"], total: 100}
]

Is this something I can just put in data.forEach(function (d)) ?

    d3.csv("data.csv", function(data) { 
    data.forEach(function (d) { 
        d.FY = +d.FY; 
        d.total = +d.total; 
        d.subcategory = d.subcategory.split(/,/); 
        }) 
    })

I don't think is correct and it simply isn't working, but what I'm getting at here is that it's something that needs to be done repetitively within this column specifically so that I can bucket the subcategories as tags later on.. Any thoughts/feedback?

Much thanks in advance

1 Answer 1

2

D3 already has this covered, the format for d3.csv is: d3.csv(url[, accessor][, callback]) where accessor is a function called for each row in the csv to transform the data. Try this:

d3.csv("data.csv", function(d) { 
  return {
    FY: +d.FY,
    total: +d.total,
    category: d.category,
    subcategory: d.subcategory.split(/,/)
  };
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.