import { typeCollection } from 'handsome-helpers/dist/utils'; export type SetOptionsFirstArguments = (config: any, echarts: any) => any; /** * @class EchartsChain * @example: * const echartsChain = new EchartsChain() * echartsChain * .property('title') * .setOptions((config, echarts) => { * return { * ...config, * title: 'hello' * }; * }) */ export default class EchartsChain { public CONFIG: any; public echartsGlobal: any = null; private optionsName: string = ''; constructor(defaultOptions: any, echarts: any) { this.echartsGlobal = echarts; // 设置echarts配置的默认配置 this.CONFIG = typeCollection.isObject(defaultOptions) ? defaultOptions : {}; } /** * 设置echarts属性的单个属性 * @param name */ public property(name: string) { this.optionsName = name; this.CONFIG[name] = this.CONFIG[name] || {}; return this; } public setOptions(configFn: SetOptionsFirstArguments) { const newConfig = this.copyConfig(this.CONFIG[this.optionsName]); this.CONFIG[this.optionsName] = configFn ? configFn.call( null, newConfig, this.echartsGlobal) : newConfig; return this; } public copyConfig(config: any) { if(typeCollection.isObject(config)) { return Object.assign({}, config); }else if(typeCollection.isArray(config)) { return config.concat([]); } } public toConfig() { return this.CONFIG; } }