1
1
import * as d3 from 'd3'
2
2
import '../d3-styles.js'
3
+ import { labelsOcclusion } from '@rawgraphs/rawgraphs-core'
3
4
4
5
export function render (
5
6
svgNode ,
@@ -20,8 +21,13 @@ export function render(
20
21
marginLeft,
21
22
//chart
22
23
nodePadding,
24
+ anticollision,
23
25
groupsDiameter,
24
26
maxItemsDiameter,
27
+ colorScale,
28
+ showLabels,
29
+ autoHideLabels,
30
+ groupsFillColor, // @TODO : check how to expose it as option
25
31
} = visualOptions
26
32
27
33
const margin = {
@@ -33,12 +39,25 @@ export function render(
33
39
34
40
const chartWidth = width - margin . right - margin . left
35
41
const chartHeight = height - margin . top - margin . bottom
42
+
36
43
// compute radius
37
44
const radius = ( chartWidth < chartHeight ? chartWidth : chartHeight ) / 2
38
45
39
46
// select the svg node
40
- const svg = d3
41
- . select ( svgNode )
47
+ const svg = d3 . select ( svgNode )
48
+
49
+ // add background
50
+ d3 . select ( svgNode )
51
+ . append ( 'rect' )
52
+ . attr ( 'width' , width )
53
+ . attr ( 'height' , height )
54
+ . attr ( 'x' , 0 )
55
+ . attr ( 'y' , 0 )
56
+ . attr ( 'fill' , background )
57
+ . attr ( 'id' , 'backgorund' )
58
+
59
+ // add viz group
60
+ const vizGroup = svg
42
61
. append ( 'g' )
43
62
. attr ( 'transform' , 'translate(' + margin . left + ',' + margin . top + ')' )
44
63
. attr ( 'id' , 'viz' )
@@ -50,15 +69,6 @@ export function render(
50
69
let groupNodes = graph . nodes . filter ( ( d ) => d . type == 'group' )
51
70
const groupNames = groupNodes . map ( ( d ) => d . name )
52
71
53
- console . log ( graph )
54
- console . log ( d3 . max ( graph . nodes , ( d ) => d . strength ) )
55
-
56
- // define colors
57
- let colorScale = d3
58
- . scaleOrdinal ( )
59
- . domain ( groupNames )
60
- . range ( d3 . schemeCategory10 )
61
-
62
72
// scele for edges strenghts
63
73
let strengthScale = d3 . scaleLinear (
64
74
d3 . extent ( graph . edges , ( d ) => d . value ) ,
@@ -97,9 +107,6 @@ export function render(
97
107
d . color = d3 . rgb ( colr , colg , colb )
98
108
} )
99
109
100
- // TODO: expose or remove
101
- const forceNode = d3 . forceManyBody ( ) . strength ( - 5 )
102
-
103
110
// define links (edges) force
104
111
const forceLink = d3
105
112
. forceLink ( graph . edges )
@@ -127,9 +134,14 @@ export function render(
127
134
const simulation = d3
128
135
. forceSimulation ( graph . nodes )
129
136
. force ( 'link' , forceLink )
130
- . force ( 'collisionForce' , forceCollide )
131
137
. on ( 'tick' , ticked )
132
138
139
+ // add collision force according to options
140
+
141
+ if ( anticollision ) {
142
+ simulation . force ( 'collisionForce' , forceCollide )
143
+ }
144
+
133
145
// run the simulation in background
134
146
// @TODO move this to a web worker
135
147
//console.log("---------------new simulation---------------")
@@ -147,7 +159,7 @@ export function render(
147
159
//console.log("---------------end of simulation---------------")
148
160
149
161
// draw nodes
150
- const nodesLayer = svg
162
+ const nodesLayer = vizGroup
151
163
. append ( 'g' )
152
164
. attr ( 'id' , 'labelsLayer' )
153
165
. attr ( 'id' , 'nodes' )
@@ -160,7 +172,7 @@ export function render(
160
172
nodesLayer
161
173
. filter ( ( d ) => d . type == 'group' )
162
174
. attr ( 'r' , groupsDiameter / 2 )
163
- . attr ( 'fill' , 'white' )
175
+ . attr ( 'fill' , 'white' ) // @TODO : check how to expose a single color. See https://github.com/rawgraphs/rawgraphs-core/issues/67
164
176
. attr ( 'stroke' , ( d ) => colorScale ( d . name ) )
165
177
166
178
// style item nodes
@@ -170,7 +182,7 @@ export function render(
170
182
. attr ( 'fill' , ( d ) => d . color )
171
183
172
184
// add labels
173
- const labelsLayer = svg
185
+ const labelsLayer = vizGroup
174
186
. append ( 'g' )
175
187
. attr ( 'id' , 'labelsLayer' )
176
188
. attr ( 'text-anchor' , 'middle' )
@@ -179,7 +191,18 @@ export function render(
179
191
. data ( graph . nodes )
180
192
. join ( 'g' )
181
193
182
- labelsLayer . append ( 'text' ) . text ( ( d ) => d . name )
194
+ // add texts according to option
195
+ if ( showLabels ) {
196
+ labelsLayer . append ( 'text' ) . text ( ( d ) => d . name )
197
+
198
+ labelsLayer . filter ( ( d ) => d . type == 'group' ) . styles ( styles . seriesLabel )
199
+ labelsLayer . filter ( ( d ) => d . type == 'item' ) . styles ( styles . labelPrimary )
200
+ }
201
+
202
+ // auto hide labels
203
+ if ( autoHideLabels ) {
204
+ labelsOcclusion ( labelsLayer . selectAll ( 'text' ) , ( d ) => sizeScale ( d . strength ) )
205
+ }
183
206
184
207
function ticked ( ) {
185
208
nodesLayer . attr ( 'transform' , ( d ) => 'translate(' + d . x + ',' + d . y + ')' )
0 commit comments