Skip to content

Commit 134b0a2

Browse files
committed
Add labels option for overriding labels
1 parent 02cb2b1 commit 134b0a2

File tree

6 files changed

+59
-6
lines changed

6 files changed

+59
-6
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.0",
3+
"version": "0.3.1",
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: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ export default class SankeyController extends DatasetController {
143143
const me = this;
144144
const ctx = me._ctx;
145145
const nodes = me._nodes || new Map();
146+
const dataset = me.getDataset();
147+
const labels = dataset.labels;
146148
const {xScale, yScale} = me._cachedMeta;
147149

148150
ctx.save();
@@ -153,15 +155,18 @@ export default class SankeyController extends DatasetController {
153155
const y = yScale.getPixelForValue(node.y);
154156
const max = Math.max(node.in, node.out);
155157
const height = Math.abs(yScale.getPixelForValue(node.y + max) - y);
156-
ctx.fillStyle = me.getDataset().color || 'black';
158+
const label = labels && labels[node.key] || node.key;
159+
let textX = x;
160+
ctx.fillStyle = dataset.color || 'black';
157161
ctx.textBaseline = 'middle';
158162
if (x < chartArea.width / 2) {
159163
ctx.textAlign = 'left';
160-
ctx.fillText(node.key, x + 15, y + height / 2);
164+
textX += 15;
161165
} else {
162166
ctx.textAlign = 'right';
163-
ctx.fillText(node.key, x - 5, y + height / 2);
167+
textX -= 5;
164168
}
169+
ctx.fillText(label, textX, y + height / 2);
165170
}
166171
ctx.restore();
167172
}

test/fixtures/labels.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const data = [
2+
{from: 'Oil', to: 'Fossil Fuels', flow: 15},
3+
{from: 'Natural Gas', to: 'Fossil Fuels', flow: 20},
4+
{from: 'Coal', to: 'Fossil Fuels', flow: 25},
5+
{from: 'Coal', to: 'Electricity', flow: 25},
6+
{from: 'Fossil Fuels', to: 'Energy', flow: 60},
7+
{from: 'Electricity', to: 'Energy', flow: 25}
8+
];
9+
10+
const colors = {
11+
Oil: 'black',
12+
Coal: 'gray',
13+
'Fossil Fuels': 'slategray',
14+
Electricity: 'blue',
15+
Energy: 'orange'
16+
};
17+
18+
const labels = {
19+
'Natural Gas': 'Nat. gas',
20+
'Fossil Fuels': 'Fossil',
21+
};
22+
23+
function getColor(name) {
24+
return colors[name] || 'green';
25+
}
26+
27+
module.exports = {
28+
config: {
29+
type: 'sankey',
30+
data: {
31+
datasets: [
32+
{
33+
data,
34+
colorFrom: (c) => getColor(c.dataset.data[c.dataIndex].from),
35+
colorTo: (c) => getColor(c.dataset.data[c.dataIndex].to),
36+
labels
37+
}
38+
]
39+
}
40+
},
41+
options: {
42+
spriteText: true,
43+
canvas: {
44+
height: 256,
45+
width: 512
46+
}
47+
}
48+
};

test/fixtures/labels.png

33.7 KB
Loading

test/fixtures/priority.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const colors = {
1717

1818
const priority = {
1919
Oil: 1,
20-
'Narural Gas': 2,
20+
'Natural Gas': 2,
2121
Coal: 3,
2222
'Fossil Fuels': 1,
2323
Electricity: 2,

0 commit comments

Comments
 (0)