Skip to content

Commit 26a19b1

Browse files
committed
Fixed not calculate viewBox and size properly because the lines position might be negative
1 parent 902b905 commit 26a19b1

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

src/flowchart.chart.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ FlowChart.prototype.render = function() {
6060
len = 0,
6161
maxX = 0,
6262
maxY = 0,
63+
minX = 0,
64+
minY = 0,
6365
symbol,
6466
line;
6567

@@ -103,23 +105,38 @@ FlowChart.prototype.render = function() {
103105
maxY = y;
104106
}
105107
}
106-
108+
107109
for (i = 0, len = this.lines.length; i < len; i++) {
108110
line = this.lines[i].getBBox();
109-
var x = line.x2;
110-
var y = line.y2;
111-
if (x > maxX) {
112-
maxX = x;
111+
var x = line.x;
112+
var y = line.y;
113+
var x2 = line.x2;
114+
var y2 = line.y2;
115+
if (x < minX) {
116+
minX = x;
113117
}
114-
if (y > maxY) {
115-
maxY = y;
118+
if (y < minY) {
119+
minY = y;
120+
}
121+
if (x2 > maxX) {
122+
maxX = x2;
123+
}
124+
if (y2 > maxY) {
125+
maxY = y2;
116126
}
117127
}
118128

119129
var scale = this.options['scale'];
120130
var lineWidth = this.options['line-width'];
121-
this.paper.setSize((maxX * scale) + (lineWidth * scale), (maxY * scale) + (lineWidth * scale));
122-
this.paper.setViewBox(0, 0, maxX + lineWidth, maxY + lineWidth, true);
131+
132+
if (minX < 0) minX -= lineWidth;
133+
if (minY < 0) minY -= lineWidth;
134+
135+
var width = maxX + lineWidth - minX;
136+
var height = maxY + lineWidth - minY;
137+
138+
this.paper.setSize(width * scale, height * scale);
139+
this.paper.setViewBox(minX, minY, width, height, true);
123140
};
124141

125142
FlowChart.prototype.clean = function() {

0 commit comments

Comments
 (0)