D3网格线和刻度值问题

时间:2016-01-21 12:08:53

标签: javascript css d3.js

我有一个使用D3制作的堆积区域图表。我的实施面临两个问题。

1)我只想在y轴上显示替代的刻度值。例如,在我的实现中,y轴值为0, 20, 40, 60, 80, 100,依此类推。但我想显示像0, 40, 80, 120这样的值,但也保持相应的网格线。我只是想在不删除y轴网格线的情况下删除替代刻度值。

2)我想格式化y轴,因此它应始终具有上y轴网格线。所以在我的实现中,我希望网格线的值高于180



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

var x = d3.time.scale()
  .range([0, width]);

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

var z = d3.scale.category20c();

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


var yAxis = d3.svg.axis()
  .scale(y).innerTickSize(-width).outerTickSize(0)
  .orient("left");

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



var area = d3.svg.area()
  .interpolate("cardinal")
  .x(function(d) {
    return x(new Date(d[0]));;
  })
  .y0(function(d) {
    return y(d.y0);
  })
  .y1(function(d) {
    return y(d.y0 + d.y);
  });

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 b = [{
  data: [
    ["2016-01-20T05:31:17.000Z", 95.9, {}],
    ["2016-01-20T05:31:47.000Z", 95.9, {}],
    ["2016-01-20T05:32:17.000Z", 95.4, {}],
    ["2016-01-20T05:32:47.000Z", 96.1, {}],
    ["2016-01-20T05:33:17.000Z", 95.7, {}],
    ["2016-01-20T05:33:47.000Z", 95.9, {}],
    ["2016-01-20T05:34:17.000Z", 95.5, {}],
    ["2016-01-20T05:34:47.000Z", 95.9, {}],
    ["2016-01-20T05:35:17.000Z", 95.8, {}],
    ["2016-01-20T05:35:47.000Z", 95.9, {}],
    ["2016-01-20T05:36:17.000Z", 95.7, {}],
    ["2016-01-20T05:36:47.000Z", 95.7, {}],
    ["2016-01-20T05:37:17.000Z", 95.9, {}],
    ["2016-01-20T05:37:47.000Z", 95.5, {}],
    ["2016-01-20T05:38:17.000Z", 95.4, {}],
    ["2016-01-20T05:38:47.000Z", 95.8, {}],
    ["2016-01-20T05:39:17.000Z", 96.0, {}],
    ["2016-01-20T05:39:47.000Z", 96.1, {}],
    ["2016-01-20T05:40:17.000Z", 95.8, {}],
    ["2016-01-20T05:40:47.000Z", 96.0, {}],
    ["2016-01-20T05:41:17.000Z", 95.9, {}],
    ["2016-01-20T05:41:47.000Z", 94.9, {}],
    ["2016-01-20T05:42:17.000Z", 95.8, {}],
    ["2016-01-20T05:42:47.000Z", 95.9, {}],
    ["2016-01-20T05:43:17.000Z", 95.8, {}],
    ["2016-01-20T05:43:47.000Z", 96.0, {}],
    ["2016-01-20T05:44:17.000Z", 95.7, {}],
    ["2016-01-20T05:44:47.000Z", 96.0, {}],
    ["2016-01-20T05:45:17.000Z", 95.9, {}],
    ["2016-01-20T05:45:47.000Z", 96.0, {}],
    ["2016-01-20T05:46:17.000Z", 95.8, {}],
    ["2016-01-20T05:46:47.000Z", 96.0, {}],
    ["2016-01-20T05:47:17.000Z", 95.7, {}],
    ["2016-01-20T05:47:47.000Z", 96.2, {}],
    ["2016-01-20T05:48:17.000Z", 95.8, {}],
    ["2016-01-20T05:48:47.000Z", 95.9, {}],
    ["2016-01-20T05:49:17.000Z", 95.7, {}],
    ["2016-01-20T05:49:47.000Z", 95.9, {}],
    ["2016-01-20T05:50:18.000Z", 95.7, {}],
    ["2016-01-20T05:50:48.000Z", 95.8, {}],
    ["2016-01-20T05:51:18.000Z", 95.7, {}],
    ["2016-01-20T05:51:48.000Z", 95.9, {}],
    ["2016-01-20T05:52:18.000Z", 95.5, {}],
    ["2016-01-20T05:52:48.000Z", 95.9, {}],
    ["2016-01-20T05:53:18.000Z", 95.8, {}],
    ["2016-01-20T05:53:48.000Z", 95.9, {}],
    ["2016-01-20T05:54:18.000Z", 95.7, {}],
    ["2016-01-20T05:54:48.000Z", 95.9, {}],
    ["2016-01-20T05:55:18.000Z", 95.8, {}],
    ["2016-01-20T05:55:48.000Z", 95.8, {}],
    ["2016-01-20T05:56:18.000Z", 95.6, {}],
    ["2016-01-20T05:56:48.000Z", 95.7, {}],
    ["2016-01-20T05:57:18.000Z", 95.7, {}],
    ["2016-01-20T05:57:48.000Z", 95.8, {}],
    ["2016-01-20T05:58:18.000Z", 95.7, {}],
    ["2016-01-20T05:58:48.000Z", 95.7, {}],
    ["2016-01-20T05:59:18.000Z", 95.6, {}],
    ["2016-01-20T05:59:48.000Z", 95.8, {}],
    ["2016-01-20T06:00:18.000Z", 95.7, {}],
    ["2016-01-20T06:00:48.000Z", 95.7, {}],
    ["2016-01-20T06:01:18.000Z", 95.6, {}],
    ["2016-01-20T06:01:48.000Z", 95.7, {}],
    ["2016-01-20T06:02:18.000Z", 95.8, {}],
    ["2016-01-20T06:02:48.000Z", 95.8, {}],
    ["2016-01-20T06:03:18.000Z", 95.8, {}],
    ["2016-01-20T06:03:48.000Z", 95.8, {}],
    ["2016-01-20T06:04:18.000Z", 95.8, {}],
    ["2016-01-20T06:04:48.000Z", 95.8, {}],
    ["2016-01-20T06:05:18.000Z", 95.7, {}],
    ["2016-01-20T06:05:48.000Z", 95.7, {}]
  ],
  label: "a"
}, {
  data: [
    ["2016-01-20T05:31:17.000Z", 95.9, {}],
    ["2016-01-20T05:31:47.000Z", 95.9, {}],
    ["2016-01-20T05:32:17.000Z", 95.4, {}],
    ["2016-01-20T05:32:47.000Z", 96.1, {}],
    ["2016-01-20T05:33:17.000Z", 95.7, {}],
    ["2016-01-20T05:33:47.000Z", 95.9, {}],
    ["2016-01-20T05:34:17.000Z", 95.5, {}],
    ["2016-01-20T05:34:47.000Z", 95.9, {}],
    ["2016-01-20T05:35:17.000Z", 95.8, {}],
    ["2016-01-20T05:35:47.000Z", 95.9, {}],
    ["2016-01-20T05:36:17.000Z", 95.7, {}],
    ["2016-01-20T05:36:47.000Z", 95.7, {}],
    ["2016-01-20T05:37:17.000Z", 95.9, {}],
    ["2016-01-20T05:37:47.000Z", 95.5, {}],
    ["2016-01-20T05:38:17.000Z", 95.4, {}],
    ["2016-01-20T05:38:47.000Z", 95.8, {}],
    ["2016-01-20T05:39:17.000Z", 96.0, {}],
    ["2016-01-20T05:39:47.000Z", 96.1, {}],
    ["2016-01-20T05:40:17.000Z", 95.8, {}],
    ["2016-01-20T05:40:47.000Z", 96.0, {}],
    ["2016-01-20T05:41:17.000Z", 95.9, {}],
    ["2016-01-20T05:41:47.000Z", 94.9, {}],
    ["2016-01-20T05:42:17.000Z", 95.8, {}],
    ["2016-01-20T05:42:47.000Z", 95.9, {}],
    ["2016-01-20T05:43:17.000Z", 95.8, {}],
    ["2016-01-20T05:43:47.000Z", 96.0, {}],
    ["2016-01-20T05:44:17.000Z", 95.7, {}],
    ["2016-01-20T05:44:47.000Z", 96.0, {}],
    ["2016-01-20T05:45:17.000Z", 95.9, {}],
    ["2016-01-20T05:45:47.000Z", 96.0, {}],
    ["2016-01-20T05:46:17.000Z", 95.8, {}],
    ["2016-01-20T05:46:47.000Z", 96.0, {}],
    ["2016-01-20T05:47:17.000Z", 95.7, {}],
    ["2016-01-20T05:47:47.000Z", 96.2, {}],
    ["2016-01-20T05:48:17.000Z", 95.8, {}],
    ["2016-01-20T05:48:47.000Z", 95.9, {}],
    ["2016-01-20T05:49:17.000Z", 95.7, {}],
    ["2016-01-20T05:49:47.000Z", 95.9, {}],
    ["2016-01-20T05:50:18.000Z", 95.7, {}],
    ["2016-01-20T05:50:48.000Z", 95.8, {}],
    ["2016-01-20T05:51:18.000Z", 95.7, {}],
    ["2016-01-20T05:51:48.000Z", 95.9, {}],
    ["2016-01-20T05:52:18.000Z", 95.5, {}],
    ["2016-01-20T05:52:48.000Z", 95.9, {}],
    ["2016-01-20T05:53:18.000Z", 95.8, {}],
    ["2016-01-20T05:53:48.000Z", 95.9, {}],
    ["2016-01-20T05:54:18.000Z", 95.7, {}],
    ["2016-01-20T05:54:48.000Z", 95.9, {}],
    ["2016-01-20T05:55:18.000Z", 95.8, {}],
    ["2016-01-20T05:55:48.000Z", 95.8, {}],
    ["2016-01-20T05:56:18.000Z", 95.6, {}],
    ["2016-01-20T05:56:48.000Z", 95.7, {}],
    ["2016-01-20T05:57:18.000Z", 95.7, {}],
    ["2016-01-20T05:57:48.000Z", 95.8, {}],
    ["2016-01-20T05:58:18.000Z", 95.7, {}],
    ["2016-01-20T05:58:48.000Z", 95.7, {}],
    ["2016-01-20T05:59:18.000Z", 95.6, {}],
    ["2016-01-20T05:59:48.000Z", 95.8, {}],
    ["2016-01-20T06:00:18.000Z", 95.7, {}],
    ["2016-01-20T06:00:48.000Z", 95.7, {}],
    ["2016-01-20T06:01:18.000Z", 95.6, {}],
    ["2016-01-20T06:01:48.000Z", 95.7, {}],
    ["2016-01-20T06:02:18.000Z", 95.8, {}],
    ["2016-01-20T06:02:48.000Z", 95.8, {}],
    ["2016-01-20T06:03:18.000Z", 95.8, {}],
    ["2016-01-20T06:03:48.000Z", 95.8, {}],
    ["2016-01-20T06:04:18.000Z", 95.8, {}],
    ["2016-01-20T06:04:48.000Z", 95.8, {}],
    ["2016-01-20T06:05:18.000Z", 95.7, {}],
    ["2016-01-20T06:05:48.000Z", 95.7, {}]
  ],
  label: "b"
}]




