D3中不同数据系列的绘制线图

时间:2014-10-14 09:16:26

标签: d3.js graph line

我在d3中绘制了三个不同系列的折线图。 然而,这条线从一个系列延续到下一个系列,我无法弄清楚出了什么问题。 在我的代码中,我从源数据重新映射以创建一个对绘图更友好的新数组。并且,我为每个数据系列创建,并添加到每个数据系列。

有人能告诉我我的代码在哪里错了吗?

谢谢!

//sets the domain vairable to mapColor to Variable.
mapColor.domain(d3.keys(data[0]).filter(function(key) { return key == "Variable"
    }));


xScale = d3.scale.linear().domain([min,max])
        .range([100,1000])

var ymin = d3.min(data, function(d){
    return d.Value;
})

var ymax = d3.max(data, function(d){
    return d.Value;
})

yScale = d3.scale.linear().domain([0,ymax])
    .range([1300,100])


var line2 = d3.svg.line()
        .x(function(d)
            {return xScale(d.date);
            })
        .y(function(d){
            return yScale(d.value);
            })

var cities = mapColor.domain().map(function(name) {

console.log(mapColor.domain())

var a = {name: name, values: data.map(function(d) {
    console.log(d)
    console.log(d[name])
    return { date: d.Year, value: d.Value };
    })

}

return a;

  });


//create group element for each disease
var disease=  svg.selectAll(".disease")
        .data(cities)
    .enter()
    .append('g')
    .attr('class','disease')


disease.append('path')  
    .attr("d",function(d){
            return line2(d.values)   //d.values is array of yearly values for each    disease.
    })
    .style("stroke","blue")
    .style("stroke-width","1px")
    .style("fill","none")


}) // end of d3.csv("immune.csv")

数据文件为CSV格式 变量,单位,地区,年份,价值,旗 "乙型肝炎","%的儿童接种疫苗","澳大利亚",#34; 2001",94,

"乙型肝炎","%的儿童接种疫苗","澳大利亚"," 2002",94,

"乙型肝炎","%的儿童接种疫苗","澳大利亚",#34; 2003",95,

"乙型肝炎","%的儿童接种疫苗","澳大利亚",#34; 2004",95,

"乙型肝炎","%的儿童接种疫苗","澳大利亚"," 2005",95,

"乙型肝炎","%的儿童接种疫苗","澳大利亚",#34; 2006",95,

"乙型肝炎","%的儿童接种疫苗","澳大利亚"," 2007",94,

"乙型肝炎","%的儿童接种疫苗","澳大利亚"," 2008",94,

"乙型肝炎","%的儿童接种疫苗","澳大利亚",#34; 2009",92,

"乙型肝炎","%的儿童接种疫苗","澳大利亚",#34; 2010",92,

"乙型肝炎","%的儿童接种疫苗","澳大利亚",#34; 2011",92,

"乙型肝炎","%的儿童接种疫苗","澳大利亚"," 2012",92,

"白喉,破伤风,百日咳","%的儿童接种疫苗","澳大利亚"," 1980",33,

"白喉,破伤风,百日咳","%的儿童接种疫苗","澳大利亚",#34; 1981",40,

"白喉,破伤风,百日咳","%的儿童接种疫苗","澳大利亚",#34; 1982",48,

"白喉,破伤风,百日咳","%的儿童接种疫苗","澳大利亚"," 1983",55,

"白喉,破伤风,百日咳","%的儿童接种疫苗","澳大利亚"," 1984",62,

"白喉,破伤风,百日咳","%的儿童接种疫苗","澳大利亚",#34; 1985",68,

"白喉,破伤风,百日咳","%的儿童接种疫苗","澳大利亚"," 1986",75,

"白喉,破伤风,百日咳","%的儿童接种疫苗","澳大利亚"," 1987",81,

"白喉,破伤风,百日咳","%的儿童接种疫苗","澳大利亚"," 1988",88,

"白喉,破伤风,百日咳","%的儿童接种疫苗","澳大利亚"," 1989",93,

0 个答案:

没有答案