d3.js条形图在多个csv文件之间转换

时间:2014-03-14 15:59:08

标签: csv d3.js transition histogram

我正在研究我收集的关于冬季奥运会的一些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>

1 个答案:

答案 0 :(得分:2)

在您的更新功能中,您执行与最初相同的代码 - 您需要在此处考虑现有元素。代码看起来像这样:

// update axes
svg.select(".x").call(xAxis);
svg.select(".y").call(yAxis);

// update bars
var sel = svg.selectAll(".bar").data(data);
// add new bars
sel.enter().append("rect")
  .attr("class", "bar");
// update existing (and new) bars
sel.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);
// remove bars no longer present
sel.exit().remove();

要通过转换进行更改,请在更新属性之前添加.transition(),例如

sel.transition()
  .attr("x", function(d) { return x(d.letter); })
  // etc