D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
MrMattSim
Full window
Github gist
Iconclass depth 1 codes, over time
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>AIDS poster ICONCLASS</title> <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> <style type="text/css"> body { background-color: white; font-family: Helvetica, Arial, sans-serif; } h1 { font-size: 24px; margin: 0; } p { font-size: 14px; margin: 10px 0 0 0; } svg { background-color: white; } circle:hover { fill: orange; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11px; } .legend {font-size: 10px;} </style> </head> <body> <h1>Iconclass depth 1 codes, over time</h1> <p></p> <script type="text/javascript"> //Dimensions and padding var w = 880; var h = 450; var padding = [ 20, 10, 50, 100 ]; //Top, right, bottom, left //Set up date formatting and years var dateFormat = d3.time.format("%Y"); //Set up scales var xScale = d3.time.scale() .range([ padding[3], w - padding[1] - padding[3] ]); var yScale = d3.scale.linear() .range([ padding[0], h - padding[2] ]); //Configure axis generators var xAxis = d3.svg.axis() .scale(xScale) .orient("bottom") .ticks(15) .tickFormat(function(d) { return dateFormat(d); }); var yAxis = d3.svg.axis() .scale(yScale) .orient("left"); var legend = { 0 : {label:"Abstract", color:'rgb(255,255,255)'}, 1 : {label:"Religion", color:'rgb(225,223,85)' }, 2 : {label:"Nature", color:'rgb(164,231,15)' }, 3 : {label:"Humans", color:'rgb(15,197,149)' }, 4 : {label:"Culture", color:'rgb(133,95,227)' }, 5 : {label:"Ideas", color:'rgb(127,127,127)' }, 6 : {label:"History", color:'rgb(0,0,0)' }, 7 : {label:"Bible", color:'rgb(224,171,0)'} , 8 : {label:"Literature", color:'rgb(128,98,0)' }, 9 : {label:"Mythology", color:'rgb(113,88,143)' } } var legendColor = function(n){ return +n < 10 ? legend[n].color : 'rgb(0,0,0)' ; } //Configure line generator var line = d3.svg.line() .x(function(d) { return xScale(dateFormat.parse(d.year)); }) .y(function(d) { return yScale(+d.value); }); //Create the empty SVG image var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); //Load data d3.csv("d3_ic_pivot_d1-year.csv", function(data) { var years = ["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"]; //Create a new, empty array to hold our restructured dataset var dataset = []; //Loop once for each row in data for (var i = 0; i < data.length; i++) { //Create new object with this ic_d1 name and empty array dataset[i] = { d1: data[i].ic_d1, count: [] }; //Loop through all the years for (var j = 0; j < years.length; j++) { // If value is not empty if (data[i][years[j]]) { //Add a new object to the count data array //for this ic_d1 dataset[i].count.push({ year: years[j], value: data[i][years[j]] }); } } } //Set scale domains xScale.domain([ d3.min(years, function(d) { return dateFormat.parse(d); }), d3.max(years, function(d) { return dateFormat.parse(d); }) ]); yScale.domain([ d3.max(dataset, function(d) { return d3.max(d.count, function(d) { return +d.value; }); }), 0 ]); //Make a group for each ic_d1 var groups = svg.selectAll("g") .data(dataset) .enter() .append("g"); //Append a title with the ic_d1 name (so we get easy tooltips) groups.append("title") .text(function(d) { return d.d1; }); //Within each group, create a new line/path, //binding just the emissions data to each one groups.selectAll("path") .data(function(d) { return [ d.count ]; }) .enter() .append("path") .attr("class", "line") .attr("d", line) .attr("fill", "none") .attr("stroke", function(d){ return legendColor(this.parentElement.childNodes[0].innerHTML) }) .attr("stroke-width", 2); //Axes svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + (h - padding[2]) + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + (padding[3]) + ",0)") .call(yAxis); var lgnd = svg.append("g") .attr("class","legend") .attr("transform","translate(700,50)") for (i=0; i < 10; i++) { lgnd.append("circle") .attr("fill", legendColor(i)) .attr("r",6) .attr("cx",0) .attr("cy",i*20 - 4) .attr("opacity",0.6) .attr("stroke","#000") .attr("stroke-width", i == 0 ? 1 : 0); lgnd.append("text") .attr("x",12) .attr("y",i*20) .text(i + " : " + legend[i].label); } }); </script> </body> </html>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js