Skip to content

Commit ebf25bf

Browse files
committed
Fix: Resolve redundant map labels on zoom and add regression test
1 parent 9994b01 commit ebf25bf

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,6 @@ NetJSON format used internally is based on [networkgraph](http://netjson.org/rfc
255255
- If set to `false`, labels are completely disabled and will never be shown.
256256
- If set to a number (e.g., `13`), labels will be shown when the map zoom level is greater than or equal to that value.
257257

258-
In graph mode, the overlapping labels are hidden automatically when zooming (use `showGraphLabelsAtZoom` for graph mode).
259-
260258
- `showGraphLabelsAtZoom`
261259

262260
Provide an explicit label-visibility threshold for graph mode (ECharts `graph`).

src/js/netjsongraph.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,15 @@ class NetJSONGraph {
3232
this.setupGraph();
3333
this.config.onInit.call(this.graph);
3434
this.initializeECharts();
35+
// Maintain backward compatibility with old config option "showLabelsAtZoomLevel"
36+
// TODO: remove in future versions
3537
if (
3638
this.config.showMapLabelsAtZoom === undefined &&
3739
this.config.showLabelsAtZoomLevel !== undefined
3840
) {
41+
console.warn(
42+
"showLabelsAtZoomLevel has been renamed to showMapLabelsAtZoom, please update your code accordingly.",
43+
);
3944
this.config.showMapLabelsAtZoom = this.config.showLabelsAtZoomLevel;
4045
}
4146
// eslint-disable-next-line no-constructor-return

src/js/netjsongraph.render.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class NetJSONGraphRender {
180180
const baseGraphSeries = {...configs.graphConfig.series};
181181
const baseGraphLabel = {...(baseGraphSeries.label || {})};
182182

183-
// Added this for label hover issue
183+
// Prevent redundant overlapping labels
184184
baseGraphLabel.silent = true;
185185

186186
// Shared helper to get current graph zoom level
@@ -577,7 +577,6 @@ class NetJSONGraphRender {
577577
}
578578

579579
self.echarts.on("mouseover", () => {
580-
// FIX: Removed the variable declaration. We use the one from upper scope.
581580
if (
582581
showMapLabelsAtZoom &&
583582
self.leaflet &&
@@ -619,13 +618,13 @@ class NetJSONGraphRender {
619618

620619
self.leaflet.on("zoomend", () => {
621620
const currentZoom = self.leaflet.getZoom();
622-
const show = showMapLabelsAtZoom && currentZoom >= showMapLabelsAtZoom;
621+
const showLabel = showMapLabelsAtZoom && currentZoom >= showMapLabelsAtZoom;
623622
self.echarts.setOption({
624623
series: [
625624
{
626625
id: "geo-map",
627626
label: {
628-
show,
627+
show: showLabel,
629628
silent: true,
630629
},
631630
emphasis: {

test/netjsongraph.browser.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,4 +361,31 @@ describe("Chart Rendering Test", () => {
361361
expect(consoleErrors.length).toBe(0);
362362
expect(canvas).not.toBeNull();
363363
});
364+
test("Regression: Labels should hide when zoomed out below showMapLabelsAtZoom", async () => {
365+
await driver.get(urls.geographicMap);
366+
await getElementByCss(driver, ".ec-extension-leaflet", 2000);
367+
368+
// FIX: Removed 'async' and 'await' to satisfy linter
369+
await driver.wait(
370+
() => driver.executeScript("return typeof window.graph !== 'undefined'"),
371+
5000,
372+
"Timed out waiting for window.graph to initialize",
373+
);
374+
375+
await driver.executeScript(`
376+
window.graph.config.showMapLabelsAtZoom = 12;
377+
window.graph.leaflet.setZoom(13);
378+
`);
379+
await driver.sleep(500);
380+
let isVisible = await driver.executeScript(
381+
"return window.graph.echarts.getOption().series[0].label.show;",
382+
);
383+
expect(isVisible).toBe(true);
384+
await driver.executeScript("window.graph.leaflet.setZoom(10);");
385+
await driver.sleep(500);
386+
isVisible = await driver.executeScript(
387+
"return window.graph.echarts.getOption().series[0].label.show;",
388+
);
389+
expect(isVisible).toBe(false);
390+
});
364391
});

0 commit comments

Comments
 (0)