Browse Source

Put node-label alignment into 'align' rather than 'textAlign'

Node labels don't currently make use of the 'align' property, so this reuses
the existing property.

The label code has a number of alignment options that make sense to edges only,
so the Edge/Node now passes a boolean to the Label ctor to explain which the label
is being rendered for
codeClimate
Coleman Kane 9 years ago
parent
commit
c966a5feba
5 changed files with 27 additions and 24 deletions
  1. +2
    -3
      docs/network/nodes.html
  2. +1
    -2
      lib/network/modules/NodesHandler.js
  3. +1
    -1
      lib/network/modules/components/Edge.js
  4. +1
    -1
      lib/network/modules/components/Node.js
  5. +22
    -17
      lib/network/modules/components/shared/Label.js

+ 2
- 3
docs/network/nodes.html View File

@ -133,8 +133,7 @@ var options = {
background: 'none',
strokeWidth: 0, // px
strokeColor: '#ffffff',
align: 'horizontal',
textAlign: 'center'
align: 'center'
},
group: undefined,
hidden: false,
@ -372,7 +371,7 @@ network.setOptions(options);
<td>This is the color of the stroke <i>assuming the value for stroke is higher than 0</i>.</td>
</tr>
<tr parent="font" class="hidden">
<td class="indent">font.textAlign</td>
<td class="indent">font.align</td>
<td>String</td>
<td><code>'center'</code></td>
<td>This can be set to 'left' to make the label left-aligned. Otherwise,

+ 1
- 2
lib/network/modules/NodesHandler.js View File

@ -49,8 +49,7 @@ class NodesHandler {
background: 'none',
strokeWidth: 0, // px
strokeColor: '#ffffff',
align: 'horizontal',
textAlign: 'center'
align: 'center'
},
group: undefined,
hidden: false,

+ 1
- 1
lib/network/modules/components/Edge.js View File

@ -48,7 +48,7 @@ class Edge {
this.connected = false;
this.labelModule = new Label(this.body, this.options);
this.labelModule = new Label(this.body, this.options, true /* It's an edge label */);
this.setOptions(options);
}

+ 1
- 1
lib/network/modules/components/Node.js View File

@ -66,7 +66,7 @@ class Node {
this.selected = false;
this.hover = false;
this.labelModule = new Label(this.body, this.options);
this.labelModule = new Label(this.body, this.options, false /* Not edge label */);
this.setOptions(options);
}

+ 22
- 17
lib/network/modules/components/shared/Label.js View File

@ -1,7 +1,7 @@
let util = require('../../../../util');
class Label {
constructor(body,options) {
constructor(body,options,edgelabel = false) {
this.body = body;
this.pointToSelf = false;
@ -9,6 +9,7 @@ class Label {
this.fontOptions = {};
this.setOptions(options);
this.size = {top: 0, left: 0, width: 0, height: 0, yLine: 0}; // could be cached
this.isEdgeLabel = edgelabel;
}
setOptions(options, allowDeletion = false) {
@ -87,19 +88,23 @@ class Label {
let lineMargin = 2;
switch (this.fontOptions.align) {
case 'middle':
ctx.fillRect(-this.size.width * 0.5, -this.size.height * 0.5, this.size.width, this.size.height);
break;
case 'top':
ctx.fillRect(-this.size.width * 0.5, -(this.size.height + lineMargin), this.size.width, this.size.height);
break;
case 'bottom':
ctx.fillRect(-this.size.width * 0.5, lineMargin, this.size.width, this.size.height);
break;
default:
ctx.fillRect(this.size.left, this.size.top - 0.5*lineMargin, this.size.width, this.size.height);
break;
if (this.isEdgeLabel) {
switch (this.fontOptions.align) {
case 'middle':
ctx.fillRect(-this.size.width * 0.5, -this.size.height * 0.5, this.size.width, this.size.height);
break;
case 'top':
ctx.fillRect(-this.size.width * 0.5, -(this.size.height + lineMargin), this.size.width, this.size.height);
break;
case 'bottom':
ctx.fillRect(-this.size.width * 0.5, lineMargin, this.size.width, this.size.height);
break;
default:
ctx.fillRect(this.size.left, this.size.top - 0.5*lineMargin, this.size.width, this.size.height);
break;
}
} else {
ctx.fillRect(this.size.left, this.size.top - 0.5*lineMargin, this.size.width, this.size.height);
}
}
}
@ -128,8 +133,8 @@ class Label {
ctx.font = (selected && this.nodeOptions.labelHighlightBold ? 'bold ' : '') + fontSize + "px " + this.fontOptions.face;
ctx.fillStyle = fontColor;
// When the textAlign property is 'left', make label left-justified
if (this.options.font.textAlign === 'left') {
ctx.textAlign = this.options.font.textAlign;
if ((!this.isEdgeLabel) && this.fontOptions.align === 'left') {
ctx.textAlign = this.fontOptions.align;
x = x - 0.5 * this.size.width; // Shift label 1/2-distance to the left
} else {
ctx.textAlign = 'center';
@ -155,7 +160,7 @@ class Label {
_setAlignment(ctx, x, yLine, baseline) {
// check for label alignment (for edges)
// TODO: make alignment for nodes
if (this.fontOptions.align !== 'horizontal' && this.pointToSelf === false) {
if (this.isEdgeLabel && this.fontOptions.align !== 'horizontal' && this.pointToSelf === false) {
x = 0;
yLine = 0;

Loading…
Cancel
Save