Skip to content

Commit cca07ea

Browse files
author
wfr
committed
Apply transformation for reversed case; add mocks und schema
1 parent 9d8150c commit cca07ea

14 files changed

Lines changed: 728 additions & 4 deletions

src/traces/sankey/render.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,22 @@ function linkTextGetter(trace) {
586586
};
587587
}
588588

589+
// Counter-transform that cancels the group matrix applied in sankeyTransform, so
590+
// the label glyphs always read left-to-right and upright. Mirrors the node-label
591+
// handling: for every combination the product (group matrix x this) is the identity.
592+
// h + forward : matrix( 1 0 0 1) -> ''
593+
// h + reversed: matrix(-1 0 0 1) -> scale(-1,1)
594+
// v + forward : matrix( 0 1 1 0) -> scale(-1,1) rotate(90)
595+
// v + reversed: matrix( 0 -1 1 0) -> rotate(90)
596+
function linkLabelFlip(p) {
597+
if(p.horizontal) return p.reversed ? 'scale(-1,1)' : '';
598+
return p.reversed ? strRotate(90) : ('scale(-1,1)' + strRotate(90));
599+
}
600+
589601
// Positions a permanent link label at the link midpoint (layout frame) and keeps
590-
// the glyphs upright.
602+
// the glyphs upright. The midpoint stays in the layout frame: the enclosing
603+
// `.sankey` group already carries the orientation/direction transform, so the
604+
// coordinates themselves must not be mirrored here.
591605
function linkLabelTransform(d) {
592606
var l = d.link;
593607
var midX, midY;
@@ -599,9 +613,7 @@ function linkLabelTransform(d) {
599613
midX = (l.source.x1 + l.target.x0) / 2;
600614
midY = (l.y0 + l.y1) / 2;
601615
}
602-
var p = d.parent;
603-
var flip = p.horizontal ? '' : ('scale(-1,1)' + strRotate(90));
604-
return strTranslate(midX, midY) + flip;
616+
return strTranslate(midX, midY) + linkLabelFlip(d.parent);
605617
}
606618

