1
1
'use strict' ;
2
2
3
3
import { DatasetController } from 'chart.js' ;
4
+ import { valueOrDefault } from 'chart.js/helpers' ;
4
5
import { layout } from './layout' ;
5
6
6
7
export function buildNodesFromFlows ( data ) {
@@ -114,6 +115,9 @@ export default class SankeyController extends DatasetController {
114
115
const { xScale, yScale} = me . _cachedMeta ;
115
116
const firstOpts = me . resolveDataElementOptions ( start , mode ) ;
116
117
const sharedOptions = me . getSharedOptions ( mode , elems [ start ] , firstOpts ) ;
118
+ const dataset = me . getDataset ( ) ;
119
+ const borderWidth = valueOrDefault ( dataset . borderWidth , 1 ) / 2 + 0.5 ;
120
+ const nodeWidth = valueOrDefault ( dataset . nodeWidth , 10 ) ;
117
121
118
122
for ( let i = start ; i < start + count ; i ++ ) {
119
123
const parsed = me . getParsed ( i ) ;
@@ -123,9 +127,9 @@ export default class SankeyController extends DatasetController {
123
127
elems [ i ] ,
124
128
i ,
125
129
{
126
- x : xScale . getPixelForValue ( parsed . x ) + 11 ,
130
+ x : xScale . getPixelForValue ( parsed . x ) + nodeWidth + borderWidth ,
127
131
y,
128
- x2 : xScale . getPixelForValue ( custom . x ) - 1 ,
132
+ x2 : xScale . getPixelForValue ( custom . x ) - borderWidth ,
129
133
y2 : yScale . getPixelForValue ( custom . y ) ,
130
134
from : custom . from ,
131
135
to : custom . to ,
@@ -144,6 +148,8 @@ export default class SankeyController extends DatasetController {
144
148
const ctx = me . _ctx ;
145
149
const nodes = me . _nodes || new Map ( ) ;
146
150
const dataset = me . getDataset ( ) ;
151
+ const borderWidth = valueOrDefault ( dataset . borderWidth , 1 ) ;
152
+ const nodeWidth = valueOrDefault ( dataset . nodeWidth , 10 ) ;
147
153
const labels = dataset . labels ;
148
154
const { xScale, yScale} = me . _cachedMeta ;
149
155
@@ -161,10 +167,10 @@ export default class SankeyController extends DatasetController {
161
167
ctx . textBaseline = 'middle' ;
162
168
if ( x < chartArea . width / 2 ) {
163
169
ctx . textAlign = 'left' ;
164
- textX += 15 ;
170
+ textX += nodeWidth + borderWidth + 4 ;
165
171
} else {
166
172
ctx . textAlign = 'right' ;
167
- textX -= 5 ;
173
+ textX -= borderWidth + 4 ;
168
174
}
169
175
ctx . fillText ( label , textX , y + height / 2 ) ;
170
176
}
@@ -175,19 +181,25 @@ export default class SankeyController extends DatasetController {
175
181
const me = this ;
176
182
const ctx = me . _ctx ;
177
183
const nodes = me . _nodes || new Map ( ) ;
184
+ const dataset = me . getDataset ( ) ;
178
185
const { xScale, yScale} = me . _cachedMeta ;
186
+ const borderWidth = valueOrDefault ( dataset . borderWidth , 1 ) ;
187
+ const nodeWidth = valueOrDefault ( dataset . nodeWidth , 10 ) ;
179
188
180
189
ctx . save ( ) ;
181
- ctx . strokeStyle = 'black' ;
190
+ ctx . strokeStyle = dataset . borderColor || 'black' ;
191
+ ctx . lineWidth = borderWidth ;
182
192
183
193
for ( const node of nodes . values ( ) ) {
184
194
ctx . fillStyle = node . color ;
185
195
const x = xScale . getPixelForValue ( node . x ) ;
186
196
const y = yScale . getPixelForValue ( node . y ) ;
187
197
const max = Math . max ( node . in , node . out ) ;
188
198
const height = Math . abs ( yScale . getPixelForValue ( node . y + max ) - y ) ;
189
- ctx . strokeRect ( x , y , 10 , height ) ;
190
- ctx . fillRect ( x , y , 10 , height ) ;
199
+ if ( borderWidth ) {
200
+ ctx . strokeRect ( x , y , nodeWidth , height ) ;
201
+ }
202
+ ctx . fillRect ( x , y , nodeWidth , height ) ;
191
203
}
192
204
ctx . restore ( ) ;
193
205
}
0 commit comments