// Generated by CoffeeScript 1.10.0 (function() { var celestial_body_types, color, height, margin, sun_radius, svg, vis, width; sun_radius = 30; margin = 15; celestial_body_types = [ { label: '', value: 'NULL', color: '#ffc252' }, { label: 'Terrestrial', value: 'terrestrial', color: '#e5d8bd' }, { label: 'Gas giant', value: 'gas_giant', color: 'lightgray' }, { label: 'Ice giant', value: 'ice_giant', color: '#b3cde3' }, { label: 'Asteroid', value: 'asteroid', color: '#fbb4ae' }, { label: 'Plutoid', value: 'plutoid', color: '#decbe4' }, { label: 'Satellite', value: 'satellite', color: 'gray' } ]; color = d3.scaleOrdinal().domain(celestial_body_types.map(function(d) { return d.value; })).range(celestial_body_types.map(function(d) { return d.color; })); svg = d3.select('svg'); width = svg.node().getBoundingClientRect().width; height = svg.node().getBoundingClientRect().height; vis = svg.append('g').attrs({ transform: "translate(0, " + (height / 2) + ")" }); d3.csv('data.csv', function(data) { var all_legend, all_orbits, all_planets, all_suborbits, all_subplanets, angles, distance, en_legend, en_orbits, en_planets, en_suborbits, en_subplanets, leg, legend, orbits, planets, radius, solar_system, suborbits, subplanets; data = data.sort(function(a, b) { return a.distance_from_primary_km - b.distance_from_primary_km; }); solar_system = (d3.stratify().id(function(d) { if (d.label === 'NULL') { return ''; } else { return d.label; } }).parentId(function(d) { if (d.primary === 'NULL') { return ''; } else { return d.primary; } }))(data); distance = d3.scalePow().exponent(0.33).domain([ 8000000, d3.max(solar_system.children, function(d) { return +d.data.distance_from_primary_km; }) ]).range([0, width - sun_radius / 2 - margin]); radius = d3.scalePow().exponent(0.4).domain([ 0, d3.max(solar_system.children, function(d) { return +d.data.equatorial_radius_km; }) ]).range([0, 20]); /* Sun */ vis.append('circle').attrs({ r: sun_radius * 2, cx: -sun_radius / 1.5, fill: function(d) { return color(solar_system.data.subtype); }, stroke: '#ffe27a', 'stroke-width': '5px' }); /* Orbits */ orbits = vis.selectAll('.orbit').data(solar_system.children); en_orbits = orbits.enter().append('g').attrs({ "class": 'orbit' }); all_orbits = en_orbits.merge(orbits); all_orbits.append('circle').attrs({ r: function(d) { return distance(d.data.distance_from_primary_km); } }); orbits.exit().remove(); /* Sub-orbits */ angles = [45, 22.5, 0, -22.5, -45, -22.5, 0, 11, 0, -11, 0, 11, 0]; suborbits = all_orbits.selectAll('.suborbit').data(function(d) { if (d.children != null) { return d.children; } else { return []; } }); en_suborbits = suborbits.enter().append('g').attrs({ "class": 'suborbit', transform: function(d) { var i, x, y; i = d.parent.parent.children.indexOf(d.parent); x = distance(d.parent.data.distance_from_primary_km) * Math.cos(angles[i] * Math.PI / 180); y = distance(d.parent.data.distance_from_primary_km) * Math.sin(angles[i] * Math.PI / 180); return "translate(" + x + ", " + y + ")"; } }); all_suborbits = en_suborbits.merge(suborbits); all_suborbits.append('circle').attrs({ r: function(d, i) { return radius(d.data.distance_from_primary_km); } }); suborbits.exit().remove(); /* Planets */ planets = vis.selectAll('.planet').data(solar_system.children); en_planets = planets.enter().append('g').attrs({ "class": 'planet', transform: function(d, i) { var x, y; x = distance(d.data.distance_from_primary_km) * Math.cos(angles[i] * Math.PI / 180); y = distance(d.data.distance_from_primary_km) * Math.sin(angles[i] * Math.PI / 180); return "translate(" + x + ", " + y + ")"; } }); all_planets = en_planets.merge(planets); all_planets.append('circle').attrs({ "class": 'halo', r: function(d) { return radius(d.data.equatorial_radius_km) + 3; } }); all_planets.append('circle').attrs({ r: function(d) { return radius(d.data.equatorial_radius_km); } }).styles({ fill: function(d) { return color(d.data.subtype); } }).append('title').text(function(d) { return d.data.label; }); all_planets.append('text').attrs({ 'text-anchor': 'middle', y: function(d, i) { return -radius(d.data.equatorial_radius_km) - 5; } }).text(function(d) { return d.id; }); planets.exit().remove(); /* Sub-planets */ subplanets = all_planets.selectAll('.subplanet').data(function(d) { if (d.children != null) { return d.children; } else { return []; } }); en_subplanets = subplanets.enter().append('g').attrs({ "class": 'subplanet' }); all_subplanets = en_subplanets.merge(subplanets); all_subplanets.append('circle').attrs({ "class": 'halo', r: function(d) { return radius(d.data.equatorial_radius_km); }, cx: function(d, i) { var a, r; r = radius(d.data.distance_from_primary_km); a = Math.cos((i * 30 + 60) * Math.PI / 180); return r * a; }, cy: function(d, i) { var a, r; r = radius(d.data.distance_from_primary_km); a = Math.sin((i * 30 + 60) * Math.PI / 180); return r * a; } }); all_subplanets.append('circle').attrs({ r: function(d) { return radius(d.data.equatorial_radius_km); }, cx: function(d, i) { var a, r; r = radius(d.data.distance_from_primary_km); a = Math.cos((i * 30 + 60) * Math.PI / 180); return r * a; }, cy: function(d, i) { var a, r; r = radius(d.data.distance_from_primary_km); a = Math.sin((i * 30 + 60) * Math.PI / 180); return r * a; } }).styles({ fill: function(d) { return color(d.data.type); } }).append('title').text(function(d) { return d.data.label; }); all_subplanets.append('text').attrs({ 'text-anchor': function(d, i) { var a, r; r = radius(d.data.distance_from_primary_km); a = Math.cos((i * 30 + 60) * Math.PI / 180); if (r * a < 0) { return 'end'; } else { return 'start'; } }, x: function(d, i) { var a, r; r = radius(d.data.distance_from_primary_km); a = Math.cos((i * 30 + 60) * Math.PI / 180); return r * a; }, y: function(d, i) { var a, r; r = radius(d.data.distance_from_primary_km); a = Math.sin((i * 30 + 60) * Math.PI / 180); return r * a + 15; } }).text(function(d) { return d.id; }); subplanets.exit().remove(); /* Legend */ leg = vis.append('g').attrs({ "class": 'legend', transform: "translate(15, " + (height / 2 - 105) + ")" }); leg.insert('rect').attrs({ x: -7, y: -7, width: 105, height: 110 }); legend = leg.selectAll('.item').data(celestial_body_types.slice(1)); en_legend = legend.enter().append('g').attrs({ "class": 'item' }); all_legend = en_legend.merge(legend); all_legend.append('circle').attrs({ r: 7, cx: 0, cy: function(d, i) { return i * 18; } }).styles({ fill: function(d) { return d.color; } }); all_legend.append('text').attrs({ x: 12, y: function(d, i) { return i * 18; }, dy: '0.35em' }).text(function(d) { return d.label.toUpperCase(); }); return legend.exit().remove(); }); }).call(this);