Skip to content

Instantly share code, notes, and snippets.

@JHawk
Last active December 25, 2020 19:07
Show Gist options
  • Save JHawk/952262145299ffd7fa58d22a51de905d to your computer and use it in GitHub Desktop.
Save JHawk/952262145299ffd7fa58d22a51de905d to your computer and use it in GitHub Desktop.
Perspective Streaming Example
license: apache-2.0

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/build/perspective.js"></script>
<script src="https://unpkg.com/@finos/perspective-viewer/build/perspective.view.js"></script>
<script src="https://unpkg.com/@finos/perspective-viewer-hypergrid/build/hypergrid.plugin.js"></script>
<script src="https://unpkg.com/@finos/perspective-viewer-d3fc/build/d3fc.plugin.js"></script>
<script src="streaming.js"></script>
<link rel='stylesheet' href="index.css">
<link rel='stylesheet' href="https://unpkg.com/@finos/perspective-viewer/build/material.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("WebComponentsReady", 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 = 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