使用时间序列数据和scaleBand在D3条形图上指定刻度

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用时间序列数据和scaleBand在D3条形图上指定刻度相关的知识,希望对你有一定的参考价值。

我正在尝试使用时间序列数据为d3 v4条形图指定多个刻度和多个刻度标签,如下图所示:

enter image description here

基于this tutorial,我有一个使用时间序列数据的条形图,但我无法得到滴答以显示我想要的方式。例如,要显示每隔10个刻度以留出标签的空间。任何帮助是极大的赞赏!

@prmko感谢您的回复。我已经看到你在评论中引用的两个帖子,但是使用带有tickValues的解决方案完全删除了x轴上的刻度,这使我得出结论我的情况有些不同。这就是我在代码片段中注释掉tickValues行的原因。我喜欢错,但也许我没有正确地阅读解决方案。你能提供一个实现这种方法的工作实例吗?谢谢你的帮助。

var margin = {top: 20, right: 20, bottom: 70, left: 40},
    width = 600 - margin.left - margin.right,
    height = 200 - margin.top - margin.bottom;

// Parse the date / time
var	parseDate = d3.isoParse

var x = d3.scaleBand().rangeRound([0, width], .05).padding(0.1);

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

var xAxis = d3.axisBottom()
    .scale(x)
    .tickFormat(d3.timeFormat("%Y-%m-%d"))
    //.tickValues(x.domain().filter(function(d, i) {return !(i%4);}));
 
var yAxis = d3.axisLeft()
    .scale(y)
    .ticks(4);

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 + ")");

// d3.csv("bar-data.csv", function(error, data) {
var data = [
  {
    "date": "2016-03-02T00:00:00-05:00",
    "value": "53"
  },
  {
    "date": "2016-03-03T00:00:00-05:00",
    "value": "165"
  },
  {
    "date": "2016-03-04T00:00:00-05:00",
    "value": "269"
  },
  {
    "date": "2016-03-05T00:00:00-05:00",
    "value": "344"
  },
  {
    "date": "2016-03-06T00:00:00-05:00",
    "value": "376"
  },
  {
    "date": "2016-03-07T00:00:00-05:00",
    "value": "410"
  },
  {
    "date": "2016-03-08T00:00:00-05:00",
    "value": "421"
  },
  {
    "date": "2016-03-09T00:00:00-05:00",
    "value": "405"
  },
  {
    "date": "2016-03-10T00:00:00-05:00",
    "value": "376"
  },
  {
    "date": "2016-03-11T00:00:00-05:00",
    "value": "359"
  },
  {
    "date": "2016-03-12T00:00:00-05:00",
    "value": "392"
  },
  {
    "date": "2016-03-13T00:00:00-05:00",
    "value": "433"
  },
  {
    "date": "2016-03-14T00:00:00-05:00",
    "value": "455"
  },
  {
    "date": "2016-03-15T00:00:00-05:00",
    "value": "478"
  }
];
    data.forEach(function(d) {
        d.date = parseDate(d.date);
        d.value = +d.value;
    });

  x.domain(data.map(function(d) { return d.date; }));
  y.domain([0, d3.max(data, function(d) { return d.value; })]);

  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis)
    .selectAll("text")
      .style("text-anchor", "end")
      .attr("dx", "-.8em")
      .attr("dy", "-.55em")
      .attr("transform", "rotate(-90)" );

  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
    .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .text("Value");

  svg.selectAll("bar")
      .data(data)
    .enter().append("rect")
      .style("fill", "steelblue")
      .attr("x", function(d) { return x(d.date); })
      .attr("width", x.bandwidth())
      .attr("y", function(d) { return y(d.value); })
      .attr("height", function(d) { return height - y(d.value); });

// });
	.axis {
	  font: 10px sans-serif;
	}

	.axis path,
	.axis line {
	  fill: none;
	  stroke: #000;
	  shape-rendering: crispEdges;
	}
