简体   繁体   English

D3使堆叠的条形图动态化

[英]D3 make stacked bar chart dynamic

I am trying to follow this example: http://bl.ocks.org/mbostock/3886208 我正在尝试遵循以下示例: http : //bl.ocks.org/mbostock/3886208

在此处输入图片说明

In my application I have to show a stacked bar chart where I can see for example how many different animals specific user bought. 在我的应用程序中,我必须显示一个堆积的条形图,例如,在其中我可以查看特定用户购买了多少种不同的动物。

Here is my object: 这是我的对象:

[["PersonName1", 10, 10],["PersonName2", 4, 16]];

First property is name, second is animal type 1, and third is animal type 2. 第一个属性是名称,第二个属性是动物类型1,第三个属性是动物类型2。

I've got stuck in this foreach loop: 我陷入了这个foreach循环中:

data.forEach(function(d) {
    var y0 = 0;
    d.ages = color.domain().map(function(name) {
        return {name: name, y0: y0, y1: y0 += +d[name]};
    });
    d.total = d.ages[d.ages.length - 1].y1;
});

What exactly do the code lines below do? 下面的代码行到底做什么?

d.ages = color.domain().map(function(name) {
    return {name: name, y0: y0, y1: y0 += +d[name]};
});

I can't exactly figure out how to do the same thing with my objects. 我无法完全弄清楚如何对对象执行相同的操作。

I've tried to do something like this: 我试图做这样的事情:

scope.data.forEach(function(d) {
    var y0 = 0;
    d.animals = color.domain().map(function(name) {
        return {name: d[0], y0: y0, y1: y0 += +d[1]};
    });
    d.total = d.animals[d.animals.length - 1].y1;                     
});

But d.animals is always empty. 但是d.animals总是空的。

And i have to use it later on in: 而且我以后必须使用它:

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

Any ideas? 有任何想法吗?

That particular bit of code is just a rather confusing way of converting all the values in the row into an array of cumulative values. 特定的代码位只是将行中的所有值转换为累积值数组的一种相当混乱的方式。 It depends on the previous line of code, where the domain of the color scale is defined as all the keys from the first data object (ie, all the column names in the csv file) except for "State". 它取决于上一行代码,其中色标的域定义为第一个数据对象中的所有键(即,csv文件中的所有列名),但不包括“状态”。 It then maps this array of column names to an array of objects, each of which contains the column name, the total of all previous values ( y0 ), and the new total created by adding the corresponding data value (extracted from the data-row object with data[name] ) to the old total. 然后,它将此列名称数组映射到一个对象数组,每个对象包含列名称,所有先前值的总和( y0 ),以及通过添加相应的数据值(从数据行中提取)而创建的新总数带有data[name]对象)到旧总数。 In addition to being assigned to the y1 property of the object in the ages array, the new total is assigned to the y0 variable so that it carries over for mapping the next value in the array. 除了分配给ages数组中对象的y1属性外,新的总计也分配给y0变量,以便它可以继续映射数组中的下一个值。

This wasn't working for you, because your data rows are represented as arrays, not as key:value objects created from reading a csv file. 这对您不起作用,因为您的数据行表示为数组,而不表示为通过读取csv文件创建的key:value对象。 So you aren't able to create an array of column names, and you aren't able to use those names to access the data. 因此,您无法创建列名称的数组,也无法使用这些名称来访问数据。 You'll have to create the sub-arrays yourself, using array index numbers to access the different data values. 您必须自己创建子数组,使用数组索引号访问不同的数据值。

If that's still confusing, I would recommend you look at this or this example , which both use the d3 stack layout object to calculate the top and bottom positions of the stacked bars. 如果这仍然混乱,我建议你看看这个这个例子中 ,这两者都使用D3栈布局对象来计算堆叠条的顶部和底部位置。

Regardless of how you do the calculation, the end goal is to create a sub-array for each of your data rows, containing objects for each of the values that you want to stack. 不管您如何进行计算,最终目标都是为每个数据行创建一个子数组,其中包含要堆叠的每个值的对象。 Each object needs to have a y0 value representing the total of all previous values in the stack (ie, the bottom of the bar), and a y1 value representing the total of that bar and all the previous ones (ie, the top of the bar). 每个对象都需要有一个y0值,它代表堆栈中所有先前值的总和(即,条形的底部),以及一个y1值,它代表该条形与所有之前的值的总和(即,条形的顶部)。酒吧)。

You could just save the height of the bar instead of one of these values, but the stack layout creates the y0/y1 structure, since it is easy to use with stacked area graphs. 您可以只保存条形的高度 ,而不是保存这些值之一,但是堆栈布局会创建y0 / y1结构,因为它很容易与堆积面积图一起使用。 An area is defined by the position of the lines, not by the height. 面积由线条的位置而不是高度定义。

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

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