我正在尝试使用来自WHO的CSV数据构建堆积面积图,其格式如下:
date,governate,cases,deaths,fatalRate,attackRate,english
05/22/17,Abyan,1068,10,0.9,1.75,Abyan
05/22/17,Aden,489,12,2.5,0.51,Aden
05/22/17,Al Bayda,1498,6,0.4,1.95,Al Bayda
05/22/17,Al Dhale'e,1401,8,0.6,1.86,Al Dhale'e
05/22/17,Al-Hudaydah,1397,32,2.3,0.42,Al Hudaydah
05/22/17,Al_Jawf,189,3,1.6,0.29,Al Jawf
05/22/17,Al Mahwit,2486,34,1.4,3.27,Al Mahwit
05/22/17,Amanat Al Asimah,9216,33,0.4,2.79,Amanat Al Asimah
05/22/17,Amran,3743,45,1.2,2.45,Amran
05/22/17,Dhamar,1617,33,2,0.76,Dhamar
05/22/17,Hajjah,4664,42,0.9,2.1,Hajjah
05/22/17,Ibb,1378,37,2.7,0.45,Ibb
...
我设法嵌套了数据,并建立了一个折线图,显示随时间推移每个省的死亡人数。但是,我想显示一段时间内的累积死亡人数,这需要堆积面积图,例如此处的示例:https://bl.ocks.org/greencracker/e08d5e789737e91d6e73d7dcc34969bf
我之前已经创建了堆叠的面积图,但是通常数据的顶部都有相关的键,例如在我发布链接的示例中,或者像这样的数据:
day,titanic,avatar,akira,frozen,deliverance,avengers
1,20,8,3,0,0,0
2,18,5,1,13,0,0
3,14,3,1,10,0,0
4,7,3,0,5,27,15
5,4,3,0,2,20,14
6,3,1,0,0,10,13
7,2,0,0,0,8,12
格式化区域堆栈图的数据仅需要我运行
d3.stack().keys(["titanic","avatar","akira","frozen","deliverance","avengers"])
但是,使用WHO的这种特定数据格式,我希望对数据进行排序的相关“关键字”实际上全部在“主管”列中。好像上面的数据是这样格式化的:
day,movie,value
1,titanic,20
1,avatar,8
1,akira,3
1,frozen,0
1,deliverance,0
1,avengers,0
2,titanic,18
2,avatar,5
2,akira,1
2,frozen,13
...etc
如何格式化数据,以便可以按时间跨x轴按管理部门进行堆叠?
谢谢你的帮助!
最佳答案
最简单,最懒惰的解决方案是,在d3.csv
加载后将您拥有的数据结构简单地转换为所需的数据结构,然后将其解析为适合传递给堆栈生成器的CSV。
在此解决方案中,我首先使用行函数摆脱了不必要的列...
function(d) {
return {
date: d.date,
governate: d.governate,
deaths: +d.deaths
}
}
然后,我使用此代码段(其中
rawData
是d3.csv
解析的数据)来获取唯一的日期以及这些日期的所有值:var data = [...new Set(rawData.map(function(d) {
return d.date
}))].map(function(d) {
var obj = {};
rawData.forEach(function(e) {
if (e.date === d) {
obj[e.governate] = e.deaths
}
obj.date = d;
})
return obj;
})
这是演示:
var csv = `date,governate,cases,deaths,fatalRate,attackRate,english
05/22/17,Abyan,1068,10,0.9,1.75,Abyan
05/22/17,Aden,489,12,2.5,0.51,Aden
05/22/17,Al Bayda,1498,6,0.4,1.95,Al Bayda
05/22/17,Al Dhale'e,1401,8,0.6,1.86,Al Dhale'e
05/22/17,Al-Hudaydah,1397,32,2.3,0.42,Al Hudaydah
05/22/17,Al_Jawf,189,3,1.6,0.29,Al Jawf
05/22/17,Al Mahwit,2486,34,1.4,3.27,Al Mahwit
05/22/17,Amanat Al Asimah,9216,33,0.4,2.79,Amanat Al Asimah
05/22/17,Amran,3743,45,1.2,2.45,Amran
05/22/17,Dhamar,1617,33,2,0.76,Dhamar
05/22/17,Hajjah,4664,42,0.9,2.1,Hajjah
05/22/17,Ibb,1378,37,2.7,0.45,Ibb
05/23/17,Abyan,1068,9,9.9,1.75,Abyan
05/23/17,Aden,489,92,9.5,0.51,Aden
05/23/17,Al Bayda,1498,9,0.4,1.95,Al Bayda
05/23/17,Al Dhale'e,1401,9,0.6,1.86,Al Dhale'e
05/23/17,Al-Hudaydah,1397,92,2.3,0.42,Al Hudaydah
05/23/17,Al_Jawf,189,9,1.6,0.29,Al Jawf
05/23/17,Al Mahwit,2486,94,1.4,3.27,Al Mahwit
05/23/17,Amanat Al Asimah,9216,93,0.4,2.79,Amanat Al Asimah
05/23/17,Amran,3743,95,1.2,2.45,Amran
05/23/17,Dhamar,1617,93,2,0.76,Dhamar
05/23/17,Hajjah,4664,92,0.9,2.1,Hajjah
05/23/17,Ibb,1378,97,2.7,0.45,Ibb`;
var rawData = d3.csvParse(csv, function(d) {
return {
date: d.date,
governate: d.governate,
deaths: +d.deaths
}
});
var data = [...new Set(rawData.map(function(d) {
return d.date
}))].map(function(d) {
var obj = {};
rawData.forEach(function(e) {
if (e.date === d) {
obj[e.governate] = e.deaths
}
obj.date = d;
})
return obj;
})
console.log(data)
<script src="https://d3js.org/d3.v4.min.js"></script>
现在,通过这种
data
数组结构,您可以使用堆栈生成器(d3.stack()
)。