Javascript D3如何计算此图表上显示的日期值?

Javascript D3如何计算此图表上显示的日期值?,javascript,d3.js,Javascript,D3.js,我正在研究本例中的代码: 但这是如何导致几年内的日期显示为“2009年4月”等的呢?这是D3中的自动设置吗?它与刻度线间距有关吗?我不确定x轴设置在哪里 您引用的行与D3输出日期的方式无关:它是将CSV中的日期(例如2008年2月11日)转换为可用日期的函数 x轴标度定义为一个byvar x=d3.time.scale,格式由中的默认值给出 返回适合于显示刻度值的时间格式函数。 您不必使用刻度的内置刻度格式,但它 根据输入自动计算相应的显示 日期 考虑以下时间格式: %Y-表示年份边界,如“

我正在研究本例中的代码:


但这是如何导致几年内的日期显示为“2009年4月”等的呢?这是D3中的自动设置吗?它与刻度线间距有关吗?我不确定x轴设置在哪里

您引用的行与D3输出日期的方式无关:它是将CSV中的日期(例如2008年2月11日)转换为可用日期的函数

x轴标度定义为一个by
var x=d3.time.scale
,格式由中的默认值给出

返回适合于显示刻度值的时间格式函数。 您不必使用刻度的内置刻度格式,但它 根据输入自动计算相应的显示 日期

考虑以下时间格式:

  • %Y-表示年份边界,如“2011”

  • %B-用于月份边界,如“二月”

  • %b%d-一周 边界,如“2006年2月”

  • %a%d-表示日边界,如“Mon 07”

  • %I%p-小时界限,如“01 AM”

  • %I:%M-分钟 边界,例如“01:23”

  • :%S-用于第二个边界,如“:45”

  • .%L—所有其他时间的毫秒数,例如“.012”

基本上,D3的魔力在发挥作用

var formatPercent = d3.format("+.0%"),
    formatChange = function(x) { return formatPercent(x - 1); },
    parseDate = d3.time.format("%d-%b-%y").parse;

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

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

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

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left")
    .tickSize(-width, 0)
    .tickFormat(formatChange);

var line = d3.svg.line()
    .x(function(d) { return x(d.date); })
    .y(function(d) { return y(d.ratio); });

var area = d3.svg.area()
    .x(function(d) { return x(d.date); })
    .y(function(d) { return y(d.ratio); });

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 gX = svg.append("g")
    .attr("class", "axis axis--x")
    .attr("transform", "translate(0," + height + ")");

var gY = svg.append("g")
    .attr("class", "axis axis--y");

gY.append("text")
    .attr("class", "axis-title")
    .attr("transform", "rotate(-90)")
    .attr("y", 6)
    .attr("dy", ".71em")
    .text("Change in Price");

d3.tsv("data.tsv", function(error, data) {
  if (error) throw error;

  // Compute price relative to base value (hypothetical purchase price).
  var baseValue = +data[0].close;
  data.forEach(function(d) {
    d.date = parseDate(d.date);
    d.ratio = d.close / baseValue;
  });

  x.domain(d3.extent(data, function(d) { return d.date; }));
  y.domain(d3.extent(data, function(d) { return d.ratio; }));

  area.y0(y(1));

  // Use a second linear scale for ticks.
  yAxis.tickValues(d3.scale.linear()
      .domain(y.domain())
      .ticks(20));

  gX.call(xAxis);

  gY.call(yAxis)
    .selectAll(".tick")
      .classed("tick--one", function(d) { return Math.abs(d - 1) < 1e-6; });

  var defs = svg.append("defs");

  defs.append("clipPath")
      .attr("id", "clip-above")
    .append("rect")
      .attr("width", width)
      .attr("height", y(1));

  defs.append("clipPath")
      .attr("id", "clip-below")
    .append("rect")
      .attr("y", y(1))
      .attr("width", width)
      .attr("height", height - y(1));

  svg.append("path")
      .datum(data)
      .attr("clip-path", "url(#clip-above)")
      .attr("class", "area area--above")
      .attr("d", area);

  svg.append("path")
      .datum(data)
      .attr("clip-path", "url(#clip-below)")
      .attr("class", "area area--below")
      .attr("d", area);

  svg.append("path")
      .datum(data)
      .attr("class", "line")
      .attr("d", line);
});
parseDate = d3.time.format("%d-%b-%y").parse;