[英]d3 adding new key value to existing json data
I have a question that would like to seek your expertise on. 我有一个问题想寻求您的专业知识。
This is my JSON array that I have in: 这是我的JSON数组:
[{
key : "Group 0",
values : [{
x : 0.12817352913159197,
y : 4.360475517778741,
size : 0.8945912778023366
}, {
x : -1.8709283296311146,
y : 1.4047025584603734,
size : 0.3621509721039796
}]
}, {
key : "Group 1",
values : [{
x : 0.6574795166435595,
y : -0.6037258352005531,
size : 0.0942101986534386
}, {
x : -0.4951370796031603,
y : -0.18739226817326712,
size : 0.8843348380774309
}]
}]
i am passing this to scatterChart so the data inside contain this, now i want to add shape to each values, finaly i want it like this to be in chart data: 我将其传递到scatterChart,以便其中的数据包含此数据,现在我想为每个值添加形状,最后我希望它像这样在图表数据中显示:
[{
key : "Group 0",
values : [{
x : 0.12817352913159197,
y : 4.360475517778741,
size : 0.8945912778023366,
shape : "circle"
}, {
x : -1.8709283296311146,
y : 1.4047025584603734,
size : 0.3621509721039796,
shape : "diamond"
}]
}, {
key : "Group 1",
values : [{
x : 0.6574795166435595,
y : -0.6037258352005531,
size : 0.0942101986534386,
shape : "circle"
}, {
x : -0.4951370796031603,
y : -0.18739226817326712,
size : 0.8843348380774309,
shape : "diamond"
}]
}]
how to acheive this using d3 or nvd3? 如何使用d3或nvd3实现呢?
If I understood your question correctly 如果我正确理解了您的问题
var demo = [{
key: "Group 0",
values: [{
x: 0.12817352913159197,
y: 4.360475517778741,
size: 0.8945912778023366,
}, {
x: -1.8709283296311146,
y: 1.4047025584603734,
size: 0.3621509721039796,
}]
}, {
key: "Group 1",
values: [{
x: 0.6574795166435595,
y: -0.6037258352005531,
size: 0.0942101986534386,
}, {
x: -0.4951370796031603,
y: -0.18739226817326712,
size: 0.8843348380774309,
}]
}];
for (var i = 0, j = demo.length; i < j; i++) {
group = demo[i]['values'];
for (var x = 0, y = group.length; x < y; x++) {
if (x == 0) {
// Assuming the first is always circle
group[x]['shape'] = "circle";
} else {
// Assuming the second is always diamond
group[x]['shape'] = "diamond";
}
};
};
console.log(demo);
There is a working version here . 有一个工作版本在这里 。
Hope it helps 希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.