D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
BenHeubl
Full window
Github gist
Texas death penalty
<!DOCTYPE html> <meta charset="utf-8"> <style> h1 { font: 40px Helvetica; color: #FFF200; } h2 { font: 25px Helvetica; color: #FFF200; } h3 { font: 20px Helvetica; color: white; } body { font: 8px Helvetica, white; background: black; color: white; } .axis path, .axis line { fill: none; stroke: none; shape-rendering: crispEdges; } .axis text { fill: white; } .dot { stroke: none; } .tooltip { position: absolute; color: white; width: 100px; height: 10px; pointer-events: none; } </style> <body> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js" charset="utf-8"></script> <h1>Seeing Humans, Not Only Convicts!</h1> <h2>Last Statements of Texas Death Penalty Prisoners and What It Reveals:<h2> <h3>...They mentioned <strong><font color="#17becf">"God"</font></strong>, <strong><font color="#d62728">"Love"</font></strong>, <strong><font color="#7f7f7f">"Didn’t mean to hurt"</font></strong>, and even <strong><font color="#8c564b">"I didn't kill"</font></strong></h3> #8c564b <script> var margin = {top: 20, right: 20, bottom: 30, left: 40}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var dateFormat = d3.time.format("%Y"); var xScale = d3.time.scale() .range([0, width]); // d3.time.scale() // .range([ padding[3], w - padding[1] - padding[3] ]); var yScale = d3.scale.linear() .range([height, 0]); var color = d3.scale.category10(); var xAxis = d3.svg.axis() .scale(xScale) .orient("bottom") .ticks(30) .tickFormat(function(d) { return dateFormat(d); }); var yAxis = d3.svg.axis() .scale(yScale) .orient("left"); 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 + ")"); var tooltip = d3.select("body") .append("div") .attr("class", "tooltip") .style("opacity", 0); d3.csv("data3.csv", function(error, data) { data.forEach(function(d) { // d.years = +d.years; d.count = +d.count; }); xScale.domain([ d3.min(data, function(d) { return dateFormat.parse(d.years); }), d3.max(data, function(d) { return dateFormat.parse(d.years); }) ]); // xScale.domain(d3.extent(data, function(d) { return d.sepalWidth; })).nice(); // yScale.domain([ // d3.max(data, function(d) { // return +d.count; // }), // 0 // ]); yScale.domain(d3.extent(data, function(d) { return d.count; })).nice(); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis) .append("text") .attr("class", "label") .attr("x", width - width + 40) .attr("y", -6) .style("text-anchor", "end") .text("Years"); svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("class", "label") .attr("transform", "rotate(-30)") .attr("y", 200) .attr("x", 200) .style("font-size", 20) .attr("dy", ".71em") .style("text-anchor", "end") .text("Number of Executions in Texas") svg.selectAll(".dot") .data(data) .enter().append("circle") .attr("class", "dot") .attr("r", function(d) { return d.Age * 0.1 }) .attr("cx", function(d) { return xScale(dateFormat.parse(d.years)); }) .attr("cy", function(d) { return yScale(d.count);}) .style("fill", function(d) { return color(d.Keywords) ;}) .on("mouseover", function(d) { d3.select(this).transition().duration(600) .attr("r", 250) .style("fill", "black") .style("opacity", .7); tooltip.transition() .duration(500) .style("opacity", .9); tooltip.html(d.Statement) .style("left", (d3.event.pageX + 5) + "px") .style("top", (d3.event.pageY - 28) + "px"); }) .on("mouseout", function(d) { d3.select(this).transition().duration(100).attr("r", function(d) { return d.Age * 0.1 }) .style("fill", function(d) { return color(d.Keywords) ;}) .style("opacity", valOpacity); tooltip.transition() .duration(100) .style("opacity", 0); tooltip.html(d.Statement) .style("left", (d3.event.pageX + 5) + "px") .style("top", (d3.event.pageY - 28) + "px"); }); var legend = svg.selectAll(".legend") .data(color.domain()) .enter().append("g") .attr("class", "legend") .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }); legend.append("rect") .attr("x", width - 10) .attr("width", 18) .attr("height", 18) .style("fill", color); legend.append("text") .attr("x", width - 20) .attr("y", 9) .attr("dy", ".35em") .style("fill", color) .style("text-anchor", "end") .text(function(d) { return d; }); }); </script>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js