已知显示图表的API是:chart.setOption(option: Object); 其中option为配置项。如果我们要对x坐标的show, fontSize进行配置,就是这样:
const option = {
xAxis: {
data: ["一班", "二班", "三班"],
axisLabel: {
fontSize: 24,
show: true
}
},
// 此处省略其它配置项
};
chart.setOption(option);
如果有很多的配置信息,option这个对象将很快变得无法维护,因此做了以下封装:
const argsEachToObj: Function = (...args: (() => Object)[]) => {
const obj = {};
args.forEach((item, index) => {
Object.assign(obj, item());
});
return obj;
};
const judgeAndReturn: Function = (defaultVal: any, val: any) => {
const valNew = val === undefined ? defaultVal : val;
return valNew;
};
// title start
export const titleText: Function = (val?: string) => {
return () => ({ text: judgeAndReturn('', val) });
};
export const titleConstructor = (...args: Function[]) => {
return () => {
return { title: argsEachToObj(...args) };
};
};
// title end