<div class="chart"></div>
<script src="https://d3js.org/d3.v4.min.js"  charset="utf-8"></script>
答案

我更新了你的代码段。在指定.tickValues(x.domain().filter(function(d,i){ return !(i%10)}));时,必须设置x scale域,在原始代码段中调用它时尚未设置。在设置域后,我将轴创建移动到了它的工作原理。

var margin = {top: 20, right: 20, bottom: 70, left: 40},
    width = 600 - margin.left - margin.right,
    height = 200 - margin.top - margin.bottom;

// Parse the date / time
var	parseDate = d3.isoParse

var x = d3.scaleBand().rangeRound([0, width], .05).padding(0.1);

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

var yAxis = d3.axisLeft()
    .scale(y)
    .ticks(4);

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 + ")");

// d3.csv("bar-data.csv", function(error, data) {
var data = [
  {
    "date": "2016-03-02T00:00:00-05:00",
    "value": "53"
  },
  {
    "date": "2016-03-03T00:00:00-05:00",
    "value": "165"
  },
  {
    "date": "2016-03-04T00:00:00-05:00",
    "value": "269"
  },
  {
    "date": "2016-03-05T00:00:00-05:00",
    "value": "344"
  },
  {
    "date": "2016-03-06T00:00:00-05:00",
    "value": "376"
  },
  {
    "date": "2016-03-07T00:00:00-05:00",
    "value": "410"
  },
  {
    "date": "2016-03-08T00:00:00-05:00",
    "value": "421"
  },
  {
    "date": "2016-03-09T00:00:00-05:00",
    "value": "405"
  },
  {
    "date": "2016-03-10T00:00:00-05:00",
    "value": "376"
  },
  {
    "date": "2016-03-11T00:00:00-05:00",
    "value": "359"
  },
  {
    "date": "2016-03-12T00:00:00-05:00",
    "value": "392"
  },
  {
    "date": "2016-03-13T00:00:00-05:00",
    "value": "433"
  },
  {
    "date": "2016-03-14T00:00:00-05:00",
    "value": "455"
  },
  {
    "date": "2016-03-15T00:00:00-05:00",
    "value": "478"
  }
];
    data.forEach(function(d) {
        d.date = parseDate(d.date);
        d.value = +d.value;
    });

  x.domain(data.map(function(d) { return d.date; }));
  y.domain([0, d3.max(data, function(d) { return d.value; })]);
  
  var xAxis = d3.axisBottom()
    .scale(x)
    .tickFormat(d3.timeFormat("%Y-%m-%d"))
    .tickValues(x.domain().filter(function(d,i){ return !(i%4)}));

  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis)
    .selectAll("text")
      .style("text-anchor", "end")
      .attr("dx", "-.8em")
      .attr("dy", "-.55em")
      .attr("transform", "rotate(-90)" );

  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
    .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .text("Value");

  svg.selectAll("bar")
      .data(data)
    .enter().append("rect")
      .style("fill", "steelblue")
      .attr("x", function(d) { return x(d.date); })
      .attr("width", x.bandwidth())
      .attr("y", function(d) { return y(d.value); })
      .attr("height", function(d) { return height - y(d.value); });

// });
	.axis {
	  font: 10px sans-serif;
	}

	.axis path,
	.axis line {
	  fill: none;
	  stroke: #000;
	  shape-rendering: crispEdges;
	}
<div class="chart"></div>
<script src="https://d3js.org/d3.v4.min.js"  charset="utf-8"></script>

以上是关于使用时间序列数据和scaleBand在D3条形图上指定刻度的主要内容,如果未能解决你的问题,请参考以下文章

d3.scaleBand 是如何工作的?

D3 - 设置类名的三元运算符

带有比例带标签的平滑滚动画笔

如何在条形图上更正 R 中 y 轴的比例和顺序

如何有条件地在条形图上的条形图上显示数据框的不同列值?

d3.js制作条形时间范围选择器