【问题标题】:d3.layout.stack() with 'wiggle' offset issued3.layout.stack() 带有“摆动”偏移问题
【发布时间】:2014-09-05 21:09:33
【问题描述】:

我在 d3 中编写了一个函数,以便在示例页面中创建多个显示不同数据集的图。

我正在使用流图和堆栈布局,但我无法理解为什么使用相同的代码我可以使用偏移量 zeroexpandsilhouette 生成绘图,同时使用相同的数据集和相同的堆栈布局wiggle offset 不会产生任何东西。 (我已经不止一次阅读过文档)

这里摘录一段代码:

var stack = d3.layout.stack()
    .values(function(d) {
        return d.values;
    }).offset(o || 'silhouette');// o could be one of [expand, wiggle, zero, silhouette]

var layers = stack(stacked.datalayers()); // See after this piece of code

var maxY = d3.max(layers, function(c) {
    return d3.max(c.values, function(d) {
        return d.y0 + d.y;
    });
});

var x = d3.scale.ordinal().domain(stacked.x()).rangeRoundBands([0, w]); // stacked.x() returns an array with the min and the max values for X
var y = d3.scale.linear().domain([0, maxY]).range([h, 0]);

var area = d3.svg.area()
        .x(function(d) {
            return x(d.x) + (x.rangeBand() / 2);
        })
        .y0(function(d) {
            return y(d.y0);
        })
        .y1(function(d) {
            return y(d.y0 + d.y);
        }).interpolate("monotone");

...

series.append("path")
        .attr("d", function(d) {
            return area(d.values);
        })
        .style("fill", function(d) {
            return color(cdomain.indexOf(d.name));
        })
        .style("fill-opacity", ".5")
        .style("stroke", function(d) {
            return color(cdomain.indexOf(d.name));
        })
        .style("stroke-width", "2px");

这是stacked.datalayer()返回的数据结构:

[
  {
    "name": "US",
    "values": [
      {
        "x": "01/2014",
        "y": 1.726118500604595,
        "name": "US",
        "y0": 0.8662854227545267
      },
      {
        "x": "02/2014",
        "y": 2.5897229845496037,
        "name": "US",
        "y0": 0.38969767845094694
      },
      {
        "x": "03/2014",
        "y": 2.388349800480026,
        "name": "US",
        "y0": 0.518912280793379
      }
    ]
  },
  {
    "name": "Europe28",
    "values": [
      {
        "x": "01/2014",
        "y": 0.42541539123546496,
        "name": "Europe28",
        "y0": 2.5924039233591216
      },
      {
        "x": "02/2014",
        "y": 0.5149863958976154,
        "name": "Europe28",
        "y0": 2.9794206630005506
      },
      {
        "x": "03/2014",
        "y": 0.4579303752823291,
        "name": "Europe28",
        "y0": 2.907262081273405
      }
    ]
  }
]

以下是一些结果的截图(使用相同的数据集):

偏移“零”:

偏移“扩展”:

偏移“剪影”:

偏移“摆动”:

【问题讨论】:

    标签: javascript layout d3.js offset stream-graph


    【解决方案1】:

    来自documentation for stack.x

    访问器的返回值必须是数字。

    默认访问器返回d.x,并且您的数据当前包含此属性的字符串(例如"01/2014")。您可以设置自己的访问器,也可以修改数据以使 x 值为数字。它只影响wiggle 模式的原因是它使用x 值来计算梯度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-13
      • 1970-01-01
      • 2017-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多