如何在d3 js javascript中制作维恩图的数据结构
How to make datastructure of venn diagram in d3 js javascript
我有这个数组data
var array = [
{"package": "20Dresses - Shop Women Fashion", "total": 4857},
{"total": 24, "competingapps": "com.stylecracker.android"},
{"total": 578, "competingapps": "com.shopping.limeroad"},
{"total": 291, "competingapps": "com.craftsvilla.app"},
{"total": 430, "competingapps": "com.voonik.android"}
];
从这个数组数据中,我必须为维恩图制作以下数据结构。
var vennSets = [
{"sets": [0], "label": "20Dresses - Shop Women Fashion", "size": 4857},
{"sets": [1], "label": "com.stylecracker.android", "size": 24},
{"sets": [2], "label": "com.shopping.limeroad", "size": 578},
{"sets": [3], "label": "com.craftsvilla.app", "size": 291},
{"sets": [4], "label": "com.voonik.android", "size": 430},
{"sets": [0, 1], "size": 24},
{"sets": [0, 2], "size": 578},
{"sets": [0, 3], "size": 291},
{"sets": [0, 4], "size": 430},
]
我不明白我如何才能实现上述结构。如有任何帮助,我将不胜感激。
谢谢
它只需要一些基本的转换
// considering array is your initial array;
var singleItems = [];
var intersections = [];
var vennSets;
array.forEach(function(item, index) {
singleItems.push({
sets: [index],
label: item.package || item.competingapps,
size: item.total
});
if (item.competingapps) {
intersections.push({
sets: [0, index],
size: item.total
});
}
});
vennSets = singleItems.concat(intersections);
你可能会坚持使用更实用的转换方式,以避免临时变量(singleItems
和intersections
)造成外部作用域污染;
var result = array.reduce(function(prev, item, index) {
prev.push({
sets: [index],
label: item.package || item.competingapps,
size: item.total
});
if (item.competingapps) {
prev.push({
sets: [0, index],
size: item.total
});
}
return prev;
}, []).sort(function(a, b) { return a.sets.length < b.sets.length; });
相关文章:
- 添加新数据时D3.JS条形图列偏移量
- JS库支持各种数据结构?(如爪哇的番石榴)
- JavaScript数据结构
- 通过内部数组将JSON数据导入D3.js+Google Map
- Node JS,传统的数据结构?(如Set等),任何类似Java.util的node
- 更正扁平数据模型和noSQL数据结构
- 用于筛选无模式集合的最快数据结构
- 将数据结构转换为二进制数据
- JavaScript 设置具有对数搜索时间的数据结构
- 更好的数据结构来处理这个数组
- Firebase 数据结构理念
- javascript和python返回的相同数据结构在d3.js中表现不同
- 以数组为数据结构的d3.js树布局
- D3.nest() 没有给我我需要的数据结构
- 哪个数据结构发送到d3.js脚本
- 如何在d3 js javascript中制作维恩图的数据结构
- 多折线图D3.js Json数据结构
- D3.JS:为我的标签数据结构实现强制导向图
- D3饼图的数据结构
- 如何在具有不规则数据结构的React (vanilla)中使用D3's时间尺度