当前位置:   article > 正文

D3js关系图谱(力导向布局)_d3.js 关系图谱

d3.js 关系图谱

在这里插入图片描述

   "d3": "^3.5.17",
  • 1
import React, { Component } from 'react'
import * as d3 from 'd3'
import './chart.css'

import { List, message, Avatar, Spin } from 'antd';
import InfiniteScroll from 'react-infinite-scroller';

export class Chart1 extends Component {

    constructor(props) {
        super(props)

        this.state = {
            nodes: [],
            links: []
        }
    }

    componentWillMount() {

    }

    componentWillReceiveProps(nextProps) {
        if (nextProps.nodes.length && nextProps.nodes != this.props.nodes) {
            this.setState({
                nodes: nextProps.nodes,
                links: nextProps.links
            }, function () {
                this.drawChart()
            })
        }
    }

    componentDidMount() {
        if (this.props.nodes.length) {
            this.drawChart();
        }
    }

    _force = null;
    _svg = null;
    drawChart = () => {
        // 获取数据
        var graph = this.state;
        // 移除旧元素
        d3.select(".d3").selectAll('*').remove();

        var width = 880,
            height = 700;
        // 设置颜色
        var color = d3.scale.category20();
        // 设置布局
        this._force = d3.layout.force()
            .charge(-1500)//排斥力
            .linkDistance(50)//距离
            .size([width, height])
            .nodes(graph.nodes)
            .links(graph.links)
            .start();

        // 设置平移和缩放
        var zoom = d3.behavior.zoom()//缩放配置,
            .scaleExtent([0.4, 2])//缩放比例
            .on("zoom", zoomed);

        function zoomed() {//缩放函数
            d3.selectAll("g").attr("transform",//svg下的g标签移动大小
                "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
        }

        // 设置画布
        this._svg = d3.select(".d3").append("svg")
            .attr("width", width)
            .attr("height", height)
            .call(zoom);

        // 设置连线
        var link = this._svg.append("g").selectAll(".link")
            .data(graph.links)
            .enter()
            .append("line")
            .attr("class", "link");
        // .style("stroke", "#000")
        // .style("stroke-width", function (d) {
        //     return Math.sqrt(d.value);
        // });

        //边上的文字(人物之间的关系)            
        // var edges_text = this._svg.selectAll(".linetext")
        //     .data(graph.links)
        //     .enter()
        //     .append("text")
        //     .attr("class", "linetext")
        //     .text(function (d) {
        //         return d.type;
        //     })
        //     .style("fill-opacity", 1.0);

        // 设置节点
        var node = this._svg.selectAll(".node")
            .data(graph.nodes)
            .enter()
            .append("g")
            .attr("class", "node")
            .call(this._force.drag)
        // 节点圆
        node.append("circle")
            .attr("r", 10)//圆的大小
            .style("fill", function (d) {
                return color(d.group);
            })
        // 节点文字
        node.append("text")
            .attr("dx", 15)
            .attr("dy", ".25em")
            .attr("y", function (d) {
                // 多行文字显示
                if (d.name.length <= 17) {
                    d3.select(this).append('tspan').attr("ix", 0)
                        .text(function () { return d.name; });
                } else {//大于16个字符时,
                    var top = d.name.substring(0, 17) + "...";
                    d3.select(this).append('tspan').attr("ix", 0).text(top);
                    // var sum = d.name.length / 17
                    // console.log(d.name,sum);
                    // for (let i = 0; i < sum+1; i++) {
                    //     // const element = array[i];
                    //     var top = d.name.substring(i * 17, (i + 1) * 17);
                    //     d3.select(this).append('tspan').attr("ix", i).text(top);
                    // }
                }
            })

        // 添加箭头
        this._svg.append("g").append("defs").selectAll("marker")
            .data(["suit", "licensing", "resolved"])
            .enter().append("marker")
            .attr("id", function (d) { return d; })
            .attr("viewBox", "0 -5 10 10")
            .attr("refX", 25)
            .attr("refY", 0)
            .attr("markerWidth", 6)
            .attr("markerHeight", 6)
            .attr("orient", "auto")
            .append("path")
            .attr("d", "M0,-5L10,0L0,5 L10,0 L0, -5")
            .style("stroke", "#4679BD")
            .style("opacity", "0.6");

        //坐标计算
        this._force.on("tick", function () {

            link.attr("x1", function (d) {
                return d.source.x;
            })
                .attr("y1", function (d) {
                    return d.source.y;
                })
                .attr("x2", function (d) {
                    return d.target.x;
                })
                .attr("y2", function (d) {
                    return d.target.y;
                });
            d3.selectAll("circle")
                .attr("cx", function (d) {
                    return d.x;
                })
                .attr("cy", function (d) {
                    return d.y;
                });

            d3.selectAll("text")
                .attr("x", function (d) {
                    return d.x;
                })
                .attr("y", function (d) {
                    return d.y;
                });

            d3.selectAll("tspan")
                .attr("x", function (d) {
                    return d.x;
                })
                .attr("y", function (d) {
                    return d.y + (this.getAttribute('ix') * 20);
                });

            //更新连接线上文字的位置
            // edges_text.attr("x", function (d) { return (d.source.x + d.target.x) / 2; });
            // edges_text.attr("y", function (d) { return (d.source.y + d.target.y) / 2; });
            //End Changed11
        });

    }
    render() {
        return <div>
            <div className="d3" id='d33' style={{ background: '#ccc', width: '880px' }} />
            <div className="infinite-container">
                <List
                    dataSource={this.state.nodes}
                    renderItem={item => (
                        <List.Item key={item.id}>
                            <h3 style={{ width: '200px' }}>{item.type}</h3>
                            <h5 style={{ width: '1800px' }}>{item.name}</h5>
                        </List.Item>
                    )}
                >
                </List>

            </div>
        </div >

    }
}

export default Chart1

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
.link {
    stroke: #000;
    stroke-width: 0.5px;
}
  

.node {
    font-size: 12px;
    font-family: SimSun;
    fill:#000;
}

.linetext {
    font-size: 12px;
    font-family: SimSun;
    fill:#1f77b4;
    fill-opacity: 0.0;
}

.infinite-container {
    border: 1px solid #e8e8e8;
    border-radius: 4px;
    overflow: auto;
    padding: 8px 24px;
    height: 500px;
    width: 880px;
    margin-top: 20px;
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/563000
推荐阅读
相关标签
  

闽ICP备14008679号