UNPKG

wui-bigdata

Version:

Big data visualization library for WUI framework.

168 lines (144 loc) 6.25 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 PieChart = function (_Component) { _inherits(PieChart, _Component); function PieChart() { _classCallCheck(this, PieChart); var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(PieChart).call(this)); _this.state = { tooltip: '', style: {} }; _this.pie = _d2.default.layout.pie().value(function (d) { return d[1]; }); _this.dataset = [['小米', 60.8], ['三星', 58.4], ['联想', 47.3], ['苹果', 46.6], ['华为', 41.3], ['酷派', 40.1], ['其他', 111.5]]; _this.pieData = _this.pie(_this.dataset); _this.pieData.forEach(function (data) { data.dx = 300; data.dy = 300; }); return _this; } _createClass(PieChart, [{ key: 'onMouseOver', value: function onMouseOver(d) { this.setState({ tooltip: d.data[0] + '的出货量为\n ' + d.data[1] + ' 百万台' }); } }, { key: 'onMouseMove', value: function onMouseMove(color, event) { this.setState({ style: { left: event.pageX, top: event.pageY + 20, boxShadow: '10px 0px 0px ' + color } }); } }, { key: 'onMouseOut', value: function onMouseOut() { this.setState({ style: { opacity: 0 } }); } }, { key: 'onDragMove', value: function onDragMove(d, index) { this.pieData[index].dx += _d2.default.event.dx; this.pieData[index].dy += _d2.default.event.dy; this.forceUpdate(); } }, { key: 'onDragEnd', value: function onDragEnd(d, i) { var dis2 = d.dx * d.dx + d.dy * d.dy; if (dis2 > 200 * 200) { this.dataset.splice(i, 1); this.pieData = this.pie(this.dataset); this.pieData.forEach(function (data) { data.dx = 300; data.dy = 300; }); this.forceUpdate(); } } }, { key: 'componentDidMount', value: function componentDidMount() { var drag = _d2.default.behavior.drag().origin(null).on('drag', this.onDragMove.bind(this)).on('dragend', this.onDragEnd.bind(this)); _d2.default.select(this.refs['pie-svg']).selectAll('g').data(this.pieData).call(drag); } }, { key: 'render', value: function render() { var _this2 = this; var width = 600; var height = 600; var outerRadius = width / 3; var innerRadius = 0; var arc = _d2.default.svg.arc().innerRadius(innerRadius).outerRadius(outerRadius); var color = _d2.default.scale.category20(); return _react2.default.createElement( 'div', null, _react2.default.createElement( 'svg', { width: width, height: height, ref: 'pie-svg' }, this.pieData.map(function (data, index) { return _react2.default.createElement( 'g', { transform: 'translate(' + data.dx + ', ' + data.dy + ')', key: index }, _react2.default.createElement('path', { fill: color(index), d: arc(data), onMouseOver: _this2.onMouseOver.bind(_this2, data), onMouseMove: _this2.onMouseMove.bind(_this2, color(index)), onMouseOut: _this2.onMouseOut.bind(_this2) }), _react2.default.createElement( 'text', { transform: 'translate(' + arc.centroid(data)[0] * 1.4 + ', ' + arc.centroid(data)[1] * 1.4 + ')', textAnchor: 'middle' }, (Number(data.value) / _d2.default.sum(_this2.dataset, function (d) { return d[1]; }) * 100).toFixed(1) + '%' ), _react2.default.createElement('line', { stroke: 'black', x1: arc.centroid(data)[0] * 2, y1: arc.centroid(data)[1] * 2, x2: arc.centroid(data)[0] * 2.2, y2: arc.centroid(data)[1] * 2.2 }), _react2.default.createElement( 'text', { transform: 'translate(' + arc.centroid(data)[0] * 2.5 + ', ' + arc.centroid(data)[1] * 2.5 + ')', textAnchor: 'middle' }, data.data[0] ) ); }) ), _react2.default.createElement( 'div', { className: 'tooltip', style: this.state.style }, this.state.tooltip ) ); } }]); return PieChart; }(_react.Component); exports.default = PieChart;