Published
Edited
Oct 2, 2019
Insert cell
Insert cell
Insert cell
L = require('leaflet@1.5.1')
Insert cell
function container() {
return `
<main role="main" class="container">

<div class="row">
<h4> Earthquakes in New Zealand</h4>
</div>

<div class='row'>
<div id="mapid" class="col-6">
</div>
<div class="col-6">
<div id='magnitude-chart'>
<h5> Number of Events by Magnitude </h5>
</div>

<div id='depth-chart'>
<h5> Events by Depth (km) </h5>
</div>
</div>
</div>

<div class='row'>
<div id='time-chart' class="single-col">
<h5> Events per hour </h5>
</div>
</div>

<table class="table table-hover" id="dc-table-graph">
<thead>
<tr class="header">
<th>DTG</th>
<th>Magnitude</th>
<th>Depth</th>
<th>Latitude</th>
<th>Longitude</th>
</tr>
</thead>
</table>

<p>Earthquake data via <a href="https://quakesearch.geonet.org.nz/">Geonet</a>.</p>

</main>
`
}
Insert cell
buildvis = {
let view = md`${container()}`
let dataTable= dc.dataTable(view.querySelector("#dc-table-graph"))
dataTable.width(960)
.height(800)
.dimension(dateDim)
.group(d => "List of all earthquakes corresponding to the filters")
.size(10)
.columns(['dtg','latitude','longitude','magnitude','depth'])
.sortBy(d => d.dtg)
.order(d3.ascending)
let barChartMag = dc.barChart(view.querySelector("#magnitude-chart"))
barChartMag.width(480)
.height(150)
.dimension(magnitudeDim)
.x(d3.scaleLinear()
.range([0,width])
.domain([0,8]))
.xUnits(dc.units.integers)
.gap(56)
.elasticY(true)
.group(terremotoByMagnitude)
let barChartDepth = dc.barChart(view.querySelector("#depth-chart"))
barChartDepth.width(480)
.height(150)
.dimension(depthDim)
.x(d3.scaleLinear()
.range([0,width])
.domain([0,100]))
.xUnits(dc.units.integers)
.gap(1)
.elasticY(true)
.group(terremotoByDepth)

let lineChart = dc.lineChart(view.querySelector("#time-chart"))
lineChart.width(960)
.height(150)
.dimension(depthDim)
.x(d3.scaleTime()
.domain(d3.extent(dataset, d => d.dtg)))
.group(terremotoByHour)
dc.renderAll()
return view
}
Insert cell
dataset = d3.csv("https://gist.githubusercontent.com/emanueles/65a308ffa630689c11a031252998ef8d/raw/a004c770786229d54264406118ae21ba7e4c51a8/earthquakes.csv").then(function(data){
let parseDate = d3.utcParse("%Y-%m-%dT%H:%M:%S")
let magFormat = d3.format(".1f")
let depthFormat = d3.format("d")
data.forEach(function(d){
d.dtg = parseDate(d.origintime.substr(0,19))
d.latitude = +d.latitude
d.longitude = +d.longitude
d.magnitude = magFormat(+d.magnitude)
d.depth = depthFormat(+d.depth)
})
return data
})
Insert cell
map = {
buildvis;
let mapInstance = L.map('mapid').setView([-41.05,172.93], 5)
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://www.openstreetmap.org/">OpenStreetMap</a> contributors',
maxZoom: 17
}).addTo(mapInstance)
return mapInstance
}
Insert cell
circlesLayer = L.layerGroup().addTo(map)
Insert cell
circles = {
circlesLayer.clearLayers()
dataset.forEach( function(d) {
let circle = L.circle([d.latitude, d.longitude], 10000, {
color: '#fd8d3c',
weight: 2,
fillColor: '#fecc5c',
fillOpacity: 0.5
})
let txt = "Magnitude: "+d.magnitude+"<br>Time: "+d.dtg
circle.bindPopup(txt)
circlesLayer.addLayer(circle) })
}
Insert cell
facts = crossfilter(dataset)
Insert cell
dateDim = facts.dimension(d => d.dtg)
Insert cell
magnitudeDim = facts.dimension(d => d.magnitude)
Insert cell
terremotoByMagnitude = magnitudeDim.group()
Insert cell
depthDim = facts.dimension(d => d.depth)
Insert cell
terremotoByDepth = depthDim.group()
Insert cell
hourDim = facts.dimension(d => d3.timeHour(d.dtg))
Insert cell
terremotoByHour = hourDim.group()
Insert cell
Insert cell
dc = require('dc')
Insert cell
crossfilter = require('crossfilter2')
Insert cell
d3 = require('d3')
Insert cell
$ = require('jquery').then(jquery => {
window.jquery = jquery;
return require('popper@1.0.1/index.js').catch(() => jquery);
})
Insert cell
bootstrap = require('bootstrap')
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