admin管理员组文章数量:1303460
How do I adapt the code below from to select specific columns?
var line = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.temperature); });
d3.tsv("data.tsv", function(error, data) {
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "date"; }));
data.forEach(function(d) {
d.date = parseDate(d.date);
});
var cities = color.domain().map(function(name) {
return {
name: name,
values: data.map(function(d) {
return {date: d.date, temperature: +d[name]};
})
};
});
In the data being used in the example above, there are 4 columns: dates and 3 columns for temperature in 3 cities. In my use case, i have 10 columns: dates and 3 variables per city for 3 cities i.e. (1 + [3 * 3]). I would like to load the entire data set (for use in tooltips), but only want to chart one of the variables per city which is in column index # 1, 4 and 7. How do I do this? (see rough syntax below).
var cities = color.domain().map(function(name) {
return {
name: name,
values: data.map(function(d) {
return {date: d.date, maxTemperature: *+d[arrays in column index 1, 4 and 7]*};
})
values2: data.map(function(d) {
return {date: d.date, minTemperature: *+d[arrays in column index 2, 5 and 8]*};
})
values3: data.map(function(d) {
return {date: d.date, avgTemperature: *+d[arrays in column index 3, 8 and 9]*};
})
};
});
How do I adapt the code below from http://bl.ocks/mbostock/3884955 to select specific columns?
var line = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.temperature); });
d3.tsv("data.tsv", function(error, data) {
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "date"; }));
data.forEach(function(d) {
d.date = parseDate(d.date);
});
var cities = color.domain().map(function(name) {
return {
name: name,
values: data.map(function(d) {
return {date: d.date, temperature: +d[name]};
})
};
});
In the data being used in the example above, there are 4 columns: dates and 3 columns for temperature in 3 cities. In my use case, i have 10 columns: dates and 3 variables per city for 3 cities i.e. (1 + [3 * 3]). I would like to load the entire data set (for use in tooltips), but only want to chart one of the variables per city which is in column index # 1, 4 and 7. How do I do this? (see rough syntax below).
var cities = color.domain().map(function(name) {
return {
name: name,
values: data.map(function(d) {
return {date: d.date, maxTemperature: *+d[arrays in column index 1, 4 and 7]*};
})
values2: data.map(function(d) {
return {date: d.date, minTemperature: *+d[arrays in column index 2, 5 and 8]*};
})
values3: data.map(function(d) {
return {date: d.date, avgTemperature: *+d[arrays in column index 3, 8 and 9]*};
})
};
});
Share
Improve this question
edited Oct 1, 2013 at 14:34
WittyID
asked Oct 1, 2013 at 12:24
WittyIDWittyID
6192 gold badges6 silver badges15 bronze badges
3 Answers
Reset to default 1You shouldn't need to make any changes to the existing code. The way cities
is set up only requires you to give a meaningful name to the data you need and then reference that later. So you would have something like
var cities = color.domain().map(function(name) {
return {
name: name,
values: data.map(function(d) {
return {date: d.date, first: +d[name + " first"], second: +d[name + " second"]};
})
};
d3.min(cities, function(c) { return d3.min(c.values, function(v) { return v.first; }); });
Note that the actual names of course depend on what's in your data.
I was able to solve it by adding filter key conditions to exlude the other data categories i don't want to chart. In the example i used above, i had a min, max and avg temp per city and wanted to grab only the avg temperatures. I added filters excluding column headers with strings that included "max" or "min" like so.
color.domain(d3.keys(data[0]).filter(function(key)
{ return key !== "date" && key.indexOf("max") == -1 &&
key.indexOf("min") == -1; });
I don't necessarily know what the full column name is, but in my use case, the different variables are always tagged with max, min or avg which makes this a workable solution for me, but probably not for someone with totally unknown column headers. I had initially wanted to select columns based on index #, but this works just fine.
This worked for me in terms of selecting columns:
d3.csv("data.csv", function(error, data) {
color.domain(d3.keys(data[0]).filter(function(key) {
return key == "avg" || key == "additional_columns";
});
});
本文标签: javascriptSelecting d3 data subset based on columnStack Overflow
版权声明:本文标题:javascript - Selecting d3 data subset based on column - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741760171a2396356.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论