Published
Edited
Sep 15, 2019
Insert cell
Insert cell
Insert cell
Insert cell
firstName = "Arvin"
Insert cell
lastName = "Huang"
Insert cell
handle = "arvinhv"
Insert cell
viewof radius = html`<input type=range min=100 max=300>`
Insert cell
s = 450
Insert cell
// Your D3 code goes here.
// The function badgeCode gets called below to generate the previews.
function badgeCode(g, c, frameNumber) {
// g: a <g>, i.e. d3.select(... the node)
// context: a canvas context, useful for doing canvas drawing
// frameNumber: a number that represents the frame (1, 2, … 10) for psuedo-animation.
// width & height are available via the environment, as is d3
// Note: the <g> is drawn on top of the <canvas>

c.fillStyle = "#34495e";
c.fillRect(0, 0, width, height);
const xPos = width/3.5 + s/2;
const yPos = height/2.5 + s/2;
var projection = d3.geoOrthographic().scale(radius).translate([xPos, yPos]);
projection.rotate([240 + frameNumber * 0.5, -30 + frameNumber * 0.5]);
var path = d3.geoPath(projection, c);
// Draw the seas.
c.lineWidth = 1.5;
c.fillStyle = "#3f3f3f";
c.strokeStyle = "#81a980";
c.beginPath(), c.arc(xPos, yPos, radius, 0, 2 * Math.PI), c.fill(), c.stroke();
// Draw the land.
c.lineWidth = 1.5;
c.strokeStyle = "#81a980";
c.beginPath();
path(world);
c.fill();
c.stroke();
// Draw the cities.
let color = d3.color('#ffba00');
color.opacity = 0.5;
c.fillStyle = color;
path.pointRadius(cityRadius(frameNumber));
cities.features.forEach(city => {
c.beginPath();
path(city);
c.fill();
});
}
Insert cell
isTextLight = true
Insert cell
Insert cell
result = animation
Insert cell
Insert cell
Insert cell
result
Insert cell
Insert cell
import {preview, animation, download, width, height, d3}
with {firstName, lastName, handle, isTextLight, badgeCode}
from "b93171820ba3f268"
Insert cell
Insert cell
topojson = require('topojson')
Insert cell
world = {
let world = await (await fetch("https://unpkg.com/world-atlas@1/world/110m.json")).json();
return topojson.feature(world, world.objects.countries);
}
Insert cell
citiesGeoUrl = 'https://gist.githubusercontent.com/ArvinH/cec17ab68a8173e29706192da30584ca/raw'
Insert cell
cities = {
return await (await fetch(citiesGeoUrl)).json();
}
Insert cell
peoplePerPixel = 50000;
Insert cell
cityRadius = (frameNumber) => {
const rScale = d3.scaleSqrt();
const population_array = [];
for (let i = 0; i < cities.features.length; i++) {
population_array.push(cities.features[i].properties.population);
}
const max_population = population_array.sort(d3.descending)[0]
let rMin = 0;
let rMax = Math.sqrt(max_population / (peoplePerPixel * Math.PI));
rScale.domain([0, max_population]);
rScale.range([rMin, rMax]);
return function(city) {
return city.properties ? rScale(city.properties.population) + frameNumber * 0.2 : 1;
}
}
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