Published
Edited
Sep 19, 2022
1 fork
3 stars
Insert cell
Insert cell
Insert cell
starMap = {
const svg = DOM.svg(width,width);
function toRadians(angle) {
return angle * (Math.PI / 180);
};
var split_stars = stars.split(/\r?\n/);
var all_stars = [];
for (var star of split_stars) {
var newStar = {
'name': star.substring(0,25).trim(),
'heading': rotateIt + parseFloat(star.substring(41,46).trim()), // 0 through 360, 0 is toward galaxy center
'ascension': star.substring(47,52).trim(), // -90 through 90
'apparent_mag': star.substring(73,78).trim(),
'abs_mag': star.substring(79,84).trim(),
'distance': star.substring(92,97).trim()
}
all_stars.push(newStar)
}
var grid_lines = [];
for (var i = 0; i < 360; i+=10) {
grid_lines.push(i + rotateIt);
}
var grid_circles = [10,20,30,40,50];
var scale_num = 7;
var scale_factor = 2;
var select = d3.select(svg);
select.append('rect')
.attr('x',0).attr('y',0).attr('width',width).attr('height',width)
.attr('fill','black');
// zero point
select.append("line")
.attr('x1',width/2 + 50*scale_num*Math.sin(toRadians(0+rotateIt)))
.attr('y1',width/2 + 50*scale_num*Math.cos(toRadians(0+rotateIt))/scale_factor)
.attr('x2',width/2 + 60*scale_num*Math.sin(toRadians(0+rotateIt)))
.attr('y2',width/2 + 60*scale_num*Math.cos(toRadians(0+rotateIt))/scale_factor)
.attr('stroke','goldenrod')
select.append("line")
.attr('x1',width/2 + 55*scale_num*Math.sin(toRadians(2+rotateIt)))
.attr('y1',width/2 + 55*scale_num*Math.cos(toRadians(2+rotateIt))/scale_factor)
.attr('x2',width/2 + 60*scale_num*Math.sin(toRadians(0+rotateIt)))
.attr('y2',width/2 + 60*scale_num*Math.cos(toRadians(0+rotateIt))/scale_factor)
.attr('stroke','goldenrod')
select.append("line")
.attr('x1',width/2 + 55*scale_num*Math.sin(toRadians(-2+rotateIt)))
.attr('y1',width/2 + 55*scale_num*Math.cos(toRadians(-2+rotateIt))/scale_factor)
.attr('x2',width/2 + 60*scale_num*Math.sin(toRadians(0+rotateIt)))
.attr('y2',width/2 + 60*scale_num*Math.cos(toRadians(0+rotateIt))/scale_factor)
.attr('stroke','goldenrod')
// Radial grid, circles
select.selectAll('ellipse.grid_circle')
.data(grid_circles)
.enter()
.append('ellipse')
.attr('class','grid_circle')
.attr('cx',width/2)
.attr('cy',width/2)
.attr('rx',d => d*scale_num)
.attr('ry',d => d*scale_num/scale_factor)
.attr('fill','none')
.attr('stroke','goldenrod')
// .attr('stroke-dasharray','5,5')
// Radial grid, lines
select.selectAll('line.grid_lines')
.data(grid_lines)
.enter()
.append('line')
.attr('class','grid_lines')
.attr('x1',width/2)
.attr('y1',width/2)
.attr('x2',d => width/2 + (50*scale_num)*Math.sin(toRadians(d)))
.attr('y2',d => width/2 + (50*scale_num)*Math.cos(toRadians(d))/scale_factor)
.attr('stroke','goldenrod')
// .attr('stroke-dasharray','5,5')
// stars
// the point math is translate to center, then calculate radius * heading
// radius is distance * abs value of trig math for ascension (cah) times scale
// also fun fact, absolute magnitude big numbers means dimmer
// select.selectAll('circle.star')
// .data(all_stars)
// .enter()
// .append('circle')
// .attr('class','star')
// .attr('cx',d => width/2 + (d.distance*Math.abs(Math.cos(toRadians(d.ascension)))*scale_num) * Math.sin(toRadians(d.heading)))
// .attr('cy',d => width/2 + (d.distance*Math.abs(Math.cos(toRadians(d.ascension)))*scale_num) * Math.cos(toRadians(d.heading)) / scale_factor)
// .attr('r',1)
// .attr('fill','white')
// .attr('stroke','white')
// straight line
// select.selectAll('line.star_line')
// .data(all_stars)
// .enter()
// .append('line')
// .attr('class','star_line')
// .attr('x1',width/2)
// .attr('y1',width/2)
// .attr('x2',d => width/2 + (d.distance*Math.abs(Math.cos(toRadians(d.ascension)))*scale_num) * Math.sin(toRadians(d.heading)))
// .attr('y2',d => width/2 + (d.distance*Math.abs(Math.cos(toRadians(d.ascension)))*scale_num) * Math.cos(toRadians(d.heading)) / scale_factor - (d.distance*scale_num*Math.sin(toRadians(d.ascension))))
// .attr('stroke','blue')
// elevated line
select.selectAll('line.star_elv')
.data(all_stars)
.enter()
.append('line')
.attr('class','star_elv')
.attr('x1',d => width/2 + (d.distance*Math.abs(Math.cos(toRadians(d.ascension)))*scale_num) * Math.sin(toRadians(d.heading)))
.attr('y1',d => width/2 + (d.distance*Math.abs(Math.cos(toRadians(d.ascension)))*scale_num) * Math.cos(toRadians(d.heading)) / scale_factor)
.attr('x2',d => width/2 + (d.distance*Math.abs(Math.cos(toRadians(d.ascension)))*scale_num) * Math.sin(toRadians(d.heading)))
.attr('y2',d => width/2 + (d.distance*Math.abs(Math.cos(toRadians(d.ascension)))*scale_num) * Math.cos(toRadians(d.heading)) / scale_factor - (d.distance*scale_num*Math.sin(toRadians(d.ascension))))
.attr('stroke','goldenrod')
.attr('stroke-width',function(d){
if(Math.sin(toRadians(d.ascension)) > 0) {
return '1'
} else {
return '1'
}
})
.attr('stroke-dasharray',function(d){
if(Math.sin(toRadians(d.ascension)) > 0) {
return 'none'
} else {
return '5,5'
}
})
// plane line
// select.selectAll('line.star_line_plane')
// .data(all_stars)
// .enter()
// .append('line')
// .attr('class','star_line_plane')
// .attr('x1',width/2)
// .attr('y1',width/2)
// .attr('x2',d => width/2 + (d.distance*Math.abs(Math.cos(toRadians(d.ascension)))*scale_num) * Math.sin(toRadians(d.heading)))
// .attr('y2',d => width/2 + (d.distance*Math.abs(Math.cos(toRadians(d.ascension)))*scale_num) * Math.cos(toRadians(d.heading)) / scale_factor)
// .attr('stroke','purple')
// elevated stars
select.selectAll('circle.star_elv')
.data(all_stars)
.enter()
.append('circle')
.attr('class','star_elv')
.attr('cx',d => width/2 + (d.distance*Math.abs(Math.cos(toRadians(d.ascension)))*scale_num) * Math.sin(toRadians(d.heading)))
.attr('cy',d => width/2 + (d.distance*Math.abs(Math.cos(toRadians(d.ascension)))*scale_num) * Math.cos(toRadians(d.heading)) / scale_factor - (d.distance*scale_num*Math.sin(toRadians(d.ascension)))) // may want to divide by scale factor, not sure
.attr('r',4)
.attr('fill',function(d){
if(Math.sin(toRadians(d.ascension)) > 0) {
return 'white'
} else {
return 'gray'
}
})
// Sol
select.append("circle")
.attr('cx',width/2)
.attr('cy',width/2)
.attr('r',8)
.attr('fill','white')
return svg;
}
Insert cell
stars = d3.text("https://gist.githubusercontent.com/kcsluis/72adca708f8d5027dc0d712696032e40/raw/07ba0652e96f3c5b76fb468f40155bb84ab5b33f/all_stars.txt")
Insert cell
import {slider} from "@jashkenas/inputs"
Insert cell
d3 = require('d3@5.8')
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