Skip to content

Commit

Permalink
edges included in graph bounding box calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
skanaar committed Jul 8, 2020
1 parent 67dfbe2 commit bb38fc0
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
10 changes: 8 additions & 2 deletions dist/nomnoml.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,21 @@ var nomnoml;
nodes[name].x = node.x;
nodes[name].y = node.y;
});
var edgeWidth = 0;
var edgeHeight = 0;
g.edges().forEach(function (edgeObj) {
var edge = g.edge(edgeObj);
var start = nodes[edgeObj.v];
var end = nodes[edgeObj.w];
rels[edge.id].path = nomnoml.skanaar.flatten([[start], edge.points, [end]]).map(toPoint);
edgeWidth = nomnoml.skanaar.max(edge.points.map(function (e) { return e.x; }));
edgeHeight = nomnoml.skanaar.max(edge.points.map(function (e) { return e.y; }));
});
var graph = g.graph();
var graphHeight = graph.height ? graph.height + 2 * config.gutter : 0;
var graphWidth = graph.width ? graph.width + 2 * config.gutter : 0;
var width = Math.max(graph.width, edgeWidth);
var height = Math.max(graph.height, edgeHeight);
var graphHeight = height ? height + 2 * config.gutter : 0;
var graphWidth = width ? width + 2 * config.gutter : 0;
c.width = Math.max(textSize.width, graphWidth) + 2 * config.padding;
c.height = textSize.height + graphHeight + config.padding;
}
Expand Down
10 changes: 8 additions & 2 deletions src/layouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,21 @@ namespace nomnoml {
nodes[name].x = node.x
nodes[name].y = node.y
})
var edgeWidth = 0;
var edgeHeight = 0;
g.edges().forEach(function(edgeObj) {
var edge = g.edge(edgeObj)
var start = nodes[edgeObj.v]
var end = nodes[edgeObj.w]
rels[edge.id].path = skanaar.flatten([[start], edge.points, [end]]).map(toPoint)
edgeWidth = skanaar.max(edge.points.map(e => e.x))
edgeHeight = skanaar.max(edge.points.map(e => e.y))
})
var graph = g.graph()
var graphHeight = graph.height ? graph.height + 2*config.gutter : 0
var graphWidth = graph.width ? graph.width + 2*config.gutter : 0
var width = Math.max(graph.width, edgeWidth)
var height = Math.max(graph.height, edgeHeight)
var graphHeight = height ? height + 2*config.gutter : 0
var graphWidth = width ? width + 2*config.gutter : 0

c.width = Math.max(textSize.width, graphWidth) + 2*config.padding
c.height = textSize.height + graphHeight + config.padding
Expand Down
2 changes: 1 addition & 1 deletion src/nomnoml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ interface Nomnoml {
draw(canvas: HTMLCanvasElement, code: string, scale: number): { config: Config }
render(graphics: Graphics, config: Config, compartment: nomnoml.Compartment, setFont: nomnoml.SetFont): void
renderSvg(code: string, docCanvas?: HTMLCanvasElement): string
parse(source: string): any
parse(source: string): { root: nomnoml.Compartment; config: Config }
intermediateParse(source: string): nomnoml.AstRoot
transformParseIntoSyntaxTree(entity: nomnoml.AstRoot): nomnoml.Compartment
layout(measurer: Measurer, config: Config, ast: nomnoml.Compartment): nomnoml.Compartment
Expand Down
13 changes: 13 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,19 @@ <h1>Label placement</h1>
</div>
</div>

<div class=testcase>
<h1>Graph size</h1>
<div nomnoml=canvas>
<script type="text/vnd.nomnoml">
[a] - [foo]
[foo] - [b]
[a] - [bar]
[bar] - [b]
[a] - [b]
</script>
</div>
</div>

<div class=testcase>
<h1>CLI output</h1>
<img src="output.node-test.svg">
Expand Down
12 changes: 11 additions & 1 deletion test/nomnoml.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,18 @@ suite.test('layouter should handle [apa|[flea]->[dandruff]] relation placement',

suite.test('gracefully handle equivalent relations', function(){
var parsedGraph = nomnoml.parse('[a]-[b]\n[a]-[b]')

assertEqual(parsedGraph.root.relations.length, 1)
})

suite.test('include edges in canvas size calculation', function(){
var { root, config } = nomnoml.parse(`
[a] - [foo]
[foo] - [b]
[a] - [bar]
[bar] - [b]
[a] - [b]`)
var compartment = nomnoml.layout(measurer, config, root)
assertEqual(compartment.width > 300, true)
})

suite.report()

0 comments on commit bb38fc0

Please sign in to comment.