Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,16 @@ If _url_ is specified, sets the default location for all WASM files. If _url_ i

Global variable for setting default WASM location, this is an alternative to [wasmFolder](#wasmFolder)

### GraphViz (namespace `graphviz`)
### GraphViz (`graphvizlib.wasm`)
GraphViz WASM library, see [graphviz.org](https://www.graphviz.org/) for c++ details.

**Note**: While this package is similar to [Viz.js](https://github.com/mdaines/viz.js), it employs a completely different build methodology taken from [GraphControl](https://github.com/hpcc-systems/GraphControl).
**Note 1**: While this package is similar to [Viz.js](https://github.com/mdaines/viz.js), it employs a completely different build methodology taken from [GraphControl](https://github.com/hpcc-systems/GraphControl).

**Note 2**: All API functions are namespaced in `graphviz`.
**Note 2**: The _GraphViz_ library comes in two flavours
* An exported `graphviz` namespace, where each API function is **asynchrounous**.
* A `graphvizSync` **asynchrounous** function which returns an instance of `graphviz`, where each API function is **synchrounous**.

#### API

<a name="layout" href="#layout">#</a> <b>layout</b>(<i>dotSource</i>[, <i>outputFormat</i>][, <i>layoutEngine</i>]) · [Source](https://github.com/hpcc-systems/hpcc-js-wasm/blob/master/src/graphviz.ts)

Expand Down Expand Up @@ -101,7 +105,6 @@ Convenience function that performs **patchwork** layout, is equivalent to `layou

Convenience function that performs **twopi** layout, is equivalent to `layout(dotSource, outputFormat, "twopi");`


## Quick Example (CDN hosting courtesy of [unpkg.com](https://unpkg.com))
```html
<!DOCTYPE html>
Expand All @@ -118,6 +121,7 @@ Convenience function that performs **twopi** layout, is equivalent to `layout(do

<body>
<div id="placeholder"></div>
<div id="placeholder2"></div>
<script>
const dot = `
digraph G {
Expand Down Expand Up @@ -155,6 +159,11 @@ Convenience function that performs **twopi** layout, is equivalent to `layout(do
const div = document.getElementById("placeholder");
div.innerHTML = svg;
});

hpccWasm.graphvizSync().then(graphviz => {
const div = document.getElementById("placeholder2");
div.innerHTML = graphviz.layout(dot, "svg", "dot"); // Synchronous
});
</script>

</body>
Expand Down
18 changes: 18 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@
</head>

<body>
<h3>One</h3>
<div id="placeholder"></div>
<br>
<h3>Two</h3>
<div id="placeholder2"></div>
<br>
<h3>Three</h3>
<div id="placeholder3"></div>
<h3>End</h3>
<script>
const dot = `
digraph G {
Expand Down Expand Up @@ -53,6 +61,16 @@
const div = document.getElementById("placeholder");
div.innerHTML = svg;
});

hpccWasm.graphviz.layout(dot, "svg", "dot").then(svg => {
const div = document.getElementById("placeholder2");
div.innerHTML = svg;
});

hpccWasm.graphvizSync().then(graphviz => {
const div = document.getElementById("placeholder3");
div.innerHTML = graphviz.layout(dot, "svg", "dot");
});
</script>

</body>
Expand Down
42 changes: 42 additions & 0 deletions src/graphviz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,45 @@ export const graphviz = {
return this.layout(dotSource, outputFormat, "twopi");
}
};

class GraphvizSync {

constructor(private _wasm: any) {
}

layout(dotSource: string, outputFormat: Format = "svg", layoutEngine: Engine = "dot"): string {
return this._wasm.Main.prototype.layout(dotSource, outputFormat, layoutEngine);
}

circo(dotSource: string, outputFormat: Format = "svg"): string {
return this.layout(dotSource, outputFormat, "circo");
}

dot(dotSource: string, outputFormat: Format = "svg"): string {
return this.layout(dotSource, outputFormat, "dot");
}

fdp(dotSource: string, outputFormat: Format = "svg"): string {
return this.layout(dotSource, outputFormat, "fdp");
}

neato(dotSource: string, outputFormat: Format = "svg"): string {
return this.layout(dotSource, outputFormat, "neato");
}

osage(dotSource: string, outputFormat: Format = "svg"): string {
return this.layout(dotSource, outputFormat, "osage");
}

patchwork(dotSource: string, outputFormat: Format = "svg"): string {
return this.layout(dotSource, outputFormat, "patchwork");
}

twopi(dotSource: string, outputFormat: Format = "svg"): string {
return this.layout(dotSource, outputFormat, "twopi");
}
}

export function graphvizSync(): Promise<GraphvizSync> {
return loadWasm(graphvizlib).then(wasm => new GraphvizSync(wasm));
}
62 changes: 53 additions & 9 deletions test/graphviz.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,47 +34,91 @@ digraph G {
`;

describe("graphviz", function () {
const placeholder = document.getElementById("placeholder");
it("circo", function () {
hpccWasm.graphviz.circo(dot, "svg").then(svg => {
expect(svg).to.exist;
return hpccWasm.graphviz.circo(dot, "svg").then(svg => {
expect(svg).to.be.a("string");
expect(svg).to.not.be.empty;
});
});
it("dot", function () {
return hpccWasm.graphviz.dot(dot, "svg").then(svg => {
expect(svg).to.exist;
expect(svg).to.be.a("string");
expect(svg).to.not.be.empty;
});
});
it("fdp", function () {
return hpccWasm.graphviz.fdp(dot, "svg").then(svg => {
expect(svg).to.exist;
expect(svg).to.be.a("string");
expect(svg).to.not.be.empty;
});
});
it("neato", function () {
return hpccWasm.graphviz.neato(dot, "svg").then(svg => {
expect(svg).to.exist;
expect(svg).to.be.a("string");
expect(svg).to.not.be.empty;
});
});
it("osage", function () {
return hpccWasm.graphviz.osage(dot, "svg").then(svg => {
expect(svg).to.exist;
expect(svg).to.be.a("string");
expect(svg).to.not.be.empty;
});
});
it("patchwork", function () {
return hpccWasm.graphviz.patchwork(dot, "svg").then(svg => {
expect(svg).to.exist;
expect(svg).to.be.a("string");
expect(svg).to.not.be.empty;
});
});
it("twopi", function () {
return hpccWasm.graphviz.twopi(dot, "svg").then(svg => {
expect(svg).to.exist;
expect(svg).to.be.a("string");
expect(svg).to.not.be.empty;
});
});
});

describe("graphvizSync", function () {
let graphviz;
it("create", function () {
return hpccWasm.graphvizSync().then(gv => {
graphviz = gv;
expect(graphviz).to.exist;
});
})
it("circo", function () {
const svg = graphviz.circo(dot, "svg");
expect(svg).to.be.a("string");
expect(svg).to.not.be.empty;
});
it("dot", function () {
const svg = graphviz.dot(dot, "svg");
expect(svg).to.be.a("string");
expect(svg).to.not.be.empty;
});
it("fdp", function () {
const svg = graphviz.fdp(dot, "svg");
expect(svg).to.be.a("string");
expect(svg).to.not.be.empty;
});
it("neato", function () {
const svg = graphviz.neato(dot, "svg");
expect(svg).to.be.a("string");
expect(svg).to.not.be.empty;
});
it("osage", function () {
const svg = graphviz.osage(dot, "svg");
expect(svg).to.be.a("string");
expect(svg).to.not.be.empty;
});
it("patchwork", function () {
const svg = graphviz.patchwork(dot, "svg");
expect(svg).to.be.a("string");
expect(svg).to.not.be.empty;
});
it("twopi", function () {
const svg = graphviz.twopi(dot, "svg")
expect(svg).to.be.a("string");
expect(svg).to.not.be.empty;
});
});