|
| 1 | +// |
| 2 | +// Now using a D3 data join to add our data to the visualization. |
| 3 | +// |
| 4 | + |
| 5 | +function pointOnCircle (radius, angleDegrees) { // Helper function to create a point on the perimeter of a circle. |
| 6 | + var angleRadians = (angleDegrees * Math.PI) / 180; |
| 7 | + return { |
| 8 | + x: radius * Math.cos(angleRadians), |
| 9 | + y: radius * Math.sin(angleRadians) |
| 10 | + }; |
| 11 | +}; |
| 12 | + |
| 13 | +window.onload = function () { |
| 14 | + |
| 15 | + var width = window.innerWidth; // Dimensions for our visualization are derived from the size of the browser window. |
| 16 | + var height = window.innerHeight; |
| 17 | + |
| 18 | + var earthRadius = 6371; // This is the real radius of the earth! |
| 19 | + var earthTranslation = "translate(" + (width/2) + ", " + (height/2) + ")"; // Setup a translation to position the earth. |
| 20 | + var maxDistanceFromEarth = 6000; // Let's put a limit on what we can display. |
| 21 | + |
| 22 | + d3.json("data/us-space-junk.json") |
| 23 | + .then(function (spaceJunkData) { |
| 24 | + |
| 25 | + var filteredData = spaceJunkData.filter(spaceJunkRecord => spaceJunkRecord.PERIGEE <= maxDistanceFromEarth); // Filter out data beyond our limit. |
| 26 | + |
| 27 | + filteredData.forEach(function (spaceJunkRecord, rowIndex) { |
| 28 | + spaceJunkRecord.id = rowIndex; |
| 29 | + spaceJunkRecord.orbitAngle = Math.random() * 360; |
| 30 | + }); |
| 31 | + |
| 32 | + var maxOrbitRadius = d3.max(filteredData.map(spaceJunkRecord => earthRadius + spaceJunkRecord.PERIGEE)); // Determine the maximum orbit distance from the earth. |
| 33 | + |
| 34 | + var radiusScale = d3.scaleLinear() // Create a scale for the radius. |
| 35 | + .domain([0, maxOrbitRadius]) |
| 36 | + .range([0, Math.min(height/2, width/2)]); |
| 37 | + |
| 38 | + function computeSpaceJunkPosition (spaceJunkRecord, distance) { // Function to compute the position of space junk within the visualization, we need to reuse this now. |
| 39 | + var orbitRadius = radiusScale(earthRadius + distance); // The distance from the center of the earth that the space junk is orbiting. |
| 40 | + var point = pointOnCircle(orbitRadius, spaceJunkRecord.orbitAngle); // Choose a random position in orbit that is relative to the earth. |
| 41 | + return { |
| 42 | + x: (width/2) + point.x, // Translate the space junk coordinates into visualization-relative coordinates., |
| 43 | + y: (height/2) + point.y |
| 44 | + }; |
| 45 | + }; |
| 46 | + |
| 47 | + function spaceJunkTranslation (spaceJunkRecord, orbitalDistance) { |
| 48 | + var pos = computeSpaceJunkPosition(spaceJunkRecord, orbitalDistance); |
| 49 | + return "translate(" + pos.x + ", " + pos.y + ")" ; |
| 50 | + }; |
| 51 | + |
| 52 | + function spaceJunkTranslationStart (spaceJunkRecord) { |
| 53 | + return spaceJunkTranslation(spaceJunkRecord, 0); |
| 54 | + }; |
| 55 | + |
| 56 | + function spaceJunkTranslationEnd (spaceJunkRecord) { |
| 57 | + return spaceJunkTranslation(spaceJunkRecord, spaceJunkRecord.PERIGEE); |
| 58 | + }; |
| 59 | + |
| 60 | + function addText (className, text, pos, offset) { // Helper function to add some hover text. |
| 61 | + svgElement.append("text") // Append the hover text to the end of the SVG so it is rendered over the top of everything else. |
| 62 | + .attr("class", className) // Id the text so we can remove it later. |
| 63 | + .attr("x", pos.x) |
| 64 | + .attr("y", pos.y + offset) // Offset the Y position slightly so the text is below the space junk. |
| 65 | + .text(text); |
| 66 | + }; |
| 67 | + |
| 68 | + function hover (spaceJunkRecord, index) { // Function called when a space junk is hovered. |
| 69 | + |
| 70 | + d3.select(this) |
| 71 | + .select("circle") |
| 72 | + .attr("r", 6); // Make the hovered space junk larger. |
| 73 | + |
| 74 | + var pos = computeSpaceJunkPosition(spaceJunkRecord, spaceJunkRecord.PERIGEE); |
| 75 | + addText("hover-text hover-title", spaceJunkRecord.OBJECT_NAME, pos, 50); |
| 76 | + addText("hover-text", "Size: " + spaceJunkRecord.RCS_SIZE, pos, 70); |
| 77 | + addText("hover-text", "Launched: " + spaceJunkRecord.LAUNCH, pos, 85); |
| 78 | + }; |
| 79 | + |
| 80 | + function unhover (spaceJunkRecord, index) { // Function called when a space junk is unhovered. |
| 81 | + |
| 82 | + d3.select(this) |
| 83 | + .select("circle") |
| 84 | + .attr("r", 2); // Revert the hovered space junk to normal size. |
| 85 | + |
| 86 | + d3.selectAll(".hover-text") |
| 87 | + .remove(); // Remove all hover text. |
| 88 | + }; |
| 89 | + |
| 90 | + var svgElement = d3.select("svg.chart") // Select the root SVG element for our visualization. |
| 91 | + .attr("width", width) // Set the width and height of the elemnt. |
| 92 | + .attr("height", height); |
| 93 | + |
| 94 | + var theEarth = svgElement.append("circle") // Add a circle to our visualization to represent the 'earth'. |
| 95 | + theEarth.attr("class", "earth") // Set the CSS class for the element to so that we can style our 'earth'. |
| 96 | + .attr("transform", earthTranslation) // Position the circle in the middle of the visualization. |
| 97 | + .attr("r", radiusScale(earthRadius)); // Set the radius the earth. |
| 98 | + |
| 99 | + var currentYear = 1957; // Current year in the animation. |
| 100 | + addText("title-text", currentYear.toString(), { x: width/2, y: 30 }, 0); |
| 101 | + |
| 102 | + // Animate the current year forward in time. |
| 103 | + setInterval(function () { |
| 104 | + ++currentYear; |
| 105 | + |
| 106 | + svgElement.select(".title-text") // Update the title text to to the current year. |
| 107 | + .text(currentYear.toString()); |
| 108 | + |
| 109 | + var currentData = filteredData.filter(row => moment(row.LAUNCH, "DD/MM/YYYY").year() <= currentYear); // Filter data up until the 'current year'. |
| 110 | + |
| 111 | + const spaceJunk = svgElement.selectAll("g") // Select all g elements. |
| 112 | + .data(currentData, function (spaceJunkRecord) { return spaceJunkRecord.id; }); // 'Join' our data to the selection. |
| 113 | + spaceJunk.enter() // Specify what happens for each incoming data point. |
| 114 | + .append("g") // Append a group element for each data point. |
| 115 | + .on("mouseover", hover) |
| 116 | + .on("mouseout", unhover) |
| 117 | + .attr("class", function (spaceJunkRecord) { // Set CSS clas so we can style our space junk. |
| 118 | + return "junk " + spaceJunkRecord.RCS_SIZE; |
| 119 | + }) |
| 120 | + .attr("transform", spaceJunkTranslationStart); |
| 121 | + |
| 122 | + spaceJunk.transition() // Animate the space junk to its destination position. |
| 123 | + .duration(1000) |
| 124 | + .attr("transform", spaceJunkTranslationEnd) |
| 125 | + .ease(d3.easeBackOut); |
| 126 | + |
| 127 | + spaceJunk.append("circle") // Add a circle to represent the space junk. |
| 128 | + .attr("r", 5) // Hard-coded circle radius. |
| 129 | + .transition() |
| 130 | + .attr("r", 2); |
| 131 | + |
| 132 | + }, 1000); // Go forward one year ever second. |
| 133 | + |
| 134 | + |
| 135 | + }) |
| 136 | + .catch(function (err) { |
| 137 | + console.error("Failed to load data file."); |
| 138 | + console.error(err); |
| 139 | + }); |
| 140 | +}; |
0 commit comments