当前位置: 首页 > 工具软件 > react-bolt > 使用案例 >

react获取noe4j 数据 用d3js展示

华昕
2023-12-01
    "neo4j-driver": "^4.1.2",
import neo4j from 'neo4j-driver'
    async getKnowMap(cypher, user) {
        // console.log(cypher);
        // neo4j数据展示
        var config = {
            container_id: "viz",
            server_url: "bolt://192.168.51.109:7687",
            server_user: "neo4j",
            server_password: "123456",
            labels: {
                "banli": {
                    caption: "user_key",
                    size: "pagerank",
                    community: "community"
                }
            },
            relationships: {
                "tail_entity": {
                    caption: false,
                    thickness: "count"
                }
            },
            initial_cypher: cypher
        }
        var viz = new window.NeoVis.default(config);
        viz.render();
        // neo4j获取数据转d3js展示
        var driver = neo4j.driver('bolt://192.168.51.109:7687', neo4j.auth.basic("neo4j", "123456"))
        var session = driver.session()
        try {
            var result = await session.writeTransaction(tx =>
                tx.run(cypher, null)
            )
            var data = JSON.stringify(result)
            var json = JSON.parse(data)
            var records = json.records
            // console.log(records);
            // 获取所有关系数据
            var list = []
            for (let i = 0; i < records.length; i++) {
                const segments = records[i]._fields[0].segments;
                for (let j = 0; j < segments.length; j++) {
                    const line = segments[j];
                    list.push(line)
                }
            }
            // console.log(list);
            // 获取所有节点数据
            var nodes = []
            for (let i = 0; i < list.length; i++) {
                const element = list[i];
                var start = element.start
                nodes.push(getNode(start, user))
                var end = element.end
                nodes.push(getNode(end, user))
            }
            // 数据去重
            var nn = deteleObject(nodes)
            // console.log(nn);
            // 获取所有连线数据
            var links = []
            for (let i = 0; i < list.length; i++) {
                const relation = list[i].relationship;
                var link = {}
                link.source = relation.start.low
                link.target = relation.end.low
                link.type = relation.type
                if (link.source != null && link.target != null)
                    links.push(link)
            }
            // 数据去重
            var ll = deteleObject(links)
            // console.log(ll);
            //关系数据index转换
            for (var i = 0; i < nn.length; i++) {
                var n = nn[i].id;
                for (let index = 0; index < ll.length; index++) {
                    var s = ll[index].source
                    var t = ll[index].target
                    if (n === s) {
                        ll[index].source = i
                    }
                    if (n === t) {
                        ll[index].target = i
                    }
                }
            }
            // console.log(ll);
            this.setState({
                nodes: nn,
                edges: ll
            })
        } finally {
            await session.close()
        }
        // on application exit:
    }
// 数据去重
function deteleObject(obj) {
    var uniques = [];
    var stringify = {};
    for (var i = 0; i < obj.length; i++) {
        var keys = Object.keys(obj[i]);
        keys.sort(function (a, b) {
            return (Number(a) - Number(b));
        });
        var str = '';
        for (var j = 0; j < keys.length; j++) {
            str += JSON.stringify(keys[j]);
            str += JSON.stringify(obj[i][keys[j]]);
        }
        if (!stringify.hasOwnProperty(str)) {
            uniques.push(obj[i]);
            stringify[str] = true;
        }
    }
    return uniques;
}

// 获取数据类型
function getNode(pro, user) {
    var label = pro.labels[0]
    var node = {};
    node.id = pro.identity.low
    node.label = label;
    if (label === "caller") {
        node.type = "投诉人"
        if (user) {
            node.name = user.name;
        } else {
            node.name = pro.properties.name;
        }
        node.group = 0;
    }
    if (label === "appeal_class") {
        node.type = "案件类别"
        node.name = pro.properties.name;
        node.group = 1;
    }
    if (label === "appeal_address_area") {
        node.type = "承办单位";
        node.name = pro.properties.name;
        node.group = 2;
    }
    if (label === "appeal_theme") {
        node.type = "案件主题";
        node.name = pro.properties.name;
        node.group = 3;
    }
    if (label === "appeal_content") {
        node.type = "案件详情";
        node.name = pro.properties.desc;
        node.group = 4;
        if (node.name == null) {
            node.name = "未知详情"
        }
    }
    if (label === "appeal_endcase_idea") {
        node.type = "结案意见";
        node.name = pro.properties.desc;
        node.group = 5;
        if (node.name == null) {
            node.name = "未知意见"
        }
    }
    return node
}
    componentWillMount() {
        this.getKnowMap("MATCH p=()-[]->() RETURN p", null)
    }
 类似资料: