gpt4 book ai didi

javascript - 如何将数据添加到其数据位于父对象的父对象中的 selectAll 占位符?

转载 作者:行者123 更新时间:2023-11-29 22:20:29 27 4
gpt4 key购买 nike

最初我有一个来自 csv 的平面哈希结构,它具有以下字段:

zoneId,op,metricName,value

然后我把它嵌套在

d3.nest()
.key(function(d){return d.zoneId})
.key(function(d){return d.op})
.entries(data)

现在它的层次结构看起来像

zoneId -> op -> <details>

数据示例

nestedData = {
[{
"key": "zone1",
"values": [{
"key": "Get",
"values": [{
"zoneId":"zone1"
"op":"Get"
"metricName":"CompletionTime",
"value":"10ms"
}, {
"zoneId":"zone1"
"op":"Get"
"metricName":"Throughput",
"value":"100 query/s"
}]
},{
/* Similar to the "values" of last bracket */
}]
}]
}, {
"key": "zone2",
"values": [
/* Similar to the "values" of last bracket */
]
}]
}]
}

现在我想从这个嵌套数据结构构建一个表。

  • 每个区占一张表
  • 每个操作都是一行
  • 在每一行
    • 左栏是操作名称
    • 右列是指标的格式化版本(例如:“10 ms @ 100 QPS”)

问题是:

我应该如何将数据绑定(bind)到 占位符?因为当我将它们附加到

var tables = d3.select('#perfs .metrics')
.selectAll('table')
.data(nestedData)
.enter().append('table');
/* added tbody and data */
tables.append('tbody')
.selectAll('tr')
.data(???).enter()
.append('tr')
.selectAll('td')
.data(function(d){return [d.key,d.value];}) // left and right column
.enter().append('td')
.text(function(d){ /* iterate through the metrics and format them */ })

这里有两个我能想到的解决方案:

  • 将数据分配给 tbody(但听起来很老套!)
  • 访问 this.parentNode.__data__(听起来也很老套!)

你能给点建议吗?

最佳答案

如果你看<a href="https://github.com/mbostock/d3/wiki/Selections#wiki-append" rel="noreferrer noopener nofollow">selection.append()</a>API ,内容如下:

Each new element inherits the data of the current elements

换句话说,<tbody>默认情况下将具有绑定(bind)到 <table> 的相同数据.因此,您的代码将是:

var metrics = d3.select('#perfs .metrics');
var tables = metrics.selectAll('table').data(nestedData);
tables.enter().append('table');

var tbody = tables.append('tbody');
var rows = tbody.selectAll("tr").data(function(d) { return d.values; });
rows.enter().append("tr");
var cells = rows.selectAll("td").data(function(d) { return d.values; });
cells.enter().append("td")
.text(function(d){ /* iterate through the metrics and format them */ });

关于javascript - 如何将数据添加到其数据位于父对象的父对象中的 selectAll 占位符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12699265/

有数据但 没有,而 在 下。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com