Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions filter-codelab/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select {
width: 100%;
}
80 changes: 80 additions & 0 deletions filter-codelab/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const dscc = require("@google/dscc");

// a global to keep track of state
const state = {
data: undefined,
height: undefined,
width: undefined,
selected: undefined
};

// removes the selector if it already exists
const removeSelect = () => {
let selector = document.getElementsByTagName("select");
if (selector.length > 0) {
document.body.removeChild(selector[0]);
}
};

// what to do when user makes a selection
const click = (d, data) => {
const FILTER = dscc.InteractionType.FILTER;
const actionId = "onClick";
const dimId = data.fields.dimensions[0].id;

const filterData = {
concepts: [dimId],
values: [[d]]
};
dscc.sendInteraction(actionId, FILTER, filterData);
};

// check what is difference in the message
const whatChanged = data => {
const changed = {
data: false,
size: false
};
const vals = data.tables.DEFAULT.rows.map(d => d[0]);
if (JSON.stringify(vals) !== JSON.stringify(state.data)) {
changed.data = true;
}
if (dscc.getHeight() !== state.height || dscc.getWidth() !== state.width) {
changed.size = true;
}
return changed;
};

// make the selector
const makeSelect = data => {
var vals = data.tables.DEFAULT.rows.map(d => d[0]);
let selector = document.createElement("select");
for (let dimVal of vals) {
const option = document.createElement("option");
option.setAttribute("value", dimVal);
const optionText = document.createTextNode(dimVal);
option.appendChild(optionText);
selector.appendChild(option);
}
selector.addEventListener("change", function() {
click(this.value, data);
});

document.body.appendChild(selector);
state.data = vals;
state.width = dscc.getWidth();
state.height = dscc.getHeight();
};

// run every time a new message is sent
const drawViz = data => {
let changed = whatChanged(data);

// if data or height changed, redraw the selector
if (changed.data || changed.size) {
removeSelect();
makeSelect(data);
}
};

dscc.subscribeToData(drawViz, { transform: dscc.tableTransform });
26 changes: 26 additions & 0 deletions filter-codelab/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"data": [
{
"id": "concepts",
"label": "Concepts",
"elements": [
{
"id": "dimensions",
"label": "Dimensions",
"type": "DIMENSION",
"options": {
"min": 1,
"max": 1
}
}
]
}
],
"style": [ ],
"interactions": [
{
"id": "onClick",
"supportedActions": ["FILTER"]
}
]
}
24 changes: 24 additions & 0 deletions filter-codelab/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "My Visualizations",
"organization": "MyOrg",
"description": "My first visualization package.",
"logoUrl": "https://logo",
"organizationUrl": "https://url",
"supportUrl": "https://url",
"privacyPolicyUrl": "https://url",
"termsOfServiceUrl": "https://url",
"packageUrl": "https://url",
"devMode": "DEVMODE_BOOL",
"components": [
{
"name": "table",
"description": "Simple JavaScript Table",
"iconUrl": "https://url",
"resource": {
"js": "YOUR_GCS_BUCKET/index.js",
"config": "YOUR_GCS_BUCKET/index.json",
"css": "YOUR_GCS_BUCKET/index.css"
}
}
]
}