Skip to content

Commit e50592c

Browse files
committed
Add borderColor and borderWidth options
1 parent 134b0a2 commit e50592c

File tree

7 files changed

+72
-9
lines changed

7 files changed

+72
-9
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "chartjs-chart-sankey",
3-
"version": "0.3.1",
3+
"version": "0.3.2",
44
"description": "Chart.js module for creating sankey diagrams",
55
"main": "dist/chartjs-chart-sankey.js",
66
"module": "dist/chartjs-chart-sankey.esm.js",

src/controller.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
import {DatasetController} from 'chart.js';
4+
import {valueOrDefault} from 'chart.js/helpers';
45
import {layout} from './layout';
56

67
export function buildNodesFromFlows(data) {
@@ -114,6 +115,9 @@ export default class SankeyController extends DatasetController {
114115
const {xScale, yScale} = me._cachedMeta;
115116
const firstOpts = me.resolveDataElementOptions(start, mode);
116117
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);
117121

118122
for (let i = start; i < start + count; i++) {
119123
const parsed = me.getParsed(i);
@@ -123,9 +127,9 @@ export default class SankeyController extends DatasetController {
123127
elems[i],
124128
i,
125129
{
126-
x: xScale.getPixelForValue(parsed.x) + 11,
130+
x: xScale.getPixelForValue(parsed.x) + nodeWidth + borderWidth,
127131
y,
128-
x2: xScale.getPixelForValue(custom.x) - 1,
132+
x2: xScale.getPixelForValue(custom.x) - borderWidth,
129133
y2: yScale.getPixelForValue(custom.y),
130134
from: custom.from,
131135
to: custom.to,
@@ -144,6 +148,8 @@ export default class SankeyController extends DatasetController {
144148
const ctx = me._ctx;
145149
const nodes = me._nodes || new Map();
146150
const dataset = me.getDataset();
151+
const borderWidth = valueOrDefault(dataset.borderWidth, 1);
152+
const nodeWidth = valueOrDefault(dataset.nodeWidth, 10);
147153
const labels = dataset.labels;
148154
const {xScale, yScale} = me._cachedMeta;
149155

@@ -161,10 +167,10 @@ export default class SankeyController extends DatasetController {
161167
ctx.textBaseline = 'middle';
162168
if (x < chartArea.width / 2) {
163169
ctx.textAlign = 'left';
164-
textX += 15;
170+
textX += nodeWidth + borderWidth + 4;
165171
} else {
166172
ctx.textAlign = 'right';
167-
textX -= 5;
173+
textX -= borderWidth + 4;
168174
}
169175
ctx.fillText(label, textX, y + height / 2);
170176
}
@@ -175,19 +181,25 @@ export default class SankeyController extends DatasetController {
175181
const me = this;
176182
const ctx = me._ctx;
177183
const nodes = me._nodes || new Map();
184+
const dataset = me.getDataset();
178185
const {xScale, yScale} = me._cachedMeta;
186+
const borderWidth = valueOrDefault(dataset.borderWidth, 1);
187+
const nodeWidth = valueOrDefault(dataset.nodeWidth, 10);
179188

180189
ctx.save();
181-
ctx.strokeStyle = 'black';
190+
ctx.strokeStyle = dataset.borderColor || 'black';
191+
ctx.lineWidth = borderWidth;
182192

183193
for (const node of nodes.values()) {
184194
ctx.fillStyle = node.color;
185195
const x = xScale.getPixelForValue(node.x);
186196
const y = yScale.getPixelForValue(node.y);
187197
const max = Math.max(node.in, node.out);
188198
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);
191203
}
192204
ctx.restore();
193205
}

test/fixtures/border.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module.exports = {
2+
config: {
3+
type: 'sankey',
4+
data: {
5+
datasets: [
6+
{
7+
data: [
8+
{from: 'a', to: 'b', flow: 20},
9+
{from: 'c', to: 'd', flow: 10},
10+
{from: 'c', to: 'e', flow: 5},
11+
],
12+
colorFrom: 'red',
13+
colorTo: 'green',
14+
borderColor: 'blue',
15+
borderWidth: 5
16+
}
17+
]
18+
}
19+
},
20+
options: {
21+
canvas: {
22+
height: 256,
23+
width: 512
24+
}
25+
}
26+
};

test/fixtures/border.png

18.5 KB
Loading

test/fixtures/no-border.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = {
2+
config: {
3+
type: 'sankey',
4+
data: {
5+
datasets: [
6+
{
7+
data: [
8+
{from: 'a', to: 'b', flow: 20},
9+
{from: 'c', to: 'd', flow: 10},
10+
{from: 'c', to: 'e', flow: 5},
11+
],
12+
colorFrom: 'red',
13+
colorTo: 'green',
14+
borderWidth: 0
15+
}
16+
]
17+
}
18+
},
19+
options: {
20+
canvas: {
21+
height: 256,
22+
width: 512
23+
}
24+
}
25+
};

test/fixtures/no-border.png

17.7 KB
Loading

0 commit comments

Comments
 (0)