D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
Clevejones
Full window
Github gist
Top 50 Hollywood films released from 2007-2011, budget($m) vs. audience ratings(%)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Scatterplot</title> <script type="text/javascript" src="https://d3js.org/d3.v3.min.js"></script> <style type="text/css"> body { background-color: #fff1e0; font-family: Arial, sans-serif; } h1 { color: #3B3D3B; line-height: 50%; } p { color: #3B3D3B; line-height: 40%; font-size: 16px; } circle{ fill: #bb6d82; } svg { background-color: #fff1e0; } circle:hover { fill: #723545; } circle { cursor: pointer; } .tooltip { position: absolute; font-family: Arial, sans-serif; background: #eee; box-shadow: 0 0 5px #999999; color: #333; font-size: 14px; line-height: 19px; left: 130px; width: 210px; border-style: solid; border-color: #76787A; border-width: 0px; border-radius: 7px; z-index: 1000; } .axis path, .axis line { stroke: #cec5b8; stroke-dasharray:2,2; fill: none; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11px; fill: #3B3D3B; } </style> </head> <body> <h1>Top 50 Hollywood films released from 2007-2011</h1> <p>Film Hollywood budget ($m) vs. audience ratings (%)</p> <script type="text/javascript"> var body = d3.select('body'); var w = 700; var h = 600; var padding = [ 20, 10, 30, 50 ]; //Top, right, bottom, left var xScale = d3.scale.linear() .range([ padding[3], w - padding[1] - padding[3] ]); var yScale = d3.scale.linear() .range([ padding[1], h - padding[2] ]); var xAxis = d3.svg.axis() .scale(xScale) .orient("bottom") .ticks(16); var yAxis = d3.svg.axis() .scale(yScale) .orient("left") .ticks(8) .tickFormat(function(d) { return d + "%"; }); var tip=d3.select("body").append('div') .attr('class', 'tooltip') .style('position','absolute') .style('padding','5px 10px') .style('background','white') .style('opacity',0); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("top50HollywoodFilmsReleasedFrom2007_2011.csv", function(data) { xScale.domain([ d3.min(data, function(d) { return +d.budget; }), d3.max(data, function(d) { return +d.budget; }) ]); yScale.domain([100, 30, d3.max(data, function(d) { return + d.audienceRatings; }), d3.min(data, function(d) { return + d.audienceRatings; }) ]); var circles = svg.selectAll("circle") .data(data) .enter() .append("circle"); circles.attr("cx", function(d){ return xScale(d.budget); }) .attr("cy", function(d) { return yScale(d.audienceRatings); }) .attr("r", 0.1) .on('mousemove', function(d){ return toolTip(d)}) .on('mouseout', function(d){ return toolTipOff(d)}); circles.sort(function(a, b) { return d3.ascending(a.audienceRatings, b.audienceRatings); }) .transition() .delay(function(d, i) { return i * 50; }) .duration(1000) .attr("r", 6); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + (h - padding[2] + 3) + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + (padding[3] - 3) + ")") .call(yAxis); svg.append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("x", margin.top - (height / 2)) .attr("dy", ".71em") .style("text-anchor", "end") .text("Price ($)"); function toolTip(d) { var xPos = d3.event.pageX + 5; var yPos = d3.event.pageY + 5; if (xPos>w/2) { xPos=xPos-tip.style("width").replace("px", "")-10; }; if (yPos>h){ yPos=yPos-tip.style("height").replace("px", "")-25; }; tip.style('opacity',.9) .html("<strong>" + d.hollywoodFilms + "</strong> " + " </br> Hollywood film budget " + d.budget + "($m)" + " </br> Audience ratings " + d.audienceRatings + "(%)") .style("left", xPos + "px") .style("top", yPos + "px"); }; function toolTipOff(d) { tip.style('opacity',0); }; }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.min.js
to a secure url
https://d3js.org/d3.v3.min.js