UNPKG

wui-bigdata

Version:

Big data visualization library for WUI framework.

112 lines (92 loc) 4.83 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _d = require('d3'); var _d2 = _interopRequireDefault(_d); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var ForceDirectedGraph = function (_Component) { _inherits(ForceDirectedGraph, _Component); function ForceDirectedGraph() { _classCallCheck(this, ForceDirectedGraph); return _possibleConstructorReturn(this, Object.getPrototypeOf(ForceDirectedGraph).apply(this, arguments)); } _createClass(ForceDirectedGraph, [{ key: 'componentDidMount', value: function componentDidMount() { var _this2 = this; this.lines = _d2.default.select(this.refs['force-svg']).selectAll('.forceLine').data(this.edges); this.circles = _d2.default.select(this.refs['force-svg']).selectAll('.forceCircle').data(this.nodes).call(this.force.drag); this.texts = _d2.default.select(this.refs['force-svg']).selectAll('.forceText').data(this.nodes); this.force.on('tick', function () { _this2.lines.attr('x1', function (d) { return d.source.x; }); _this2.lines.attr('y1', function (d) { return d.source.y; }); _this2.lines.attr('x2', function (d) { return d.target.x; }); _this2.lines.attr('y2', function (d) { return d.target.y; }); _this2.circles.attr('cx', function (d) { return d.x; }); _this2.circles.attr('cy', function (d) { return d.y; }); _this2.texts.attr('x', function (d) { return d.x; }); _this2.texts.attr('y', function (d) { return d.y; }); }); this.force.start(); } }, { key: 'render', value: function render() { this.nodes = [{ name: '0' }, { name: '1' }, { name: '2' }, { name: '3' }, { name: '4' }, { name: '5' }, { name: '6' }]; this.edges = [{ source: 0, target: 1 }, { source: 0, target: 2 }, { source: 0, target: 3 }, { source: 1, target: 4 }, { source: 1, target: 5 }, { source: 1, target: 6 }]; var width = 400; var height = 400; this.force = _d2.default.layout.force().nodes(this.nodes).links(this.edges).size([width, height]).linkDistance(90).charge(-400); var color = _d2.default.scale.category20(); return _react2.default.createElement( 'svg', { ref: 'force-svg', width: width, height: height }, this.edges.map(function (edge, index) { return _react2.default.createElement('line', { key: index, className: 'forceLine', stroke: 'black' }); }), this.nodes.map(function (node, index) { return _react2.default.createElement('circle', { key: index, className: 'forceCircle', r: 20, fill: color(index) }); }), this.nodes.map(function (node, index) { return _react2.default.createElement( 'text', { key: index, className: 'forceText', x: node.x, y: node.y, dy: '.3em' }, node.name ); }) ); } }]); return ForceDirectedGraph; }(_react.Component); exports.default = ForceDirectedGraph;