UNPKG

ze-react-component-library

Version:
177 lines (151 loc) 5.44 kB
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } var __assign = this && this.__assign || function () { __assign = Object.assign || function (t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) { if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } } return t; }; return __assign.apply(this, arguments); }; var __rest = this && this.__rest || function (s, e) { var t = {}; for (var p in s) { if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; } if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import React, { memo } from "react"; // @ts-ignore import PieSvg from "./pie.svg"; import Chart from "../../../components/Chart"; import { getChartProperties, isChartDataValid, multiValueToChartData } from "../../../components/Chart/util"; import { getNameProperty } from "zeroetp-api-sdk"; import { formatWithProperty } from "../../../util"; var PieChart = /*#__PURE__*/memo(function (props) { var data = props.result, userOption = props.option, eventsDict = props.eventsDict, style = props.style, isMobile = props.isMobile, restProps = __rest(props, ["result", "option", "eventsDict", "style", "isMobile"]); var result = multiValueToChartData(data); var _a = getChartProperties(result), measurementProps = _a.measurementProps, dimensionProps = _a.dimensionProps; // 拿第一个measurementProp var firstMeasurementProp = measurementProps[0]; var firstDimensionProp = dimensionProps[0]; var chartOption = { tooltip: { trigger: "item" }, legend: { type: "scroll", orient: "vertical", right: 10, top: 20, bottom: 20, padding: [0, 50] }, series: [{ type: "pie", label: { position: "inside", show: true, formatter: function formatter(p) { var name = p.name; // feat: show name in steadof id if (_typeof(p.data) === "object" && p.seriesName + ".name" in p.data) { name = p.data[p.seriesName + ".name"]; } // feat: 显示绝对值,但是如果绝对值本身是百分比,那么显示 var formattedValue = formatWithProperty(firstMeasurementProp, p.data[firstMeasurementProp.name]); if (firstMeasurementProp.type === "percentage") { return name + "\n" + p.percent + "%"; } else { return name + "\n" + formattedValue + "\n" + p.percent + "%"; } } }, radius: ["50%", "95%"], bottom: isMobile ? 24 : 0, itemStyle: { borderRadius: 10, borderColor: "#fff", borderWidth: 2 }, emphasis: { label: { show: true, fontWeight: "bold" } }, labelLine: { show: false } }] }; if (isMobile) { chartOption.legend = { bottom: 0, type: "scroll" }; } // legend formatter for show entity name instead of id if (!Array.isArray(chartOption.legend)) { chartOption.legend.formatter = function (value) { if (result === null || result === void 0 ? void 0 : result.columnProperties) { var groupbyProp = firstDimensionProp; if (groupbyProp.primal_type === "object" && groupbyProp.schema) { var item = result.result.find(function (r) { return r._id === value; }); var nameProp = getNameProperty(groupbyProp.schema); if (nameProp && item && _typeof(item[groupbyProp.name]) === "object") { // 有可能因为entity id不存在,item[groupbyProp.name]返回了一个string return item[groupbyProp.name][nameProp.name]; } } } return value; }; } return /*#__PURE__*/React.createElement(Chart, __assign({}, restProps, { result: __assign(__assign({}, result), { result: result === null || result === void 0 ? void 0 : result.result.map(function (d) { var dd = {}; // boolean类型的值需要处理下,否则不能正常显示 Object.entries(d).forEach(function (_a) { var k = _a[0], v = _a[1]; if (typeof v === "boolean") { dd[k] = String(v); } else { dd[k] = v; } }); return dd; }) }), chartOption: chartOption, userOption: userOption, eventsDict: eventsDict, style: style, isMobile: isMobile })); }); var pie = { key: "pie", title: "饼图", icon: PieSvg, tip: "搜索结果中至少一个属性列和一个数值列", type: "basic", component: function component(props) { return /*#__PURE__*/React.createElement(PieChart, __assign({}, props)); }, valid: function valid(res) { return isChartDataValid(res, -1); } }; export default pie;