607619
function nodeModel(d, n) {

src/types/generated/schema.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7003,11 +7003,24 @@ export interface SankeyData {
70037003
* @default []
70047004
*/
70057005
target?: Datum[] | Datum[][] | TypedArray;
7006+
/** Sets the font for the permanent link labels. */
7007+
textfont?: Font;
7008+
/**
7009+
* Determines which trace information appears permanently on the links. Any combination of *label* and *value* joined with a *+* OR *none*.
7010+
* @default 'none'
7011+
*/
7012+
textinfo?: 'label' | 'value' | 'none' | (string & {});
7013+
/** Template string used for rendering the information text that appears permanently on the links. Note that this will override `textinfo`. Variables are inserted using %{variable}, for example *%{label}: %{value}*. Available variables are `label`, `value`, `valueLabel` (the value formatted with `valueformat`/`valuesuffix`), `source`, `target` and `customdata`. */
7014+
texttemplate?: string | string[];
70067015
/**
70077016
* A numeric value representing the flow volume value.
70087017
* @default []
70097018
*/
70107019
value?: Datum[] | Datum[][] | TypedArray;
7020+
/** Sets the value formatting rule for the permanent link labels, using d3 formatting mini-languages. Falls back to the trace-level `valueformat` when empty. */
7021+
valueformat?: string;
7022+
/** Adds a unit to follow the value in the permanent link labels. Falls back to the trace-level `valuesuffix` when empty. */
7023+
valuesuffix?: string;
70117024
};
70127025
/** Assigns extra meta information associated with this trace that can be used in various text attributes. Attributes such as trace `name`, graph, axis and colorbar `title.text`, annotation `text` `rangeselector`, `updatemenues` and `sliders` `label` text all support `meta`. To access the trace `meta` values in an attribute in the same trace, simply use `%{meta[i]}` where `i` is the index or key of the `meta` item in question. To access trace `meta` in layout attributes, use `%{data[n[.meta[i]}` where `i` is the index or key of the `meta` and `n` is the trace index. */
70137026
meta?: any;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"data": [
3+
{
4+
"type": "sankey",
5+
"node": {
6+
"label": ["0", "1", "2", "3", "4", "5"],
7+
"color": "gray"
8+
},
9+
"link": {
10+
"source": [0, 1, 4, 2, 1],
11+
"target": [1, 4, 5, 4, 3],
12+
"value": [4, 2, 3, 1, 2],
13+
"label": ["a", "b", "c", "d", "e"],
14+
"color": [
15+
"rgba(230, 25, 75, 0.6)",
16+
"rgba(255, 165, 0, 0.6)",
17+
"rgba(60, 180, 75, 0.6)",
18+
"rgba(0, 130, 200, 0.6)",
19+
"rgba(145, 30, 180, 0.6)"
20+
],
21+
"textinfo": "label+value"
22+
}
23+
}
24+
],
25+
"layout": {
26+
"title": { "text": "Sankey with permanent link labels" },
27+
"width": 800,
28+
"height": 800
29+
}
30+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"data": [
3+
{
4+
"type": "sankey",
5+
"node": {
6+
"pad": 5,
7+
"label": ["0", "1", "2", "3", "4", "5", "6"]
8+
},
9+
"link": {
10+
"source": [0, 0, 1, 2, 5, 4, 3],
11+
"target": [5, 3, 4, 3, 0, 2, 2],
12+
"value": [1, 2, 1, 1, 1, 1, 1],
13+
"textinfo": "value"
14+
}
15+
}
16+
],
17+
"layout": {
18+
"title": { "text": "Sankey with circular data and permanent link labels" },
19+
"width": 800,
20+
"height": 800
21+
}
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"data": [
3+
{
4+
"type": "sankey",
5+
"orientation": "v",
6+
"direction": "reversed",
7+
"node": {
8+
"pad": 5,
9+
"label": ["0", "1", "2", "3", "4", "5", "6"]
10+
},
11+
"link": {
12+
"source": [0, 0, 1, 2, 5, 4, 3],
13+
"target": [5, 3, 4, 3, 0, 2, 2],
14+
"value": [1, 2, 1, 1, 1, 1, 1],
15+
"textinfo": "value"
16+
}
17+
}
18+
],
19+
"layout": {
20+
"title": { "text": "Sankey with circular data and permanent link labels, vertical reversed" },
21+
"width": 800,
22+
"height": 800
23+
}
24+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"data": [
3+
{
4+
"domain": { "x": [0, 0.45] },
5+
"type": "sankey",
6+
"valueformat": ".0f",
7+
"valuesuffix": " TWh",
8+
"textfont": { "family": "Times New Roman", "size": 16, "color": "darkblue" },
9+
"node": {
10+
"label": ["0", "1", "2", "3", "4", "5"],
11+
"color": "gray"
12+
},
13+
"link": {
14+
"source": [0, 1, 4, 2, 1],
15+
"target": [1, 4, 5, 4, 3],
16+
"value": [4, 2, 3, 1, 2],
17+
"textinfo": "value"
18+
}
19+
},
20+
{
21+
"domain": { "x": [0.55, 1] },
22+
"type": "sankey",
23+
"valueformat": ".0f",
24+
"valuesuffix": " TWh",
25+
"textfont": { "family": "Times New Roman", "size": 16, "color": "darkblue" },
26+
"node": {
27+
"label": ["0", "1", "2", "3", "4", "5"],
28+
"color": "gray"
29+
},
30+
"link": {
31+
"source": [0, 1, 4, 2, 1],
32+
"target": [1, 4, 5, 4, 3],
33+
"value": [4, 2, 3, 1, 2],
34+
"textinfo": "value",
35+
"valueformat": ".2f",
36+
"valuesuffix": " GW",
37+
"textfont": { "family": "Arial", "size": 10, "color": "black" }
38+
}
39+
}
40+
],
41+
"layout": {
42+
"title": { "text": "Permanent link labels: trace level inherited (left) vs link level override (right)" },
43+
"width": 1000,
44+
"height": 600,
45+
"showlegend": false
46+
}
47+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"data": [
3+
{
4+
"type": "sankey",
5+
"direction": "reversed",
6+
"node": {
7+
"label": ["0", "1", "2", "3", "4", "5"],
8+
"color": "gray"
9+
},
10+
"link": {
11+
"source": [0, 1, 4, 2, 1],
12+
"target": [1, 4, 5, 4, 3],
13+
"value": [4, 2, 3, 1, 2],
14+
"label": ["a", "b", "c", "d", "e"],
15+
"color": [
16+
"rgba(230, 25, 75, 0.6)",
17+
"rgba(255, 165, 0, 0.6)",
18+
"rgba(60, 180, 75, 0.6)",
19+
"rgba(0, 130, 200, 0.6)",
20+
"rgba(145, 30, 180, 0.6)"
21+
],
22+
"textinfo": "label+value"
23+
}
24+
}
25+
],
26+
"layout": {
27+
"title": { "text": "Sankey with permanent link labels, reversed" },
28+
"width": 800,
29+
"height": 800
30+
}
31+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"data": [
3+
{
4+
"domain": { "x": [0, 0.45] },
5+
"type": "sankey",
6+
"node": {
7+
"label": ["0", "1", "2", "3", "4", "5"],
8+
"color": "gray"
9+
},
10+
"link": {
11+
"source": [0, 1, 4, 2, 1, 0],
12+
"target": [1, 4, 5, 4, 3, 5],
13+
"value": [4, 2, 3, 1, 2, 0],
14+
"label": ["a", "b", "", "d", "e", "zero"],
15+
"textinfo": "label"
16+
}
17+
},
18+
{
19+
"domain": { "x": [0.55, 1] },
20+
"type": "sankey",
21+
"valueformat": ".0f",
22+
"valuesuffix": " TWh",
23+
"node": {
24+
"label": ["0", "1", "2", "3", "4", "5"],
25+
"color": "gray"
26+
},
27+
"link": {
28+
"source": [0, 1, 4, 2, 1],
29+
"target": [1, 4, 5, 4, 3],
30+
"value": [4, 2, 3, 1, 2],
31+
"label": ["a", "b", "c", "d", "e"],
32+
"textinfo": "label+value",
33+
"texttemplate": "%{label} = %{valueLabel}"
34+
}
35+
}
36+
],
37+
"layout": {
38+
"title": { "text": "Permanent link labels: textinfo label only (left) vs texttemplate overriding textinfo (right)" },
39+
"width": 1000,
40+
"height": 600,
41+
"showlegend": false
42+
}
43+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"data": [
3+
{
4+
"type": "sankey",
5+
"valueformat": ".0f",
6+
"valuesuffix": " TWh",
7+
"node": {
8+
"label": ["0", "1", "2", "3", "4", "5"],
9+
"color": "gray"
10+
},
11+
"link": {
12+
"source": [0, 1, 4, 2, 1],
13+
"target": [1, 4, 5, 4, 3],
14+
"value": [4, 2, 3, 1, 2],
15+
"label": ["a", "b", "c", "d", "e"],
16+
"customdata": ["cd0", "cd1", "cd2", "cd3", "cd4"],
17+
"color": [
18+
"rgba(230, 25, 75, 0.6)",
19+
"rgba(255, 165, 0, 0.6)",
20+
"rgba(60, 180, 75, 0.6)",
21+
"rgba(0, 130, 200, 0.6)",
22+
"rgba(145, 30, 180, 0.6)"
23+
],
24+
"texttemplate": [
25+
"%{label}",
26+
"%{value}",
27+
"%{valueLabel}",
28+
"%{source}/%{target}",
29+
"%{customdata}"
30+
],
31+
"textfont": { "family": "Times New Roman", "size": 14, "color": "black" }
32+
}
33+
}
34+
],
35+
"layout": {
36+
"title": { "text": "Sankey with permanent link labels from texttemplate" },
37+
"width": 800,
38+
"height": 800
39+
}
40+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"data": [
3+
{
4+
"type": "sankey",
5+
"orientation": "v",
6+
"node": {
7+
"label": ["0", "1", "2", "3", "4", "5"],
8+
"color": "gray"
9+
},
10+
"link": {
11+
"source": [0, 1, 4, 2, 1],
12+
"target": [1, 4, 5, 4, 3],
13+
"value": [4, 2, 3, 1, 2],
14+
"label": ["a", "b", "c", "d", "e"],
15+
"color": [
16+
"rgba(230, 25, 75, 0.6)",
17+
"rgba(255, 165, 0, 0.6)",
18+
"rgba(60, 180, 75, 0.6)",
19+
"rgba(0, 130, 200, 0.6)",
20+
"rgba(145, 30, 180, 0.6)"
21+
],
22+
"textinfo": "label+value"
23+
}
24+
}
25+
],
26+
"layout": {
27+
"title": { "text": "Sankey with permanent link labels, vertical" },
28+
"width": 800,
29+
"height": 800
30+
}
31+
}

0 commit comments

Comments
 (0)