D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
35degrees
Full window
Github gist
rvsales1
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <link href='https://fonts.googleapis.com/css?family=Cabin:300,400' rel='stylesheet' type='text/css'> <link href='https://fonts.googleapis.com/css?family=Lato:300,400' rel='stylesheet' type='text/css'> <link rel="shortcut icon" href="https://samhooker.com/images/initials/shinitfavicon.png" type="image/jpeg" /> <script src="https://d3js.org/d3.v4.min.js"></script> <title>Movie Legs</title> <style> .line { fill: none; stroke: Orange; stroke-width: 2.5px; } .axis { font-family: Cabin; font-size: 12px; } text.shadow { stroke: white; stroke-width: 4px; opacity: 0.9 } .grid line { stroke: lightgrey; stroke-opacity: 0.6; shape-rendering: crispEdges } .grid path { stroke-width: 0; } svg { display: block; margin: auto; } h1 { font-family: Cabin; text-align: center; margin-top: 2px; } h3 { font-family: Lato; margin-left: 100px; margin-right: 100px; text-align: center; margin-top: -8px; } h5 { font-family: Lato; font-style: italic; text-align:center; line-height: 0.8em; margin-top: -12px; } #blog { font-family: Lato; display: block; text-align: center; margin: auto; font-size: 14px; text-decoration: none; } #button-row { display: block; margin: auto; text-align: center; font-family: Lato; position: relative; width: 800px; } .button { background-color: SteelBlue; -moz-border-radius:20px; -webkit-border-radius:20px; border-radius:20px; border:1px solid MidnightBlue; display:inline-block; cursor:pointer; color:#ffffff; font-family:Lato; font-size:16px; padding:5px 7px; text-decoration:none; text-shadow:0px 1px 0px MidnightBlue; } .button:hover { color: PowderBlue; } .button:active { position:relative; top:1px; } #gap { margin: 25px 25px 0 20px; } #init { display: visible; position: absolute; margin-left: 10px; margin-top: -2px; } </style> </head> <body> <a href="https://samhooker.com" target="_blank"><img id="init" src="https://samhooker.com/images/initials/shinitslategry.png" width="35"></a> <script> var margin = {top: 30, right: 50, bottom: 340, left: 70}, width = 960 - margin.left - margin.right, height = 750 - margin.top - margin.bottom; // set the ranges var x = d3.scalePoint().domain(["Week 1","Week 2","Week 3","Week 4","Week 5","Week 6","Week 7","Week 8","Week 9","Week 10","Week 11","Week 12","Week 13","Week 14","Week 15","Week 16","Week 17","Week 18","Week 19","Week 20","Week 21","Week 22","Week 23"]).range([0, width]); var y = d3.scaleLinear().range([height, 0]); // define the line var valueline = d3.line() .curve(d3.curveBasis) .x(function(d) { return x(d.date); }) .y(function(d) { return y(d.titanic); }); var valueline2 = d3.line() .curve(d3.curveBasis) .x(function(d) { return x(d.date); }) .y(function(d) { return y(d.frozen); }); var valueline3 = d3.line() .curve(d3.curveBasis) .x(function(d) { return x(d.date); }) .y(function(d) { return y(d.avatar); }); var valueline4 = d3.line() .curve(d3.curveBasis) .x(function(d) { return x(d.date); }) .y(function(d) { return y(d.forceawakens); }); var svg = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); function make_x_gridlines() { return d3.axisBottom(x) .ticks(10) } function make_y_gridlines() { return d3.axisLeft(y) .ticks(5) } // Get the data d3.tsv("movielegs.tsv", function(error, data) { if (error) throw error; // format the data data.forEach(function(d) { d.date = d.date; d.titanic = +d.titanic; d.frozen = +d.frozen; d.avatar = +d.avatar; d.forceawakens = +d.forceawakens; }); // Scale the range of the data y.domain([0, d3.max(data, function(d) { return Math.max(d.titanic, d.frozen, d.avatar, d.forceawakens); })]); /* svg.append("clipPath") .attr("id","chart-area") .append("rect") .attr("width",width) .attr("height", height) */ // Add the valueline path. svg.append("path") .data([data]) .attr("class", "line") .attr("d", valueline); svg.append("path") .data([data]) .attr("class", "line") .style("stroke","SteelBlue") .attr("d", valueline2); svg.append("path") .data([data]) .attr("class", "line") .style("stroke","IndianRed") .attr("d", valueline3); svg.append("path") .data([data]) .attr("class", "line") .style("stroke","LimeGreen") .attr("d", valueline4); // Add the X Axis svg.append("g") .attr("class","axis") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x).ticks(12)) .selectAll("text") .style("text-anchor","end") .attr("dx","-.8em") .attr("dy",".15em") .attr("transform","rotate(-45)"); svg.append("text") .attr("transform","translate(" + (width/2) + "," + (height + margin.top + 40) + ")") .style("text-anchor", "middle") .style("font","14px Cabin") .style("font-weight","bolder") .text("Week of Theatrical Release"); // Add the Y Axis svg.append("g") .attr("class","axis") .call(d3.axisLeft(y) ); svg.append("text") .attr("transform","rotate(-90)") .attr("y",0-margin.left) .attr("x",0-height/2) .attr("dy","2em") .attr("text-anchor","middle") .style("font","14px Cabin") .style("font-weight","bolder") .text("Weekly percentage of total domestic gross (%)"); svg.append("text") .attr("transform", "translate(" + (10) + "," + (y(data[0].avatar)-23) + ")") .attr("dy",".35em") .attr('text-anchor',"start") .attr("fill","IndianRed") .style("font-family","Cabin") .text("Avatar"); svg.append("text") .attr("transform", "translate(" + (10) + "," + y(data[0].frozen) + ")") .attr("dy",".25em") .attr('text-anchor',"start") .attr("fill","SteelBlue") .style("font-family","Cabin") .text("Frozen"); svg.append("text") .attr("transform", "translate(" + (10) + "," + (y(data[0].forceawakens)+10) + ")") .attr("dy",".25em") .attr('text-anchor',"start") .attr("fill","LimeGreen") .style("font-family","Cabin") .text("The Force Awakens"); svg.append("text") .attr("transform", "translate(" + (10) + "," + (y(data[0].titanic)-5) + ")") .attr("dy",".35em") .attr('text-anchor',"start") .attr("fill","Orange") .style("font-family","Cabin") .attr("class","shadow") .text("Titanic"); svg.append("text") .attr("transform", "translate(" + (10) + "," + (y(data[0].titanic)-5) + ")") .attr("dy",".35em") .attr('text-anchor',"start") .attr("fill","Orange") .style("font-family","Cabin") .text("Titanic"); var curtain = svg.append('rect') .attr('x', 1) .attr('height', height) .attr('width', width) .attr('class', 'curtain') .style('fill', '#ffffff') /* Create a shared transition for anything we're animating */ /* var t = d3.transition() .duration(6000) .ease(d3.easeLinear) .each('end', function() { d3.select('line.guide') .transition() .style('opacity', 0) .remove() }); */ svg.append("g") .attr("class","grid") .attr("transform","translate(0," + height + ")") .style("stroke-dasharray",("3,3")) .call(make_x_gridlines() .tickSize(-height) .tickFormat("") ) svg.append("g") .attr("class","grid") .style("stroke-dasharray",("3,3")) .call(make_y_gridlines() .tickSize(-width) .tickFormat("") ) }); function titAnic() { d3.selectAll(".curtain") .transition() .delay(800) .duration(8000) .ease(d3.easeCubic) .attr('width', 0) .attr('transform', 'translate(' + width + ', 0)') .remove(); svg.append("text") .transition() .delay(8500) .attr("x", width-125) .attr("y", 300) .attr("dy",".35em") .attr('text-anchor',"middle") .attr("fill","SteelBlue") .style("font-family","Cabin") .text("You got it!"); svg.append("text") .transition() .delay(8500) .attr("x", width-125) .attr("y", 320) .attr("dy",".35em") .attr('text-anchor',"middle") .attr("fill","SteelBlue") .style("font-family","Cabin") .text("Unlike the actual ship,"); svg.append("text") .transition() .delay(8500) .attr("x", width-125) .attr("y", 340) .attr("dy",".35em") .attr('text-anchor',"middle") .attr("fill","SteelBlue") .style("font-family","Cabin") .text("Titanic had the longest box office legs."); } function incOrrect() { d3.selectAll(".curtain") .transition() .delay(800) .duration(8000) .ease(d3.easeCubic) .attr('width', 0) .attr('transform', 'translate(' + width + ', 0)') .remove(); svg.append("text") .transition() .delay(8500) .attr("x", width-125) .attr("y", 300) .attr("dy",".35em") .attr('text-anchor',"middle") .attr("fill","SteelBlue") .style("font-family","Cabin") .text("Nope!"); svg.append("text") .transition() .delay(8500) .attr("x", width-125) .attr("y", 320) .attr("dy",".35em") .attr('text-anchor',"middle") .attr("fill","SteelBlue") .style("font-family","Cabin") .text("Unlike the actual ship,"); svg.append("text") .transition() .delay(8500) .attr("x", width-125) .attr("y", 340) .attr("dy",".35em") .attr('text-anchor',"middle") .attr("fill","SteelBlue") .style("font-family","Cabin") .text("Titanic had the longest box office legs."); } </script> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-100133416-1', 'auto'); ga('send', 'pageview'); </script> </body>
https://d3js.org/d3.v4.min.js