d3堆栈条形图问题

时间:2016-01-20 14:33:37

标签: javascript d3.js

我正在使用d3创建堆积条形图。图形轴将被渲染但不是条形图,JS控制台也没有抛出任何错误。

以下是代码:



var margin = {
    top: 20,
    right: 30,
    bottom: 30,
    left: 40
  },
  width = 960 - margin.left - margin.right,
  height = 500 - margin.top - margin.bottom;

var data = [{
  data: [
    ["2016-01-20T10:36:00.000Z", 95.6981132075472],
    ["2016-01-20T11:10:00.000Z", 95.8882352941176],
    ["2016-01-20T11:44:00.000Z", 95.8470588235294],
    ["2016-01-20T12:18:00.000Z", 95.7941176470588],
    ["2016-01-20T12:52:00.000Z", 95.675],
    ["2016-01-20T13:26:00.000Z", 95.7573529411765],
    ["2016-01-20T14:00:00.000Z", 95.8294117647059],
    ["2016-01-20T14:34:00.000Z", 95.7736842105263]
  ],
  label: "a"
}, {
  data: [
    ["2016-01-20T10:36:00.000Z", 3.18867924528302],
    ["2016-01-20T11:10:00.000Z", 3.15441176470588],
    ["2016-01-20T11:44:00.000Z", 3.15],
    ["2016-01-20T12:18:00.000Z", 3.16323529411765],
    ["2016-01-20T12:52:00.000Z", 3.13970588235294],
    ["2016-01-20T13:26:00.000Z", 3.17794117647059],
    ["2016-01-20T14:00:00.000Z", 3.16617647058824],
    ["2016-01-20T14:34:00.000Z", 3.18888888888889]
  ],
  label: "b"
}];


var svg = d3.select("body").append("svg")
  .attr("width", width + margin.left + margin.right)
  .attr("height", height + margin.top + margin.bottom)
  .append("g")
  .attr("transform", "translate(" + margin.left + "," + margin.top + ")");


var x = d3.scale.ordinal()
  .rangeRoundBands([0, width]);

var y = d3.scale.linear()
  .rangeRound([height, 0]);

var z = d3.scale.category10();

var xAxis = d3.svg.axis()
  .scale(x)
  .orient("bottom");

var yAxis = d3.svg.axis()
  .scale(y)
  .orient("left");

var stack = d3.layout.stack()
  .values(function(d) {
    return d.data;
  })
  .x(function(d) {
    return new Date(d[0]);
  })
  .y(function(d) {
    return d[1];
  });

var layers = stack(data);

var ary = [];
layers.forEach(function(d) {
  ary.push(d.data);
});

x.domain(d3.extent(d3.merge(ary), function(d) {
  return new Date(d[0]);
}));

y.domain([0, d3.max(d3.merge(ary), function(d) {
  return d.y0 + d.y;
})]);

var layer = svg.selectAll(".layer")
  .data(layers)
  .enter().append("g")
  .attr("class", "layer")
  .style("fill", function(d, i) {
    return z(i);
  });

layer.selectAll("rect")
  .data(function(d) {
    return d;
  })
  .enter().append("rect")
  .attr("x", function(d) {
    return x(d.x);
  })
  .attr("y", function(d) {
    return y(d.y + d.y0);
  })
  .attr("height", function(d) {
    return y(d.y0) - y(d.y + d.y0);
  })
  .attr("width", x.rangeBand() - 1);

svg.append("g")
  .attr("class", "axis axis--x")
  .attr("transform", "translate(0," + height + ")")
  .call(xAxis);

svg.append("g")
  .attr("class", "axis y")
  .call(yAxis);

body {
  font: 10px sans-serif;
}
.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
&#13;
&#13;
&#13;

请在上面发布的代码中告诉我我的错误,并帮我修复此代码。

2 个答案:

答案 0 :(得分:2)

我已在上面的代码中解决了我的轴的问题。并且由于@adilapapaya指出的错误,能够显示堆栈栏。

var margin = {
    top: 20,
    right: 30,
    bottom: 30,
    left: 40
  },
  width = 960 - margin.left - margin.right,
  height = 500 - margin.top - margin.bottom;

