D3多系列线图

时间:2014-10-12 02:16:34

标签: d3.js scatter-plot linegraph

多年来,我试图在几个国家的几类数据上创建线+散点图。

问题是折线图从一个数据类别继续到下一个数据类别。 我查看了http://bl.ocks.org/mbostock/3884955中的一个例子,但我不太明白我需要实现哪些更改。

我应该使用元素分隔数据吗?或者,有更简单的方法吗?

var line = d3.svg.line()

        .x(function(d)
            {return xScale(d.Year);
            })
        .y(function(d){
            return yScale(d.Value);
            })

//为scatterplot添加圆圈

var circle = svg.selectAll('circle')
        .data(data)
        .enter()
        .append("circle")

//画线。

svg.append('path')
    .datum(data)
    .attr("d",line)
    .style("stroke","blue")
    .style("stroke-width","1px")
    .style("fill","none")

// CSV数据文件的一部分。几个国家的数据重复。

Variable,Unit,Country,Year,Value,Flags
"Hepatitis B","% of children immunised","Australia","2001",94,

"Hepatitis B","% of children immunised","Australia","2002",94,

"Hepatitis B","% of children immunised","Australia","2003",95,

"Hepatitis B","% of children immunised","Australia","2004",95,

"Hepatitis B","% of children immunised","Australia","2005",95,

"Diphtheria, Tetanus, Pertussis","% of children immunised","Australia","1980",33, 

"Diphtheria, Tetanus, Pertussis","% of children immunised","Australia","1981",40,

"Diphtheria, Tetanus, Pertussis","% of children immunised","Australia","1982",48,

"Diphtheria, Tetanus, Pertussis","% of children immunised","Australia","1983",55,

"Diphtheria, Tetanus, Pertussis","% of children immunised","Australia","1984",62,

"Measles","% of children immunised","Australia","1983",68,

"Measles","% of children immunised","Australia","1984",68,

"Measles","% of children immunised","Australia","1985",68,

1 个答案:

答案 0 :(得分:0)

由于您粘贴的代码很少,我猜测问题是您传递给图表的数据结构是否正确。

http://bl.ocks.org/mbostock/3884955的图表中,按城市分类的图表,传递给图表的数据是分类城市数组:

var city = svg.selectAll(".city")
      .data(cities)
    .enter().append("g")
      .attr("class", "city");

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

城市数组的数据结构如下:

纽约 - >阵列[...]

San Fran - >阵列[...]

奥斯汀 - >阵列[...]

详见下图:

enter image description here

希望它有所帮助。