简体   繁体   English

D3条件条形图

[英]Conditional Bar Chart with D3

I have a D3 visualization with a map and a bar chart. 我有一个带有地图和条形图的D3可视化。 I am trying to get the bar chart to change depending on which circle on the map is clicked. 我试图使条形图根据单击地图上的哪个圆圈而改变。 Not sure how to do this. 不确定如何执行此操作。 I have a function in my bar_chart.js file named update(newData) and a few extra arrays for the different circles on the map. 我的bar_chart.js文件中有一个名为update(newData)的函数,以及地图上不同圆圈的一些额外数组。 Here is the link to the bl.ocks for the map and bar char. 是地图和bar char的bl.ocks的链接。

js code for map 地图的js代码

var myData = [21, 3, 5, 21, 15];
//Width and height
var w = 200;
var h = 125;
var yScale = null;

function draw(initialData) {
  var xScale = d3.scale.ordinal()
    .domain(d3.range(initialData.length))
    .rangeRoundBands([0, w], 0.05);

  yScale = d3.scale.linear()
    .domain([0, d3.max(initialData)])
    .range([0, h]);

  //Create SVG element
  var svg = d3.select("#chart")
    .append("svg")
    .attr("width", w)
    .attr("height", h);

  svg.selectAll("rect")
    .data(initialData)
    .enter()
    .append("rect")
    .attr("x", function(d, i) {
      return xScale(i);
    })
    .attr("y", function(d) {
      return h - yScale(d);
    })
    .attr("width", xScale.rangeBand())
    .attr("height", function(d) {
      return yScale(d);
    })
    .attr("fill", "steelblue");

  svg.selectAll("text")
    .data(initialData)
    .enter()
    .append("text")
    .text(function(d) {
      return d;
    })
    .attr("text-anchor", "middle")
    .attr("x", function(d, i) {
      return xScale(i) + xScale.rangeBand() / 2;
    })
    .attr("y", function(d) {
      return h - yScale(d) + 14;
    })
    .attr("font-family", "sans-serif")
    .attr("font-size", "11px")
    .attr("fill", "white");
}

draw(myData);

//update function
function update(newData) {
  yScale.domain([0, d3.max(newData)]);

  var rects = d3.select("#chart svg")
    .selectAll("rect")
    .data(newData);

  // enter selection
  rects
    .enter().append("rect");

  // update selection
  rects
    .transition()
    .duration(300)
    .attr("y", function(d) {
      return h - yScale(d);
    })
    .attr("height", function(d) {
      return yScale(d);
    })

  // exit selection
  rects
    .exit().remove();

  var texts = d3.select("#chart svg")
    .selectAll("text")
    .data(newData);

  // enter selection
  texts
    .enter().append("rect");

  // update selection
  texts
    .transition()
    .duration(300)
    .attr("y", function(d) {
      return h - yScale(d) + 14;
    })
    .text(function(d) {
      return d;
    })

  // exit selection
  texts
    .exit().remove();
}

var mk = [10,17,20,14,8];
var cn = [18,4,9,20,15];
var nd = [5,12,7,15,21];

d3.select("#update").on("click", function() { update(newData); });

You have to incorporate the barchart data in your cities.csv file. 您必须将barchart数据合并到cities.csv文件中。

In the on-click handler of cities.csv where you show the tooltip you have to transform the data from the CSV into an array and call the bar chart update() method with this array. 在显示工具提示的cities.csv的单击处理程序中,您必须将数据从CSV转换为数组,然后使用该数组调用条形图update()方法。

One way of doing is to replace the , from the bar chart data with another char and split the string and convert the parts to numbers. 这样做的一个办法是更换,用另一个字符从柱状图数据和分割字符串和部分转换为数字。

var cityData = d.barchart.split('#').map(Number);
update(cityData);

You also have to set the attributes of the new rect s and text s of the bar chart. 您还必须设置条形图的新recttext的属性。 And the x-position will change if the number of bars change. 如果条数改变,x位置也会改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM