D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
chiaochi
Full window
Github gist
Sleepless in San Francisco
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>George Lee's Sleep Log</title> <script type="text/javascript" src="https://d3js.org/d3.v3.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; } g.highlight path { stroke: red; stroke-width: 2; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11px; } </style> </head> <body> <h1>George's Sleep Log</h1> <p> I really need to sleep more ...</p> <script type="text/javascript"> //Size and Padding var w = 1000; var h = 600; var padding = [50, 50, 50, 50]; //Set up date formatting var dateFormat = d3.time.format("%Y-%m-%d"); //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] ]); //Set up axis generators var xAxis = d3.svg.axis() .scale(xScale) .orient("bottom") .tickFormat(function(d){ return dateFormat(d); }); var yAxis = d3.svg.axis() .scale(yScale) .orient("left"); //Set up line generator var line = d3.svg.line() .x(function(d) { return xScale(dateFormat.parse(d.day)); }) .y(function(d) { return yScale(+d.minutes); }); //Set up SVG var svg = d3.select("body") .append("svg") .attr("width",w) .attr("height",h); //load data & transform d3.csv("SleepLog.csv", function(data) { //To test if the data loaded //console.log(data); //console.log(data.length); //Those are the two lines we want to show. Making them an array var columns = ["Asleep", "Awake"]; //Create an empty array for the newly formatted variable var dataset = []; //Inside the array, we'll load object. Each object is a column for (var i = 0; i < columns.length; i++) { dataset[i] = { measures:columns[i], xy: [] }; for (var j = 0; j < data.length ; j++){ //console.log(data[j].Date); //console.log(data[j].Awake); if (i == 0) { dataset[i].xy.push({ day: data[j].Date, minutes: data[j].Asleep }); } else { dataset[i].xy.push({ day: data[j].Date, minutes: data[j].Awake }); } } } //Test if the data is reformatted correctly console.log(dataset); //Set scale domains xScale.domain([ d3.min(dataset, function(d) { return d3.min(d.xy, function(d) { return dateFormat.parse(d.day); }); }), d3.max(dataset, function(d) { return d3.max(d.xy, function(d) { return dateFormat.parse(d.day); }); }) ]); yScale.domain([ d3.max(dataset, function(d) { return d3.max(d.xy, function(d) { return +d.minutes; }); }), 0 ]); //Make a group for each line var groups = svg.selectAll("g") .data(dataset) .enter() .append("g") .classed("highlight", function(d) { if (d.measures == "Awake") { return true; } else { return false; } }); //Append a title for tooltip groups.append("title") .text(function(d) { return "On this day" + d.Date + d.measures; }); //Within each group, create a new line/path, groups.selectAll("path") .data(function(d) { return [ d.xy ]; }) .enter() .append("path") .attr("class", "line") .attr("d", line) .attr("fill", "none") .attr("stroke", "steelblue") .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); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js