Published
Edited
Oct 20, 2021
4 stars
Insert cell
Insert cell
Insert cell
height =500
Insert cell
Insert cell
projection = d3['geoOrthographic']()
Insert cell
md`## Globes

As a start, render some globes with standard and readily available data.`
Insert cell
sphere = ({type: "Sphere"})
Insert cell
Insert cell
graticule = d3.geoGraticule10()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
land = topojson.feature(world, world.objects.land)
Insert cell
versor = require("versor@0.0.3")
Insert cell
Insert cell
Insert cell
topojson = require("topojson-client@3")
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
top10 = ["Vega","Sirius","Capella","Rigel","Canopus","Arcturus","Procyon","Betelgeuse","Rigil Kentaurus","Achernar"]
Insert cell
md`View the table of the merged data, filtered by top10 stars.`
Insert cell
Inputs.table(starProperties.filter(x=>top10.includes(x.name)))
Insert cell
top10Ids=starProperties.filter(x=>top10.includes(x.name)).map((x)=> x.id)
Insert cell
Insert cell
vl.markPoint() // Make a dot plot
.data(starProperties)
.encode(
vl.size().fieldQ("mag").scale({reverse:"true"}),
vl.color().fieldQ("mag").scale({reverse:"true"}).legend(null),
vl.x().fieldQ("mag").title("Steller magnitude"),
)
.render()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
star_globe = {
const context = DOM.context2d(width, height);
const starPath = d3.geoPath(projection, context)
.pointRadius(d => magnitudeScale(d.properties.mag)*2);
// create our color scale
const colorScale = d3.scaleSequential(d3.interpolateRdBu)
context.fillStyle = 'black';
context.fillRect(0, 0, width, height);
stars.features.forEach(star => {
context.beginPath(), starPath(star), context.fillStyle = colorScale(star.properties.bv), context.fill();
});
yield context.canvas;
}
Insert cell
md`Now we can experiment with animation. This globe uses a while loop to infinitely change the radius ie stars.features[x].properties.mag of each of the stars with a Math.sin function`
Insert cell
sparkly_stars = {
const context = DOM.context2d(width, height);
while (true) {
const x = (Math.sin(Date.now() / 1000) + 1) / 2 ;
const starPath = d3.geoPath(projection, context)
.pointRadius(d => magnitudeScale(d.properties.mag*x));
const colorScale = d3.scaleSequential(d3.interpolateRdBu)
context.fillStyle = 'black';
context.fillRect(0, 0, width, height);
stars.features.forEach(star => {
context.beginPath(),
starPath(star),
context.fillStyle = "white",
context.fill();
});
yield context.canvas;
}
}
Insert cell
falling_stars = {
const backgroundColor = "black";

const starPath = d3.geoPath(projection).pointRadius(d => magnitudeScale(d.properties.mag));
const svg = d3.create('svg')
.attr("title", "Map")
.attr('width', width)
.attr('height', height)
svg.append("rect")
.attr("width", width)
.attr("height", height)
.attr('fill', backgroundColor);

const all = svg.selectAll('path')
.data(stars.features)
.join('path')
.attr('d', starPath)
.attr('fill', "white")
.attr('stroke', "black")
.attr('stroke-width', 1)

// View one of the SVG paths in console to
console.log('all',all._groups[0][1])

function starFall() {
all
.transition()
.duration(5000)
.attr("transform", 'translate(0,500), scale(2,2)')
}
starFall();
return svg.node();
}
Insert cell
stars.features[0].geometry.coordinates
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
d3 = require("d3@5","d3-interpolate@2")
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