javascript - d3.js 扁平化嵌套数据
问题描述
一般来说,我对 d3 和 js 很陌生。这是我的第一个项目。
我有一个具有以下结构的数据集(在使用 d3.nest 和聚合函数之后):
[
{
key: 1,
values: [
{
key: 'Tucson International',
values: {
avg_arr_delay : -2,
avg_dep_delay : -2,
coords : [489.4325699362896, 401.0612266917171]
}
},
{
key: 'Burbank-Glendale-Pasadena',
values: {
avg_arr_delay : -4.25,
avg_dep_delay : -3,
coords : [385.86516356833704, 366.2906768310514]
}
},
{
key: 'Another Airport',
values: {
avg_arr_delay : 5,
avg_dep_delay : 12,
coords : [325.86516356833704, 386.2906768310514]
}
}
]
},
{
key: 2,
values: [
{
key: 'Tucson International',
values: {
avg_arr_delay : 3,
avg_dep_delay : -1,
coords : [489.4325699362896, 401.0612266917171]
}
},
{
key: 'General Mitchell International',
values: {
avg_arr_delay : 8.666,
avg_dep_delay : 1.3333,
coords : [811.193714171889, 209.73464395018792]
}
},
{
key: '...',
values: {
avg_arr_delay : 1,
avg_dep_delay : 1,
coords : [111.111, 222.222]
}
}
]
},
]
我想使用数据在地图中绘制圆圈cx=coords[0]
,cy=coords[1]
并且r=avg_dep_delay
我尝试使用以下代码访问它,但失败了。我只画了一个圆圈。
svg.append('g')
.attr('class','bubble')
.selectAll('circle')
.data(nested)
.enter()
.append('circle')
.attr('cx', function(d,i) {
return d.values[i].values.origin_coords[0];
})
.attr('cy', function(d,i) {
return d.values[i].values.origin_coords[1];
})
.attr('r',5)
.attr('class','dest_circle');
我想将数据更改为下面的形状以使其工作。
[
{
Month: 1,
Airport: 'Tucson International',
avg_arr_delay : -2,
avg_dep_delay : -2,
coords : [489.4325699362896, 401.0612266917171]
},
{
Month: 1,
Airport: 'Burbank-Glendale-Pasadena',
avg_arr_delay : -4.25,
avg_dep_delay : -3,
coords : [385.86516356833704, 366.2906768310514]
},
{
Month: 1,
Airport: 'Another airport',
avg_arr_delay : 5,
avg_dep_delay : 12,
coords : [325.86516356833704, 386.2906768310514]
},
{
Month: 2,
Airport: 'Tucson International',
avg_arr_delay : 3,
avg_dep_delay : -1,
coords : [489.4325699362896, 401.0612266917171]
},
{
Month: 2,
Airport: 'General Mitchell International',
avg_arr_delay : 8.666,
avg_dep_delay : 1.333,
coords : [811.193714171889, 209.73464395018792]
},
{
Month: 1,
Airport: '...',
avg_arr_delay : 1,
avg_dep_delay : 1,
coords : [111.111, 222.222]
}
]
我发现以下帖子似乎是一个类似的问题,但无法让它工作StackOverflow Question。
解决方案
推荐阅读
- opengl-es - 什么是 OpenGL 和 WebGL2 中的顶点数组?
- c# - 插入电子邮件时出错
我正在尝试将表格插入电子邮件。我在 6 行代码中出现以下错误(以“body”开头,结尾 onavgErrorOutStDev +=“/table”)。错误是:
“赋值的左侧必须是变量、属性或索引器”。
<table>
开始是在创建/发送电子邮件之前执行的方法。如何修复错误?MailMessage
- bash - Makefile:特定于目标的变量总是被解析
- notepad++ - Notepad ++ 删除每 150 个字符后的回车和广告新行
- python - numba 不并行化范围
- vba - “行句柄无效”错误、Excel、VBA、SharePoint 列表自动化
- python - 单元测试:断言异常处理函数调用
- android - 如何将 AnimationDrawable 保存为 GIF?
- facebook - FB Messenger 机器人链接不会将用户重定向到 Messenger
- azure - Azure EventGrid 验证 URL