如何使用d3js动态更新我的分组条形图?

时间:2014-11-24 13:13:39

标签: javascript csv d3.js visualization

我成功地创建了一个分组条形图,显示了一个赛季中足球运动员的各种表现统计数据。此数据全部从csv文件加载。我现在想动态地改变那些数据,当按下按钮时,新的csv文件加载了与下一个足球赛季相对应的统计数据。我试图自己做这个,但每当我尝试重新加载数据时,旧数据以及旧轴仍然存在,并且enw数据加载到这些上面。这一切看起来都很混乱。那么如何根据新数据更新旧条和轴?这是我的代码:

$(document).ready(function(){

var margin = {top: 20, right: 20, bottom: 30, left: 40},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var x0 = d3.scale.ordinal()
    .rangeRoundBands([0, width], .1);

var x1 = d3.scale.ordinal();

var y = d3.scale.linear()
    .range([height, 0]);

var color = d3.scale.ordinal()
    .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);

var xAxis = d3.svg.axis()
    .scale(x0)
    .orient("bottom");

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left")
    .tickFormat(d3.format(".2s"));

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 + ")");


d3.csv("SoccerStatsCSV.csv", function(error, data) {
  console.log(data);
  var playerNames = d3.keys(data[0]).filter(function(key) { return key !== "Attribute"; });
  console.log(playerNames);

  data.forEach(function(d) {
    d.Playerstats = playerNames.map(function(name) { return {name: name, value: +d[name]}; });
    console.log(d.Playerstats);
  });    

  x0.domain(data.map(function(d) { return d.Attribute; }));
  x1.domain(playerNames).rangeRoundBands([0, x0.rangeBand()]);
  y.domain([0, d3.max(data, function(d) { return d3.max(d.Playerstats, function(d) { return d.value; }); })]);

  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("Units");

  var state = svg.selectAll(".state")
      .data(data)
      .enter().append("g")
      .attr("class", "g")
      .attr("transform", function(d) { return "translate(" + x0(d.Attribute) + ",0)"; });

  state.selectAll("rect")
      .data(function(d) { return d.Playerstats; })
      .enter().append("rect")
      .attr("width", x1.rangeBand())
      .attr("x", function(d) { return x1(d.name); })
      .attr("y", function(d) { return y(d.value); })
      .attr("height", function(d) { return height - y(d.value); })
      .style("fill", function(d) { return color(d.name); });

  var legend = svg.selectAll(".legend")
      .data(playerNames.slice())
      .enter().append("g")
      .attr("class", "legend")
      .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });

  legend.append("rect")
      .attr("x", width - 18)
      .attr("width", 18)
      .attr("height", 18)
      .style("fill", color);

  legend.append("text")
      .attr("x", width - 24)
      .attr("y", 9)
      .attr("dy", ".35em")
      .style("text-anchor", "end")
      .text(function(d) { return d; }); 





});





//The updating button

d3.select("button")
  .on('click',function(){ 
d3.csv("SoccerStatsCSV2008.csv", function(error, data) {
  console.log(data);
  var playerNames = d3.keys(data[0]).filter(function(key) { return key !== "Attribute";    });
  console.log(playerNames);

  data.forEach(function(d) {
    d.Playerstats = playerNames.map(function(name) { return {name: name, value: +d[name]}; });
   console.log(d.Playerstats);
  });    

  x0.domain(data.map(function(d) { return d.Attribute; }));
  x1.domain(playerNames).rangeRoundBands([0, x0.rangeBand()]);
  y.domain([0, d3.max(data, function(d) { return d3.max(d.Playerstats, function(d) {     return d.value; }); })]);

  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("Units");

  var state = svg.selectAll(".state")
      .data(data)
      .enter().append("g")
      .attr("class", "g")
      .attr("transform", function(d) { return "translate(" + x0(d.Attribute) + ",0)"; });

  state.selectAll("rect")
      .data(function(d) { return d.Playerstats; })
      .enter().append("rect")
      .attr("width", x1.rangeBand())
      .attr("x", function(d) { return x1(d.name); })
      .attr("y", function(d) { return y(d.value); })
      .attr("height", function(d) { return height - y(d.value); })
      .style("fill", function(d) { return color(d.name); });

  var legend = svg.selectAll(".legend")
      .data(playerNames.slice())
      .enter().append("g")
      .attr("class", "legend")
      .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });

  legend.append("rect")
      .attr("x", width - 18)
      .attr("width", 18)
      .attr("height", 18)
      .style("fill", color);

  legend.append("text")
      .attr("x", width - 24)
      .attr("y", 9)
      .attr("dy", ".35em")
      .style("text-anchor", "end")
      .text(function(d) { return d; });



});
});




});

0 个答案:

没有答案