From 747947370d4f9b59dfee97e73704b4018e668d87 Mon Sep 17 00:00:00 2001 From: Resbi Anik Date: Mon, 30 Nov 2026 19:05:56 +0600 Subject: [PATCH] fix(visualMap): show a single handle label when both handles have the same value When the two handle labels of a calculable continuous visualMap overlap and display the same text (the same value, or values formatted identically by `precision`/`formatter`), show only one label centered on the handles instead of two duplicated labels. The single label drags both handles together. Labels that are far apart keep two labels even if their text is the same. Add unit tests and a visual test case. Close #21884 --- src/component/visualMap/ContinuousView.ts | 20 ++++ .../visualMap/continuousHandleLabel.test.ts | 102 ++++++++++++++++++ test/visualMap-continuous.html | 29 +++++ 3 files changed, 151 insertions(+) create mode 100644 test/ut/spec/component/visualMap/continuousHandleLabel.test.ts diff --git a/src/component/visualMap/ContinuousView.ts b/src/component/visualMap/ContinuousView.ts index 939707b5c8..bc50864495 100644 --- a/src/component/visualMap/ContinuousView.ts +++ b/src/component/visualMap/ContinuousView.ts @@ -691,6 +691,26 @@ class ContinuousView extends VisualMapView { bidirectional: false, } ); + + // When the two labels overlap and display the same text (the same value, or values + // rendered identically after `precision`/`formatter`), showing two identical labels + // is redundant. Show only one label at the middle of the two handles instead. + const showSingleLabel = labelsOverlap && handleLabels[0].style.text === handleLabels[1].style.text; + handleLabels[1].attr({ + invisible: showSingleLabel, + silent: showSingleLabel + }); + if (showSingleLabel) { + handleLabels[0].setStyle({ + x: (textPosPair[0].x + textPosPair[1].x) / 2, + y: (textPosPair[0].y + textPosPair[1].y) / 2 + }); + // The two handles are at (almost) the same position, so the bar is hard to hit. + // Let the single label drag both handles together. + elInner(handleLabels[0]).hdlIdx = 'all'; + return; + } + if (labelsOverlap) { textPosPair[0].scaleAndAdd(mtv, -0.5); textPosPair[1].scaleAndAdd(mtv, 0.5); diff --git a/test/ut/spec/component/visualMap/continuousHandleLabel.test.ts b/test/ut/spec/component/visualMap/continuousHandleLabel.test.ts new file mode 100644 index 0000000000..4b26e35310 --- /dev/null +++ b/test/ut/spec/component/visualMap/continuousHandleLabel.test.ts @@ -0,0 +1,102 @@ +/* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +*/ + +import { createChart, getECModel } from '../../../core/utHelper'; +import { EChartsType } from '../../../../../src/echarts'; +import type { Text } from '../../../../../src/util/graphic'; + +describe('visualMap/continuous#handleLabel', function () { + + let chart: EChartsType; + beforeEach(function () { + chart = createChart(); + }); + + afterEach(function () { + chart.dispose(); + }); + + function getHandleLabels(): Text[] { + const vmModel = getECModel(chart).getComponent('visualMap', 0); + const view = (chart as any).getViewOfComponentModel(vmModel); + return view._shapes.handleLabels; + } + + function setRange(orient: 'horizontal' | 'vertical', range: number[], precision?: number, formatter?: any) { + chart.setOption({ + visualMap: { + type: 'continuous', + min: 0, + max: 100, + orient, + calculable: true, + precision, + formatter, + range + }, + xAxis: { type: 'category', data: ['a', 'b', 'c'] }, + yAxis: { type: 'category', data: ['x', 'y'] }, + series: [{ + type: 'heatmap', + data: [['a', 'x', 10], ['b', 'y', 50], ['c', 'x', 90]] + }] + }); + } + + (['horizontal', 'vertical'] as const).forEach(orient => { + + it(`shows_single_label_when_values_are_same_${orient}`, function () { + setRange(orient, [50, 50]); + const labels = getHandleLabels(); + expect(labels[0].invisible).toBeFalsy(); + expect(labels[0].style.text).toEqual('50'); + expect(labels[1].invisible).toBe(true); + expect(labels[1].silent).toBe(true); + }); + + it(`shows_single_label_when_formatted_values_are_same_${orient}`, function () { + // 50.2 and 50.4 are both displayed as '50' with precision 0. + setRange(orient, [50.2, 50.4], 0); + const labels = getHandleLabels(); + expect(labels[0].invisible).toBeFalsy(); + expect(labels[1].invisible).toBe(true); + }); + + it(`keeps_two_labels_when_same_text_but_far_apart_${orient}`, function () { + setRange(orient, [20, 80], undefined, () => 'x'); + const labels = getHandleLabels(); + expect(labels[0].style.text).toEqual('x'); + expect(labels[1].style.text).toEqual('x'); + expect(labels[0].invisible).toBeFalsy(); + expect(labels[1].invisible).toBeFalsy(); + }); + + it(`shows_two_labels_again_when_values_differ_${orient}`, function () { + setRange(orient, [50, 50]); + setRange(orient, [20, 80]); + const labels = getHandleLabels(); + expect(labels[0].invisible).toBeFalsy(); + expect(labels[1].invisible).toBeFalsy(); + expect(labels[1].silent).toBeFalsy(); + expect(labels[0].style.text).toEqual('20'); + expect(labels[1].style.text).toEqual('80'); + }); + + }); +}); diff --git a/test/visualMap-continuous.html b/test/visualMap-continuous.html index 23bb6d22be..f1ecf0910a 100644 --- a/test/visualMap-continuous.html +++ b/test/visualMap-continuous.html @@ -703,6 +703,35 @@ }); }); + createCase("Single handle label when both handles show the same text", 8, (chart) => { + // Expected: each visualMap marked "single" shows only one label centered on the handles; + // the one marked "two labels" keeps both labels. + const cases = [ + {title: 'single: range [50, 50]', range: [50, 50]}, + {title: 'single: precision 0, range [50.2, 50.4]', range: [50.2, 50.4], precision: 0}, + {title: 'two labels: formatter () => "x", range [20, 80]', range: [20, 80], formatter: () => 'x'}, + {title: 'single (vertical): range [50, 50]', range: [50, 50], orient: 'vertical', left: 650, top: 120} + ]; + chart.setOption({ + title: cases.map((c, i) => ({ + text: c.title, left: c.left != null ? c.left - 50 : 20, top: c.top != null ? 60 : 30 + i * 120, + textStyle: {fontSize: 12} + })), + visualMap: cases.map((c, i) => ({ + type: 'continuous', min: 0, max: 100, calculable: true, itemHeight: 300, + orient: c.orient || 'horizontal', + left: c.left != null ? c.left : 80, + top: c.top != null ? c.top : 60 + i * 120, + range: c.range, precision: c.precision, formatter: c.formatter, + seriesIndex: 0 + })), + xAxis: {show: false}, + yAxis: {show: false}, + grid: {width: 1, height: 1, left: 0, top: 0}, + series: [{type: 'scatter', data: [[1, 1]]}] + }); + }); + createCase("Merge handles on overlap (default formatter)", 6, (chart) => { const data0 = []; const MAX_DIM1 = 100;