UNPKG

@alicloud/cloud-charts

Version:

![](https://img.shields.io/npm/v/@alicloud/cloud-charts?color=%23ff8200)

200 lines (160 loc) 6.42 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports.isBigDataInit = isBigDataInit; exports.isBigDataBeforePaint = isBigDataBeforePaint; exports.processBarBigData = processBarBigData; exports.processLineBigData = processLineBigData; exports.processPieBigData = processPieBigData; exports.BigDataJudgement = void 0; var _log = require("../common/log"); var _common = require("../common/common"); var _themes = _interopRequireDefault(require("../themes")); /** 大数据判断条件 */ var BigDataJudgement; /** 大数据处理 */ exports.BigDataJudgement = BigDataJudgement; (function (BigDataJudgement) { BigDataJudgement[BigDataJudgement["Length"] = 0] = "Length"; BigDataJudgement[BigDataJudgement["Area"] = 1] = "Area"; BigDataJudgement[BigDataJudgement["Polar"] = 2] = "Polar"; BigDataJudgement[BigDataJudgement["Number"] = 3] = "Number"; })(BigDataJudgement || (exports.BigDataJudgement = BigDataJudgement = {})); /** 判断是否是大数据(初始化) */ function isBigDataInit(chartName, judgements, dataSize, width, height, mainAxis) { if (mainAxis === void 0) { mainAxis = 'x'; } if (!dataSize || !width || !height) { return false; } var res = false; judgements === null || judgements === void 0 ? void 0 : judgements.forEach(function (judgement) { var type = judgement.type, threshold = judgement.threshold, _judgement$period = judgement.period, period = _judgement$period === void 0 ? 'init' : _judgement$period, message = judgement.message; if (period === 'init') { var isBigData = false; if (type === BigDataJudgement.Length) { var length = mainAxis === 'x' ? width !== null && width !== void 0 ? width : 0 : height !== null && height !== void 0 ? height : 0; isBigData = length > 0 && length / dataSize < threshold; } else if (type === BigDataJudgement.Area) { isBigData = height * width / dataSize < threshold; } else if (type === BigDataJudgement.Number) { isBigData = dataSize > threshold; } else if (type === BigDataJudgement.Polar) { var radius = Math.min(height, width); isBigData = radius < threshold; } res = res || isBigData; if (isBigData && message) { (0, _log.warn)(chartName, message); } } }); return res; } /** 判断是否是大数据(绘制前) */ function isBigDataBeforePaint(chartName, judgements, chart, dataSize, mainAxis) { if (mainAxis === void 0) { mainAxis = 'x'; } var res = false; judgements === null || judgements === void 0 ? void 0 : judgements.forEach(function (judgement) { var type = judgement.type, threshold = judgement.threshold, _judgement$period2 = judgement.period, period = _judgement$period2 === void 0 ? 'init' : _judgement$period2, message = judgement.message; if (period === 'before_paint') { var isBigData = false; var _ref = (chart === null || chart === void 0 ? void 0 : chart.coordinateBBox) || {}, _ref$width = _ref.width, width = _ref$width === void 0 ? 0 : _ref$width, _ref$height = _ref.height, height = _ref$height === void 0 ? 0 : _ref$height; if (type === BigDataJudgement.Length) { var length = mainAxis === 'x' ? width : height; isBigData = length > 0 && length / dataSize < threshold; } else if (type === BigDataJudgement.Area) { isBigData = height * width / dataSize < threshold; } else if (type === BigDataJudgement.Number) { isBigData = dataSize > threshold; } if (type === BigDataJudgement.Polar) { var _chart$coordinateBBox, _chart$coordinateBBox2; var radius = Math.min(chart === null || chart === void 0 ? void 0 : (_chart$coordinateBBox = chart.coordinateBBox) === null || _chart$coordinateBBox === void 0 ? void 0 : _chart$coordinateBBox.height, chart === null || chart === void 0 ? void 0 : (_chart$coordinateBBox2 = chart.coordinateBBox) === null || _chart$coordinateBBox2 === void 0 ? void 0 : _chart$coordinateBBox2.width); isBigData = radius < threshold; } res = res || isBigData; if (isBigData && message) { (0, _log.warn)(chartName, message); } } }); return res; } /** 柱图大数据处理方式:开启slider */ function processBarBigData(chartObj, data) { var dataSize = chartObj.dataSize; return { config: {// 暂时不显示slider // slider: { // start: 1 - Math.max(Number((30 / dataSize).toFixed(2)), 0.01), // end: 1, // }, } }; } /** 线图大数据处理方式: */ function processLineBigData(chartObj, data) { var dataSize = chartObj.dataSize; return { config: { symbol: false, spline: false, area: false // 暂时不显示slider // slider: { // start: 1 - Math.max(Number((100 / dataSize).toFixed(2)), 0.01), // end: 1, // }, } }; } /** 饼图大数据处理方式:合并数据 */ function processPieBigData(chartObj, data) { var _props$config; var dataSize = chartObj.dataSize, props = chartObj.props; if (props !== null && props !== void 0 && (_props$config = props.config) !== null && _props$config !== void 0 && _props$config.autoFormat && dataSize > 5) { var _props$config2; // 数据排序 if ((props === null || props === void 0 ? void 0 : (_props$config2 = props.config) === null || _props$config2 === void 0 ? void 0 : _props$config2.autoSort) !== false) { data.sort(function (a, b) { return b.y - a.y; }); } // 计算总数据 var total = data.reduce(function (pre, cur) { return pre + cur.y; }, 0); // 计算剩余占比 var remainTotal = data.slice(5, data.length).reduce(function (pre, cur) { return pre + cur.y; }, 0); // 若剩余内容小于10%,剩余内容改为其他 if ((0, _common.numberDecimal)(remainTotal / total, 2) <= 0.1) { var newData = [].concat(data.slice(0, 5), [{ x: '其他', y: remainTotal }]); return { data: newData, config: { autoSort: false, colors: _themes["default"].category_12.slice(0, 5).concat(_themes["default"]['widgets-axis-line']) } }; } } return {}; }