Skip to content

Commit 1e9b6e1

Browse files
authored
Merge pull request #20838 from Justin-ZS/feat/20770
feat(axis): support dataMin/dataMax to calc a nice axis range
2 parents 3844954 + a4dfbec commit 1e9b6e1

File tree

3 files changed

+380
-3
lines changed

3 files changed

+380
-3
lines changed

src/coord/axisCommonTypes.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,20 @@ export interface NumericAxisBaseOptionCommon extends AxisBaseOptionCommon {
163163
* Will be ignored if interval is set.
164164
*/
165165
alignTicks?: boolean
166+
167+
/**
168+
* Data min value to be included in axis extent calculation.
169+
* The final min value will be the minimum of this value and the data min.
170+
* Only works for value axis.
171+
*/
172+
dataMin?: ScaleDataValue;
173+
174+
/**
175+
* Data max value to be included in axis extent calculation.
176+
* The final max value will be the maximum of this value and the data max.
177+
* Only works for value axis.
178+
*/
179+
dataMax?: ScaleDataValue;
166180
}
167181

168182
export interface CategoryAxisBaseOption extends AxisBaseOptionCommon {

src/coord/scaleRawExtentInfo.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { assert, isArray, eqNaN, isFunction } from 'zrender/src/core/util';
2121
import Scale from '../scale/Scale';
2222
import { AxisBaseModel } from './AxisBaseModel';
2323
import { parsePercent } from 'zrender/src/contain/text';
24-
import { AxisBaseOption, CategoryAxisBaseOption } from './axisCommonTypes';
24+
import { AxisBaseOption, CategoryAxisBaseOption, NumericAxisBaseOptionCommon } from './axisCommonTypes';
2525
import { ScaleDataValue } from '../util/types';
2626

2727

@@ -69,6 +69,9 @@ export class ScaleRawExtentInfo {
6969
// Make that the `rawExtentInfo` can not be modified any more.
7070
readonly frozen: boolean;
7171

72+
// custom dataMin/dataMax
73+
private _dataMinNum: number;
74+
private _dataMaxNum: number;
7275

7376
constructor(
7477
scale: Scale,
@@ -98,6 +101,19 @@ export class ScaleRawExtentInfo {
98101
const isOrdinal = this._isOrdinal = scale.type === 'ordinal';
99102
this._needCrossZero = scale.type === 'interval' && model.getNeedCrossZero && model.getNeedCrossZero();
100103

104+
if (scale.type === 'interval' || scale.type === 'log' || scale.type === 'time') {
105+
// Process custom dataMin/dataMax
106+
const dataMinRaw = (model as AxisBaseModel<NumericAxisBaseOptionCommon>).get('dataMin', true);
107+
if (dataMinRaw != null) {
108+
this._dataMinNum = parseAxisModelMinMax(scale, dataMinRaw);
109+
}
110+
111+
const dataMaxRaw = (model as AxisBaseModel<NumericAxisBaseOptionCommon>).get('dataMax', true);
112+
if (dataMaxRaw != null) {
113+
this._dataMaxNum = parseAxisModelMinMax(scale, dataMaxRaw);
114+
}
115+
}
116+
101117
let axisMinValue = model.get('min', true);
102118
if (axisMinValue == null) {
103119
axisMinValue = model.get('startValue', true);
@@ -173,8 +189,20 @@ export class ScaleRawExtentInfo {
173189
// (3) If no data, it should be ensured that `scale.setBlank` is set.
174190

175191
const isOrdinal = this._isOrdinal;
176-
const dataMin = this._dataMin;
177-
const dataMax = this._dataMax;
192+
let dataMin = this._dataMin;
193+
let dataMax = this._dataMax;
194+
195+
// Include custom dataMin/dataMax in calculation
196+
// If dataMin is set and less than current data minimum, update the minimum value
197+
if (this._dataMinNum != null && isFinite(dataMin) && this._dataMinNum < dataMin) {
198+
dataMin = this._dataMinNum;
199+
}
200+
201+
// If dataMax is set and greater than current data maximum, update the maximum value
202+
if (this._dataMaxNum != null && isFinite(dataMax) && this._dataMaxNum > dataMax) {
203+
dataMax = this._dataMaxNum;
204+
}
205+
178206
const axisDataLen = this._axisDataLen;
179207
const boundaryGapInner = this._boundaryGapInner;
180208

0 commit comments

Comments
 (0)