var layers = stack(b);

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


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

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

svg.append("g")
  .attr("class", "y axis")
  .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;

注意:数据值是动态的,因此y轴刻度值不固定。请帮我找到适用于任何数据的解决方案。

2 个答案:

答案 0 :(得分:1)

通过设置yAxis

的参数,可以轻松解决您的要求
  1. 可以使用axis.tickValues()覆盖自定义刻度生成器提供的值来指定自定义刻度值。对于您的情况,这意味着致电yAxis.tickValues([0, 40, 80, 120])。要在仍然绘制网格线时隐藏某些刻度值,您可以使用axis.tickFormat() - 根据您的逻辑 - 需要为每个要省略的刻度值返回空字符串。下面的示例将显示每秒的刻度值,但您可以随意实现任何逻辑。

  2. 如果你想在图表上方有一个封闭的网格线,你可以使用yAxis.outerTickSize(-width),它会在比例范围的最小/最大值处添加网格线,就像内部网格线一样。为中间值添加。但请注意,作为副作用,这也会在y轴的下端添加一条网格线,在您的情况下不会受到伤害,因为它与您的x-相同轴。

  3. 生成yAxis的代码如下所示:

    var yTicks = [40,80,120,160];               // Use whatever values you like.
    var yAxis = d3.svg.axis()
      .scale(y)
      .innerTickSize(-width)
      .outerTickSize(-width)                    // Set to -width for upper/lower grid lines
      .tickValues(yTicks)                       // Set custom grid values.
      .tickFormat(function(d) {
        return yTicks.indexOf(d) % 2 ? d : "";  // Custom tickFormat to hide certain tickValues
      })
      .orient("left");
    

    如果您不希望上面的网格线与值“#39;路径,您可以通过扩展比例域来添加一点填充:

    y.domain([0, d3.max(d3.merge(ary), function(d) {
      return d.y0 + d.y;
    }) * 1.1]);   // Extending the extent's max value by .1 to allow for padding.
    

    &#13;
    &#13;
    var margin = {
        top: 20,
        right: 30,
        bottom: 30,
        left: 40
      },
      width = 960 - margin.left - margin.right,
      height = 500 - margin.top - margin.bottom;
    
    var x = d3.time.scale()
      .range([0, width]);
    
    var y = d3.scale.linear()
      .range([height, 0]);
    
    var z = d3.scale.category20c();
    
    var xAxis = d3.svg.axis()
      .scale(x)
      .orient("bottom")
    
    var yTicks = [40,80,120,160];               // Use whatever values you like.
    var yAxis = d3.svg.axis()
      .scale(y)
      .innerTickSize(-width)
      .outerTickSize(-width)                    // Set to -width for upper/lower grid lines
      .tickValues(yTicks)                       // Set custom grid values.
      .tickFormat(function(d) {
        return yTicks.indexOf(d) % 2 ? d : "";  // Use a custom tickFormat to hide certain tickValues
      })
      .orient("left");
    
    var stack = d3.layout.stack()
      .offset("zero")
      .values(function(d) {
        return d.data;
      })
      .x(function(d) {
        return new Date(d[0]);
      })
      .y(function(d) {
        return d[1];
      });
    
    
    
    var area = d3.svg.area()
      .interpolate("cardinal")
      .x(function(d) {
        return x(new Date(d[0]));;
      })
      .y0(function(d) {
        return y(d.y0);
      })
      .y1(function(d) {
        return y(d.y0 + d.y);
      });
    
    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 b = [{
      data: [
        ["2016-01-20T05:31:17.000Z", 95.9, {}],
        ["2016-01-20T05:31:47.000Z", 95.9, {}],
        ["2016-01-20T05:32:17.000Z", 95.4, {}],
        ["2016-01-20T05:32:47.000Z", 96.1, {}],
        ["2016-01-20T05:33:17.000Z", 95.7, {}],
        ["2016-01-20T05:33:47.000Z", 95.9, {}],
        ["2016-01-20T05:34:17.000Z", 95.5, {}],
        ["2016-01-20T05:34:47.000Z", 95.9, {}],
        ["2016-01-20T05:35:17.000Z", 95.8, {}],
        ["2016-01-20T05:35:47.000Z", 95.9, {}],
        ["2016-01-20T05:36:17.000Z", 95.7, {}],
        ["2016-01-20T05:36:47.000Z", 95.7, {}],
        ["2016-01-20T05:37:17.000Z", 95.9, {}],
        ["2016-01-20T05:37:47.000Z", 95.5, {}],
        ["2016-01-20T05:38:17.000Z", 95.4, {}],
        ["2016-01-20T05:38:47.000Z", 95.8, {}],
        ["2016-01-20T05:39:17.000Z", 96.0, {}],
        ["2016-01-20T05:39:47.000Z", 96.1, {}],
        ["2016-01-20T05:40:17.000Z", 95.8, {}],
        ["2016-01-20T05:40:47.000Z", 96.0, {}],
        ["2016-01-20T05:41:17.000Z", 95.9, {}],
        ["2016-01-20T05:41:47.000Z", 94.9, {}],
        ["2016-01-20T05:42:17.000Z", 95.8, {}],
        ["2016-01-20T05:42:47.000Z", 95.9, {}],
        ["2016-01-20T05:43:17.000Z", 95.8, {}],
        ["2016-01-20T05:43:47.000Z", 96.0, {}],
        ["2016-01-20T05:44:17.000Z", 95.7, {}],
        ["2016-01-20T05:44:47.000Z", 96.0, {}],
        ["2016-01-20T05:45:17.000Z", 95.9, {}],
        ["2016-01-20T05:45:47.000Z", 96.0, {}],
        ["2016-01-20T05:46:17.000Z", 95.8, {}],
        ["2016-01-20T05:46:47.000Z", 96.0, {}],
        ["2016-01-20T05:47:17.000Z", 95.7, {}],
        ["2016-01-20T05:47:47.000Z", 96.2, {}],
        ["2016-01-20T05:48:17.000Z", 95.8, {}],
        ["2016-01-20T05:48:47.000Z", 95.9, {}],
        ["2016-01-20T05:49:17.000Z", 95.7, {}],
        ["2016-01-20T05:49:47.000Z", 95.9, {}],
        ["2016-01-20T05:50:18.000Z", 95.7, {}],
        ["2016-01-20T05:50:48.000Z", 95.8, {}],
        ["2016-01-20T05:51:18.000Z", 95.7, {}],
        ["2016-01-20T05:51:48.000Z", 95.9, {}],
        ["2016-01-20T05:52:18.000Z", 95.5, {}],
        ["2016-01-20T05:52:48.000Z", 95.9, {}],
        ["2016-01-20T05:53:18.000Z", 95.8, {}],
        ["2016-01-20T05:53:48.000Z", 95.9, {}],
        ["2016-01-20T05:54:18.000Z", 95.7, {}],
        ["2016-01-20T05:54:48.000Z", 95.9, {}],
        ["2016-01-20T05:55:18.000Z", 95.8, {}],
        ["2016-01-20T05:55:48.000Z", 95.8, {}],
        ["2016-01-20T05:56:18.000Z", 95.6, {}],
        ["2016-01-20T05:56:48.000Z", 95.7, {}],
        ["2016-01-20T05:57:18.000Z", 95.7, {}],
        ["2016-01-20T05:57:48.000Z", 95.8, {}],
        ["2016-01-20T05:58:18.000Z", 95.7, {}],
        ["2016-01-20T05:58:48.000Z", 95.7, {}],
        ["2016-01-20T05:59:18.000Z", 95.6, {}],
        ["2016-01-20T05:59:48.000Z", 95.8, {}],
        ["2016-01-20T06:00:18.000Z", 95.7, {}],
        ["2016-01-20T06:00:48.000Z", 95.7, {}],
        ["2016-01-20T06:01:18.000Z", 95.6, {}],
        ["2016-01-20T06:01:48.000Z", 95.7, {}],
        ["2016-01-20T06:02:18.000Z", 95.8, {}],
        ["2016-01-20T06:02:48.000Z", 95.8, {}],
        ["2016-01-20T06:03:18.000Z", 95.8, {}],
        ["2016-01-20T06:03:48.000Z", 95.8, {}],
        ["2016-01-20T06:04:18.000Z", 95.8, {}],
        ["2016-01-20T06:04:48.000Z", 95.8, {}],
        ["2016-01-20T06:05:18.000Z", 95.7, {}],
        ["2016-01-20T06:05:48.000Z", 95.7, {}]
      ],
      label: "a"
    }, {
      data: [
        ["2016-01-20T05:31:17.000Z", 95.9, {}],
        ["2016-01-20T05:31:47.000Z", 95.9, {}],
        ["2016-01-20T05:32:17.000Z", 95.4, {}],
        ["2016-01-20T05:32:47.000Z", 96.1, {}],
        ["2016-01-20T05:33:17.000Z", 95.7, {}],
        ["2016-01-20T05:33:47.000Z", 95.9, {}],
        ["2016-01-20T05:34:17.000Z", 95.5, {}],
        ["2016-01-20T05:34:47.000Z", 95.9, {}],
        ["2016-01-20T05:35:17.000Z", 95.8, {}],
        ["2016-01-20T05:35:47.000Z", 95.9, {}],
        ["2016-01-20T05:36:17.000Z", 95.7, {}],
        ["2016-01-20T05:36:47.000Z", 95.7, {}],
        ["2016-01-20T05:37:17.000Z", 95.9, {}],
        ["2016-01-20T05:37:47.000Z", 95.5, {}],
        ["2016-01-20T05:38:17.000Z", 95.4, {}],
        ["2016-01-20T05:38:47.000Z", 95.8, {}],
        ["2016-01-20T05:39:17.000Z", 96.0, {}],
        ["2016-01-20T05:39:47.000Z", 96.1, {}],
        ["2016-01-20T05:40:17.000Z", 95.8, {}],
        ["2016-01-20T05:40:47.000Z", 96.0, {}],
        ["2016-01-20T05:41:17.000Z", 95.9, {}],
        ["2016-01-20T05:41:47.000Z", 94.9, {}],
        ["2016-01-20T05:42:17.000Z", 95.8, {}],
        ["2016-01-20T05:42:47.000Z", 95.9, {}],
        ["2016-01-20T05:43:17.000Z", 95.8, {}],
        ["2016-01-20T05:43:47.000Z", 96.0, {}],
        ["2016-01-20T05:44:17.000Z", 95.7, {}],
        ["2016-01-20T05:44:47.000Z", 96.0, {}],
        ["2016-01-20T05:45:17.000Z", 95.9, {}],
        ["2016-01-20T05:45:47.000Z", 96.0, {}],
        ["2016-01-20T05:46:17.000Z", 95.8, {}],
        ["2016-01-20T05:46:47.000Z", 96.0, {}],
        ["2016-01-20T05:47:17.000Z", 95.7, {}],
        ["2016-01-20T05:47:47.000Z", 96.2, {}],
        ["2016-01-20T05:48:17.000Z", 95.8, {}],
        ["2016-01-20T05:48:47.000Z", 95.9, {}],
        ["2016-01-20T05:49:17.000Z", 95.7, {}],
        ["2016-01-20T05:49:47.000Z", 95.9, {}],
        ["2016-01-20T05:50:18.000Z", 95.7, {}],
        ["2016-01-20T05:50:48.000Z", 95.8, {}],
        ["2016-01-20T05:51:18.000Z", 95.7, {}],
        ["2016-01-20T05:51:48.000Z", 95.9, {}],
        ["2016-01-20T05:52:18.000Z", 95.5, {}],
        ["2016-01-20T05:52:48.000Z", 95.9, {}],
        ["2016-01-20T05:53:18.000Z", 95.8, {}],
        ["2016-01-20T05:53:48.000Z", 95.9, {}],
        ["2016-01-20T05:54:18.000Z", 95.7, {}],
        ["2016-01-20T05:54:48.000Z", 95.9, {}],
        ["2016-01-20T05:55:18.000Z", 95.8, {}],
        ["2016-01-20T05:55:48.000Z", 95.8, {}],
        ["2016-01-20T05:56:18.000Z", 95.6, {}],
        ["2016-01-20T05:56:48.000Z", 95.7, {}],
        ["2016-01-20T05:57:18.000Z", 95.7, {}],
        ["2016-01-20T05:57:48.000Z", 95.8, {}],
        ["2016-01-20T05:58:18.000Z", 95.7, {}],
        ["2016-01-20T05:58:48.000Z", 95.7, {}],
        ["2016-01-20T05:59:18.000Z", 95.6, {}],
        ["2016-01-20T05:59:48.000Z", 95.8, {}],
        ["2016-01-20T06:00:18.000Z", 95.7, {}],
        ["2016-01-20T06:00:48.000Z", 95.7, {}],
        ["2016-01-20T06:01:18.000Z", 95.6, {}],
        ["2016-01-20T06:01:48.000Z", 95.7, {}],
        ["2016-01-20T06:02:18.000Z", 95.8, {}],
        ["2016-01-20T06:02:48.000Z", 95.8, {}],
        ["2016-01-20T06:03:18.000Z", 95.8, {}],
        ["2016-01-20T06:03:48.000Z", 95.8, {}],
        ["2016-01-20T06:04:18.000Z", 95.8, {}],
        ["2016-01-20T06:04:48.000Z", 95.8, {}],
        ["2016-01-20T06:05:18.000Z", 95.7, {}],
        ["2016-01-20T06:05:48.000Z", 95.7, {}]
      ],
      label: "b"
    }]
    
    
    
    
    var layers = stack(b);
    
    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;
    }) * 1.1]);   // Extending the extent's max value by .1 to allow for padding.
    
    svg.selectAll(".layer")
      .data(layers)
      .enter().append("path")
      .attr("class", "layer")
      .attr("d", function(d) {
        return area(d.data);
      })
      .style("fill", function(d, i) {
        return z(i);
      });
    
    svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);
    
    svg.append("g")
      .attr("class", "y axis")
      .call(yAxis);
    &#13;
    body {
      font: 10px sans-serif;
    }
    .axis path,
    .axis line {
      fill: none;
      stroke: #000;
      shape-rendering: crispEdges;
    }
    &#13;
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
    &#13;
    &#13;
    &#13;

答案 1 :(得分:1)

要制作刻度值,请执行以下操作:

var max = y.domain()[1];
var values = [];
for(var j =0; j <=max; j=j+20){
    values.push(j)
}
values.push(max)

yAxis.tickValues(values);

隐藏备用网格文本:

var last = svg.selectAll(".y .tick text")[0].length;  

svg.selectAll(".y .tick text")[0].forEach(function(d, i){
  if (i==last-1){
    return;//for showing last tick
  }
  if(i%2==0)
    d3.select(d).style("display", "none");//for showing alternate ticks
});

工作代码here