I've been given the following working code:
var datasetBarChart = [{
group: "X",
category: "Oranges",
measure: 100
}, {
group: "Y",
category: "Apples",
measure: 200
}];
// set initial group value
var group = "X";
function datasetBarChosen(group) {
var ds = [];
for (x in datasetBarChart) {
if (datasetBarChart[x].group == group) {
ds.push(datasetBarChart[x]);
}
}
return ds;
}
Rather than hard-coding the data in datasetBarChart
I've moved the data to a csv that looks like this:
group,category, measure
X, Oranges, 100
Y, Applies, 200
Now I'm trying to amend the function - it still needs to return ds;
- the following is one of my attempts that does not work:
function datasetBarChosen(group) {
var ds = [];
d3.csv("http://ourServer/testingtesting/DThree/datasetBarChart.csv", function(rows) {
for (x in rows) {
if (rows[x].group == group) {
ds.push(rows[x]);
}
}
}
return ds;
}
Here is another failed attempt!!
function datasetBarChosen(group) {
var ds = [];
d3.csv("http://ourServer/testingtesting/DThree/datasetBarChart.csv", function(rows) {
ds = rows.map(function(d) {
//each d is one line of the csv file represented as a json object
if (d.group == group) {
return {
"group": d.group,
"category": d.category,
"measure": +d.measure
}
};
})
}
return ds;
}
....is either of the following close?