Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added files for selector #34

Open
wants to merge 1 commit into
base: new-codelabs
Choose a base branch
from
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
musiciancodes marked this conversation as resolved.
Show resolved Hide resolved
const removeSelect = () => {
let selector = document.getElementsByTagName("select");
musiciancodes marked this conversation as resolved.
Show resolved Hide resolved
if (selector.length > 0) {
document.body.removeChild(selector[0]);
}
};

// what to do when user makes a selection
const click = (d, data) => {
musiciancodes marked this conversation as resolved.
Show resolved Hide resolved
musiciancodes marked this conversation as resolved.
Show resolved Hide resolved
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
musiciancodes marked this conversation as resolved.
Show resolved Hide resolved
const whatChanged = data => {
musiciancodes marked this conversation as resolved.
Show resolved Hide resolved
const changed = {
data: false,
size: false
};
const vals = data.tables.DEFAULT.rows.map(d => d[0]);
if (JSON.stringify(vals) !== JSON.stringify(state.data)) {
musiciancodes marked this conversation as resolved.
Show resolved Hide resolved
changed.data = true;
}
if (dscc.getHeight() !== state.height || dscc.getWidth() !== state.width) {
musiciancodes marked this conversation as resolved.
Show resolved Hide resolved
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) {
musiciancodes marked this conversation as resolved.
Show resolved Hide resolved
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);
musiciancodes marked this conversation as resolved.
Show resolved Hide resolved
});

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"
}
}
]
}