Skip to content

Instantly share code, notes, and snippets.

@texodus
Forked from JHawk/.block
Last active Feb 2, 2022
Embed
What would you like to do?
Perspective / Streaming
license: apache-2.0

Demo of Perspective.

This is an example of streaming data into the perspective viewer. This example creates random rows of tick data updating the table every 50ms and limiting the data set to 500 rows.

perspective-viewer {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no" />
<script src="https://cdn.jsdelivr.net/npm/@finos/perspective@latest"></script>
<script src="https://cdn.jsdelivr.net/npm/@finos/perspective-viewer@latest"></script>
<script src="https://cdn.jsdelivr.net/npm/@finos/perspective-viewer-datagrid@latest"></script>
<script src="https://cdn.jsdelivr.net/npm/@finos/perspective-viewer-d3fc@latest"></script>
<script src="streaming.js"></script>
<link rel="stylesheet" href="index.css" />
<link rel="stylesheet" crossorigin="anonymous" href="https://cdn.jsdelivr.net/npm/@finos/perspective-viewer/dist/css/themes.css" />
</head>
<body>
<perspective-viewer></perspective-viewer>
</body>
</html>
var SECURITIES = [
"AAPL.N",
"AMZN.N",
"QQQ.N",
"NVDA.N",
"TSLA.N",
"FB.N",
"MSFT.N",
"TLT.N",
"XIV.N",
"YY.N",
"CSCO.N",
"GOOGL.N",
"PCLN.N",
];
var CLIENTS = [
"Homer",
"Marge",
"Bart",
"Lisa",
"Maggie",
"Moe",
"Lenny",
"Carl",
"Krusty",
];
// Create 5 random rows of data.
function newRows() {
var rows = [];
for (var x = 0; x < 50; x++) {
rows.push({
name: SECURITIES[Math.floor(Math.random() * SECURITIES.length)],
client: CLIENTS[Math.floor(Math.random() * CLIENTS.length)],
lastUpdate: new Date(),
chg: Math.random() * 20 - 10,
bid: Math.random() * 10 + 90,
ask: Math.random() * 10 + 100,
vol: Math.random() * 10 + 100,
});
}
return rows;
}
window.addEventListener("DOMContentLoaded", async function () {
// Get element from the DOM.
var elem = document.getElementsByTagName("perspective-viewer")[0];
// Create a new Perspective WebWorker instance.
var worker = perspective.worker();
// Create a new Perspective table in our `worker`, and limit it it 500 rows.
var table = await worker.table(newRows(), {
limit: 500,
});
// Load the `table` in the `<perspective-viewer>` DOM reference.
await elem.load(Promise.resolve(table));
elem.restore({
plugin: "Datagrid",
columns: ["(-)chg", "chg", "(+)chg"],
expressions: [
'//(-)chg\nif("chg"<0){"chg"}else{0}',
'//(+)chg\nif("chg">0){"chg"}else{0}',
],
group_by: ["name"],
split_by: ["client"],
aggregates: {"(-)chg": "avg", "(+)chg": "avg", chg: "avg"},
sort: [["chg", "desc"]],
plugin_config: {
"(-)chg": {
number_color_mode: "bar",
gradient: 10,
},
"(+)chg": {
number_color_mode: "bar",
gradient: 10,
},
chg: {
number_color_mode: "gradient",
gradient: 10,
},
},
});
// Add more rows every 50ms using the `update()` method on the `table` directly.
(function postRow() {
table.update(newRows());
setTimeout(postRow, 50);
})();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment