Skip to content

Instantly share code, notes, and snippets.

@texodus
Forked from JHawk/.block
Last active July 15, 2019 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save texodus/08fb5f1afccbd33e333453dc70db88ea to your computer and use it in GitHub Desktop.
Save texodus/08fb5f1afccbd33e333453dc70db88ea to your computer and use it in GitHub Desktop.
Perspective Viewers Linked Through Click Events Example
license: apache-2.0
height: 1200

A demo of linked <perspective-viewer>s using superstore.arrow.

const ARROW = "https://unpkg.com/@jpmorganchase/perspective-examples/build/superstore.arrow";
window.addEventListener('WebComponentsReady', function() {
// Fetch and load arrow data.
var xhr = new XMLHttpRequest();
xhr.open('GET', ARROW, true);
xhr.responseType = "arraybuffer"
xhr.onload = function() {
// Get `<perspective-viewer>`s from the DOM.
var el1 = document.getElementsByTagName('perspective-viewer')[0];
var el2 = document.getElementsByTagName('perspective-viewer')[1];
// Create a `table()` using the arrow response.
var table = perspective.shared_worker().table(xhr.response);
// Load the table in each of the `<perspective-viewer>`s DOM reference.
el1.load(table);
el2.load(table);
// Add an event listener to the first `<perspective-viewer>`.
el1.addEventListener("perspective-click", e => {
// Update the second `<perspective-viewer>`, filtering to the row
// clicked on in the first `<perspective-viewer>`.
el2.restore(e.detail.config)
});
}
xhr.send(null);
});
#grid {
display: flex;
max-width: 600px;
max-height: 1200px;
margin: auto;
flex-direction: column;
}
#grid perspective-viewer {
height: 600px;
width: 600px;
flex: 1;
display: block;
}
<!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-hypergrid/dist/umd/perspective-viewer-hypergrid.js"></script>
<script src="https://unpkg.com/@finos/perspective-viewer-d3fc/dist/umd/perspective-viewer-d3fc.js"></script>
<script src="example.js"></script>
<link rel='stylesheet' href="index.css">
<link rel='stylesheet' href="https://unpkg.com/@finos/perspective-viewer/dist/umd/material.css" is="custom-style">
</head>
<body>
<div id="grid">
<perspective-viewer row-pivots='["Region","State","City"]' sort='[["Profit","desc"]]' columns='["Sales", "Profit"]'></perspective-viewer>
<perspective-viewer plugin="d3_sunburst" row-pivots='["Category","Sub-Category","Segment"]' sort='[["Profit","desc"]]' columns='["Sales", "Profit"]'></perspective-viewer>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment