【发布时间】:2014-03-14 15:59:08
【问题描述】:
我正在为我收集的关于冬奥会的一些 Twitter 数据可视化词频。我想使用 d3 制作一个直方图,它将这些计数可视化并允许用户在游戏的每一天的数据集之间切换。我有一个我对here 相当满意的初始可视化,从this 示例修改。但是,当单击右下角的“更新”按钮时,这只会在现有直方图之上覆盖一个新的直方图。这两个数据集都是 CSV,我希望能够切换其中的七个。
我在 Stack Overflow 上搜索了其他相关提示并找到了这个,但它似乎并不能完全满足我的需求:Transitioning bar chart with combobox variable toggle in d3.js。使用此示例的主要问题是它使用相同的 json 数据在不同的直方图之间创建转换,而我需要在直方图之间创建转换,每个直方图从不同的 CSV 数据文件中提取数据。到目前为止,这是我的代码。对主要的混淆点进行了适当的评论。任何指导将不胜感激。
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: #3c9dd0;
}
.bar:hover {
fill: #ffc073;
}
.x.axis path {
display: none;
}
.d3-tip {
line-height: 1;
font-weight: bold;
padding: 12px;
background: rgba(0, 0, 0, 0.8);
color: #fff;
border-radius: 2px;
}
/* Creates a small triangle extender for the tooltip */
.d3-tip:after {
box-sizing: border-box;
display: inline;
font-size: 10px;
width: 100%;
line-height: 1;
color: rgba(0, 0, 0, 0.8);
content: "\25BC";
position: absolute;
text-align: center;
}
/* Style northward tooltips differently */
.d3-tip.n:after {
margin: -1px 0 0 0;
top: 100%;
left: 0;
}
</style>
<body>
<button onclick="update()">Update</button>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
<script>
var margin = {top: 40, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// var formatPercent = d3.format(".0%");
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return "<strong>Term: </strong><span style='color:orangered'>" + d.letter + "</span>" + " <strong>Frequency:</strong> <span style='color:orangered'>" + d.frequency + "</span>";
})
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.call(tip);
d3.csv("data1.csv", type, function(error, data) {
x.domain(data.map(function(d) { return d.letter; }));
y.domain([0, d3.max(data, function(d) { return d.frequency; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Frequency");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.letter); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.frequency); })
.attr("height", function(d) { return height - y(d.frequency); })
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
});
// Test on updating data. Confusion starts here.
function update(){
d3.csv("data2.csv", type, function(error, data) {
x.domain(data.map(function(d) { return d.letter; }));
y.domain([0, d3.max(data, function(d) { return d.frequency; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Frequency");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.letter); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.frequency); })
.attr("height", function(d) { return height - y(d.frequency); })
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
})
// Confusion continues here. I know I need a transition, but I have
// no idea how to implement it.
d3.transition().selectAll(".bar")
.attr('d', function(d) {return y(d.frequency);})
}
function type(d) {
d.frequency = +d.frequency;
return d;
}
</script>
【问题讨论】:
-
This tutorial 应该会有所帮助。这个想法是绑定新数据并相应地更新
rect元素。 -
嗨@LarsKotthoff,感谢您的提示。不幸的是,我有点卡在教程中。我知道我需要 .remove() 从第一个 CSV 中删除数据,但我不确定如何实现这一点。在最后的教程中,有一个 .remove() 语句作为最后一行代码,但这不会从第二个 CSV 中删除我刚刚绑定到新条形形状的数据吗?从概念上讲,本教程会有所帮助,但具体来说,我觉得我的起点是正确的......
标签: csv d3.js transition histogram