From 4db73f194e262f15edc0de68d34d3cbc5b2ff320 Mon Sep 17 00:00:00 2001 From: jianneng-fit2cloud Date: Mon, 17 Feb 2025 13:19:49 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E5=9B=BE=E8=A1=A8):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E4=BB=AA=E8=A1=A8=E7=9B=98=E3=80=81=E6=B0=B4=E6=B3=A2=E5=9B=BE?= =?UTF-8?q?=E9=81=87=E7=A9=BA=E6=95=B0=E6=8D=AE=E6=8C=87=E6=A0=87=E6=97=B6?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../js/panel/charts/liquid/liquid.ts | 6 ++++ .../js/panel/charts/others/gauge.ts | 5 +++ .../components/js/panel/common/common_antv.ts | 34 +++++++++++++++++++ .../components/js/panel/types/impl/g2plot.ts | 7 +++- 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/core/core-frontend/src/views/chart/components/js/panel/charts/liquid/liquid.ts b/core/core-frontend/src/views/chart/components/js/panel/charts/liquid/liquid.ts index acf61b3e6c..52e87c3199 100644 --- a/core/core-frontend/src/views/chart/components/js/panel/charts/liquid/liquid.ts +++ b/core/core-frontend/src/views/chart/components/js/panel/charts/liquid/liquid.ts @@ -72,6 +72,12 @@ export class Liquid extends G2PlotChartView { } }) }) + // 处理空数据, 只要有一个指标是空数据,就不显示图表 + const hasNoneData = chart.data?.series.some(s => !s.data?.[0]) + this.configEmptyDataStyle(newChart, hasNoneData ? [] : [1], container) + if (hasNoneData) { + return + } return newChart } diff --git a/core/core-frontend/src/views/chart/components/js/panel/charts/others/gauge.ts b/core/core-frontend/src/views/chart/components/js/panel/charts/others/gauge.ts index e2faaaa49e..e6928d529e 100644 --- a/core/core-frontend/src/views/chart/components/js/panel/charts/others/gauge.ts +++ b/core/core-frontend/src/views/chart/components/js/panel/charts/others/gauge.ts @@ -109,6 +109,11 @@ export class Gauge extends G2PlotChartView { } }) }) + const hasNoneData = chart.data?.series.some(s => !s.data?.[0]) + this.configEmptyDataStyle(newChart, hasNoneData ? [] : [1], container) + if (hasNoneData) { + return + } return newChart } diff --git a/core/core-frontend/src/views/chart/components/js/panel/common/common_antv.ts b/core/core-frontend/src/views/chart/components/js/panel/common/common_antv.ts index f2589eff8e..fa77addb96 100644 --- a/core/core-frontend/src/views/chart/components/js/panel/common/common_antv.ts +++ b/core/core-frontend/src/views/chart/components/js/panel/common/common_antv.ts @@ -1922,3 +1922,37 @@ export const getTooltipItemConditionColor = item => { } return color } + +/** + * 配置空数据样式 + * @param newChart + * @param newData + * @param container + */ +export const configEmptyDataStyle = (newChart, newData, container) => { + /** + * 辅助函数:移除空数据dom + */ + const removeEmptyDom = () => { + const emptyElement = document.getElementById(container + '_empty') + if (emptyElement) { + emptyElement.parentElement.removeChild(emptyElement) + } + } + removeEmptyDom() + if (newData.length > 0) return + if (!newData.length) { + const emptyDom = document.createElement('div') + emptyDom.id = container + '_empty' + emptyDom.textContent = tI18n('data_set.no_data') + emptyDom.setAttribute( + 'style', + `position: absolute; + left: 45%; + top: 50%;` + ) + const parent = document.getElementById(container) + parent.insertBefore(emptyDom, parent.firstChild) + newChart.destroy() + } +} diff --git a/core/core-frontend/src/views/chart/components/js/panel/types/impl/g2plot.ts b/core/core-frontend/src/views/chart/components/js/panel/types/impl/g2plot.ts index b4939e0bab..918d135953 100644 --- a/core/core-frontend/src/views/chart/components/js/panel/types/impl/g2plot.ts +++ b/core/core-frontend/src/views/chart/components/js/panel/types/impl/g2plot.ts @@ -13,7 +13,8 @@ import { getYAxis, getConditions, handleConditionsStyle, - addConditionsStyleColorToData + addConditionsStyleColorToData, + configEmptyDataStyle } from '@/views/chart/components/js/panel/common/common_antv' import { AntVAbstractChartView, @@ -190,6 +191,10 @@ export abstract class G2PlotChartView< return addConditionsStyleColorToData(chart, data) } + protected configEmptyDataStyle(newChart, newData: any[], container: string) { + configEmptyDataStyle(newChart, newData, container) + } + /** * 流式配置公共参数,处理常用的配置,后续如果有其他通用配置也可以放进来,需要单独配置的属性在各个图表自行实现。 * @param chart 数据库图表对象。