From 70695eea89d0f3dbd62254c5b618fd8bfef5fe73 Mon Sep 17 00:00:00 2001 From: wisonic Date: Thu, 15 Jan 2026 20:39:01 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E5=9B=BE=E8=A1=A8):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=A0=86=E5=8F=A0=E6=9F=B1=E6=9D=A1=E5=9B=BE=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E5=9B=BE=E4=BE=8B=E9=A1=BA=E5=BA=8F=E5=92=8C=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E6=8E=92=E5=BA=8F=E6=95=88=E6=9E=9C=E5=86=B2?= =?UTF-8?q?=E7=AA=81=20#17649?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/js/panel/charts/bar/bar.ts | 72 ++++++++++--------- .../js/panel/charts/bar/horizontal-bar.ts | 72 ++++++++++--------- .../src/views/chart/components/js/util.ts | 4 ++ 3 files changed, 79 insertions(+), 69 deletions(-) diff --git a/core/core-frontend/src/views/chart/components/js/panel/charts/bar/bar.ts b/core/core-frontend/src/views/chart/components/js/panel/charts/bar/bar.ts index 9ffdde256f..1ccaeb6ff4 100644 --- a/core/core-frontend/src/views/chart/components/js/panel/charts/bar/bar.ts +++ b/core/core-frontend/src/views/chart/components/js/panel/charts/bar/bar.ts @@ -497,6 +497,43 @@ export class StackBar extends Bar { } protected configColor(chart: Chart, options: ColumnOptions): ColumnOptions { + const customStyle = parseJson(chart.customStyle) + const { sort } = customStyle.legend + const extStack = chart.extStack[0] + if ((!sort || sort === 'none') && extStack?.customSort?.length > 0) { + // 图例自定义排序 + const sort = extStack.customSort ?? [] + if (sort?.length) { + // 用值域限定排序,有可能出现新数据但是未出现在图表上,所以这边要遍历一下子维度,加到后面,让新数据显示出来 + const data = options.data + const cats = + data?.reduce((p, n) => { + const cat = n['category'] + if (cat && !p.includes(cat)) { + p.push(cat) + } + return p + }, []) || [] + const values = sort.reduce((p, n) => { + if (cats.includes(n)) { + const index = cats.indexOf(n) + if (index !== -1) { + cats.splice(index, 1) + } + p.push(n) + } + return p + }, []) + cats.length > 0 && values.push(...cats) + options.meta = { + ...options.meta, + category: { + type: 'cat', + values + } + } + } + } return this.configStackColor(chart, options) } @@ -533,41 +570,6 @@ export class StackBar extends Bar { return optionTmp } const extStack = chart.extStack[0] - if (extStack?.customSort?.length > 0) { - // 图例自定义排序 - const sort = extStack.customSort ?? [] - if (sort?.length) { - // 用值域限定排序,有可能出现新数据但是未出现在图表上,所以这边要遍历一下子维度,加到后面,让新数据显示出来 - const data = optionTmp.data - const cats = - data?.reduce((p, n) => { - const cat = n['category'] - if (cat && !p.includes(cat)) { - p.push(cat) - } - return p - }, []) || [] - const values = sort.reduce((p, n) => { - if (cats.includes(n)) { - const index = cats.indexOf(n) - if (index !== -1) { - cats.splice(index, 1) - } - p.push(n) - } - return p - }, []) - cats.length > 0 && values.push(...cats) - optionTmp.meta = { - ...optionTmp.meta, - category: { - type: 'cat', - values - } - } - } - } - const customStyle = parseJson(chart.customStyle) let size if (customStyle && customStyle.legend) { diff --git a/core/core-frontend/src/views/chart/components/js/panel/charts/bar/horizontal-bar.ts b/core/core-frontend/src/views/chart/components/js/panel/charts/bar/horizontal-bar.ts index 616762af07..7ab6b693f5 100644 --- a/core/core-frontend/src/views/chart/components/js/panel/charts/bar/horizontal-bar.ts +++ b/core/core-frontend/src/views/chart/components/js/panel/charts/bar/horizontal-bar.ts @@ -445,6 +445,44 @@ export class HorizontalStackBar extends HorizontalBar { } } protected configColor(chart: Chart, options: BarOptions): BarOptions { + const customStyle = parseJson(chart.customStyle) + const { sort } = customStyle.legend + const extStack = chart.extStack[0] + if ((!sort || sort === 'none') && extStack?.customSort?.length > 0) { + // 图例自定义排序 + const sort = extStack.customSort ?? [] + if (sort?.length) { + // 用值域限定排序,有可能出现新数据但是未出现在图表上,所以这边要遍历一下子维度,加到后面,让新数据显示出来 + const data = options.data + const cats = + data?.reduce((p, n) => { + const cat = n['category'] + if (cat && !p.includes(cat)) { + p.push(cat) + } + return p + }, []) || [] + const values = sort.reduce((p, n) => { + if (cats.includes(n)) { + const index = cats.indexOf(n) + if (index !== -1) { + cats.splice(index, 1) + } + p.push(n) + } + return p + }, []) + cats.length > 0 && values.push(...cats) + options.meta = { + ...options.meta, + category: { + type: 'cat', + values + } + } + } + } + return this.configStackColor(chart, options) } public setupSeriesColor(chart: ChartObj, data?: any[]): ChartBasicStyle['seriesColor'] { @@ -484,40 +522,6 @@ export class HorizontalStackBar extends HorizontalBar { return optionTmp } const extStack = chart.extStack[0] - if (extStack?.customSort?.length > 0) { - // 图例自定义排序 - const sort = extStack.customSort ?? [] - if (sort?.length) { - // 用值域限定排序,有可能出现新数据但是未出现在图表上,所以这边要遍历一下子维度,加到后面,让新数据显示出来 - const data = optionTmp.data - const cats = - data?.reduce((p, n) => { - const cat = n['category'] - if (cat && !p.includes(cat)) { - p.push(cat) - } - return p - }, []) || [] - const values = sort.reduce((p, n) => { - if (cats.includes(n)) { - const index = cats.indexOf(n) - if (index !== -1) { - cats.splice(index, 1) - } - p.push(n) - } - return p - }, []) - cats.length > 0 && values.push(...cats) - optionTmp.meta = { - ...optionTmp.meta, - category: { - type: 'cat', - values - } - } - } - } const customStyle = parseJson(chart.customStyle) let size diff --git a/core/core-frontend/src/views/chart/components/js/util.ts b/core/core-frontend/src/views/chart/components/js/util.ts index f20a5fe9a2..9865e95aba 100644 --- a/core/core-frontend/src/views/chart/components/js/util.ts +++ b/core/core-frontend/src/views/chart/components/js/util.ts @@ -932,6 +932,10 @@ export function getStackColor(chart: Chart, opt const seriesSet = new Set() data?.forEach(d => d.category !== null && seriesSet.add(d.category)) const tmp = [...seriesSet] + const values = options.meta?.category?.values + if (values?.length) { + tmp.sort((a, b) => values.indexOf(a) - values.indexOf(b)) + } tmp.forEach((c, i) => { const curAxisColor = seriesMap[c as string] if (curAxisColor) {