【问题标题】:D3js stack chart X-axis customizationD3js栈图X轴自定义
【发布时间】:2014-11-22 01:08:20
【问题描述】:

我遵循了这个例子d3js example。 然后按照中给出的方式自定义此图表

我的 JSON 数据是:

{"test": [
            {"date":"1/5/2014","allocated":"14.14","unallocated":"7.14"},
            {"date":"1/6/2014","allocated":"10.38","unallocated":"1.14"},
            {"date":"1/7/2014","allocated":"1.43","unallocated":"3.14"},
            {"date":"1/8/2014","allocated":"12","unallocated":"6.14"},
            {"date":"1/9/2014","allocated":"13.34","unallocated":"4.44"},
            {"date":"1/10/2014","allocated":"6.34","unallocated":"1.14"},
            {"date":"1/11/2014","allocated":"8.34","unallocated":"2.14"},
            {"date":"1/12/2014","allocated":"6.88","unallocated":"4.14"},
            {"date":"1/13/2014","allocated":"23.34","unallocated":"2.14"},
            {"date":"1/14/2014","allocated":"3.34","unallocated":"0.14"}
]}

而Index.html代码为:

    <!DOCTYPE html>
<html>
  <head>
    <title>Bar Graph</title>
    <script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?1.29.1"></script>
    <script type="text/javascript" src="http://mbostock.github.com/d3/d3.layout.js?1.29.1"></script>
    <script type="text/javascript" src="http://mbostock.github.com/d3/d3.time.js?1.29.1"></script>
    <script type="text/javascript" src="http://mbostock.github.com/d3/d3.csv.js?1.29.1"></script>

svg {
  width: 550px;
  height: 500px;
  border: solid 1px #ccc;
  font: 10px sans-serif;
  shape-rendering: crispEdges;
}

    </style>
  </head>
  <body>
    <script type="text/javascript">

var w = 550,
    h = 500,
    p = [20, 30, 30, 20],
    x = d3.scale.ordinal().rangeRoundBands([0, w - p[1] - p[3]]),
    y = d3.scale.linear().range([0, h - p[0] - p[2]]),
    z = d3.scale.ordinal().range(["lightpink", "lightblue"]),

    parse = d3.time.format("%m/%d/%Y").parse,
    format = d3.time.format("%a");

var svg = d3.select("body").append("svg:svg")
    .attr("width", w)
    .attr("height", h)
  .append("svg:g")
    .attr("transform", "translate(" + p[3] + "," + (h - p[2]) + ")");
var t

d3.json("test.json", function(test) {
  // Transpose the data into layers by cause.
  var causes = d3.layout.stack()(["allocated", "unallocated"].map(function(cause) {

    return test.test.map(function(d) {
      // var temp = _.each(test, function(record){
        date = parse(d.date);
        array = [date]
        // console.log(array)

        var week = d3.time.format("%U");

        var nest = d3.nest()
            .key(function(d) { return week(new Date(array)); })
            .entries(array);
        // console.log(nest)
        disp = [nest]
        console.log(nest)


      return {x: array[0], y: +d[cause]};
    });
  }));

  // Compute the x-domain (by date) and y-domain (by top).
  x.domain(causes[0].map(function(d) { return d.x; }));
  y.domain([0, d3.max(causes[causes.length - 1], function(d) { return d.y0 + d.y; })]);

  // Add a group for each cause.
  var cause = svg.selectAll("g.cause")
      .data(causes)
    .enter().append("svg:g")
      .attr("class", "cause")
      .style("fill", function(d, i) { return z(i); })
      .style("stroke", function(d, i) { return d3.rgb(z(i)).darker(); });

  // Add a rect for each date.
  var rect = cause.selectAll("rect")
      .data(Object)
    .enter().append("svg:rect")
      .attr("x", function(d) { return x(d.x); })
      .attr("y", function(d) { return -y(d.y0) - y(d.y); })
      .attr("height", function(d) { return y(d.y); })
      .attr("width", x.rangeBand());

  // Add a label per date.
  var label = svg.selectAll("text")
      .data(x.domain())
    .enter().append("svg:text")
      .attr("x", function(d) { return x(d) + x.rangeBand() / 2; })
      .attr("y", 6)
      .attr("text-anchor", "middle")
      .attr("dy", ".71em")
      .text(format);

  // Add y-axis rules.
  var rule = svg.selectAll("g.rule")
      .data(y.ticks(5))
    .enter().append("svg:g")
      .attr("class", "rule")
      .attr("transform", function(d) { return "translate(0," + -y(d) + ")"; });

  rule.append("svg:line")
      .attr("x2", w - p[1] - p[3])
      .style("stroke", function(d) { return d ? "#fff" : "#000"; })
      .style("stroke-opacity", function(d) { return d ? .7 : null; });

  rule.append("svg:text")
      .attr("x", w - p[1] - p[3] + 6)
      .attr("dy", ".35em")
      .text(d3.format(",d"));
});

    </script>
    <tr>
      <!-- <td>Previous</td> -->
      <td>Next</td>
    </tr>
  </body>
</html>

现在在 X 轴上,此代码显示给定 JSON 数据中的所有行。 我的需要是我想一次只显示一周的数据。意味着我在 JSON 中有许多行,并且我设法将几天嵌套到一周中。

现在请帮我用给定的代码自定义 X 轴。我试图在 X 轴上调用嵌套数组的值,但它没有给出正确的输出。 提前谢谢

【问题讨论】:

  • 您希望 X 轴看起来像什么?
  • 它应该只显示 Sun Mon Tue Wed Thu Fri Sat(第 1 周)数据的列。然后在下一步单击它应该显示(第 2 周)数据。参考上图
  • 嗯,好的。我认为您必须(1)制作一个显示“下一步”的按钮,然后(2)向该按钮添加一个 .on("mouseclick" ...) 事件。该事件可能会调用某种包含进入-更新-退出转换的redraw() 函数。我自己还在学习,并且有很多教程。这个对我帮助很大:blog.visual.ly/creating-animations-and-transitions-with-d3-js

标签: json d3.js graph


【解决方案1】:

为此,您需要对 JSON 数据执行 groupBy。

你可以试试这个简单的方法来实现:

var groupedByWeek = _.groupBy(data, function(item) {
        var dateMoment = moment(item.day,"DD/MM/YYYY");
        return dateMoment.week();

它将天转换为一周!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    • 2014-03-02
    • 2021-05-18
    • 2012-11-14
    • 1970-01-01
    • 2013-07-19
    • 2019-04-02
    相关资源
    最近更新 更多