UNPKG

data-vis-ui

Version:

## [使用文档](https://temp-static-domain.jd.com/data-vis-ui)

231 lines (197 loc) 7.2 kB
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } /* * @Author: 石丽丽 * @description : 接线图,用于点线连接 * @@copyRight: 京东科技IOT */ import React, { useRef, useState, useMemo } from 'react'; import { SmartChart } from 'data-vis-ui'; import { merge } from 'lodash'; import { ErrorBoundary } from 'data-vis-ui'; var getGraphData = function getGraphData(nodes) { return nodes.map(function (ele) { var id = ele.id, _ele$labelPos = ele.labelPos, labelPos = _ele$labelPos === void 0 ? 'right' : _ele$labelPos, value = ele.value, data = ele.data, label = ele.label, name = ele.name, symbolSize = ele.symbolSize, image = ele.image; var defaultLabel = { show: true, position: labelPos, formatter: function formatter() { var res = []; if (name) { res.push("{title|".concat(name, "}")); } if (!!data && data.length > 0) { data.forEach(function (item) { res.push("{span|".concat(item, "}")); }); } return res.join('\n'); }, rich: { title: { fontSize: 16 }, span: { color: '#66DB42', lineHeight: 16, fontSize: 14 } } }; var _label = merge({}, defaultLabel, label); var _data = { id: id, value: value, symbol: "image://".concat(image), symbolSize: symbolSize || 40, label: _label }; return _data; }); }; var getNodeDot = function getNodeDot(nodes, nodeId, direction) { var gap = 10; var target = nodes.find(function (e) { return e.id === nodeId; }); if (target) { if (!target.symbolSize) { target.symbolSize = [50, 50]; // 默认节点宽高50 } switch (direction) { case 'top': return [target.value[0], target.value[1] + target.symbolSize[1] / 2 + gap]; case 'bottom': return [target.value[0], target.value[1] - target.symbolSize[1] / 2 - gap]; case 'left': return [target.value[0] - target.symbolSize[0] / 2 - gap, target.value[1]]; case 'right': return [target.value[0] + target.symbolSize[0] / 2 + gap, target.value[1]]; default: return [target.value[0], target.value[1]]; } } return [0, 0]; }; /** * @description: 得到连线的数据 * @param {NodeItem} nodes * @param {LineItem} links * @return {*} */ var getLineData = function getLineData(nodes, links) { var lineItem = { type: 'lines', polyline: true, coordinateSystem: 'cartesian2d', lineStyle: { type: 'dashed', width: 5, color: '#66DB42' }, data: [] }; var lines = []; links.forEach(function (item) { var coords = item.coords, lineStyle = item.lineStyle; var lineData = []; if (item.type === 'nodeTonode') { lineData = [{ coords: [getNodeDot(nodes, coords[0][0], coords[0][1]), getNodeDot(nodes, coords[1][0], coords[1][1])] }]; } else { lineData = [{ coords: coords }]; } var ele = Object.assign(Object.assign({}, lineItem), { lineStyle: lineStyle || lineItem.lineStyle, data: lineData }); lines.push(ele); }); return lines; }; /** * @description: 设置options * @param {NodeItem} nodes * @param {LineItem} Lines * @return {*} */ export var getOption = function getOption(nodes, links, _option) { var graphData = getGraphData(nodes); console.log(graphData, 'graphData---'); var LineData = getLineData(nodes, links); console.log(LineData, 'getOption'); var option = { xAxis: { min: 0, max: 1000, show: false, type: 'value' }, yAxis: { min: 0, max: 1000, show: false, type: 'value' }, series: [{ type: 'graph', symbolSize: 50, coordinateSystem: 'cartesian2d', label: { show: true, color: '#EEE', distance: 10 }, data: graphData }].concat(_toConsumableArray(LineData)) }; option = merge({}, option, _option); return option; }; var ConnectGraph = function ConnectGraph(props) { var errorChildren = /*#__PURE__*/React.createElement("h1", { style: { color: '#fff' } }, "\u8BF7\u68C0\u67E5\u63A5\u53E3\u6570\u636E\u683C\u5F0F\uFF0C\u9700\u8981\u9075\u5B88\u6309\u7167\u56FE\u8868\u6570\u636E\u683C\u5F0F\u89C4\u7EA6"); var style = props.style, nodes = props.nodes, lines = props.lines, _option = props.option; var _useState = useState({}), _useState2 = _slicedToArray(_useState, 2), option = _useState2[0], setOption = _useState2[1]; var key = useRef(Date.now()); useMemo(function () { setOption(getOption(nodes, lines, _option)); }, [nodes, lines, _option]); return /*#__PURE__*/React.createElement(ErrorBoundary, { errorChildren: errorChildren }, /*#__PURE__*/React.createElement(SmartChart, { style: style, key: key.current, option: option })); }; export default ConnectGraph;