简体   繁体   English

d3.js类别的堆叠条形图自定义高度

[英]d3.js stacked bar chart custom height for Category

I am using recently d3.js and I came up with an problem, for which I can´t find an solution. 我最近使用的是d3.js,但遇到一个问题,无法找到解决方案。

I need an stacked Bar/column where I can configure how long an Category (a part of one line) is and which color it has. 我需要一个堆叠的Bar / column,可以在其中配置Category(一行的一部分)多长时间以及它具有哪种颜色。 This length is computed from an formula. 该长度是根据公式计算的。 This formula is the same for the whole Line and chart. 对于整个折线和图表,此公式相同。

I would like to knew how to assign an custom length to that categories and colors. 我想知道如何为该类别和颜色分配自定义长度。 At the beginning I only have a Matrix, whose data is used by the formula. 一开始我只有一个矩阵,其数据由公式使用。 I suppose I have to do something with the data. 我想我必须对数据做些事情。 -Attribute. -属性。 Btw, the formula takes also the graph height, and width into account so it is possible that the data has to be recomputed at runtime. 顺便说一句,该公式还考虑了图形的高度和宽度,因此有可能必须在运行时重新计算数据。

I can show no code, since I have no idea where to begin. 我不显示任何代码,因为我不知道从哪里开始。

EDIT: 编辑:

Here it´s written that you have to use .stack() to use stacked bars. 在这里 ,您必须使用.stack()来使用堆叠的条。 And [here][2] is .stack() used, but here it´s not. [here] [2]使用的是.stack(),但这里没有。 I don´t get why those in link3 don´t need the stack() function? 我不明白为什么link3中的那些不需要stack()函数? Furthermore it´s written that I have to provide data in 此外,我写到我必须提供数据

      var dataset = [
    [ { x: 0, y: 5 },
      { x: 1, y: 4 },      
    ],
    [
     { x: 0, y: 10 },...]

form, my data is like that matrix = [[val1,val2,.....,val10], [val1,...val10]] 形式,我的数据就像那个矩阵= [[val1,val2,.....,val10],[val1,... val10]]

The data in one inner Array should be stacked, that Martix has around 5k entries. 应该将一个内部Array中的数据堆叠起来,Martix大约有5k个条目。 Do I have to make an Matrix with 10 inner Array where each Array consists of 5k entries? 我是否必须制作一个包含10个内部数组的矩阵,其中每个数组包含5k个条目? And at last, that values are from 0 to 1, is it better to control the height of the bars with style or with scale? 最后,该值是从0到1,是否最好用样式或比例来控制条的高度?

D3js' examples are all really great. D3js的示例非常棒。 There is a ton of them, and they all contain the source code and sample data. 它们有很多,它们都包含源代码和示例数据。

I would start there and see if you can find one to your liking. 我将从那里开始,看看您是否可以找到自己喜欢的一个。

This one is my best guess of the closest one to what you want. 是我最接近您想要的一个的最佳猜测。

He rearranges the width of the bars, so he already supplies you with a function to change the size, you just have to make it work with your data. 他重新排列了条形图的宽度,因此他已经为您提供了更改大小的功能,您只需要使其与数据一起使用即可。

Update 更新资料

In the first example you linked to, the .stack() function is used for data that is already in the format that you provided in your answer. 在您链接到的第一个示例中, .stack()函数用于已采用您在答案中提供的格式的数据。 Therefore, that function won't be of much help to you. 因此,该功能对您没有太大帮助。

The second example he states that he avoids using the .stack() function because you can do it manually. 他的第二个示例指出,他避免使用.stack()函数,因为您可以手动进行操作。

This example doesn't use d3.layout.stack because it's easy to just stack each state independently via array.forEach. 这个示例不使用d3.layout.stack,因为通过array.forEach单独堆叠每个状态很容易。

I'm not sure what you mean by one inner Array should be stacked . 我不确定one inner Array should be stacked意思。

In the examples, the data is stacked like so (I changed the names and added comments for clarity): 在示例中,数据像这样堆叠(为了清楚起见,我更改了名称并添加了注释):

var dataset = [
    // Columns a, b, and c
    { a: 5, b: 10, c: 22 }, // row 1
    { a: 4, b: 12, c: 28 }, // row 2
    { a: 2, b: 19, c: 32 }, // row 3
    { a: 7, b: 23, c: 35 }, // row 4
    { a: 23, b: 17, c: 43 } // row 5
];


var bars_dataset = [
    // Stacked Bar #1
    [
            { x: 0, y: 5 }, // row 1, column a
            { x: 1, y: 4 }, // row 2, column a
            { x: 2, y: 2 }, // row 3, column a
            { x: 3, y: 7 }, // row 4, column a
            { x: 4, y: 23 } // row 5, column a
    ],
    // Stacked Bar #2
    [
            { x: 0, y: 10 }, // row 1, column b
            { x: 1, y: 12 }, // row 2, column b
            { x: 2, y: 19 }, // row 3, column b
            { x: 3, y: 23 }, // row 4, column b
            { x: 4, y: 17 }  // row 5, column b
    ],
    // Stacked Bar #3
    [
            { x: 0, y: 22 }, // row 1, column c
            { x: 1, y: 28 }, // row 2, column c
            { x: 2, y: 32 }, // row 3, column c
            { x: 3, y: 35 }, // row 4, column c
            { x: 4, y: 43 }  // row 5, column c
    ]
];

As I think you figured out already, there are three rows in the bars_dataset because there are three columns in the dataset . 当我想你想通了,已经有三排在bars_dataset因为有三列dataset If this is how you want your bars to be stacked, then yes, you will have a bars_dataset with 10 arrays that consist of ~5k objects. 如果这是您希望条形图堆叠的方式,那么是的,您将拥有一个带有10个由bars_dataset对象组成的数组的bars_dataset

However, if you want the first array with 10 values in your matrix to make up the first bar, and so on... Then you will need to have a bars_dataset of ~5k arrays with 10 objects in each of them. 但是,如果您希望矩阵中的第一个数组包含10个值,以此类推,依此类推...那么您将需要拥有大约5k数组的bars_dataset ,每个数组中包含10个对象。

From the way you worded your question, it seems you want the second solution, and not the first. 从您表达问题的方式来看,您似乎想要第二个解决方案,而不是第一个。 Whichever way you plan on stacking them, let me know and I will update my answer with some sample code. 无论您打算采用哪种方式堆叠它们,请让我知道,我将用一些示例代码来更新我的答案。

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

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