zz-chart
Version:
Alauda Chart components by Alauda Frontend Team
77 lines • 2.43 kB
JavaScript
import { get, isNumber } from 'lodash-es';
import { BaseComponent } from './base.js';
import uPlot from 'uplot';
export class Scale extends BaseComponent {
constructor() {
super(...arguments);
this.name = 'scale';
}
get strategy() {
return this.ctrl.strategyManage.getStrategy('uPlot');
}
render() {
this.option = get(this.ctrl.getOption(), this.name, { x: {}, y: {} });
}
update() {
// ..
this.option = get(this.ctrl.getOption(), this.name, { x: {}, y: {} });
const x = get(this.option, 'x');
const y = get(this.option, 'y');
x && this.setScale('x', { min: x.min, max: x.max });
y && this.setScale('y', { min: y.min, max: y.max });
}
getOptions() {
return {
scales: {
x: this.getXOptions(),
y: this.getYOptions(),
},
};
}
setScale(field, limits) {
const iUPlot = this.strategy.getUPlotChart();
const scale = iUPlot.scales[field];
this.ctrl.setOption([this.name, field], limits);
iUPlot.setScale(field, {
min: limits.min || scale.min,
max: limits.max || scale.max,
});
}
getXOptions() {
if (!this.option.x) {
return {};
}
const { max, min } = this.option.x || {};
const maxV = isNumber(max) ? { max } : {};
const minV = isNumber(min) ? { min } : {};
return {
...maxV,
...minV,
};
}
getYOptions() {
const { max, min } = this.option.y || {};
return {
auto: true,
range: (_u, dataMin, dataMax) => {
const rangeConfig = {
min: {
pad: 0.1,
hard: min ?? -Infinity,
soft: 0,
mode: 3,
},
max: {
pad: 0.1,
hard: max ?? Infinity,
soft: 0,
mode: 3,
},
};
const minMax = uPlot.rangeNum(min != null && min != undefined ? min : dataMin, max != null && max != undefined ? max : dataMax, rangeConfig);
return [minMax[0], minMax[1]];
},
};
}
}
//# sourceMappingURL=scale.js.map