简体   繁体   English

将stack()调用从v3转换为v4

[英]Converting stack() call from v3 to v4

I've had a modified version of Mike Bostock's animated stack ( seen here ) working for awhile now. 我已经有一段时间的Mike Bostock动画堆栈的修改版本了( 见此处 )。 On the advice of others here I've been trying to convert all of my D3 version 3 charts to version 4, so everything is compatible moving forward. 根据这里其他人的建议,我一直在尝试将我的所有D3版本3图表转换为版本4,因此所有功能都兼容。

Unfortunately, I can't seem to figure out why this code segment won't compile. 不幸的是,我似乎无法弄清楚为什么此代码段无法编译。 It's throwing the "Uncaught TypeError". 它引发了“未捕获的TypeError”。 Which I've been told has to do with the newer version wanting an array passed to stack(). 我被告知与较新的版本有关,该版本需要将数组传递给stack()。

var stack = d3.stack()
    .values(data, function(d) { return d.values; })
    .x(function(d) { return d.date; })
    .y(function(d) { return d.value; })
    .out(function(d, y0) { d.valueOffset = y0; });

How should this be done correctly under D3 version 4? 在D3版本4下如何正确完成此操作?

Edit: I also found this link to a support request on github , but it seems no one over there could simply explain how to convert this chart either. 编辑:我还在github上找到了此链接,指向支持请求 ,但似乎没有人可以简单地解释如何转换此图表。

D3 v4 d3.stack() constructor don't have values , x , y and out chaining methods. D3 v4 d3.stack()构造函数没有valuesxyout链接方法。 It's syntax for 3rd version. 这是第三版的语法。 Check docs here . 在此处检查文档。 Look at this example of stacked bar chart. 这个堆积条形图的例子 Note, as d3.stack uses in this example: 注意,如d3.stack在此示例中使用的:

...
  g.append("g")
   .selectAll("g")
   .data(d3.stack().keys(keys)(data))
   .enter().append("g")
...

Update: I rewrote "bl.ocks" by Mike Bostock mentioned in the question to the d3 v4. 更新:我将问题中提到的Mike Bostock的“ bl.ocks”重写为d3 v4。 Look at this fiddle - https://jsfiddle.net/levvsha/vrbq7ebz/ 看看这个小提琴-https: //jsfiddle.net/levvsha/vrbq7ebz/

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

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