本文介绍了parseDate = d3.time.format("%Y") 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的图表完全按预期工作,只是我的 parseDate 函数不想在 x 轴上给我正确的日期.我相信这很简单.

My chart is working completely as expected, except that my parseDate function doesn't want to give me correct dates on the x-axis. I'm sure this is something simple.

目前我正在添加 d3.v2.min.js,没有任何额外的帮助库 - 我需要其他东西来让 d3.time.format() 工作吗?

Currently I'm adding d3.v2.min.js, without any additional helper libraries - do I need something else to get d3.time.format() working?

不解析日期我的数据返回一个 x 轴:

.960   .965   .970   .975

而不是

1960   1965   1970   1975

JSON 的结构如下:

[{"year":1959,"average":315.97},
{"year":1960,"average":316.91},
{"year":1961,"average":317.64},
...etc
{"year":2011,"average":391.57}]

带有//注释部分的代码是问题所在:

<script>
var margin = {top: 20, right: 20, bottom: 30, left: 50},
    width = 920 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

// 下面的 parseDate 不起作用

// parseDate below not working

var parseDate = d3.time.format("%Y").parse;

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

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

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

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

// data taking format
d3.json("{{ asset('js/data_co2.json') }}", function(data) {

// parseDate() 在下面不起作用

// parseDate() not working below

给出错误:未捕获的类型错误:对象 1959 没有方法子字符串"

Gives error: Uncaught TypeError: Object 1959 has no method 'substring'

  data.forEach(function(d) {
    d.year = parseDate(d.year);
    d.average = +d.average;
  });

  x.domain(d3.extent(data, function(d) { return d.year; }));
  y.domain([260, 420]);

var area = d3.svg.area()
    .x(function(d) { return x(d.year); })
    .y0(height)
    .y1(function(d) { return y(d.average); });

var svg = d3.select("#co2").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 + ")");

svg.append("path")
  .datum(data)
    .attr("class", "area")
    .attr("d", area);

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

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("CO2 Levels (ppm)");

});

</script>

推荐答案

parseDate,d3准备"的函数,期望传递一个字符串进行解析.您的年份是(适当地)数字,因此您需要将它们转换为字符串:

parseDate, the function "prepared" by d3, expects to be passed a string for parsing. Your years are (appropriately) numbers, so you would need to convert them to strings:

parseDate(String(d.year));

parseDate(d.year.toString());

但是,请记住,同样的结果——Date 对象——可以在本机实现,如下所示:

However, keep in mind that the same result – a Date object – could be achieved natively, like this:

var date = new Date(d.year, 0);//需要的0,为一月

这篇关于parseDate = d3.time.format("%Y") 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 05:58