Published
Edited
Jun 18, 2021
Insert cell
Insert cell
viewof myMap = deckMap({
geography: usaData.geojsonData,
data: data,
height: height,
width: width,
initialViewState: initialViewState,
geographyIdVariable:'dept',
dataIdVariable:'dept',
colorVariable:'Litercy',
breaks: breaks,
colorBins: colorBins,
// ordinalLabels: ['Lowest', 'Lower', 'Medium', 'Higher','Highest'],
controller: false,
title: "France Literacy ca. 1830",
})
Insert cell
a = [1,2,3]
Insert cell
b = [2,3,4]
Insert cell
[...a,...b]
Insert cell
height = 300
Insert cell
width = 500
Insert cell
usaData = loadGeojson(geoda, 'https://nofurtherinformation.static.observableusercontent.com/files/602dde7c42338e81f502e2dc7d76e911439faf63cc6e6b83a67a4d82e46c154a1f729feeabe2e981b92e1286af2e7036e51f22aed9d1bf7bf3c8cc17dd4e19f9?response-content-disposition=attachment%3Bfilename*%3DUTF-8%27%27guerry_wgs84.geojson&Expires=1624017600000&Key-Pair-Id=APKAJCHFJLLLU4Y2WVSQ&Signature=kHeohtthYoQwhlvD7u~uUyfKPhB9hA-yVs3rFFRT2W5G7D7Eq6imc2EfpCZDNQDEbvLzLDG84Ukw~SfkikNGe8UzBt9lFZk4odfdhDbORZvtMeQPPTyoOZ6Hf8uFaGylnMd2-f7hEWEYax-q4I2011PHFogqioCQxpiCM8aPABUph-5jJ7KzMWlqlfv-eM37sof6F8saN3sL2I~N~~6b3jilcp6tXKQXFj4aGE4ECjD8MvCz67EmQlx435V8aVmhTGhwl6SG438rZ6idl0kVDaMlhw17L5IvBVDcpiHgltffvg9HipkHv2a854e4f2Qdnz5c6nQ3e3XaHqPTGE1cjg__')
Insert cell
k = 5
Insert cell
colorBins = d3.schemeGreens[5]
Insert cell
breaks = geoda.naturalBreaks(k, geoda.getColumn(usaData.id, 'Litercy'));
Insert cell
data = usaData.geojsonData.features.map(f => f.properties)
Insert cell
initialViewState = getViewPort(geoda, usaData.id, height, width)
Insert cell
Insert cell
function deckMap({
initialViewState={
latitude:0,
longitude:0,
zoom:0
},
geography={},
data={},
colorBins=[],
breaks=[],
ordinalLabels=[],
controller=true,
geographyIdVariable='',
dataIdVariable='',
colorVariable='',
width=500,
height=500,
customDeckParams={},
title='',
strokeColor="#999999"
}){
const container = html `<div style="height:${height}px;width:${width}px"></div>`
const dataById = d3.map(data, d => d[dataIdVariable]);
const deckgl = new deck.DeckGL({
container: container,
map: false,
// Viewport settings
initialViewState,
controller,
glOptions: {
preserveDrawingBuffer: true // https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext
},
});

const colorScale = d3.scaleThreshold(breaks, colorBins)

const layer = new deck.GeoJsonLayer({
id: `geojson`,
data: geography,
getFillColor: f => {
if (dataById.has(f.properties[geographyIdVariable])) {
const datum = dataById.get(f.properties[geographyIdVariable]);
return hexToRgb(colorScale(datum[colorVariable]));
} else {
return [240,240,240]
}
},
getLineColor: hexToRgb(strokeColor),
lineWidthScale: 1,
lineWidthMinPixels: 1,
getLineWidth: 1,
...customDeckParams
});
deckgl.setProps({layers: [layer]});

const mapLegend = ordinalLabels.length
? swatches({color: d3.scaleOrdinal(ordinalLabels, colorBins), columns: 1})
: legend({color: colorScale, width: width/3})
return html`<div style="height:${height}px;width:${width}px">
<span id="meta" style="z-index:5">
<h2>${title}</h2>
${mapLegend}
</span>
${container}
</div>`
}
Insert cell
Insert cell
// thanks! https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb
function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? [
parseInt(result[1], 16),
parseInt(result[2], 16),
parseInt(result[3], 16)
] : null;
}
Insert cell
async function loadGeojson(geoda, url){
if (!geoda.version) return;
const response = await fetch(url)
const clone = response.clone()
const [geojsonData, ab] = await Promise.all([response.json(), clone.arrayBuffer()])
const id = geoda.readGeoJSON(ab)
return {geojsonData, id}
}
Insert cell
function getViewPort(geoda, id, height, width){
const bounds = geoda.getBounds(id)
return mathgl.fitBounds({
height: height,
width: width,
bounds: [[bounds[0],bounds[2]],[bounds[1],bounds[3]]]
})
}
Insert cell
Insert cell
geoda = await jsgeoda.New()
Insert cell
jsgeoda = import('https://cdn.skypack.dev/jsgeoda')
Insert cell
deck = require.alias({
h3: {},
s2Geometry: {}
})('deck.gl@^8.0.0/dist.min.js')
Insert cell
d3 = require("https://d3js.org/d3.v5.min.js")
Insert cell
mathgl = import('https://cdn.skypack.dev/@math.gl/web-mercator')
Insert cell
import {legend, swatches} from "@d3/color-legend"
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