Skip to content

Commit a83c2ee

Browse files
[feature] Show node labels only after a suitable zoom level #148
Closes #148
1 parent 441c123 commit a83c2ee

File tree

5 files changed

+51
-13
lines changed

5 files changed

+51
-13
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ NetJSON format used internally is based on [networkgraph](http://netjson.org/rfc
9898

9999
Whether to allow switching between graph and map render or not. You can also set it `true` to enable it.
100100

101+
- `showLabelsAtZoomLevel`
102+
103+
**Default**: `7`
104+
105+
The zoom level at which the labels are shown. This only works when `render` is set to `map`.
106+
In graph mode, the overlapping labels are hidden automatically when zooming.
107+
101108
- `dealDataByWorker`
102109

103110
The url to the worker file if you want to deal the data by a worker.

dist/netjsongraph.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/netjsongraph.config.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const NetJSONGraphDefaultConfig = {
3333
svgRender: false,
3434
switchMode: false,
3535
showMetaOnNarrowScreens: false,
36+
showLabelsAtZoomLevel: 7,
3637
echartsOption: {
3738
aria: {
3839
show: true,
@@ -65,6 +66,9 @@ const NetJSONGraphDefaultConfig = {
6566
color: "#fff",
6667
position: "top",
6768
},
69+
labelLayout: {
70+
hideOverlap: true,
71+
},
6872
force: {
6973
gravity: 0.1,
7074
edgeLength: [20, 60],
@@ -101,9 +105,6 @@ const NetJSONGraphDefaultConfig = {
101105
series: [
102106
{
103107
zoom: 0.7,
104-
labelLayout: {
105-
hideOverlap: true,
106-
},
107108
},
108109
],
109110
toolbox: {
@@ -119,9 +120,6 @@ const NetJSONGraphDefaultConfig = {
119120
series: [
120121
{
121122
zoom: 1,
122-
labelLayout: {
123-
hideOverlap: false,
124-
},
125123
},
126124
],
127125
toolbox: {

src/js/netjsongraph.render.js

+36
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,42 @@ class NetJSONGraphRender {
356356
);
357357
}
358358

359+
if (self.leaflet.getZoom() < self.config.showLabelsAtZoomLevel) {
360+
self.echarts.setOption({
361+
series: [
362+
{
363+
label: {
364+
show: false,
365+
},
366+
},
367+
],
368+
});
369+
}
370+
371+
self.leaflet.on("zoomend", () => {
372+
if (self.leaflet.getZoom() >= self.config.showLabelsAtZoomLevel) {
373+
self.echarts.setOption({
374+
series: [
375+
{
376+
label: {
377+
show: true,
378+
},
379+
},
380+
],
381+
});
382+
} else {
383+
self.echarts.setOption({
384+
series: [
385+
{
386+
label: {
387+
show: false,
388+
},
389+
},
390+
],
391+
});
392+
}
393+
});
394+
359395
self.event.emit("onLoad");
360396
self.event.emit("onReady");
361397
self.event.emit("renderArray");

test/netjsongraph.spec.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ describe("NetJSONGraph Specification", () => {
3232
color: "#fff",
3333
position: "top",
3434
},
35+
labelLayout: {
36+
hideOverlap: true,
37+
},
3538
force: {
3639
gravity: 0.1,
3740
edgeLength: [20, 60],
@@ -68,9 +71,6 @@ describe("NetJSONGraph Specification", () => {
6871
series: [
6972
{
7073
zoom: 0.7,
71-
labelLayout: {
72-
hideOverlap: true,
73-
},
7474
},
7575
],
7676
toolbox: {
@@ -86,9 +86,6 @@ describe("NetJSONGraph Specification", () => {
8686
series: [
8787
{
8888
zoom: 1,
89-
labelLayout: {
90-
hideOverlap: false,
91-
},
9289
},
9390
],
9491
toolbox: {

0 commit comments

Comments
 (0)