Published
Edited
Dec 5, 2019
1 fork
Insert cell
md`# Notebook 2 IAT 355 Parks`
Insert cell
import {data} from "@zavenanarsh/park-data-wrangling"
Insert cell
d3 = require('d3@5')
Insert cell
VegaLite = require('vega-embed@5')
Insert cell
VegaLite({
data: {values: data.unionByYear},
mark: "line",
encoding: {
x: {field: "year", type: "quantitative", sort: {encoding: "x", order: "assending"}},
y: {field: "parkVisits", type: "quantitative", aggregate:"sum"},
color: {field: "parkType"}
}
})
Insert cell
VegaLite({
data: {values: data.unionInbound},
mark: "circle",
encoding: {
x: {field: "population", type: "quantitative"},
y: {field: "parkVisits", type: "quantitative", aggregate:"sum"},
color: {field: "year"}
}
})
Insert cell
VegaLite({
data: {values: data.unionByYear},
mark: "area",
encoding: {
x: {field: "year", type: "quantitative"},
y: {field: "percentPerTypePerYear", type: "quantitative", aggregate:"mean"},
color: {field: "parkType", type: "nominal"}
}
})
Insert cell
viewof Population = VegaLite({
width: 800,
height: 100,
data: {values: data.unionByYear},
layer: [{
mark: {type: "area", clip: true, orient: "vertical"},
encoding: {
x: {
field: "year",
type: "quantitative",
scale: {
zero: false,
nice: false
},
},
y: {
field: "population", type: "quantitative",
scale: {domain: [0,100000000]}
},
opacity: { value: 0.3 }
}
},
{
transform: [
{
calculate: "datum.population - 100000000",
as: "popShifted"
}
],
mark: {type: "area", clip: true, orient: "vertical"},

encoding: {
x: {
field: "year",
type: "quantitative",
},
y: {
field: "popShifted", type: "quantitative",
scale: {domain: [0,100000000]}
},
opacity: { value: 0.3 }
}
},
{
transform: [
{
calculate: "datum.population - 200000000",
as: "popShifted2"
}
],
mark: {type: "area", clip: true, orient: "vertical"},

encoding: {
x: {
field: "year",
type: "quantitative",
},
y: {
field: "popShifted2", type: "quantitative",
scale: {domain: [0,100000000]}
},
opacity: { value: 0.3 }
}
},
{
transform: [
{
calculate: "datum.population - 300000000",
as: "popShifted3"
}
],
mark: {type: "area", clip: true, orient: "vertical"},

encoding: {
x: {
field: "year",
type: "quantitative",
},
y: {
field: "popShifted3", type: "quantitative",
scale: {domain: [0,100000000]},
axis: {title: "population"}

},
opacity: { value: 0.3 }
}
},
],
config: {
area: {interpolate: "monotone"}
}
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof Graphs = VegaLite({
data: { values: data.unionByYear },
columns:2,
hconcat: [
//////////////1
{
title: "Relationship of Park Visits to Gas Prices",
width: 300,
height: 280,
transform: [
{filter: {field: "year", lte: range[1]}},
{filter: {field: "year", gte: range[0]}},
],
selection: {
area: { type: "interval" }
},
mark: { type: "circle", clip: true },
encoding: {
x: {
field: "gasPrice",
type: "quantitative",
scale: { domain: [1, d3.max(data.unionByYear, function(d) { return d.gasPrice; })]}

},
y: {
field: "parkVisits",
type: "quantitative",
aggregate:"sum",
axis: {title: "park visits"},
},
size: {
condition: {
selection: "area",
value: 20,
},
value: 5
},
color: {
condition: {
selection: "area",
value: "green",
},
value: "lightgray"
},
}
},
//////////////2

{
title: "Relationship of Park Visits to US Population",

width: 300,
height: 280,
transform: [
{filter: {field: "year", lte: range[1]}},
{filter: {field: "year", gte: range[0]}},
],
selection: {
area: { type: "interval" }
},
mark: { type: "circle", clip: true },
encoding: {
x: {
field: "population",
type: "quantitative",
scale: { domain: [80000000, d3.max(data.unionByYear, function(d) { return d.population; })]
}
},
y: {
field: "parkVisits",
type: "quantitative",
aggregate:"sum",
axis: {title: "park visits"},

scale: {
}
},
size: {
condition: {
selection: "area",
value: 20,
},
value: 5
},
color: {
condition: {
selection: "area",
value: "green",
},
value: "lightgray"
},
}
}
,
//////////////3

{
title: "Relationship of Park Visits to Number of Parks",

width: 300,
height: 280,
transform: [
{filter: {field: "year", lte: range[1]}},
{filter: {field: "year", gte: range[0]}},
],
selection: {
area: { type: "interval" }
},
mark: { type: "circle", clip: true },
encoding: {
y: {
field: "parkVisits",
type: "quantitative",
aggregate:"sum",
axis: {title: "park visits"},

scale: {
}
},
x: {
field: "numberOfParks",
type: "quantitative",
scale: {
}
},
size: {
condition: {
selection: "area",
value: 20,
},
value: 5
},
color: {
condition: {
selection: "area",
value: "green",
},
value: "lightgray"
},
}
},
//////////////4

{
title: "Percentage of Visitors for Each Park Type",

width: 300,
height: 280,
transform: [
{filter: {field: "year", lte: range[1]}},
{filter: {field: "year", gte: range[0]}},
],
selection: {
area: { type: "interval" }
},
mark: { type: "area", clip: true },
encoding: {
x: {field: "year", type: "quantitative"},
y: {field: "percentPerTypePerYear", type: "quantitative", aggregate:"mean", scale: {domain: [0,100]},
axis: {title: "percentage"}},
color: {field: "parkType", type: "nominal"}
}
}
,
//////////////5
{
title: "Relationship of Park Visits to the Year",

width: 300,
height: 280,
transform: [
{filter: {field: "year", lte: range[1]}},
{filter: {field: "year", gte: range[0]}},
],
mark: {
type: "line"
},

encoding: {

x: {
field: "year",
type: "quantitative"
},
y: {
field: "parkVisits",
type: "quantitative",
aggregate: "sum",
axis: {title: "park visits"},


},
color: {
field: "parkType",
type: "nominal"
}

}
},
//////////////6

{
title: "Relationship of Mean Park Visits to Gas Prices",

width: 300,
height: 280,
transform: [
{filter: {field: "year", lte: range[1]}},
{filter: {field: "year", gte: range[0]}},
],
mark: {
type: "line"
},

encoding: {

x: {
field: "year",
type: "quantitative"
},
y: {
field: "perTypePerYearRatioWithVisitor",
type: "quantitative",
axis: {title: "mean park visits"},

aggregate: "mean"
},
color: {
field: "parkType",
type: "nominal"
}

}
}
]
})
Insert cell
viewof ParksLocation = {

let container = DOM.element('div', {
style: `width:${width/1.6}px;height:${width/3}px`
});

yield container;
let unionCentroidFiltered = data.unionCentroid.features.filter(d => {
return d.properties.yearMax > range[0] && d.properties.yearMin < range[1];
});
console.log(unionCentroidFiltered.length);

let map = L.map(container).setView([153.2527, -123.1207], 5).fitBounds(L.geoJson(unionCentroidFiltered).getBounds());
let osmLayer = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

L.geoJSON(unionCentroidFiltered, {
pointToLayer(geoJsonPoint, latlng) {
console.log(geoJsonPoint);
let type = geoJsonPoint.properties.type;
let iconURL = "https://www.sfu.ca/~ridgez/IAT355FinalData/";
return L.marker(latlng, {
icon: L.icon({
iconUrl: iconURL+type+".png",
iconSize: [14, 20],
// iconAnchor: [22, 94],
// popupAnchor: [-3, -76],
// shadowUrl: 'my-icon-shadow.png',
// shadowSize: [68, 95],
// shadowAnchor: [22, 94]
})
});
}
})
.addTo(map)
.on("click", event => {
let popup = new L.Popup();
popup.setLatLng(event.latlng)
let filteredData = data.unionByYear.filter(entry => entry.parkName == event.layer.feature.properties.name);
VegaLite({
width: 200,
height: 200,
data: {
values: filteredData
},
title: {
text: event.layer.feature.properties.name,
fontSize: 10
},
mark: "line",
transform: [
{filter: {field: "year", lte: range[1]}},
{filter: {field: "year", gte: range[0]}},
],

encoding: {

x: {
field: "year",
type: "quantitative",
scale: {domain: [range[0], range[1]]}

},
y: {
field: "parkVisits",
type: "quantitative",
aggregate: "sum"
},

}

}).then(chart => {
popup.setContent(chart);
map.openPopup(popup);
});
});
}
Insert cell
{
const button = html`<button>Fullscreen`;
button.onclick = () => button.parentElement.nextElementSibling.requestFullscreen();
return button;
}
Insert cell
html`
<style>
.body{
height:100vh;
width:100vh;
padding:5rem;
}
.flex-container {
display: flex;
background-color: DodgerBlue;
}
.flex-container-first {
background-color: #f1f1f1;
font-size: 30px;
flex-basis: 60rem;
}
.flex-container-second {
background-color: #f1f1f1;
font-size: 30px;
flex-basis: 60rem;
}
.flex-box {
width: 100%;
}
</style>
<body>
<div class="flex-container">
<div class="flex-container-first">
<div name="a" class="flex-box">${viewof Graphs}</div>
</div>
<div class="flex-container-second">
<div name="b" class="flex-box">${viewof ParksLocation}</div>
<div name="c" class="flex-box">${viewof Population}</div>
<div name="d" class="flex-box">${viewof range}</div>
</div>
</div>
</body>

`
Insert cell
L = require('leaflet@1.2.0')
Insert cell
V = require("@observablehq/vega-lite@0.2")
Insert cell
import {rangeSlider} from '@mootari/range-slider'
Insert cell
html`<link href='${resolve('leaflet@1.2.0/dist/leaflet.css')}' rel='stylesheet' />`
Insert cell
VegaLite({
data: {
values: data.unionByYear
},

columns: 2,
hconcat: [

//////////////1

{
transform: [{
filter: {
field: "year",
lte: range[1]
}
},
{
filter: {
field: "year",
gte: range[0]
}
},
],
selection: {
area: {
type: "interval"
}
},
mark: {
type: "circle",
clip: true
},
encoding: {
x: {
field: "gasPrice",
type: "quantitative",
},
y: {
field: "parkVisits",
type: "quantitative",
aggregate: "average"
},
size: {
condition: {
selection: "area",
value: 20,
},
value: 5
},
color: {
condition: {
selection: "area",
value: "green",
},
value: "lightgray"
},

}
},
]
})
Insert cell
data.unionByYear
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more