diff --git a/src/coord/cartesian/AxisModel.ts b/src/coord/cartesian/AxisModel.ts index 1b0d35a137..f502d9013e 100644 --- a/src/coord/cartesian/AxisModel.ts +++ b/src/coord/cartesian/AxisModel.ts @@ -37,6 +37,8 @@ export type CartesianAxisOption = AxisBaseOption & { // Offset is for multiple axis on the same position. offset?: number; categorySortInfo?: OrdinalSortInfo; + // Whether to include marker data (markPoint, markLine, markArea) in axis extent calculation + includeMarkerInExtent?: boolean; }; export type XAXisOption = CartesianAxisOption & { diff --git a/src/coord/cartesian/Grid.ts b/src/coord/cartesian/Grid.ts index 97ac081bf0..d80b871e3c 100644 --- a/src/coord/cartesian/Grid.ts +++ b/src/coord/cartesian/Grid.ts @@ -44,7 +44,7 @@ import GlobalModel from '../../model/Global'; import ExtensionAPI from '../../core/ExtensionAPI'; import { Dictionary } from 'zrender/src/core/types'; import {CoordinateSystemMaster} from '../CoordinateSystem'; -import { NullUndefined, ScaleDataValue } from '../../util/types'; +import { NullUndefined, ScaleDataValue, SeriesOption } from '../../util/types'; import SeriesData from '../../data/SeriesData'; import OrdinalScale from '../../scale/Ordinal'; import { @@ -53,7 +53,7 @@ import { updateCartesianAxisViewCommonPartBuilder, isCartesian2DInjectedAsDataCoordSys } from './cartesianAxisHelper'; -import { CategoryAxisBaseOption, NumericAxisBaseOptionCommon } from '../axisCommonTypes'; +import { CategoryAxisBaseOption, NumericAxisBaseOptionCommon, OptionAxisType } from '../axisCommonTypes'; import { AxisBaseModel } from '../AxisBaseModel'; import { isIntervalOrLogScale } from '../../scale/helper'; import { alignScaleTicks } from '../axisAlignTicks'; @@ -81,6 +81,8 @@ type AxesMap = { type ParsedOuterBoundsContain = 'all' | 'axisLabel'; +type MarkerTypes = 'markPoint' | 'markLine' | 'markArea'; + // margin is [top, right, bottom, left] const XY_TO_MARGIN_IDX = [ [3, 1], // xyIdx 0 => 'x' @@ -542,6 +544,16 @@ class Grid implements CoordinateSystemMaster { unionExtent(data, xAxis); unionExtent(data, yAxis); + + // 处理 markPoint、markLine、markArea + const markerTypes: MarkerTypes[] = ['markPoint', 'markLine', 'markArea']; + + markerTypes.forEach(markerType => { + const markerOpt = seriesModel.get(markerType as keyof SeriesOption); + if (markerOpt && markerOpt.data) { + unionMarkerExtent(markerOpt.data, xAxis, yAxis); + } + }); } }, this); @@ -550,6 +562,50 @@ class Grid implements CoordinateSystemMaster { axis.scale.unionExtentFromData(data, dim); }); } + + function UnionExtentForAxisByValue( + value: any, + axis: Axis2D, + axisType: OptionAxisType, + ): void { + const includeMarkerInExtent = axis.model.get('includeMarkerInExtent') ?? true; + if (includeMarkerInExtent && value != null && typeof value !== 'string' && axisType !== 'category') { + const val = axis.scale.parse(value); + if (!isNaN(val)) { + // Construct the parameter and use unionExtentFromData to avoid using the private method _innerUnionExtent + axis.scale.unionExtentFromData({ + getApproximateExtent: () => [val, val] + } as any, 0); + } + } + } + + function unionMarkerExtent( + markerData: any[], + xAxis: Axis2D, + yAxis: Axis2D, + ): void { + each(markerData, function (item) { + if (!item) { + return; + } + const items = Array.isArray(item) ? item : [item]; + + each(items, function (markerItem) { + if (!markerItem) { + return; + } + + UnionExtentForAxisByValue(markerItem.xAxis, xAxis, xAxis.type); + UnionExtentForAxisByValue(markerItem.yAxis, yAxis, yAxis.type); + + if (markerItem.coord && Array.isArray(markerItem.coord)) { + UnionExtentForAxisByValue(markerItem.coord[0], xAxis, xAxis.type); + UnionExtentForAxisByValue(markerItem.coord[1], yAxis, yAxis.type); + } + }); + }); + } } /** diff --git a/test/axis-marker-extent.html b/test/axis-marker-extent.html new file mode 100644 index 0000000000..4d26d05a40 --- /dev/null +++ b/test/axis-marker-extent.html @@ -0,0 +1,336 @@ + + + + + + + + + + + +
仅有 markLine(includeMarkerInExtent: false)
+
+
+
+
+ +
仅有 markLine
+
+
+
+
+ +
markLine为负值用例
+
+
+
+
+ +
仅有 markLine(includeMarkerInExtent: true)
+
+
+
+
+ +
仅有 markPoint
+
+
+
+
+ +
仅有 markArea
+
+
+
+
+ + + +