gpt4 book ai didi

javascript - 附加时访问父数据

转载 作者:行者123 更新时间:2023-11-29 18:49:18 25 4
gpt4 key购买 nike

我基本上有一些像这样的嵌套数据:

const shapeGroups = [
{
title: 'shapeGroup_01',
color: 'blue',
shapes: [
{
shape:'rect',
width: 30,
height: 100,
x: 250,
y: 450
}
]
},
{
title: 'shapeGroup_01',
color: 'blue',
shapes: [
{
shape:'rect',
width: 10,
height: 40,
x: 350,
y:50
}
]
}
]

我感兴趣的是在每个 rect 中设置颜色,因为它是在 shapeGroup 的 color 属性中定义的。

const shapeGroups = [{
title: 'shapeGroup_01',
color: 'blue',
shapes: [{
shape: 'rect',
width: 30,
height: 100,
x: 250,
y: 450
}]
},
{
title: 'shapeGroup_01',
color: 'blue',
shapes: [{
shape: 'rect',
width: 10,
height: 40,
x: 350,
y: 50
}]
}
]

const stage = d3.select('#stageContainer')
.append('svg')
.attr('id', '#stage')
.attr('width', 1000)
.attr('height', 1000)


const groups = stage
.selectAll('.group')
.data(shapeGroups)

const groupEnter = groups
.enter()
.append('g')
.attr('class', 'group');

const getGroup = group => group.shapes;
const createShape = shape => document.createElementNS(d3.namespaces.svg, shape.shape)

groupEnter
.selectAll('.shape')
.data(getGroup)
.enter()
.append(createShape)
.attr('fill', 'red') // I want the color as defined in the current group
.attr('width', d => d.width)
.attr('height', d => d.height)
.attr('x', d => d.x)
.attr('y', d => d.y)
<script src="https://d3js.org/d3.v5.min.js"></script>
<div id="stageContainer"></div>

换句话说,我需要一种方法来在组级别设置 color,或者在我附加单个 时以某种方式访问​​父 datum 对象.shapes.

最佳答案

正如您在我的 answer here 中看到的那样,您无法从 D3 v4/v5 中的选择内部访问父级的数据或其索引。

这里的解决方案是获取 parentNode 的数据:

.attr('fill', (_, i, n) => d3.select(n[i].parentNode).datum().color)

这是您进行更改后的代码(我正在使两个矩形具有不同的颜色和更小的 SVG,以便更好地可视化它):

const shapeGroups = [{
title: 'shapeGroup_01',
color: 'blue',
shapes: [{
shape: 'rect',
width: 30,
height: 100,
x: 250,
y: 50
}]
},
{
title: 'shapeGroup_01',
color: 'green',
shapes: [{
shape: 'rect',
width: 10,
height: 40,
x: 350,
y: 20
}]
}
];

const stage = d3.select('body')
.append('svg')
.attr('width', 400)
.attr('height', 400)


const groups = stage
.selectAll('.group')
.data(shapeGroups)

const groupEnter = groups
.enter()
.append('g')
.attr('class', 'group');

const getGroup = group => group.shapes;
const createShape = shape => document.createElementNS(d3.namespaces.svg, shape.shape)

groupEnter
.selectAll('.shape')
.data(getGroup)
.enter()
.append(createShape)
.attr('fill', (_, i, n) => d3.select(n[i].parentNode).datum().color)
.attr('width', d => d.width)
.attr('height', d => d.height)
.attr('x', d => d.x)
.attr('y', d => d.y)
<script src="https://d3js.org/d3.v5.min.js"></script>

然而,到目前为止,最简单的解决方案就是将 fill 设置为父选择本身:

const groupEnter = groups
.enter()
.append('g')
.attr('class', 'group')
.attr('fill', d => d.color);

这是演示:

const shapeGroups = [{
title: 'shapeGroup_01',
color: 'blue',
shapes: [{
shape: 'rect',
width: 30,
height: 100,
x: 250,
y: 50
}]
},
{
title: 'shapeGroup_01',
color: 'green',
shapes: [{
shape: 'rect',
width: 10,
height: 40,
x: 350,
y: 20
}]
}
];

const stage = d3.select('body')
.append('svg')
.attr('width', 400)
.attr('height', 400)


const groups = stage
.selectAll('.group')
.data(shapeGroups)

const groupEnter = groups
.enter()
.append('g')
.attr('class', 'group')
.attr('fill', d=>d.color);

const getGroup = group => group.shapes;
const createShape = shape => document.createElementNS(d3.namespaces.svg, shape.shape)

groupEnter
.selectAll('.shape')
.data(getGroup)
.enter()
.append(createShape)
.attr('width', d => d.width)
.attr('height', d => d.height)
.attr('x', d => d.x)
.attr('y', d => d.y)
<script src="https://d3js.org/d3.v5.min.js"></script>

关于javascript - 附加时访问父数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51874259/

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