Skip to content

Instantly share code, notes, and snippets.

@zemeolotu
Last active March 28, 2020 05:07
Show Gist options
  • Save zemeolotu/68f3f1c2535bdde1a296b90e9b434717 to your computer and use it in GitHub Desktop.
Save zemeolotu/68f3f1c2535bdde1a296b90e9b434717 to your computer and use it in GitHub Desktop.
Perspective Workspace Olympics Example
license: apache-2.0
height: 800

An example of Perspective Workspace with multiple perspective viewers sharing the same table.

Clicking on rows on the perspective viewers in the dark area on the left, filters out the viewers on the right

body {
display: flex;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: 0;
}
<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"></script>
<script src="https://unpkg.com/@finos/perspective-workspace"></script>
<script src="https://unpkg.com/@finos/perspective-viewer-hypergrid"></script>
<script src="https://unpkg.com/@finos/perspective-viewer-d3fc"></script>
<link rel='stylesheet' href="index.css">
<link rel='stylesheet' href="https://unpkg.com/@finos/perspective-workspace@0.4.5/dist/umd/material.css">
<script src="index.js"></script>
</head>
<body>
<perspective-workspace id="workspace"></perspective-workspace>
</body>
</html>
const SCHEMA = {
athlete: "string",
age: "integer",
country: "string",
year: "string",
date: "date",
sport: "string",
gold: "integer",
silver: "integer",
bronze: "integer",
total: "integer",
}
// Define the perspective-viewer configs
const viewers = {
One: {
"row-pivots": ["country"],
sort: [["gold", "desc"]],
columns: ["gold", "silver", "bronze"],
table: "olympics"
},
Two:{
"row-pivots": ["athlete"],
"column-pivots": ["year"],
aggregates: {
age: "avg",
sport: "dominant"
},
sort: [["total", "desc"]],
columns: ["gold", "silver", "bronze", "total"],
name: "Athletes by Year",
table: "olympics"
},
Three:{
plugin: "xy_scatter",
"row-pivots": ["athlete"],
aggregates: {
age: "avg",
sport: "dominant"
},
columns: ["age", "sport", "total", null, null],
name: "Sports by Age",
table: "olympics"
},
Four: {
plugin: "y_bar",
"row-pivots": ["year"],
filters: [
["country", "==", "Great Britain"],
["year", "in", ["2000", "2004", "2008", "2012", ""]]
],
columns: ["gold", "total"],
name: "Medals by Year (Summer)",
table: "olympics"
},
Five: {
plugin: "y_bar",
"row-pivots": ["year"],
filters: [
["country", "==", "Great Britain"],
["year", "in", ["2002", "2006", "2010"]]
],
columns: ["gold", "total"],
name: "Medals by Year (Winter)",
table: "olympics"
}
}
const detail = {
main: {
type: "split-area",
orientation: "vertical",
children: [
{
type: "tab-area",
widgets: ["Two"],
currentIndex: 0
},
{
type: "split-area",
orientation: "horizontal",
children: [
{
type: "tab-area",
widgets: ["Three"],
currentIndex: 0
},
{
type: "tab-area",
widgets: ["Four","Five"],
currentIndex: 0
}
],
sizes: [0.7,0.3]
}
],
sizes: [0.5,0.5]
}
}
window.addEventListener("load", function() {
//request data and initialise table
const request = fetch("https://raw.githubusercontent.com/ag-grid/ag-grid/master/packages/ag-grid-docs/src/olympicWinnersSmall.json")
const worker = perspective.worker();
const table = worker.table(SCHEMA);
request
.then(response => response.json()
.then(data => {
table.update(data)
}))
const workspace = this.document.getElementById("workspace")
window.workspace.tables.set("olympics", table);
const config = {
master: {
widgets: ["One"]
},
detail,
viewers
};
workspace.restore(config)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment