diff --git a/core/core-frontend/src/locales/zh-CN.ts b/core/core-frontend/src/locales/zh-CN.ts index 775f11d110..a85fa55c7b 100644 --- a/core/core-frontend/src/locales/zh-CN.ts +++ b/core/core-frontend/src/locales/zh-CN.ts @@ -1165,7 +1165,9 @@ export default { top_n_input_2: ', 其余合并至其他', top_n_label: '其他项名称', progress_target: '目标值', - progress_current: '实际值' + progress_current: '实际值', + gauge_axis_label: '显示刻度', + gauge_percentage_tick: '百分比刻度' }, dataset: { scope_edit: '仅编辑时生效', diff --git a/core/core-frontend/src/models/chart/chart-attr.d.ts b/core/core-frontend/src/models/chart/chart-attr.d.ts index 1a86f4292e..4dd8778dac 100644 --- a/core/core-frontend/src/models/chart/chart-attr.d.ts +++ b/core/core-frontend/src/models/chart/chart-attr.d.ts @@ -104,6 +104,14 @@ declare interface ChartBasicStyle { * 仪表盘样式 */ gaugeStyle: string + /** + * 仪表盘刻度显示 + */ + gaugeAxisLine: boolean + /** + * 仪表盘百分比刻度 + */ + gaugePercentLabel: boolean /** * 配色方案 */ diff --git a/core/core-frontend/src/views/chart/components/editor/editor-senior/components/Threshold.vue b/core/core-frontend/src/views/chart/components/editor/editor-senior/components/Threshold.vue index 17e363c6ba..c7049ae8c9 100644 --- a/core/core-frontend/src/views/chart/components/editor/editor-senior/components/Threshold.vue +++ b/core/core-frontend/src/views/chart/components/editor/editor-senior/components/Threshold.vue @@ -231,7 +231,12 @@ init()
- + - + { /> + + + {{ t('chart.gauge_axis_label') }} + + + + {{ t('chart.gauge_percentage_tick') }} + { // 禁用线上地图数据 customFetchGeoData: () => null } - options = this.setupOptions(chart, options, drawOption, geoJson) + const context = { drawOption, geoJson } + options = this.setupOptions(chart, options, context) const view = new Choropleth(container, options) const dotLayer = this.getDotLayer(chart, geoJson, drawOption) this.configZoomButton(chart, view) @@ -170,10 +171,10 @@ export class BubbleMap extends L7PlotChartView { private configBasicStyle( chart: Chart, options: ChoroplethOptions, - extra: any[] + context: Record ): ChoroplethOptions { - const { areaId }: L7PlotDrawOptions = extra[0] - const geoJson: FeatureCollection = extra[1] + const { areaId }: L7PlotDrawOptions = context.drawOption + const geoJson: FeatureCollection = context.geoJson const { basicStyle, label } = parseJson(chart.customAttr) const senior = parseJson(chart.senior) const curAreaNameMapping = senior.areaMapping?.[areaId] @@ -208,7 +209,7 @@ export class BubbleMap extends L7PlotChartView { protected setupOptions( chart: Chart, options: ChoroplethOptions, - ...extra: any[] + context: Record ): ChoroplethOptions { return flow( this.configEmptyDataStrategy, @@ -216,6 +217,6 @@ export class BubbleMap extends L7PlotChartView { this.configStyle, this.configTooltip, this.configBasicStyle - )(chart, options, extra) + )(chart, options, context) } } diff --git a/core/core-frontend/src/views/chart/components/js/panel/charts/map/map.ts b/core/core-frontend/src/views/chart/components/js/panel/charts/map/map.ts index 69ecae76d9..586cf742d6 100644 --- a/core/core-frontend/src/views/chart/components/js/panel/charts/map/map.ts +++ b/core/core-frontend/src/views/chart/components/js/panel/charts/map/map.ts @@ -96,7 +96,8 @@ export class Map extends L7PlotChartView { // 禁用线上地图数据 customFetchGeoData: () => null } - options = this.setupOptions(chart, options, drawOption, geoJson) + const context = { drawOption, geoJson } + options = this.setupOptions(chart, options, context) const view = new Choropleth(container, options) this.configZoomButton(chart, view) view.once('loaded', () => { @@ -120,10 +121,10 @@ export class Map extends L7PlotChartView { private configBasicStyle( chart: Chart, options: ChoroplethOptions, - extra: any[] + context: Record ): ChoroplethOptions { - const { areaId }: L7PlotDrawOptions = extra[0] - const geoJson: FeatureCollection = extra[1] + const { areaId }: L7PlotDrawOptions = context.drawOption + const geoJson: FeatureCollection = context.geoJson const { basicStyle, label } = parseJson(chart.customAttr) const senior = parseJson(chart.senior) const curAreaNameMapping = senior.areaMapping?.[areaId] @@ -176,7 +177,7 @@ export class Map extends L7PlotChartView { protected setupOptions( chart: Chart, options: ChoroplethOptions, - ...extra: any[] + context: Record ): ChoroplethOptions { return flow( this.configEmptyDataStrategy, @@ -185,6 +186,6 @@ export class Map extends L7PlotChartView { this.configTooltip, this.configBasicStyle, this.configLegend - )(chart, options, extra) + )(chart, options, context) } } 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 d89051a874..e5242809a4 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 @@ -13,6 +13,7 @@ import { import { valueFormatter } from '@/views/chart/components/js/formatter' import { getPadding, setGradientColor } from '@/views/chart/components/js/panel/common/common_antv' import { useI18n } from '@/hooks/web/useI18n' +import { merge } from 'lodash-es' const { t } = useI18n() @@ -28,7 +29,7 @@ export class Gauge extends G2PlotChartView { ] propertyInner: EditorPropertyInner = { 'background-overall-component': ['all'], - 'basic-style-selector': ['colors', 'alpha', 'gaugeStyle', 'gradient'], + 'basic-style-selector': ['colors', 'alpha', 'gradient', 'gaugeAxisLine', 'gaugePercentLabel'], 'label-selector': ['fontSize', 'color', 'labelFormatter'], 'title-selector': [ 'title', @@ -77,10 +78,6 @@ export class Gauge extends G2PlotChartView { label: { style: { fontSize: getScaleValue(12, scale) // 刻度值字体大小 - }, - formatter: function (v) { - const r = parseFloat(v) - return v === '0' || !r ? v : r * 100 + '%' } }, tickLine: { @@ -98,11 +95,15 @@ export class Gauge extends G2PlotChartView { } } } - const options = this.setupOptions(chart, initOptions, scale) + const options = this.setupOptions(chart, initOptions, { scale }) return new G2Gauge(container, options) } - protected configMisc(chart: Chart, options: GaugeOptions): GaugeOptions { + protected configMisc( + chart: Chart, + options: GaugeOptions, + context: Record + ): GaugeOptions { const customAttr = parseJson(chart.customAttr) const data = chart.data.series[0].data[0] let min, max, startAngle, endAngle @@ -123,6 +124,8 @@ export class Gauge extends G2PlotChartView { } startAngle = (misc.gaugeStartAngle * Math.PI) / 180 endAngle = (misc.gaugeEndAngle * Math.PI) / 180 + context.min = min + context.max = max } const percent = (parseFloat(data) - parseFloat(min)) / (parseFloat(max) - parseFloat(min)) const tmp = { @@ -133,8 +136,12 @@ export class Gauge extends G2PlotChartView { return { ...options, ...tmp } } - private configRange(chart: Chart, options: GaugeOptions, extra: any[]): GaugeOptions { - const [scale] = extra + private configRange( + chart: Chart, + options: GaugeOptions, + context: Record + ): GaugeOptions { + const { scale } = context const range = [0] let index = 0 let flag = false @@ -216,36 +223,55 @@ export class Gauge extends G2PlotChartView { return { ...options, ...rangOptions } } - protected configLabel(chart: Chart, options: GaugeOptions): GaugeOptions { + protected configLabel( + chart: Chart, + options: GaugeOptions, + context?: Record + ): GaugeOptions { const customAttr = parseJson(chart.customAttr) const data = chart.data.series[0].data[0] - let labelContent - if (customAttr.label) { - const label = customAttr.label - const labelFormatter = label.labelFormatter ?? DEFAULT_LABEL.labelFormatter - if (label.show) { - labelContent = { - style: () => ({ - fontSize: label.fontSize, - color: label.color - }), - formatter: function () { - let value - if (labelFormatter.type === 'percent') { - value = options.percent - } else { - value = data - } - return valueFormatter(value, labelFormatter) + let labelContent: GaugeOptions['statistic']['content'] = false + const label = customAttr.label + const labelFormatter = label.labelFormatter ?? DEFAULT_LABEL.labelFormatter + if (label.show) { + labelContent = { + style: { + fontSize: `${label.fontSize}`, + color: label.color + }, + formatter: function () { + let value + if (labelFormatter.type === 'percent') { + value = options.percent + } else { + value = data } + return valueFormatter(value, labelFormatter) } - } else { - labelContent = false - } + } as GaugeOptions['statistic']['content'] } const statistic = { content: labelContent } + const { gaugeAxisLine, gaugePercentLabel } = customAttr.basicStyle + const { min, max } = context + const tmp = { + axis: { + label: { + formatter: v => { + if (gaugeAxisLine === false) { + return '' + } + if (gaugePercentLabel === false) { + const val = v === '0' ? min : v === '1' ? max : min + (max - min) * v + return valueFormatter(val, labelFormatter) + } + return v === '0' ? v : v * 100 + '%' + } + } + } + } + options = merge(options, tmp) return { ...options, statistic } } @@ -263,13 +289,17 @@ export class Gauge extends G2PlotChartView { return chart } - protected setupOptions(chart: Chart, options: GaugeOptions, ...extra: any[]): GaugeOptions { + protected setupOptions( + chart: Chart, + options: GaugeOptions, + context: Record + ): GaugeOptions { return flow( this.configTheme, this.configMisc, this.configLabel, this.configRange - )(chart, options, extra) + )(chart, options, context) } constructor() { super('gauge', DEFAULT_DATA) 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 22191b7482..75b5271902 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 @@ -139,7 +139,7 @@ export abstract class G2PlotChartView< * @param chart 数据库图表对象。 * @param options 各个图表的参数,泛化的 Options,可以自行扩展,比如加个扩展 X 轴或者扩展 Y 轴字段。 */ - protected abstract setupOptions(chart: Chart, options: O): O + protected abstract setupOptions(chart: Chart, options: O, context?: Record): O protected constructor(name: string, defaultData: any[]) { super(ChartLibraryType.G2_PLOT, name, defaultData) } diff --git a/core/core-frontend/src/views/chart/components/js/panel/types/impl/l7plot.ts b/core/core-frontend/src/views/chart/components/js/panel/types/impl/l7plot.ts index e308670c91..6e65dea0b7 100644 --- a/core/core-frontend/src/views/chart/components/js/panel/types/impl/l7plot.ts +++ b/core/core-frontend/src/views/chart/components/js/panel/types/impl/l7plot.ts @@ -81,5 +81,5 @@ export abstract class L7PlotChartView< super(ChartLibraryType.L7_PLOT, name) this.defaultData = defaultData } - protected abstract setupOptions(chart: Chart, options: O): O + protected abstract setupOptions(chart: Chart, options: O, context?: Record): O } 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 71f11fe10b..4c5fe2928b 100644 --- a/core/core-frontend/src/views/chart/components/js/util.ts +++ b/core/core-frontend/src/views/chart/components/js/util.ts @@ -388,12 +388,12 @@ export function parseJson(str: T | JSONString): T { return JSON.parse(str) as T } -type FlowFunction = (param: P, result: R, extra?: any[]) => R +type FlowFunction = (param: P, result: R, context?: Record) => R export function flow(...flows: FlowFunction[]): FlowFunction { - return (param: P, result: R, extra?: any[]) => { + return (param: P, result: R, context?: Record) => { return flows.reduce((result: R, flow: FlowFunction) => { - return flow(param, result, extra) + return flow(param, result, context) }, result) } }