This is an automated email from the ASF dual-hosted git repository. ovilia pushed a commit to branch feat-bar-race in repository https://gitbox.apache.org/repos/asf/incubator-echarts.git
commit e2dac51319842f3a7d31aeeaee7b2a7774d09703 Author: Ovilia <[email protected]> AuthorDate: Thu Apr 23 17:39:39 2020 +0800 WIP(bar-racing): sort axis --- src/coord/Axis.ts | 3 ++- src/coord/cartesian/AxisModel.ts | 4 +++- src/coord/cartesian/Grid.ts | 41 ++++++++++++++++++++++++++++++++++++++++ src/scale/Ordinal.ts | 19 ++++++++++++++++++- 4 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/coord/Axis.ts b/src/coord/Axis.ts index 0870273..ee92be7 100644 --- a/src/coord/Axis.ts +++ b/src/coord/Axis.ts @@ -121,6 +121,7 @@ class Axis { dataToCoord(data: ScaleDataValue, clamp?: boolean): number { let extent = this._extent; const scale = this.scale; + // data = (scale instanceof OrdinalScale && typeof data === 'number') ? scale.getSortedDataIndex(data) : data; data = scale.normalize(data); if (this.onBand && scale.type === 'ordinal') { @@ -178,7 +179,7 @@ class Axis { const ticksCoords = map(ticks, function (tickValue) { return { coord: this.dataToCoord(tickValue), - tickValue: tickValue + tickValue: this.scale instanceof OrdinalScale ? this.scale.getSortedDataIndex(tickValue) : tickValue }; }, this); diff --git a/src/coord/cartesian/AxisModel.ts b/src/coord/cartesian/AxisModel.ts index d8e4aee..d36faec 100644 --- a/src/coord/cartesian/AxisModel.ts +++ b/src/coord/cartesian/AxisModel.ts @@ -35,6 +35,7 @@ interface CartesianAxisOption extends AxisBaseOption { position?: CartesianAxisPosition; // Offset is for multiple axis on the same position. offset?: number; + sort?: boolean; } class CartesianAxisModel extends ComponentModel<CartesianAxisOption> @@ -76,7 +77,8 @@ zrUtil.mixin(CartesianAxisModel, AxisModelCommonMixin); const extraOption: CartesianAxisOption = { // gridIndex: 0, // gridId: '', - offset: 0 + offset: 0, + sort: false }; axisModelCreator<CartesianAxisOption, typeof CartesianAxisModel>('x', CartesianAxisModel, extraOption); diff --git a/src/coord/cartesian/Grid.ts b/src/coord/cartesian/Grid.ts index 0b0f2dd..e598e50 100644 --- a/src/coord/cartesian/Grid.ts +++ b/src/coord/cartesian/Grid.ts @@ -48,6 +48,7 @@ import {CoordinateSystemMaster} from '../CoordinateSystem'; import { ScaleDataValue } from '../../util/types'; import List from '../../data/List'; import SeriesModel from '../../model/Series'; +import OrdinalScale from '../../scale/Ordinal'; type Cartesian2DDimensionName = 'x' | 'y'; @@ -411,6 +412,9 @@ class Grid implements CoordinateSystemMaster { each(this._axesList, function (axis) { axis.scale.setExtent(Infinity, -Infinity); }); + const sortedDataValue: number[] = []; + const sortedDataIndex: number[] = []; + ecModel.eachSeries(function (seriesModel) { if (isCartesian2D(seriesModel)) { const axesModels = findAxesModels(seriesModel); @@ -433,6 +437,11 @@ class Grid implements CoordinateSystemMaster { if (data.type === 'list') { unionExtent(data, xAxis); unionExtent(data, yAxis); + if (!sortedDataIndex.length) { + // Only sort by the first series + sortCategory(data, xAxis, xAxisModel, yAxis); + sortCategory(data, yAxis, yAxisModel, xAxis); + } } } }, this); @@ -447,6 +456,38 @@ class Grid implements CoordinateSystemMaster { ); }); } + + function sortCategory( + data: List, + axis: Axis2D, + axisModel: CartesianAxisModel, + otherAxis: Axis2D + ): void { + const sort = axisModel.get('sort'); + if (axis.type === 'category' && sort) { + data.each(otherAxis.dim, value => { + for (let i = 0, len = sortedDataValue.length; i < len; ++i) { + if (value > sortedDataValue[i]) { + sortedDataValue.splice(i, 0, value as number); + + for (let j = 0; j <= len; ++j) { + if (sortedDataIndex[j] >= i) { + ++sortedDataIndex[j]; + } + } + sortedDataIndex.push(i); + console.log(sortedDataValue, sortedDataIndex); + return; + } + } + // Smallest for now, insert at the end + sortedDataValue.push(value as number); + sortedDataIndex.push(sortedDataIndex.length); + }); + console.log(sortedDataValue, sortedDataIndex); + (axis.scale as OrdinalScale).setSortedDataIndices(sortedDataIndex); + } + } } /** diff --git a/src/scale/Ordinal.ts b/src/scale/Ordinal.ts index f7057b7..b28b76c 100644 --- a/src/scale/Ordinal.ts +++ b/src/scale/Ordinal.ts @@ -39,6 +39,7 @@ class OrdinalScale extends Scale { readonly type = 'ordinal'; private _ordinalMeta: OrdinalMeta; + private _sortedDataIndices: number[]; constructor(setting?: { @@ -54,6 +55,7 @@ class OrdinalScale extends Scale { ordinalMeta = new OrdinalMeta({categories: ordinalMeta}); } this._ordinalMeta = ordinalMeta; + this._sortedDataIndices = []; this._extent = this.getSetting('extent') || [0, ordinalMeta.categories.length - 1]; } @@ -74,10 +76,12 @@ class OrdinalScale extends Scale { * Normalize given rank or name to linear [0, 1] */ normalize(val: OrdinalRawValue | OrdinalNumber): number { - return scaleHelper.normalize(this.parse(val), this._extent); + val = this.getSortedDataIndex(this.parse(val)); + return scaleHelper.normalize(val, this._extent); } scale(val: number): OrdinalNumber { + val = this.getSortedDataIndex(val); return Math.round(scaleHelper.scale(val, this._extent)); } @@ -99,6 +103,15 @@ class OrdinalScale extends Scale { return; } + getSortedDataIndex(n: OrdinalNumber): OrdinalNumber { + if (this._sortedDataIndices.length) { + return this._sortedDataIndices[n]; + } + else { + return n; + } + } + /** * Get item on rank n */ @@ -111,6 +124,10 @@ class OrdinalScale extends Scale { } } + setSortedDataIndices(index: number[]): void { + this._sortedDataIndices = index; + } + count(): number { return this._extent[1] - this._extent[0] + 1; } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
