Published
Edited
Nov 22, 2019
4 stars
Insert cell
Insert cell
md `For example, here is a cell that summarizes the results of the Vancouver crime data set and returns the view.`
Insert cell
viewof crimesTotal=embed({
data: {values: vanCrime2},
width: 200,
mark: "bar",
selection: {
barSelection: {fields: ["Offense"], on: "click", type: "multi"}
},
encoding: {
y:{field: "Offense", type: "nominal"},
x:{aggregate: "count", field: "*", type: "quantitative"},
color:{ field: "Offense", type:"nominal","scale": {"scheme": "dark2"}}
}
})
Insert cell
Insert cell
crimesTotal.addSignalListener("barSelection", function(name, value) {
if (value == null)
mutable type = null
else
mutable type = value.Offense
})
Insert cell
Insert cell
Insert cell
offenseSelected = {
if(isEmpty(type))
{
return mutable type = offenseNames
}
return type
}
Insert cell
md `make a table here`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
mutable wall = null
Insert cell
Insert cell
dashboard = html`<table style="width:100%">
<tr>
<td>${viewof crimesTotal}</td>
<td>${viewof splot}</td>
</tr>
</table>`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
md `## Data, Libraries and Functions`
Insert cell
d3 = require("d3@5")
Insert cell
embed = require("vega-embed@6")
Insert cell
function isEmpty(obj) {
for (var prop in obj) {
return false;
}
return true;
}
Insert cell
function getNames(arr)
{
let unique = [...new Set(arr.map(name => name.Offense))];
return(unique);
}
Insert cell
function getFieldVals(arr,field)
{
let unique = [...new Set(arr.map(name =>name[field]))];
var names = {[field]:unique};
return(names);
}
Insert cell
offenses = getNames(vanCrime2);
Insert cell
md `A generalized tool to extract unique values from an array of objects`
Insert cell
Insert cell
Insert cell
Insert cell
vanPop =
d3.csv("https://www.sfu.ca/~lyn/data/VanPop2011.csv", function(d) {
return {
Area: d.Area,
Age: d.Age,
Gender : d.Gender,
Population: +d.Population
};
})
Insert cell
nnames=getFieldVals(vanPop,['Area']);
Insert cell
md `Add the population of the area to the crime data just for ease, as this worknook is about sending signals back and forth and not about data manipulation.`
Insert cell
crimepop = populations
Insert cell
import {populations} from "@drlynb/vancouver-mapping-again"
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more