var data = [{
  data: [
    ["2016-01-20T10:36:00.000Z", 95.6981132075472],
    ["2016-01-20T11:10:00.000Z", 95.8882352941176],
    ["2016-01-20T11:44:00.000Z", 95.8470588235294],
    ["2016-01-20T12:18:00.000Z", 95.7941176470588],
    ["2016-01-20T12:52:00.000Z", 95.675],
    ["2016-01-20T13:26:00.000Z", 95.7573529411765],
    ["2016-01-20T14:00:00.000Z", 95.8294117647059],
    ["2016-01-20T14:34:00.000Z", 95.7736842105263]
  ],
  label: "a"
}, {
  data: [
    ["2016-01-20T10:36:00.000Z", 3.18867924528302],
    ["2016-01-20T11:10:00.000Z", 3.15441176470588],
    ["2016-01-20T11:44:00.000Z", 3.15],
    ["2016-01-20T12:18:00.000Z", 3.16323529411765],
    ["2016-01-20T12:52:00.000Z", 3.13970588235294],
    ["2016-01-20T13:26:00.000Z", 3.17794117647059],
    ["2016-01-20T14:00:00.000Z", 3.16617647058824],
    ["2016-01-20T14:34:00.000Z", 3.18888888888889]
  ],
  label: "b"
}];

var parseDate = d3.time.format("%Y-%m-%dT%H:%M:%S.%LZ").parse;

var svg = d3.select("body").append("svg")
  .attr("width", width + margin.left + margin.right)
  .attr("height", height + margin.top + margin.bottom)
  .append("g")
  .attr("transform", "translate(" + margin.left + "," + margin.top + ")");


var x = d3.scale.ordinal()
  .rangeRoundBands([0, width], 0.7);

var y = d3.scale.linear()
  .range([height, 0]);

var z = d3.scale.category10();

var xAxis = d3.svg.axis()
  .scale(x)
  .orient("bottom").ticks(d3.time.hour, 1)
  .tickSize(0).innerTickSize(-height).outerTickSize(0).tickFormat(d3.time.format("%H:%M"));

var yAxis = d3.svg.axis()
  .scale(y)
  .orient("left");

var stack = d3.layout.stack()
  .values(function(d) {
    return d.data;
  })
  .x(function(d) {
    return parseDate(d[0]);
  })
  .y(function(d) {
    return d[1];
  });

var layers = stack(data);

var ary = [];
layers.forEach(function(d) {
  ary.push(d.data);
});

console.log("d = ", ary)

x.domain(ary[0].map(function(d) {
  return parseDate(d[0]);
}));

y.domain([0, d3.max(d3.merge(ary), function(d) {
  return d.y0 + d.y;
})]);

var layer = svg.selectAll(".layer")
  .data(layers)
  .enter().append("g")
  .attr("class", "layer")
  .style("fill", function(d, i) {
    return z(i);
  });

layer.selectAll("rect")
  .data(function(d) {
    return d.data;
  })
  .enter().append("rect")
  .attr("x", function(d) {
    return x(parseDate(d[0]));
  })
  .attr("y", function(d) {
    return y(d.y + d.y0);
  })
  .attr("height", function(d) {
    return y(d.y0) - y(d.y + d.y0);
  })
  .attr("width", x.rangeBand() - 1);

svg.append("g")
  .attr("class", "axis axis--x")
  .attr("transform", "translate(0," + height + ")")
  .call(xAxis);

svg.append("g")
  .attr("class", "axis y")
  .call(yAxis);
body {
  font: 10px sans-serif;
}
.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

答案 1 :(得分:0)

问题出在本节:

d

data是一个对象,但d3需要一个数组,因此它不存储您传递的数据。

我认为这就是你想要的(尽管输出图表看起来很偏,所以我不是100%肯定......)。无论哪种方式,只需记住您要在layer.selectAll("rect") .data(function(d) { return d.data; }) 函数中返回an array

RewriteEngine on
RewriteRule ^/(.+)/?$ ./article/article.php?id=$1 [L]