2

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

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

特此摘录一段代码:

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
      }
    ]
  }
]

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

偏移“零”: 偏移“零”

偏移“扩展”: 偏移“扩展”

偏移“剪影”: 偏移“剪影”

偏移“摆动”: 偏移“摆动”

4

1 回答 1

3

From the documentation for stack.x:

The return value of the accessor must be a number.

The default accessor returns d.x, and your data currently contains strings for this property (e.g. "01/2014"). You can either set your own accessor, or modify the data so that the x-value is a number. The reason it only affected the wiggle mode is that it uses the x-value to compute the gradient.

于 2014-07-16T13:25:02.847 回答