diff --git a/src/helper.ts b/src/helper.ts index cfd319a..9504fbe 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -738,6 +738,10 @@ export const sortSeries = (props: { const legendNames = new Set(); let newData = [...data]; + // When seriesField exists, we need to group by dimension even without explicit sorting + const needGrouping = seriesField != null; + const commonSortFunc = getSortFuncByField(mainAxisName, dimensionField); + if (axisSortType) { const { axis, sortType } = axisSortType; const axisItem = Object.entries(dimensionMetricsMap).find(item => item[1].key === axis)!; @@ -747,7 +751,6 @@ export const sortSeries = (props: { const isDESC = sortType === 'DESC'; // x-axis sorting - const commonSortFunc = getSortFuncByField(mainAxisName, dimensionField); if (sortByXaxis) { newData = sortBy(newData, [commonSortFunc], false); } else { @@ -773,31 +776,35 @@ export const sortSeries = (props: { if (isDESC) { newData.reverse(); } - - // Percentage processing - if (isPercent) { - const sums: number[] = []; - // Summation - for (let i = 0; i < newData.length; i++) { - const sortList = newData[i]; - for (let j = 0; j < sortList.length; j++) { - if (sums[i] == null) { - sums[i] = 0; - } - sums[i] += sortList[j][yKey]; + } else if (needGrouping) { + // No explicit sorting, but we need to group by dimension for series processing + newData = sortBy(newData, [commonSortFunc], false); + } + + // Percentage processing + if (isPercent) { + const sums: number[] = []; + // Summation + for (let i = 0; i < newData.length; i++) { + const sortList = newData[i]; + for (let j = 0; j < sortList.length; j++) { + if (sums[i] == null) { + sums[i] = 0; } + sums[i] += sortList[j][yKey]; } - // Percentage processing - for (let i = 0; i < newData.length; i++) { - const sortList = newData[i]; - for (let j = 0; j < sortList.length; j++) { - const val = sortList[j][yKey]; - sortList[j][yKey] = safeParseNumberOrText(val / sums[i] * 100, 2); - } + } + // Percentage processing + for (let i = 0; i < newData.length; i++) { + const sortList = newData[i]; + for (let j = 0; j < sortList.length; j++) { + const val = sortList[j][yKey]; + sortList[j][yKey] = safeParseNumberOrText(val / sums[i] * 100, 2); } } + } - if (seriesField) { + if (seriesField) { // Direct reading of seriesField properties is problematic, // performance is too low, each chained call to a seriesField property takes between 20 - 40 milliseconds. let paramField = seriesField; @@ -891,7 +898,6 @@ export const sortSeries = (props: { sortedSeries: result.slice(0, maxRenderNum), max, }; - } } newData = newData.flat(); diff --git a/src/model/echarts_column.ts b/src/model/echarts_column.ts index 98d3655..4720507 100644 --- a/src/model/echarts_column.ts +++ b/src/model/echarts_column.ts @@ -201,7 +201,7 @@ export class EchartsColumn extends EchartsBase { ); const series: BarSeriesOption[] = []; - if (axisSortType && seriesFieldInstance) { + if (seriesFieldInstance) { const dataIndex = isColumn ? 0 : 1; const axisKey = isColumn ? 'xAxisIndex' : 'yAxisIndex'; const isNormal = this.stackType === StackType.None; diff --git a/src/model/echarts_line.ts b/src/model/echarts_line.ts index 85f0a71..0637f14 100644 --- a/src/model/echarts_line.ts +++ b/src/model/echarts_line.ts @@ -155,7 +155,7 @@ export class EchartsLine extends EchartsBase { }); const series: LineSeriesOption[] = []; - if (axisSortType && seriesFieldInstance) { + if (seriesFieldInstance) { for (let i = 0; i < sortedSeries.length; i++) { const item = sortedSeries[i]; series.push({ diff --git a/src/utils.ts b/src/utils.ts index d9ee095..bd53aad 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -90,7 +90,7 @@ export const safeParseNumberOrText = (num : number | string | undefined, precisi return ''; } return a.toFixed(precision); - +}; export const safeParseNumberOrTextWithSeparator = (num : number | string | undefined, precision: number) => {