Published
Edited
Nov 11, 2020
1 fork
Insert cell
Insert cell
container = html`<div style='height:500px;' />`
Insert cell
map = {
let map = this;

const mapboxStyle = FileAttachment("os_night.json").url();
if (!map) {
map = new mapboxgl.Map({
container,
center: [0.05, 51.459],
zoom: 12,
pitch: 45,
style:
'https://s3.us-east-2.amazonaws.com/eviction-lab-prototypes/bubble-colors/assets/style.json'
});
}

let trafficLayer = {
id: "traffic_points",
type: "circle",
source: "traffic",
paint: {
"circle-color": circleColor,
"circle-opacity": circleOpacity,
"circle-stroke-color": circleStrokeColor,
"circle-stroke-width": 0.75,
"circle-radius": 10
}
};

// Wait until the map loads, then yield the container again.
yield new Promise(resolve => {
if (map.loaded()) resolve(map);
else map.on('load', () => resolve(map));
});

let bgLayer = map.setPaintProperty('background', 'background-color', bgColor);

if (!map.getSource('traffic')) {
map.addSource('traffic', {
type: 'geojson',
data: {
type: "FeatureCollection",
features: traffic
}
});
}
if (map.getLayer(trafficLayer.id)) {
map.removeLayer(trafficLayer.id);
}
map.addLayer(trafficLayer);

var popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false
});

map.on('mouseenter', 'traffic_points', function(e) {
map.getCanvas().style.cursor = 'pointer';

var coordinates = e.features[0].geometry.coordinates.slice();

var props = Object.keys(e.features[0].properties).filter(
el => el.indexOf('road') !== -1
);
var filter = {};
props.map(el => (filter[el] = e.features[0].properties[el]));
var description = `<pre>${JSON.stringify(filter, null, 2).replace(
/{|}/g,
''
)}</pre>`;

// Ensure that if the map is zoomed out such that multiple
// copies of the feature are visible, the popup appears
// over the copy being pointed to.
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}

popup
.setLngLat(coordinates)
.setHTML(description)
.addTo(map);
});

map.on('mouseleave', 'traffic_points', function() {
map.getCanvas().style.cursor = '';
popup.remove();
});
}
Insert cell
json = function() {
const url =
'https://roadtraffic.dft.gov.uk/api/count-points?filter[local_authority_id]=105';
const r = d3.json(url);
return r;
}
Insert cell
Insert cell
traffic
Insert cell
viewof circleColor = html`<input type="color" value="#ff0400">`
Insert cell
viewof circleStrokeColor = html`<input type="color" value="#ffffff">`
Insert cell
viewof bgColor = html`<input type="color" value="#46464D">`
Insert cell
viewof circleOpacity = html`<input type="number" min="0", max="1" step="0.01" value="0.65">`
Insert cell
FileAttachment("os_night.json").url()
Insert cell
import { soFetch } from '@alecglassford/so-fetch'
Insert cell
d3 = require('d3')
Insert cell
mapboxgl = {
let mapboxgl = await require('mapbox-gl@0.43.0');
return mapboxgl;
}
Insert cell
Insert cell
md`# Bibliography`
Insert cell
md`@pjsier/mapbox-gl-bubble-map#counties`
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