1

My elex.csv is in this format

"CONUM","PIXPMK","PIXMAR"

"1","461","436"
"2","398","428",
"3","447","436"

I want my dataset in this format-

var dataset1 = [ [1, 417], [1, 436], [2, 398], [2, 428], [3, 447], [3, 436]];

I have tried several times to get the fetch the data from csv in this form but all in vain. I am attaching my code

var dataset1 = [];

d3.csv("elex.csv", function(data) {
   dataset1 = data.map(function(d) { return [ [+d["CONUM"], +d["PIXMAR"]],[+d["CONUM"],+d["PIXPMK"]]]; });
console.log(dataset1)
});

this returns dataset1 as

[[[1,417],[1,436]],[[2,398],[2,428]]]

1 Answer 1

3

Iterate over each element, and push the first and second part into dataset1 separately:

dataset1 = [];
d3.csv("elex.csv", function(data) {
  data.forEach(function(d) {
    dataset1.push([d['CONUM'], d['PIXMAR']]);
    dataset1.push([d['CONUM'], d['PIXPMK']*100])
  });
  console.log(dataset1);
});
Sign up to request clarification or add additional context in comments.

6 Comments

Why the multiply by 100 ??
@thatOneGuy, it was the calculation OP used
could u please tell me the entire code...i tried this..but facing some problem
Realised i was looking at it wrong. Here's yours implemented in PLNKR : plnkr.co/edit/WerxLZgyLrr7PJt3YAYw?p=preview
@KuhuGupta, d3 helps you to bind data to the graphics you build. it's powerful that you can build any graphics you can imagine, but unlike other charting libraries, you have to do some extra work. this link will get you started
|

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.