Skip to content

Commit

Permalink
Add pie wasm example
Browse files Browse the repository at this point in the history
  • Loading branch information
aleics committed Aug 29, 2024
1 parent 49ef415 commit f4d59db
Show file tree
Hide file tree
Showing 9 changed files with 3,951 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bruc-web/examples/pie-chart/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
1 change: 1 addition & 0 deletions bruc-web/examples/pie-chart/.node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v16.19.1
1 change: 1 addition & 0 deletions bruc-web/examples/pie-chart/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v16.19.1
5 changes: 5 additions & 0 deletions bruc-web/examples/pie-chart/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// A dependency graph that contains any wasm must all be imported
// asynchronously. This `bootstrap.js` file does the single async import, so
// that no one else needs to worry about it again.
import("./index.js")
.catch(e => console.error("Error importing `index.js`:", e));
11 changes: 11 additions & 0 deletions bruc-web/examples/pie-chart/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html>
<body>
<div id="first"></div>
<noscript
>This page contains webassembly and javascript content, please
enable javascript in your browser.</noscript
>
<script src="./index.js"></script>
</body>
</html>
52 changes: 52 additions & 0 deletions bruc-web/examples/pie-chart/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Bruc } from "bruc";

const spec = `{
"dimensions": {
"width": 500,
"height": 500
},
"data": [
{
"name": "primary",
"values": []
}
],
"visual": {
"shapes": [
{
"from": "primary",
"type": "pie",
"properties": {
"value": { "field": "y" }
}
}
]
}
}`;

const bruc = Bruc.build(spec);

await bruc.renderAsSvg("#first");

while (true) {
const data = randomData();
await bruc.setData("primary", data);

await delay(1000);
}

function randomData() {
const values = [];
for (let i = 0; i <= randomValue(200); i++) {
values.push({ x: i, y: randomValue(50) });
}
return values;
}

function randomValue(max) {
return Math.floor(Math.random() * max);
}

function delay(time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
Loading

0 comments on commit f4d59db

Please sign in to comment.