我正在寻找一些关于如何修复如何将我的数据可视化为条形图的指示。我创建了一个随时间出现的数据的小倍数可视化。由于数据收集方式的性质,数据具有某些属性。例如,您可以在下面看到 .csv 的摘录,其中group
和date
重复:
group,date,totals
acid,1641,8438
acid,1641,0
acid,1641,0
beef,1641,977.85
beef,1641,0
beef,1641,0
beef,1641,164
beef,1641,5.25
bird,1641,121
bird,1641,0
bird,1641,12
bird,1641,1558
bird,1641,729
bird,1641,1680
bird,1641,0
bird,1641,343
bird,1641,3
bird,1641,2092
bird,1641,0
bird,1641,0
bird,1641,107
bird,1641,2679
bird,1641,167
bird,1641,649
boar,1641,41
boar,1641,13
cheese,1641,13
cheese,1641,22
cheese,1641,1071
cheese,1641,17195
(您可以在此处查看整个数据集。)
我遇到的问题date
是共享 a 的任何地方,而不是对totals
列求和,而是将数据堆叠在一起。例如,请参阅:
我正在努力将数据显示为单个条形图。(我有一个 y 轴无法正确缩放的次要问题)。我曾尝试使用nest来解决多年来的问题(这适用于groups
),但我似乎无法使用date
.
到目前为止,这是我的代码:
var margin = {top: 20, right: 30, bottom: 30, left: 50},
width = 400 - margin.right - margin.left,
height = 150 - margin.top - margin.bottom,
parse = d3.time.format("%Y").parse;
// scales
var x = d3.time.scale().range([0, width]),
y = d3.scale.linear().range([0, height]),
yscaled = d3.scale.linear().range([height, 0]);
// load data
d3.csv("revised.csv", function(error, data) {
// nest values by group
var groups = d3.nest()
.key(function(d) { return d.group; })
.entries(data);
// parse dates and numbers, assume values are sorted by date
// compute the maximum totals per group
groups.forEach(function(s) {
s.values.forEach(function(d) { d.date = parse(d.date); d.totals = +d.totals; });
s.maxtotals = d3.max(s.values, function(d) { return d.totals; });
});
// compute the minimum and maximum date across groups
x.domain([
d3.min(groups, function(s) { return s.values[0].date; }),
d3.max(groups, function(s) { return s.values[s.values.length - 1].date; })
]);
// y.domain([0, d3.max(data, function(d) { return d.totals; })]);
// add an SVG element for each group
var svg = d3.select("body").selectAll("g")
.data(groups)
.enter().append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
var canvas_g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.each(function(d) {
var g = d3.select(this);
g.append("g");
// .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
y.domain([0, d3.max(d.values, function(r) { return r.totals; })]);
yscaled.domain([0, d3.max(d.values, function(r) { return r.totals; })]);
// Draw the charts.
g.selectAll("rect.aevents")
.data(d.values)
.enter().append("rect")
.attr("height", function(p) {return y(p.totals)})
.attr("width", 5)
.attr("x", function(p, q) {return x(p.date)})
.attr("y", function(p) {return height - y(p.totals)})
// .on("click", function(p) {console.log(p.date)})
.attr("class", "bar");
});
// draw x axis
canvas_g.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.svg.axis().scale(x).orient("bottom"));
// draw y axis
canvas_g.append("g")
.attr("class", "y axis")
.call(d3.svg.axis().scale(yscaled).orient("left"));
// add a small label for the group name
svg.append("text")
.attr("x", width + 40)
.attr("y", height + 15)
.attr("text-anchor", "end")
.text(function(d) { return d.key; });
});
我应该以不同的方式遍历数据吗?或者,我是否需要以某种方式重组数据?先感谢您!