@alicloud/cloud-charts
Version:

188 lines (177 loc) • 5.42 kB
JavaScript
;
import _extends from "@babel/runtime/helpers/extends";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import Base from '../common/Base';
import errorWrap from '../common/errorWrap';
import themes from '../themes';
import { propertyAssign, propertyMap } from '../common/common';
import guide from '../common/guide';
import rectXAxis from '../common/rectXAxis';
import rectYAxis from '../common/rectYAxis';
import autoTimeScale from '../common/autoTimeScale';
import rectTooltip from '../common/rectTooltip';
import rectLegend from '../common/rectLegend';
import legendFilter from '../common/legendFilter';
import rectZoom from '../common/rectZoom';
import rectSlider from '../common/rectSlider';
import drawLine from '../common/drawLine';
import { getText } from '../ChartProvider';
import "./index.css";
export var Line = /*#__PURE__*/function (_Base) {
_inheritsLoose(Line, _Base);
function Line() {
var _this;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _Base.call.apply(_Base, [this].concat(args)) || this;
_this.chartName = 'G2Line';
return _this;
}
var _proto = Line.prototype;
_proto.getDefaultConfig = function getDefaultConfig() {
return {
colors: themes.category_12,
areaColors: [],
xAxis: {
type: 'time',
// 默认为时间连续
mask: 'auto' // 上述type为time时,此字段生效
},
yAxis: {
labelFormatter: null,
// 可以强制覆盖,手动设置label
max: null,
min: null
},
legend: {
visible: true,
align: 'left',
nameFormatter: null // 可以强制覆盖,手动设置label
},
tooltip: {
titleFormatter: null,
nameFormatter: null,
valueFormatter: null,
lockable: false
},
area: false,
stack: false,
// 仅Area有效
spline: false,
grid: false,
symbol: false,
zoom: false,
label: false,
step: null
// TODO
// mini: false,
// dataConfig: {
// nameKey: 'name',
// valueKey: 'value',
// // valueKey: ['value1', 'value2'],
// typeKey: 'type'
// }
};
};
_proto.init = function init(chart, config, data) {
var _this2 = this;
var defs = {
x: propertyAssign(propertyMap.axis, {
type: 'time',
// 折线图X轴的范围默认覆盖全部区域,保证没有空余
range: [0, 1]
}, config.xAxis),
type: {
type: 'cat'
}
};
if (Array.isArray(config.yAxis)) {
config.yAxis.forEach(function (axis, yIndex) {
defs["y" + yIndex] = propertyAssign(propertyMap.axis, {
type: 'linear',
tickCount: 5,
nice: true
}, axis);
});
} else {
defs.y = propertyAssign(propertyMap.axis, {
type: 'linear',
tickCount: 5,
nice: true
}, config.yAxis);
}
autoTimeScale(defs, this.rawData, this.language || this.context.language);
// rectAutoTickCount(chart, config, defs, false);
chart.scale(defs);
chart.data(data);
// 如果开启标签或者标记点则需要留出右边的空余,尺寸需要通过计算获得
// 配置的scale和生成的会有不符合的地方
// 设置X轴
rectXAxis(this, chart, config);
if (Array.isArray(config.yAxis)) {
config.yAxis.forEach(function (axis, yIndex) {
var yAxisConfig = {
line: {
style: {
stroke: themes['widgets-axis-line']
}
}
};
if (yIndex !== 0) {
yAxisConfig.grid = null;
}
rectYAxis(_this2, chart, _extends({}, config, {
yAxis: axis
}), "y" + yIndex, yAxisConfig);
});
} else {
// 设置单个Y轴
rectYAxis(this, chart, config);
}
// 设置图例
rectLegend(this, chart, config, null, 'multiple', 'type');
legendFilter(this, chart);
// tooltip的标记点配置
var markerOptions = {
marker: {
symbol: 'circle'
}
};
if (config.symbol && typeof config.symbol === 'object') {
// 样式合并
markerOptions.marker = Object.assign(markerOptions.marker, config.symbol.geomStyle);
// 图形
if (config.symbol.shape) {
markerOptions.marker.symbol = config.symbol.shape;
}
// 大小
if (typeof config.symbol.size === 'number') {
markerOptions.marker = Object.assign(markerOptions.marker, {
r: config.symbol.size + 4,
width: config.symbol.size + 4,
height: config.symbol.size + 4
});
}
}
// tooltip
rectTooltip(this, chart, config, markerOptions);
// 绘制辅助线,辅助背景区域
guide(chart, config);
if (Array.isArray(config.yAxis)) {
config.yAxis.forEach(function (axis, yIndex) {
drawLine(chart, config, "y" + yIndex);
});
} else {
drawLine(chart, config);
}
// 拖拽缩放
rectZoom(chart, config, getText('reset', this.language || this.context.language, this.context.locale));
// 缩略轴
rectSlider(chart, config);
};
return Line;
}(Base);
/** Wline 折线图 */
var Wline = errorWrap(Line);
export default Wline;