D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
Mavromatika
Full window
Github gist
Timeline of the discovery of the Solar System
<!DOCTYPE html> <meta charset="utf-8"> <style> body { background: #000000; font-family: sans-serif; } h1 { color: #eaeaea; text-align: center; } #intro { color: white; font-size: 80%; width: 300px; text-align: justify; margin-left: auto; margin-right: auto; margin-bottom: 60px; } #sources { font-size : 70%; } #sources a { color: white; } #content { width: 1200px; margin-left: auto; margin-right: auto; } .planet{ float:left; position: relative; } .svgPlanet{ position: absolute; top: 0; left: 0; } .low { opacity: 0.2; } text { cursor : default; } </style> <body> <script src="//d3js.org/d3.v3.min.js"></script> <h1>Timeline of the discovery of the Solar System</h1> <div id="intro"><p>The graph below shows the time of discovery of the main celestial bodies in the solar system, as well as the people who discovered them. The planets and dwarf planets and their main satellites (radius > 500 km) are shown.</p><p>Press Ctrl key to toggle interactive mode.</p><p id="sources">Sources : <a href="https://en.wikipedia.org/wiki/Timeline_of_discovery_of_Solar_System_planets_and_their_moons">Wikipedia</a> for the dates, <a href="https://solarsystem.nasa.gov/planets/solarsystem">NASA</a> for the radiuses.</p></div> <div id="content"></div> <script> var historyW = 1200, historyH = 550; var domHistory = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]; var paddingL = 75, paddingR = 120, paddingT = 80, paddingB = 30; var spaceMoons = 4; var tabTypes = {"font":{"planet":"80%","moon":"60%","dwarf":"80%"}, "color":{"planet":"#9a529d","moon":"white","dwarf":"#555db9"}, "size":{"planet":3,"moon":1,"dwarf":3} }; var historyScale = d3.scale.linear().rangeRound([paddingL,historyW-paddingR]).domain([1550,2016]); var catScale = d3.scale.ordinal().rangeRoundPoints([historyH-paddingB-20,paddingT]).domain(domHistory); var historyContainer = d3.select("#content") .append("div").attr("class","history"); var svgHistory = historyContainer.append("svg").attr("width",historyW).attr("height",historyH); //------------------------------------------------------------------------------------------------------------------------------- // History of the discovery of the solar system d3.json("datahistory.json", function(error,data){ if (error) return console.warn(error); // Reformat data for horizontal lines var t = 0; var n = ""; var donnees = data.map(function(c,i,m){ return c.data.map(function(d){ if ((n == c.who && t == c.date) || (c.date-t) < 7 ){ d.visibility = "hidden"; } else { d.visibility = "visible"; } d.date = c.date; if (c.date <= 1550){ d.label = "2000 BC"; } else { d.label = d.date.toString();} d.who = c.who; n = c.who; t = c.date; return d; }); }); donnees = [].concat.apply([],donnees); // Make single array of all the sub arrays' objects. // Sort the data by descending date (so that groups are created from r to l, and vert don't show //through) donnees.sort(function (a, b) { if (a.date > b.date) { return -1; } if (a.date < b.date) { return 1; } return 0; }); var overlay = svgHistory.append("rect") .attr("x",0) .attr("y",0) .attr("width",historyW) .attr("height",historyH) .attr("fill","transparent"); //.attr("stroke","white"); var groups = svgHistory.selectAll(".group") .data(donnees) .enter() .append("g").attr("class","group"); var marks = groups.append("line"); var textY = groups.append("text"); var textWho = groups.append("text"); marks.attr("class","vertical") .attr("x1",function(d){return Math.round(historyScale(d.date));}) .attr("y1",paddingT) .attr("x2",function(d){return Math.round(historyScale(d.date));}) .attr("y2",historyH-paddingB-10) .attr("visibility",function(d){return d.visibility}) .attr("stroke","#696969") .attr("stroke-width",1) .attr("stroke-dasharray","2,7"); textY.attr("class","vertical") .attr("x",function(d){return historyScale(d.date);}) .attr("y",historyH-paddingB) .attr("text-anchor","end") //.attr("dy",".3em") .attr("fill","white") .attr("visibility",function(d){return d.visibility}) .style("font-size","70%") .text(function(d){return d.label;}) .attr("transform",function(d){ return "rotate(-30 " + historyScale(d.date).toString() + " " + (historyH-paddingB).toString() + ")"; }); textWho.attr("class","vertical") .attr("x",function(d){return historyScale(d.date);}) .attr("y",paddingT-10) .attr("text-anchor","start") //.attr("dy",".3em") .attr("fill","white") .attr("visibility",function(d){return d.visibility}) .style("font-size","70%") .text(function(d){return d.who;}) .attr("transform",function(d){ return "rotate(-30 " + historyScale(d.date).toString() + " " + (paddingT-10).toString() + ")"; }); // Then create horizontal elements var lines = groups.append("line"); var textR = groups.append("text"); var textL = groups.append("text"); lines.attr("class","lines") .attr("x1",function(d){return historyScale(d.date);}) .attr("y1",function(d){ if (d.row == 0){ return catScale(d.col); } else { return catScale(d.col) + spaceMoons*d.row; } }) .attr("x2",historyScale(2016)) .attr("y2",function(d){ if (d.row == 0){ return catScale(d.col); } else { return catScale(d.col) + spaceMoons*d.row; } }) .attr("stroke-width",function(d){ return tabTypes.size[d.type]; }) .attr("stroke",function(d){ return tabTypes.color[d.type]; }); textR.attr("x",function(d){ if (d.row == 0){return historyW-paddingR + 10;} else {return historyW-paddingR + 60;} }) .attr("y",function(d){ if (d.row == 0){ return catScale(d.col); } else { return catScale(d.col) + spaceMoons*d.row; } }) .attr("text-anchor","start") .attr("dy",".3em") .attr("fill","white") .style("font-size",function(d){ return tabTypes.font[d.type]; }) .text(function(d){return d.name;}); textL.attr("x",paddingL - 10) .attr("y",function(d){return catScale(d.col);}) .attr("text-anchor","end") .attr("dy",".3em") .attr("fill","white") .style("font-size",function(d){ if (d.date <= 1550){ return tabTypes.font[d.type]; } else {return "0px";} }) .text(function(d){return d.name;}); // Manage interactivity var flag = 0; d3.select("body").on("keydown",function(){ if (d3.event.keyCode == 17){ flag = 1 - flag; } if (flag == 0){ var gr = d3.selectAll(".group"); var vert = gr.selectAll(".vertical"); gr.classed("low",false); vert.each(function(d){ if (d.visibility == "hidden"){ d3.select(this).attr("visibility","hidden"); } }); } }); groups.on("mouseenter",function(){ if (flag){ var gr = d3.selectAll(".group"); gr.classed("low",true); var vert = d3.selectAll(".vertical"); vert.each(function(d){ if (d.visibility == "hidden"){ d3.select(this).attr("visibility","hidden"); } }); d3.select(this).classed("low",false); d3.select(this).selectAll(".vertical").attr("visibility","visible"); } }); }); </script> </body> </html>
https://d3js.org/d3.v3.min.js