Skip to content

Commit 1808e50

Browse files
committed
feat(axis): support dataMin/dataMax to calc a nice axis range
1 parent be1028e commit 1808e50

File tree

3 files changed

+342
-3
lines changed

3 files changed

+342
-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: 33 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,11 @@ 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 _dataMinRaw: ScaleDataValue;
74+
private _dataMaxRaw: ScaleDataValue;
75+
private _dataMinNum: number;
76+
private _dataMaxNum: number;
7277

7378
constructor(
7479
scale: Scale,
@@ -98,6 +103,19 @@ export class ScaleRawExtentInfo {
98103
const isOrdinal = this._isOrdinal = scale.type === 'ordinal';
99104
this._needCrossZero = scale.type === 'interval' && model.getNeedCrossZero && model.getNeedCrossZero();
100105

106+
if (scale.type === 'interval' || scale.type === 'log') {
107+
// Process custom dataMin/dataMax
108+
this._dataMinRaw = (model as AxisBaseModel<NumericAxisBaseOptionCommon>).get('dataMin', true);
109+
if (this._dataMinRaw != null) {
110+
this._dataMinNum = parseAxisModelMinMax(scale, this._dataMinRaw);
111+
}
112+
113+
this._dataMaxRaw = (model as AxisBaseModel<NumericAxisBaseOptionCommon>).get('dataMax', true);
114+
if (this._dataMaxRaw != null) {
115+
this._dataMaxNum = parseAxisModelMinMax(scale, this._dataMaxRaw);
116+
}
117+
}
118+
101119
let axisMinValue = model.get('min', true);
102120
if (axisMinValue == null) {
103121
axisMinValue = model.get('startValue', true);
@@ -173,8 +191,20 @@ export class ScaleRawExtentInfo {
173191
// (3) If no data, it should be ensured that `scale.setBlank` is set.
174192

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

test/axis-data-min-max.html

Lines changed: 295 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)