davidardz07 commited on
Commit
76f6f02
·
1 Parent(s): 06f25d9

Smart Text position

Browse files
Files changed (2) hide show
  1. index.html +3 -1
  2. script.js +49 -4
index.html CHANGED
@@ -83,9 +83,11 @@
83
  <div>
84
  <label class="block text-sm text-gray-600 mb-1">Text Position</label>
85
  <select id="text-position" class="w-full p-2 border rounded-md">
 
86
  <option value="left">Left</option>
87
- <option value="middle" selected>Middle</option>
88
  <option value="right">Right</option>
 
 
89
  </select>
90
  </div>
91
  <div>
 
83
  <div>
84
  <label class="block text-sm text-gray-600 mb-1">Text Position</label>
85
  <select id="text-position" class="w-full p-2 border rounded-md">
86
+ <option value="default" selected>Default</option>
87
  <option value="left">Left</option>
 
88
  <option value="right">Right</option>
89
+ <option value="top">Top</option>
90
+ <option value="bottom">Bottom</option>
91
  </select>
92
  </div>
93
  <div>
script.js CHANGED
@@ -663,19 +663,59 @@ function initDiagram() {
663
 
664
  // Add text with customizable position
665
  let textX, textY, textAnchor;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
666
 
667
  switch(textPosition) {
 
 
 
 
 
 
668
  case 'left':
669
  textX = -6;
 
670
  textAnchor = "end";
671
  break;
672
  case 'right':
673
  textX = d => d.dx + 6;
 
674
  textAnchor = "start";
675
  break;
676
- default: // middle
 
 
 
 
 
677
  textX = d => d.dx / 2;
 
678
  textAnchor = "middle";
 
679
  }
680
 
681
  // Get font size from settings
@@ -697,9 +737,14 @@ function initDiagram() {
697
 
698
  node.append("text")
699
  .attr("x", textX)
700
- .attr("y", d => d.dy / 2)
701
- .attr("dy", ".35em")
702
- .attr("text-anchor", textAnchor)
 
 
 
 
 
703
  .text(d => d.name)
704
  .attr("fill", document.getElementById('text-color').value)
705
  .attr("font-size", fontSize)
 
663
 
664
  // Add text with customizable position
665
  let textX, textY, textAnchor;
666
+
667
+ function getDefaultTextPosition(node) {
668
+ const diagramWidth = width - textPadding;
669
+ const xRatio = node.x / diagramWidth;
670
+
671
+ if (xRatio < 0.33) { // Left nodes
672
+ return {
673
+ x: node.dx + 6, // Position text on right side
674
+ y: node.dy / 2,
675
+ anchor: "start"
676
+ };
677
+ } else if (xRatio > 0.66) { // Right nodes
678
+ return {
679
+ x: -6, // Position text on left side
680
+ y: node.dy / 2,
681
+ anchor: "end"
682
+ };
683
+ } else { // Middle nodes
684
+ return {
685
+ x: node.dx + 6, // Position text on right side
686
+ y: node.dy / 2,
687
+ anchor: "start"
688
+ };
689
+ }
690
+ }
691
 
692
  switch(textPosition) {
693
+ case 'default':
694
+ // Text position will be determined per node
695
+ textX = d => getDefaultTextPosition(d).x;
696
+ textY = d => getDefaultTextPosition(d).y;
697
+ textAnchor = d => getDefaultTextPosition(d).anchor;
698
+ break;
699
  case 'left':
700
  textX = -6;
701
+ textY = d => d.dy / 2;
702
  textAnchor = "end";
703
  break;
704
  case 'right':
705
  textX = d => d.dx + 6;
706
+ textY = d => d.dy / 2;
707
  textAnchor = "start";
708
  break;
709
+ case 'top':
710
+ textX = d => d.dx / 2;
711
+ textY = -6;
712
+ textAnchor = "middle";
713
+ break;
714
+ case 'bottom':
715
  textX = d => d.dx / 2;
716
+ textY = d => d.dy + 6;
717
  textAnchor = "middle";
718
+ break;
719
  }
720
 
721
  // Get font size from settings
 
737
 
738
  node.append("text")
739
  .attr("x", textX)
740
+ .attr("y", textY || (d => d.dy / 2))
741
+ .attr("dy", d => {
742
+ // Adjust vertical alignment based on position
743
+ if (textPosition === 'top') return '-0.5em';
744
+ if (textPosition === 'bottom') return '1em';
745
+ return '.35em';
746
+ })
747
+ .attr("text-anchor", typeof textAnchor === 'function' ? textAnchor : d => textAnchor)
748
  .text(d => d.name)
749
  .attr("fill", document.getElementById('text-color').value)
750
  .attr("font-size", fontSize)