|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -var c = require('./constants'); |
| 3 | +var d3Force = require('d3-force'); |
| 4 | +var interpolateNumber = require('d3-interpolate').interpolateNumber; |
4 | 5 | var d3 = require('@plotly/d3');
|
| 6 | +var d3Sankey = require('@plotly/d3-sankey'); |
| 7 | +var d3SankeyCircular = require('@plotly/d3-sankey-circular'); |
| 8 | + |
| 9 | +var c = require('./constants'); |
5 | 10 | var tinycolor = require('tinycolor2');
|
6 | 11 | var Color = require('../../components/color');
|
7 | 12 | var Drawing = require('../../components/drawing');
|
8 |
| -var d3Sankey = require('@plotly/d3-sankey'); |
9 |
| -var d3SankeyCircular = require('@plotly/d3-sankey-circular'); |
10 |
| -var d3Force = require('d3-force'); |
11 | 13 | var Lib = require('../../lib');
|
12 | 14 | var strTranslate = Lib.strTranslate;
|
| 15 | +var strRotate = Lib.strRotate; |
13 | 16 | var gup = require('../../lib/gup');
|
14 | 17 | var keyFun = gup.keyFun;
|
15 | 18 | var repeat = gup.repeat;
|
16 | 19 | var unwrap = gup.unwrap;
|
17 |
| -var interpolateNumber = require('d3-interpolate').interpolateNumber; |
18 | 20 | var svgTextUtils = require('../../lib/svg_text_utils');
|
19 | 21 |
|
20 | 22 | var Registry = require('../../registry');
|
21 | 23 |
|
| 24 | +var alignmentConstants = require('../../constants/alignment'); |
| 25 | +var CAP_SHIFT = alignmentConstants.CAP_SHIFT; |
| 26 | +var LINE_SPACING = alignmentConstants.LINE_SPACING; |
| 27 | +var TEXTPAD = 3; |
| 28 | + |
22 | 29 | // view models
|
23 | 30 |
|
24 | 31 | function sankeyModel(layout, d, traceIndex) {
|
@@ -547,22 +554,6 @@ function sankeyTransform(d) {
|
547 | 554 | return offset + (d.horizontal ? 'matrix(1 0 0 1 0 0)' : 'matrix(0 1 1 0 0 0)');
|
548 | 555 | }
|
549 | 556 |
|
550 |
| -function nodeCentering(d) { |
551 |
| - return strTranslate(d.horizontal ? 0 : d.labelY, d.horizontal ? d.labelY : 0); |
552 |
| -} |
553 |
| - |
554 |
| -function textGuidePath(d) { |
555 |
| - return d3.svg.line()([ |
556 |
| - [d.horizontal ? (d.left ? -d.sizeAcross : d.visibleWidth + c.nodeTextOffsetHorizontal) : c.nodeTextOffsetHorizontal, 0], |
557 |
| - [d.horizontal ? (d.left ? - c.nodeTextOffsetHorizontal : d.sizeAcross) : d.visibleHeight - c.nodeTextOffsetHorizontal, 0] |
558 |
| - ]); |
559 |
| -} |
560 |
| - |
561 |
| -function sankeyInverseTransform(d) {return d.horizontal ? 'matrix(1 0 0 1 0 0)' : 'matrix(0 1 1 0 0 0)';} |
562 |
| -function textFlip(d) {return d.horizontal ? 'scale(1 1)' : 'scale(-1 1)';} |
563 |
| -function nodeTextColor(d) {return d.darkBackground && !d.horizontal ? 'rgb(255,255,255)' : 'rgb(0,0,0)';} |
564 |
| -function nodeTextOffset(d) {return d.horizontal && d.left ? '100%' : '0%';} |
565 |
| - |
566 | 557 | // event handling
|
567 | 558 |
|
568 | 559 | function attachPointerEvents(selection, sankey, eventSet) {
|
@@ -970,88 +961,55 @@ module.exports = function(gd, svg, calcData, layout, callbacks) {
|
970 | 961 | .ease(c.ease).duration(c.duration)
|
971 | 962 | .call(sizeNode);
|
972 | 963 |
|
973 |
| - var nodeCapture = sankeyNode.selectAll('.' + c.cn.nodeCapture) |
974 |
| - .data(repeat); |
975 |
| - |
976 |
| - nodeCapture.enter() |
977 |
| - .append('rect') |
978 |
| - .classed(c.cn.nodeCapture, true) |
979 |
| - .style('fill-opacity', 0); |
980 |
| - |
981 |
| - nodeCapture |
982 |
| - .attr('x', function(d) {return d.zoneX;}) |
983 |
| - .attr('y', function(d) {return d.zoneY;}) |
984 |
| - .attr('width', function(d) {return d.zoneWidth;}) |
985 |
| - .attr('height', function(d) {return d.zoneHeight;}); |
986 |
| - |
987 |
| - var nodeCentered = sankeyNode.selectAll('.' + c.cn.nodeCentered) |
988 |
| - .data(repeat); |
989 |
| - |
990 |
| - nodeCentered.enter() |
991 |
| - .append('g') |
992 |
| - .classed(c.cn.nodeCentered, true) |
993 |
| - .attr('transform', nodeCentering); |
994 |
| - |
995 |
| - nodeCentered |
996 |
| - .transition() |
997 |
| - .ease(c.ease).duration(c.duration) |
998 |
| - .attr('transform', nodeCentering); |
999 |
| - |
1000 |
| - var nodeLabelGuide = nodeCentered.selectAll('.' + c.cn.nodeLabelGuide) |
1001 |
| - .data(repeat); |
1002 |
| - |
1003 |
| - nodeLabelGuide.enter() |
1004 |
| - .append('path') |
1005 |
| - .classed(c.cn.nodeLabelGuide, true) |
1006 |
| - .attr('id', function(d) {return d.uniqueNodeLabelPathId;}) |
1007 |
| - .attr('d', textGuidePath) |
1008 |
| - .attr('transform', sankeyInverseTransform); |
1009 |
| - |
1010 |
| - nodeLabelGuide |
1011 |
| - .transition() |
1012 |
| - .ease(c.ease).duration(c.duration) |
1013 |
| - .attr('d', textGuidePath) |
1014 |
| - .attr('transform', sankeyInverseTransform); |
1015 |
| - |
1016 |
| - var nodeLabel = nodeCentered.selectAll('.' + c.cn.nodeLabel) |
| 964 | + var nodeLabel = sankeyNode.selectAll('.' + c.cn.nodeLabel) |
1017 | 965 | .data(repeat);
|
1018 | 966 |
|
1019 | 967 | nodeLabel.enter()
|
1020 | 968 | .append('text')
|
1021 | 969 | .classed(c.cn.nodeLabel, true)
|
1022 |
| - .attr('transform', textFlip) |
1023 |
| - .style('cursor', 'default') |
1024 |
| - .style('fill', 'black'); |
| 970 | + .style('cursor', 'default'); |
1025 | 971 |
|
1026 | 972 | nodeLabel
|
1027 |
| - .style('text-shadow', function(d) { |
1028 |
| - return d.horizontal ? svgTextUtils.makeTextShadow(1, '#fff') : 'none'; |
| 973 | + .attr('data-notex', 1) // prohibit tex interpretation until we can handle tex and regular text together |
| 974 | + .text(function(d) { return d.node.label; }) |
| 975 | + .each(function(d) { |
| 976 | + var e = d3.select(this); |
| 977 | + Drawing.font(e, d.textFont); |
| 978 | + svgTextUtils.convertToTspans(e, gd); |
1029 | 979 | })
|
1030 |
| - .each(function(d) {Drawing.font(nodeLabel, d.textFont);}); |
1031 |
| - |
1032 |
| - nodeLabel |
1033 |
| - .transition() |
1034 |
| - .ease(c.ease).duration(c.duration) |
1035 |
| - .attr('transform', textFlip); |
1036 |
| - |
1037 |
| - var nodeLabelTextPath = nodeLabel.selectAll('.' + c.cn.nodeLabelTextPath) |
1038 |
| - .data(repeat); |
| 980 | + .style('text-shadow', svgTextUtils.makeTextShadow(gd._fullLayout.paper_bgcolor)) |
| 981 | + .attr('text-anchor', function(d) { |
| 982 | + return (d.horizontal && d.left) ? 'end' : 'start'; |
| 983 | + }) |
| 984 | + .attr('transform', function(d) { |
| 985 | + var e = d3.select(this); |
| 986 | + // how much to shift a multi-line label to center it vertically. |
| 987 | + var nLines = svgTextUtils.lineCount(e); |
| 988 | + var blockHeight = d.textFont.size * ( |
| 989 | + (nLines - 1) * LINE_SPACING - CAP_SHIFT |
| 990 | + ); |
| 991 | + |
| 992 | + var posX = d.nodeLineWidth / 2 + TEXTPAD; |
| 993 | + var posY = ((d.horizontal ? d.visibleHeight : d.visibleWidth) - blockHeight) / 2; |
| 994 | + if(d.horizontal) { |
| 995 | + if(d.left) { |
| 996 | + posX = -posX; |
| 997 | + } else { |
| 998 | + posX += d.visibleWidth; |
| 999 | + } |
| 1000 | + } |
1039 | 1001 |
|
1040 |
| - nodeLabelTextPath.enter() |
1041 |
| - .append('textPath') |
1042 |
| - .classed(c.cn.nodeLabelTextPath, true) |
1043 |
| - .attr('alignment-baseline', 'middle') |
1044 |
| - .attr('xlink:href', function(d) {return '#' + d.uniqueNodeLabelPathId;}) |
1045 |
| - .attr('startOffset', nodeTextOffset) |
1046 |
| - .style('fill', nodeTextColor); |
| 1002 | + var flipText = d.horizontal ? '' : ( |
| 1003 | + 'scale(-1,1)' + strRotate(90) |
| 1004 | + ); |
1047 | 1005 |
|
1048 |
| - nodeLabelTextPath |
1049 |
| - .text(function(d) {return d.horizontal || d.node.dy > 5 ? d.node.label : '';}) |
1050 |
| - .attr('text-anchor', function(d) {return d.horizontal && d.left ? 'end' : 'start';}); |
| 1006 | + return strTranslate( |
| 1007 | + d.horizontal ? posX : posY, |
| 1008 | + d.horizontal ? posY : posX |
| 1009 | + ) + flipText; |
| 1010 | + }); |
1051 | 1011 |
|
1052 |
| - nodeLabelTextPath |
| 1012 | + nodeLabel |
1053 | 1013 | .transition()
|
1054 |
| - .ease(c.ease).duration(c.duration) |
1055 |
| - .attr('startOffset', nodeTextOffset) |
1056 |
| - .style('fill', nodeTextColor); |
| 1014 | + .ease(c.ease).duration(c.duration); |
1057 | 1015 | };
|
0 commit comments