标题把 D3.js可使用的json数据转换成echarts可以使用的json数据
D3.js [ { “data”: { “start”: { “identity”: 84, “labels”: [ “person” ], “properties”: { “mysql_id”: 107964, “create_time”: “20211210”, “sex”: “1”, “name”: “董小姐”, “birth_address”: “贵州-贵阳市-南明区” } }, “end”: { “identity”: 83, “labels”: [ “person” ], “properties”: { “area”: “贵州-贵阳市-南明区”, “mysql_id”: 107941, “education”: “1”, “create_time”: “20211210”, “sex”: “1”, “name”: “孙月梅”, “birth_address”: “贵州-贵阳市-南明区” } }, “segments”: [ { “start”: { “identity”: 84, “labels”: [ “person” ], “properties”: { “mysql_id”: 107964, “create_time”: “20211210”, “sex”: “1”, “name”: “董小姐”, “birth_address”: “贵州-贵阳市-南明区” } }, “relationship”: { “identity”: 167, “start”: 84, “end”: 83, “type”: “invite”, “properties”: {} }, “end”: { “identity”: 83, “labels”: [ “person” ], “properties”: { “area”: “贵州-贵阳市-南明区”, “mysql_id”: 107941, “education”: “1”, “create_time”: “20211210”, “sex”: “1”, “name”: “孙月梅”, “birth_address”: “贵州-贵阳市-南明区” } } } ], “length”: 1.0 } }, { “data”: { “start”: { “identity”: 84, “labels”: [ “person” ], “properties”: { “mysql_id”: 107964, “create_time”: “20211210”, “sex”: “1”, “name”: “董小姐”, “birth_address”: “贵州-贵阳市-南明区” } }, “end”: { “identity”: 168, “labels”: [ “person” ], “properties”: { “area”: “贵州-贵阳市-南明区”, “mysql_id”: 107938, “education”: “1”, “create_time”: “20211209”, “sex”: “1”, “name”: “杨祺”, “birth_address”: “贵州-贵阳市-南明区” } }, “segments”: [ { “start”: { “identity”: 84, “labels”: [ “person” ], “properties”: { “mysql_id”: 107964, “create_time”: “20211210”, “sex”: “1”, “name”: “董小姐”, “birth_address”: “贵州-贵阳市-南明区” } }, “relationship”: { “identity”: 167, “start”: 84, “end”: 83, “type”: “invite”, “properties”: {} }, “end”: { “identity”: 83, “labels”: [ “person” ], “properties”: { “area”: “贵州-贵阳市-南明区”, “mysql_id”: 107941, “education”: “1”, “create_time”: “20211210”, “sex”: “1”, “name”: “孙月梅”, “birth_address”: “贵州-贵阳市-南明区” } } }, { “start”: { “identity”: 83, “labels”: [ “person” ], “properties”: { “area”: “贵州-贵阳市-南明区”, “mysql_id”: 107941, “education”: “1”, “create_time”: “20211210”, “sex”: “1”, “name”: “孙月梅”, “birth_address”: “贵州-贵阳市-南明区” } }, “relationship”: { “identity”: 83, “start”: 83, “end”: 168, “type”: “invite”, “properties”: {} }, “end”: { “identity”: 168, “labels”: [ “person” ], “properties”: { “area”: “贵州-贵阳市-南明区”, “mysql_id”: 107938, “education”: “1”, “create_time”: “20211209”, “sex”: “1”, “name”: “杨祺”, “birth_address”: “贵州-贵阳市-南明区” } } } ], “length”: 2.0 } } ]
echarts { “nodes”: [ { “id”: “84”, “mysql_id”: 107964, “create_time”: “20211210”, “sex”: “1”, “name”: “董小姐”, “area”: “贵州-贵阳市-南明区” }, { “id”: “83”, “mysql_id”: 107941, “create_time”: “20211210”, “sex”: “1”, “name”: “孙月梅”, “area”: “贵州-贵阳市-南明区” }, { “id”: “168”, “mysql_id”: 107938, “create_time”: “20211209”, “sex”: “1”, “name”: “杨祺”, “area”: “贵州-贵阳市-南明区” } ], “links”: [ { “source”: “84”, “target”: “83” }, { “source”: “84”, “target”: “168” } ], “categories”: [ { “name”: “人” } ] } 创建js文件将D3直接使用的json,转换成echarts可以使用的json; let data = [ { “data”: { “start”: { “identity”: 84, “labels”: [ “person” ], “properties”: { “mysql_id”: 107964, “create_time”: “20211210”, “sex”: “1”, “name”: “董小姐”, “birth_address”: “贵州-贵阳市-南明区” } }, “end”: { “identity”: 83, “labels”: [ “person” ], “properties”: { “area”: “贵州-贵阳市-南明区”, “mysql_id”: 107941, “education”: “1”, “create_time”: “20211210”, “sex”: “1”, “name”: “孙月梅”, “birth_address”: “贵州-贵阳市-南明区” } }, “segments”: [ { “start”: { “identity”: 84, “labels”: [ “person” ], “properties”: { “mysql_id”: 107964, “create_time”: “20211210”, “sex”: “1”, “name”: “董小姐”, “birth_address”: “贵州-贵阳市-南明区” } }, “relationship”: { “identity”: 167, “start”: 84, “end”: 83, “type”: “invite”, “properties”: {} }, “end”: { “identity”: 83, “labels”: [ “person” ], “properties”: { “area”: “贵州-贵阳市-南明区”, “mysql_id”: 107941, “education”: “1”, “create_time”: “20211210”, “sex”: “1”, “name”: “孙月梅”, “birth_address”: “贵州-贵阳市-南明区” } } } ], “length”: 1.0 } }, { “data”: { “start”: { “identity”: 84, “labels”: [ “person” ], “properties”: { “mysql_id”: 107964, “create_time”: “20211210”, “sex”: “1”, “name”: “董小姐”, “birth_address”: “贵州-贵阳市-南明区” } }, “end”: { “identity”: 168, “labels”: [ “person” ], “properties”: { “area”: “贵州-贵阳市-南明区”, “mysql_id”: 107938, “education”: “1”, “create_time”: “20211209”, “sex”: “1”, “name”: “杨祺”, “birth_address”: “贵州-贵阳市-南明区” } }, “segments”: [ { “start”: { “identity”: 84, “labels”: [ “person” ], “properties”: { “mysql_id”: 107964, “create_time”: “20211210”, “sex”: “1”, “name”: “董小姐”, “birth_address”: “贵州-贵阳市-南明区” } }, “relationship”: { “identity”: 167, “start”: 84, “end”: 83, “type”: “invite”, “properties”: {} }, “end”: { “identity”: 83, “labels”: [ “person” ], “properties”: { “area”: “贵州-贵阳市-南明区”, “mysql_id”: 107941, “education”: “1”, “create_time”: “20211210”, “sex”: “1”, “name”: “孙月梅”, “birth_address”: “贵州-贵阳市-南明区” } } }, { “start”: { “identity”: 83, “labels”: [ “person” ], “properties”: { “area”: “贵州-贵阳市-南明区”, “mysql_id”: 107941, “education”: “1”, “create_time”: “20211210”, “sex”: “1”, “name”: “孙月梅”, “birth_address”: “贵州-贵阳市-南明区” } }, “relationship”: { “identity”: 83, “start”: 83, “end”: 168, “type”: “invite”, “properties”: {} }, “end”: { “identity”: 168, “labels”: [ “person” ], “properties”: { “area”: “贵州-贵阳市-南明区”, “mysql_id”: 107938, “education”: “1”, “create_time”: “20211209”, “sex”: “1”, “name”: “杨祺”, “birth_address”: “贵州-贵阳市-南明区” } } } ], “length”: 2.0 } } ] let nodeList = { nodes:[], links:[], categories:[] } data.forEach((item)=>{
let n = { id:item.data.start.identity+“”, mysql_id: item.data.start.properties.mysql_id, create_time: item.data.start.properties.create_time, sex: item.data.start.properties.sex, name: item.data.start.properties.name, area: item.data.start.properties.birth_address } let m = { id:item.data.end.identity+“”, mysql_id: item.data.end.properties.mysql_id, create_time: item.data.end.properties.create_time, sex: item.data.end.properties.sex, name: item.data.end.properties.name, area: item.data.end.properties.birth_address } nodeList.nodes.push(n); nodeList.nodes.push(m);
for(let i = 0;i<item.data.segments.length;i++){ let related = { source: item.data.segments[i].relationship.start+“”, target: item.data.segments[i].relationship.end+“” } if(nodeList.links.indexOf(related)==-1){ nodeList.links.push(related); }
}
item.data.start.labels.forEach((label)=>{ if(label==“person”){ label = “人”; } let obj = { name:label } if(nodeList.categories.indexOf(obj)==-1){ nodeList.categories.push(obj); } })
})
nodeList.nodes = unique(nodeList.nodes); nodeList.links = unique1(nodeList.links); nodeList.categories = unique2(nodeList.categories); // nodeList.links = unique(nodeList.links); // nodeList.categories=unique(nodeList.categories); console.log(“formatJson>>>>>>”,JSON.stringify(nodeList)); function unique(arr){ for(var i=0; i<arr.length; i++){ for(var j=i+1; j<arr.length; j++){ if(arr[i].id==arr[j].id){ //第一个等同于第二个,splice方法删除第二个 arr.splice(j,1); j–; } } } return arr; }
function unique1(arr){ for(var i=0; i<arr.length; i++){ for(var j=i+1; j<arr.length; j++){ if(arr[i].sourcearr[j].source&&arr[i].targetarr[j].target){ //第一个等同于第二个,splice方法删除第二个 arr.splice(j,1); j–; } } } return arr; }
function unique2(arr){ for(var i=0; i<arr.length; i++){ for(var j=i+1; j<arr.length; j++){ if(arr[i].name==arr[j].name){ //第一个等同于第二个,splice方法删除第二个 arr.splice(j,1); j–; } } } return arr; }
|