Skip to content

Instantly share code, notes, and snippets.

@texodus

texodus/.block

Forked from JHawk/.block
Last active Apr 20, 2021
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://unpkg.com/@finos/perspective/dist/umd/perspective.js"></script>
<script src="https://unpkg.com/@finos/perspective-viewer/dist/umd/perspective-viewer.js"></script>
<script src="https://unpkg.com/@finos/perspective-viewer-datagrid"></script>
<script src="https://unpkg.com/@finos/perspective-viewer-d3fc"></script>
<script src="streaming.js"></script>
<link rel='stylesheet' href="index.css">
<link rel='stylesheet' href="https://unpkg.com/@finos/perspective-viewer/dist/umd/material-dense.css" is="custom-style">
</head>
<body>
<perspective-viewer row-pivots='["name"]' column-pivots='["client"]' columns='["chg","vol"]'></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 < 5; 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.
elem.load(table);
// 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