1

我从这个简单的例子开始,但我有以下错误(使用 Firebug)

TypeError: string is undefined [Break On This Error]
var c, p, i = 0, n = template.length, m = string.length;

有什么提示吗??(我尝试了类似的响应,但没有成功,我猜时间和日期格式有问题,但我相信“%Y-%m-%d”格式是我在 csv 中的正确格式文件...)

谢谢!!

//这里是我的 prueba.html 文件

<!DOCTYPE html>
<meta charset="utf-8">
<style>

body{font: 12px arial;}

path{stroke: steelblue;
    stroke-width: 2;
    fill: none;}

.axis path,
.axis line {fill: none;
            stroke: grey;
            stroke-width: 1;
            shape-rendering: crispEdges;}

</style>
<body>
<script type="text/javascript" src="d3/d3.v3.js"></script>

<script>

var margin = {top: 30, right: 20, bottom: 30, left: 50},
    width = 600 - margin.left - margin.right,
    height = 270 - margin.top - margin.bottom;

var parseDate = d3.time.format("%Y-%m-%d").parse; // HERE ERROR !!!

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").ticks(5);

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

var valueline = d3.svg.line()
    .x(function(d) { return x(d.timestamp); })
    .y(function(d) { return y(d.temperature); });

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

// Get the data
d3.tsv("data/data.csv", function(error, data) 
{data.forEach(function(d) 
    {d.timestamp = parseDate(d.timestamp); // HERE ERROR !!!
     d.temperature = +d.temperature;});

    // Scale the range of the data
    x.domain(d3.extent(data, function(d) { return d.date; }));
    y.domain([0, d3.max(data, function(d) { return d.close; })]);

    svg.append("path")      // Add the valueline path.
        .attr("d", valueline(data));

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

    svg.append("g")         // Add the Y Axis
        .attr("class", "y axis")
        .call(yAxis);

});

</script>
</body>

//这里是我的data.csv文件

timestamp,temperature
1900-01-01,20
1900-01-02,20
1900-01-03,17
1900-01-04,23
1900-01-05,15
1900-01-06,22
1900-01-07,24
1900-01-08,15
1900-01-09,25
1900-01-10,19
1900-01-11,23
1900-01-12,19
1900-01-13,17
1900-01-14,15
1900-01-15,17
1900-01-16,21
1900-01-17,23
1900-01-18,25
1900-01-19,17
1900-01-20,22
1900-01-21,23
1900-01-22,17
1900-01-23,15
1900-01-24,23
1900-01-25,19
1900-01-26,25
1900-01-27,21
1900-01-28,22
1900-01-29,20
1900-01-30,15
1900-01-31,21
1900-02-01,19
1900-02-02,15
1900-02-03,15
1900-02-04,25
1900-02-05,23
1900-02-06,25
1900-02-07,15
1900-02-08,18
1900-02-09,22
1900-02-10,15
1900-02-11,19
1900-02-12,18
1900-02-13,24
1900-02-14,22
1900-02-15,16
1900-02-16,21
1900-02-17,24
1900-02-18,25

一张使用 Chrome 开发者工具的图片... http://i.imgur.com/0vZhCgK.jpg?1

4

2 回答 2

0

你正在使用d3.tsv,你应该使用d3.csv

于 2013-07-09T11:56:37.873 回答
0

此外,如果您想使用 tsv(制表符分隔值),您需要用表格替换每个逗号。

PS:注意不要使用自动用空格替换表格的编辑器保存文件:)

于 2013-07-29T15:44:15.487 回答