UNPKG

@alicloud/cloud-charts

Version:

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

142 lines (117 loc) 4.43 kB
'use strict'; import label from './label'; import geomSize from './geomSize'; import geomStyle from './geomStyle'; import themes from '../themes/index'; import { warn } from './log'; var stepNames = ['hv', 'vh', 'hvh', 'vhv']; /** * drawLine 绘制线图逻辑 * * @param {Chart} chart 图表实例 * @param {Object} config 配置项 * @param {string} yAxisKey 数据映射字段 * */ export default function drawLine(chart, config, yAxisKey) { if (yAxisKey === void 0) { yAxisKey = 'y'; } var areaColors = config.areaColors || config.colors; if (Array.isArray(config.colors) && Array.isArray(config.areaColors)) { areaColors = mergeArray([], config.colors, config.areaColors); // TODO优化赋值, 考虑status MAP areaColors = areaColors.map(function (subColor) { subColor = "l(90) 0:" + subColor + "cc 1:" + subColor + "00"; return subColor; }); } // 区域、堆叠、平滑曲线 var lineShape = config.spline ? 'smooth' : 'line'; var areaShape = config.spline ? 'smooth' : 'area'; if (config.spline) { warn('config.spline', '涉及监控、运维、运营等业务场景不推荐开启曲线,建议关闭'); } // 阶梯折线,目前区域图不支持阶梯,需特殊说明 if (config.step && !config.area) { lineShape = stepNames.indexOf(String(config.step)) > -1 ? config.step : 'hv'; } var lineGeom = null; var areaGeom = null; var geomConfig = { sortable: config.sortable }; if (config.area && config.stack) { areaGeom = chart.area(geomConfig).position(['x', yAxisKey]).color('type', areaColors).tooltip(false).shape(areaShape).adjust('stack'); lineGeom = chart.line(geomConfig).position(['x', yAxisKey]).color('type', config.colors).shape(lineShape).adjust('stack'); } else if (config.area && !config.stack) { areaGeom = chart.area(geomConfig).position(['x', yAxisKey]).color('type', areaColors).tooltip(false).shape(areaShape); lineGeom = chart.line(geomConfig).position(['x', yAxisKey]).color('type', config.colors).shape(lineShape); } else { lineGeom = chart.line(geomConfig).position(['x', yAxisKey]).color('type', config.colors).shape(lineShape); } if (typeof config.animate === 'object') { lineGeom.animate(config.animate); areaGeom && areaGeom.animate(config.animate); } if (areaGeom && typeof config.area === 'object') { if (config.area.geomStyle) { geomStyle(areaGeom, config.area.geomStyle, {}, "x*" + yAxisKey + "*type*extra"); } } geomStyle(lineGeom, config.geomStyle, { lineWidth: config.lineWidth || themes['widgets-line-width'], lineJoin: 'round' }, "x*" + yAxisKey + "*type*extra"); label({ geom: lineGeom, config: config, field: yAxisKey }); // 曲线上圆点 if (config.symbol) { var pointGeom = null; pointGeom = chart.point(geomConfig).position(['x', yAxisKey]).color('type', config.colors) // 改为空心shape .shape('hollowCircle').state({ active: { style: function style(ele) { var _ele$model; return { stroke: ele === null || ele === void 0 ? void 0 : (_ele$model = ele.model) === null || _ele$model === void 0 ? void 0 : _ele$model.color }; } }, selected: { style: function style(ele) { var _ele$model2; return { stroke: ele === null || ele === void 0 ? void 0 : (_ele$model2 = ele.model) === null || _ele$model2 === void 0 ? void 0 : _ele$model2.color }; } } }); if (config.area && config.stack) { pointGeom = pointGeom.adjust('stack'); } if (typeof config.symbol === 'object') { pointGeom.shape(config.symbol.shape || 'hollowCircle'); // 配置形状 geomSize(pointGeom, config.symbol.size, 3, yAxisKey, 'type'); if (config.symbol.geomStyle) { geomStyle(pointGeom, config.symbol.geomStyle, {}, "x*" + yAxisKey + "*type*extra"); } } } } function mergeArray(target) { for (var _len = arguments.length, source = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { source[_key - 1] = arguments[_key]; } source.forEach(function (s) { if (!s || s.length === 0) { return; } s.forEach(function (item, i) { if (i >= target.length) { target.push(item); } else { target[i] = item; } }); }); return target; }