D3多线图问题

时间:2016-02-11 16:03:29

标签: javascript json d3.js

您好我正在尝试构建一个多行图表,显示每个月的费率。我在控制台中收到“TypeError:t is undefined”错误,虽然我可以看到填充了x和y轴,但图表中没有出现任何线条。任何帮助表示赞赏!

HTML文件代码在这里:

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

 body {
   font: 10px sans-serif;
 }

 .axis path,
 .axis line {
   fill: none;
   stroke: #000;
   shape-rendering: crispEdges;
 }

 .x.axis path {
   display: none;
 }

 .line {
   fill: none;
   stroke: steelblue;
   stroke-width: 1.5px;
 }

 </style>
 <body>
 <script src="//d3js.org/d3.v3.min.js"></script>
 <script>

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


 var x = d3.scale.ordinal().rangeRoundBands([0, width], .1);

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

 var color = d3.scale.ordinal().range(["#666666", "#262626", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);

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

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

 var line = d3.svg.line().interpolate("basis").x(function(d) { return x(d.date); }).y(function(d) { return y(d.value); });

 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.json("yoy_values.json", function(error, data) {
   if (error) throw error;

   var myValues = d3.keys(data[0]).filter(function(key) { return key !== "date"; });

     data.forEach(function(d) {
        d.rates = myValues.map(function(name) { return {name: name, value: +d[name]}; });
        console.log(d.rates);

     });

   x.domain(data.map(function(d) { return d.date; }));


 y.domain([
     d3.min(data,   function(d) { return d3.min(d.rates, function(d) { return d.value; }); }),
     d3.max(data,   function(d) { return d3.max(d.rates, function(d) { return d.value; }); })
     ]);

   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("Rates");

   var state = svg.selectAll(".state")
       .data(myValues.slice().reverse())
     .enter().append("g")
       .attr("class", "state")
       .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });

   state.append("path")
       .attr("class", "line")
       .attr("d", function(d) { return line(d.rates); })
       .style("stroke", function(d) { return color(d.name); });

   state.append("text")
       .datum(function(d) { return {name: d.name, value: d.rates[d.rates.length - 1]}; })
       .attr("transform", function(d) { return "translate(" + x(d.value) + "," + y(d.name) + ")"; })
       .attr("x", 3)
       .attr("dy", ".35em")
       .text(function(d) { return d.name; });
 });

 </script>

我正在阅读的JSON文件如下所示:

[
    {"date":"Jan","2014":"0.0812","2015":"0.0780","2016":"0.0838"},
    {"date":"Feb","2014":"0.0806","2015":"0.0768","2016":"0.0893"},
    {"date":"Mar","2014":"0.0858","2015":"0.0847","2016":null},
    {"date":"Apr","2014":"0.0848","2015":"0.0889","2016":null},
    {"date":"May","2014":"0.0890","2015":"0.0890","2016":null},
    {"date":"Jun","2014":"0.0928","2015":"0.0865","2016":null},
    {"date":"Jul","2014":"0.0857","2015":"0.0799","2016":null},
    {"date":"Aug","2014":"0.0905","2015":"0.0845","2016":null},
    {"date":"Sep","2014":"0.1003","2015":"0.0934","2016":null},
    {"date":"Oct","2014":"0.0971","2015":"0.0993","2016":null},
    {"date":"Nov","2014":"0.0912","2015":"0.0973","2016":null},
    {"date":"Dec","2014":"0.0800","2015":"0.0777","2016":null}
]

1 个答案:

答案 0 :(得分:0)

很难从代码眼球中分辨出来,但我注意到了:

  1. 您从非“日期”json数据中创建一系列.rates属性,因此第一个数据的.rates将为[{name:"2014", value:"0.0812"},{name:"2015", value:"0.0780"},{name:"2016", value:"0.0838"}]

    d.rates = myValues.map(function(name) { return {name: name, value: +d[name]}; });

  2. 然后将这些传递给d3.svg.line函数

    .attr("d", function(d) { return line(d.rates); })

  3. 但是此行函数之前已设置为看起来期望.date和.value属性不是.name和.value

    var line = d3.svg.line().interpolate("basis").x(function(d) { return x(d.date); }).y(function(d) { return y(d.value); });

  4. 并且缩放x已设置为期望.date(月份字符串)作为参数,因为它们被设置为域

    x.domain(data.map(function(d) { return d.date; }));

  5. 简而言之,x scale和line函数似乎期望按月分类的数据对象每年绘制一行,但是你要传入.rates对象,这些对象按年份组织,期望绘制一行每个月?