Skip to content

[feature] Add option to show node labels only after hitting a certain zoom level#419

Merged
nemesifier merged 31 commits intoopenwisp:masterfrom
cestercian:fix-zoom-labels
Sep 18, 2025
Merged

[feature] Add option to show node labels only after hitting a certain zoom level#419
nemesifier merged 31 commits intoopenwisp:masterfrom
cestercian:fix-zoom-labels

Conversation

@cestercian
Copy link
Copy Markdown
Contributor

@cestercian cestercian commented Aug 21, 2025

Node labels now prefer label, then name, then id for display. Added dynamic label visibility in graph mode based on zoom scale, controlled by graphLabelScaleThreshold and scale limits, with debounce to avoid excessive updates.

Checklist

  • I have read the OpenWISP Contributing Guidelines.
  • I have manually tested the changes proposed in this pull request.
  • I have written new test cases for new code and/or updated existing tests for changes to existing code.
  • I have updated the documentation.

Reference to Existing Issue

Closes #148.

Description of Changes

  • Improved node label selection logic:
    • Prefer node.label → fallback to node.name → fallback to node.id.
  • Introduced dynamic label visibility in graph mode:
    • Controlled by graphLabelScaleThreshold (default: 1.0).
    • Respects scale limits defined in config.
    • Uses debounce to prevent excessive updates.
    • Ensures zoom/roam transform is not reset.
  • Added resize handling to maintain responsiveness.

Screenshot

image image

Node labels now prefer 'label', then 'name', then 'id' for display. Added dynamic label visibility in graph mode based on zoom scale, controlled by 'graphLabelScaleThreshold' and scale limits, with debounce to avoid excessive updates.
Copy link
Copy Markdown
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good progress on this @cestercian, it looks encouraging!

Here's a few details I have observed.

Labels are visible on initial page load with default settings

When I load the basic example, the labels are visible:

labels

Consider a situation like the following, this is a screenshot from a prod system taken right after page load:

Screenshot from 2025-08-21 15-37-56

On real world systems, when the chart is busy, it doesn't make sense to show the labels after the page loads, it would be better to show the only after a certain level of zoom.

Can we configure the default setting of the library so that this happens automatically?

The users will still be able to tweak the settings if needed.

I expanded these concepts in #420, which we can work on at a later stage.

Comment thread src/js/netjsongraph.render.js Outdated
Simplifies label visibility logic by using a label formatter that checks the current zoom level, instead of dynamically toggling label show/hide. Updates the example template to set showGraphLabelsAtZoom to 2 for clearer demonstration.
Simplifies node name assignment logic for clarity and consistency. Replaces Object.assign with spread syntax for cloning objects and updating series configuration, improving readability and maintainability.
@cestercian
Copy link
Copy Markdown
Contributor Author

Screen.Recording.2025-08-22.at.4.14.15.AM.mov

Hi @nemesifier — I’ve addressed your changes and fixed a related bug.

Graph Mode

  • Added showGraphLabelsAtZoom to gate labels by series zoom without resets.
  • Switched label visibility to a formatter; no more setOption-driven snap-backs.
  • Introduced stable series IDs (graph-series) and use series-only updates.

Labels

  • Fallback to node.name or String(node.id) when node.label is missing.

Introduces tests to verify graph and map label visibility logic, including zoom-based label toggling and fallback to name/label/id. Also adds tests for stable series id assignment and event handler registration for resize and label toggling.
@cestercian cestercian requested a review from nemesifier August 21, 2025 23:29
Copy link
Copy Markdown
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a lot better thank you!

However, I noticed that the graph starts moving (expanding) again when the labels are shown, see:

graph-moves

@cestercian cestercian changed the base branch from master to fix-graph-zoom-labels August 23, 2025 11:57
Simplifies node name assignment to use only the label property and refactors echarts option updates to set the entire option object instead of just the series. Also replaces object spread with Object.assign for link config in map options.
@cestercian cestercian changed the base branch from fix-graph-zoom-labels to master August 25, 2025 18:36
Node names are now determined by checking 'label', then 'name', and finally 'id' if neither is available. This ensures that nodes always have a meaningful name when rendered on the map.
Clarifies the behavior of the label-visibility threshold for graph mode in the README and updates the default value of `showGraphLabelsAtZoom` to 1 in the configuration. This makes label display more predictable and documents how to disable thresholding.
Simplified code formatting in netjsongraph.render.js for better readability and consistency. Updated test cases in netjsongraph.render.test.js to use more concise array and function calls, improving clarity and maintainability.
@cestercian cestercian added the gsoc-idea Potential step of a GSoC project idea label Sep 8, 2025
Comment thread src/js/netjsongraph.config.js Outdated
Comment thread src/js/netjsongraph.render.js Outdated
Comment thread src/js/netjsongraph.render.js Outdated
Comment thread README.md
Comment thread README.md Outdated
Comment thread src/js/netjsongraph.render.js Outdated
Changed ECharts series IDs from 'graph-series' and 'map-nodes' to 'network-graph' and 'geo-map' for consistency. Improved label visibility threshold logic for graph mode: now disabled by default and only enabled when a positive value is set. Updated related tests and documentation to reflect these changes.
Copy link
Copy Markdown
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cestercian let's proceed with the improvements discussed today, see my comments below for further input.

Comment thread README.md Outdated
Comment thread src/js/netjsongraph.render.js Outdated
Comment thread src/js/netjsongraph.render.js Outdated
Simplifies the logic for toggling graph labels based on zoom threshold by extracting zoom retrieval into a helper function and removing redundant try-catch blocks. Labels are now toggled only when crossing the threshold during zoom events, ignoring pan.
Simplified the label visibility logic in graph mode by introducing a shared helper for retrieving the current zoom level. Updated documentation to reflect the new default threshold value for label visibility.
Refactored the label display logic to directly check the zoom value from the first series in the ECharts option, removing unnecessary helper functions and improving readability.
Replaces usage of _labelsShown with LabelShown to ensure correct tracking of label visibility state in NetJSONGraphRender. This resolves potential issues with label display logic.
This reverts commit 9a3460d.
[chores] Revert Reduced Redundancy

This reverts commit a03cacc.
Copy link
Copy Markdown
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the latest changes the graph moves all the time, not just when the threshold is crossed.

PS: false alarm.

nemesifier
nemesifier previously approved these changes Sep 18, 2025
@nemesifier nemesifier merged commit 094d123 into openwisp:master Sep 18, 2025
4 of 6 checks passed
@nemesifier nemesifier changed the title [fix] Improve node label selection and dynamic label visibility [feature] Add option to show node labels only after hitting a certain zoom level Sep 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement gsoc-idea Potential step of a GSoC project idea

Projects

No open projects

Development

Successfully merging this pull request may close these issues.

[change] Show node labels only after hitting a certain zoom level

2 participants