D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
mendozaline
Full window
Github gist
Module 2
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Women in Conress</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"> </script> <style type="text/css"> body { background-color: #FFF1E0; font-family: Georgia, serif; } h1 { font-size: 25px; font-weight: bolder; padding-top: 10px; padding-left: 5px; margin: auto; } p { font-size: 20px; padding-left: 5px; margin: 10px auto; } a:link { color: black; text-decoration: underline; } a:visited { color: purple; } svg { background-color: #FFF1E0; margin: auto; } path:hover { opacity: 1.0; } .y.axis path, .y.axis line { opacity: 0; } .axis path, .axis line { fill: none; stroke: dimGray; shape-rendering: crispEdges; } .axis text { font-size: 20px; fill: dimGray; } .gridlines{ stroke: black; stroke-width: 2px; stroke-dasharray: 1, 5; } .chartLabels { font-size: 25px; text-anchor: end; } .headline, .footer { margin: auto; width: 1000px; } #container { background-color: #FFF1E0; margin: auto; width: 1000px; } </style> </head> <body> <div class="headline"> <h1>Women in the U.S. Congress</h1> <p>In 1916, Jeannette Rankin became the first woman elected to serve in Congress. Today, nearly a century later, women still have not achieved parity with male senators and representatives. Less than one-fourth of Congress is female. Most female members of Congress are <b style="color:dodgerBlue">Democrats</b>, but the proportion of <b style="color:crimson">Republican</b> women has been increasing. <br> </p> </div> <div id="container"> </div> <div class="footer"> <p>Source: <a href="https://www.cawp.rutgers.edu/history-women-us-congress">Center for American Women and Politics</a> </p> </div> <script type="text/javascript"> var stack = d3.layout.stack() .values(function(d) { return d.percent; }) var w = 1000; var h = 650; var padding = {top: 25, bottom: 30, left: 5, right: 5}; var dateFormat = d3.time.format("%Y"); var xScale = d3.time.scale() .range([ padding.left, w - padding.right ]); var yScale = d3.scale.linear() .range([ padding.top, h - padding.bottom ]); var xAxis = d3.svg.axis() .scale(xScale) .orient("bottom") .ticks(10) .tickFormat(function(d) { return dateFormat(d); }); var yAxis = d3.svg.axis() .scale(yScale) .tickValues([25, 50, 75, 100]) .tickFormat(function(d) { return d + "%"; }) .orient("right"); var area = d3.svg.area() .x(function(d) { return xScale(dateFormat.parse(d.x)); }) .y0(function(d) { return yScale(d.y0); }) .y1(function(d) { return yScale(d.y0 + d.y); }); var color = d3.scale.ordinal() .domain(["femaleDem", "femaleGOP", "malePercent"]) .range( ["dodgerBlue", "crimson", "#FFF1E0"]); var svg = d3.select("#container") .append("svg") .attr("width", w) .attr("height", h); d3.csv("genderCongress.csv", function(data) { console.log(data); var years = [ "1917", "1918", "1919", "1920", "1921", "1922", "1923", "1924", "1925", "1926", "1927", "1928", "1929", "1930", "1931", "1932", "1933", "1934", "1935", "1936", "1937", "1938", "1939", "1940", "1941", "1942", "1943", "1944", "1945", "1946", "1947", "1948", "1949", "1950", "1951", "1952", "1953", "1954", "1955", "1956", "1957", "1958", "1959", "1960", "1961", "1962", "1963", "1964", "1965", "1966", "1967", "1968", "1969", "1970", "1971", "1972", "1973", "1974", "1975", "1976", "1977", "1978", "1979", "1980", "1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015"]; var dataset = []; for (var i = 0; i < data.length; i++) { dataset[i] = { gender: data[i].genderType, percent: [] }; for (var j = 0; j < years.length; j++) { var amount = null; if (data[i][years[j]]) { amount = +data[i][years[j]]; } dataset[i].percent.push({ x: years[j], y: amount }); } } stack(dataset); xScale.domain([ d3.min(years, function(d) { return dateFormat.parse(d); }), d3.max(years, function(d) { return dateFormat.parse(d); }) ]); var totals = []; for (i = 0; i < years.length; i++) { totals[i] = 0; for (j = 0; j < dataset.length; j++) { totals[i] += dataset[j].percent[i].y; } } yScale.domain([ d3.max(totals), 0 ]); var paths = svg.selectAll("path") .data(dataset) .enter() .append("path") .attr({ "class": "area", "d": function(d) { return area(d.percent)}, "stroke": "none", "fill": function(d, i) { return color(i);}, opacity: 0.85, }); for (i = 25; i <= 100; i += 25) { svg.append("line") .attr({ x1: padding.left, y1: yScale(i), x2: w - padding.right, y2: yScale(i), "class": "gridlines", }); } svg.append("g") .attr({ "class": "x axis", "transform": "translate(0, " + (h - padding.bottom) + ")" }) .call(xAxis); svg.append("g") .attr({ "class": "y axis", "transform": "translate(" + (w - 60) + ", -16)" }) .call(yAxis); svg.append("text") .attr({ x: 990, y: 500, fill: "crimson", class: "chartLabels" }) .text("Republican women"); svg.append("text") .attr({ x: 990, y: 610, fill: "black", class: "chartLabels" }) .text("Democratic women"); }); </script> </body> </html>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js