diff --git a/demos/test.html b/demos/test.html index 54c945af4a..39821a40be 100644 --- a/demos/test.html +++ b/demos/test.html @@ -46,208 +46,123 @@ } diff --git a/package.json b/package.json index e6ba47fb67..2b1d66a56d 100644 --- a/package.json +++ b/package.json @@ -98,7 +98,7 @@ "screenshot": "node ./bin/screenshot.js", "start": "npm run dev", "test": "torch --compile --renderer --recursive ./test/unit", - "test-live": "torch --compile --interactive --watch --recursive ./test/test-spec.js", + "test-live": "torch --compile --interactive --watch --recursive ./test/unit/item/*-spec.js", "watch": "webpack --config webpack-dev.config.js", "win-dev": "node ./bin/win-dev.js" }, diff --git a/src/shape/edges/common.js b/src/shape/edges/common.js index bd298b194f..9f198111ba 100755 --- a/src/shape/edges/common.js +++ b/src/shape/edges/common.js @@ -76,7 +76,7 @@ Shape.registerEdge('common', { let label = this.getLabel(item); const group = item.getGraphicGroup(); const model = item.getModel(); - const { labelOffsetX, labelOffsetY } = model; + const { labelOffsetX, labelOffsetY, labelRotate } = model; if (label) { const center = keyShape.getPoint(0.5); if (!center) { @@ -101,7 +101,7 @@ Shape.registerEdge('common', { fill: 'white' }; const style = model.labelRectStyle ? Util.mix({}, defaultStyle, model.labelRectStyle) : defaultStyle; - group.addShape('rect', { + const rect = group.addShape('rect', { attrs: Util.mix({}, style, { x: textBox.minX - padding[3], y: textBox.minY - padding[0], @@ -109,6 +109,22 @@ Shape.registerEdge('common', { height: textBox.maxY - textBox.minY + padding[0] + padding[2] }) }); + if (labelRotate) { + const centerX = (textBox.maxX + textBox.minX) / 2; + const centerY = (textBox.maxY + textBox.minY) / 2; + + // labelRotate + label.transform([ + [ 't', -centerX, -centerY ], + [ 'r', labelRotate, labelRotate ], + [ 't', centerX, centerY ] + ]); + rect.transform([ + [ 't', -centerX, -centerY ], + [ 'r', labelRotate, labelRotate ], + [ 't', centerX, centerY ] + ]); + } Util.toFront(label); } }, diff --git a/src/shape/groups/common.js b/src/shape/groups/common.js index d97c3168fe..2bcba289fe 100755 --- a/src/shape/groups/common.js +++ b/src/shape/groups/common.js @@ -31,7 +31,7 @@ Shape.registerGroup('common', { x = x + margin[0]; y = y + margin[1]; const model = item.getModel(); - const { labelOffsetX, labelOffsetY } = model; + const { labelOffsetX, labelOffsetY, labelRotate } = model; x = labelOffsetX ? labelOffsetX + x : x; y = labelOffsetY ? labelOffsetY + y : y; const attrs = Util.mix(true, {}, Global.labelStyle, { @@ -45,10 +45,13 @@ Shape.registerGroup('common', { } else { Util.mix(attrs, label); } - group.addShape('text', { + const labelShape = group.addShape('text', { class: 'label', attrs }); + if (labelRotate) { + labelShape.rotate(labelRotate); + } }, drawKeyShape(item, box) { const { x, y, width, height } = box; diff --git a/src/shape/nodes/common.js b/src/shape/nodes/common.js index a01df26eb2..7df4d309a3 100755 --- a/src/shape/nodes/common.js +++ b/src/shape/nodes/common.js @@ -53,7 +53,7 @@ Shape.registerNode('common', { const group = item.getGraphicGroup(); const label = this.getLabel(item); const model = item.getModel(); - const { labelOffsetX, labelOffsetY } = model; + const { labelOffsetX, labelOffsetY, labelRotate } = model; if (Util.isNil(label)) { return; } @@ -66,10 +66,14 @@ Shape.registerNode('common', { } else { Util.mix(attrs, label); } - return group.addShape('text', { + const labelShape = group.addShape('text', { class: 'label', attrs }); + if (labelRotate) { + labelShape.rotate(labelRotate); + } + return labelShape; }, getPath(item) { const size = this.getSize(item); diff --git a/test/unit/item/edge-spec.js b/test/unit/item/edge-spec.js index 9e7058dc61..d5b319d341 100644 --- a/test/unit/item/edge-spec.js +++ b/test/unit/item/edge-spec.js @@ -28,7 +28,7 @@ describe('edge item test', () => { const edge = graph.find('node2->node1'); expect(edge.getTarget().id).equal('node1'); }); - it('labeloffset', () => { + it('label offset', () => { const edge = graph.find('node2->node1'); graph.update(edge, { label: 'node2->node1', @@ -38,6 +38,14 @@ describe('edge item test', () => { expect(edge.getLabel().attr('x')).to.almost.eql(162.23606797749977); expect(edge.getLabel().attr('y')).to.almost.eql(136.11803398874991); }); + it('label rotate', () => { + const edge = graph.find('node2->node1'); + graph.update(edge, { + label: 'node2->node1', + labelRotate: Math.PI / 2 + }); + expect(edge.getLabel().getMatrix()).to.almost.deep.eql([ 6.123233995736766e-17, 1, 0, -1, 6.123233995736766e-17, 0, 298.3541019662497, -26.118033988749858, 1 ]); + }); it('destroy', () => { graph.destroy(); }); diff --git a/test/unit/item/group-spec.js b/test/unit/item/group-spec.js index c6e6ed4a23..a1a3b8a973 100644 --- a/test/unit/item/group-spec.js +++ b/test/unit/item/group-spec.js @@ -32,7 +32,7 @@ describe('group item test', () => { it('getInnerEdges', () => { expect(graph.find('group1').getInnerEdges().length).eql(2); }); - it('labeloffset', () => { + it('label offset', () => { const group = graph.find('group1'); graph.update(group, { label: 'group1', @@ -42,6 +42,14 @@ describe('group item test', () => { expect(group.getLabel().attr('x')).to.almost.eql(87.5); expect(group.getLabel().attr('y')).to.almost.eql(57.5); }); + it('label rotate', () => { + const group = graph.find('group1'); + graph.update(group, { + label: 'group1', + labelRotate: Math.PI / 2 + }); + expect(group.getLabel().getMatrix()).to.almost.deep.eql([ 6.123233995736766e-17, 1, 0, -1, 6.123233995736766e-17, 0, 0, 0, 1 ]); + }); it('destroy', () => { graph.destroy(); }); diff --git a/test/unit/item/node-spec.js b/test/unit/item/node-spec.js index 47b9a54291..678d787aa0 100644 --- a/test/unit/item/node-spec.js +++ b/test/unit/item/node-spec.js @@ -50,7 +50,7 @@ describe('node item test', () => { 41 }); }); - it('labeloffset', () => { + it('label offset', () => { const node = graph.find('node1'); graph.update(node, { label: 'node1', @@ -60,6 +60,14 @@ describe('node item test', () => { expect(node.getLabel().attr('x')).to.almost.eql(10); expect(node.getLabel().attr('y')).to.almost.eql(10); }); + it('label rotate', () => { + const node = graph.find('node2'); + graph.update(node, { + label: 'node2', + labelRotate: Math.PI / 2 + }); + expect(node.getLabel().getMatrix()).to.almost.deep.eql([ 6.123233995736766e-17, 1, 0, -1, 6.123233995736766e-17, 0, 0, 0, 1 ]); + }); it('destroy', () => { graph.destroy(); });