Skip to content

Commit a0ff4c5

Browse files
authored
Add padding option (#53)
* Add padding option * changes default of padding to font.lineHeight / 2
1 parent 3747975 commit a0ff4c5

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

src/controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ export default class SankeyController extends DatasetController {
241241
const lines = isNullOrUndef(label) ? [] : me.toTextLines(label);
242242
const linesLength = lines.length;
243243
const middle = y + height / 2;
244-
const padding = 7.5;
245244
const textHeight = font.lineHeight;
245+
const padding = valueOrDefault(me.options.padding, textHeight / 2);
246246

247247
ctx.font = font.string;
248248

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

test/fixtures/labels-multiline.png

-389 Bytes
Loading

types/index.esm.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ declare module 'chart.js' {
3232
nodeWidth?: number /* defaults to 10 */
3333
color?: string /* defaults to 'black' */
3434
borderColor?: string /* defaults to 'black' */
35-
font: FontSpec /* defaults to chart.options.font */
35+
font?: FontSpec /* defaults to chart.options.font */
36+
padding?: number /* defaults to font.lineHeight / 2 */
3637
}
3738

3839
type FromToElement = {

0 commit comments

Comments
 (0)