D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
shampshire
Full window
Github gist
Time series test
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Time series test</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; } span.filetype { color: grey; font-size: 8px; } h1 { font-size: 20px; margin: 0; } p { font-size: 12px; margin: 10px 0 0 0; width: 600px; } svg { background-color: white; } circle:hover { fill-opacity: 100%; } .axis path, .axis line { fill: none; stroke: grey; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 10px; } .label { font-family: sans-serif; font-size: 14px; font-weight: bold; font-color: grey; } </style> </head> <body> <h1>UK Net Borrowing</h1> <p>Net borrowing, in £ million, excluding financial interventions. ONS data sourced from the <a href="https://www.theguardian.com/news/datablog/2010/oct/18/deficit-debt-government-borrowing-data">Guardian data blog</a>.</p> <script type="text/javascript"> //Dimensions and padding var w = 700; var h = 600; 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"); //Configure area generator var area = d3.svg.area() .x(function(d) { return xScale(dateFormat.parse(d.YEAR)); }) .y0((h-padding[2])/2) .y1(function(d) { return yScale(d.Netborexfin); }); var negarea = d3.svg.area() .x(function(d) { return xScale(dateFormat.parse(d.YEAR)); }) .y0((h-padding[2])/2) .y1(function(d) { //if(d.Netborexfin>0){ return yScale(d.Netborexfin); //}else{ // return yScale(0); //} //; }); //Create the empty SVG image var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); //Load data d3.csv("UKborrowing2.csv", function(Data) { xScale.domain([ d3.min(Data, function(d) { return dateFormat.parse(d.YEAR); }), d3.max(Data, function(d) { return dateFormat.parse(d.YEAR); }) ]); yScale.domain([ d3.min(Data, function(d) { return +d.Netborexfin; }), d3.max(Data, function(d) { return +d.Netborexfin; }), ]); area.y0(yScale(0)); negarea.y0(yScale(0)); // Add clip path svg.append("defs") .append("clipPath") .attr("id","clip") .append("rect") .attr("x","0") .attr("y","0") .attr("width",w) .attr("height",yScale(0)); svg.data([ Data ]) .append("path") .attr("class", "narea") .attr("d", negarea) .attr("fill", "red") .attr("fill-opacity","70%") .attr("stroke", "none"); svg.data([ Data ]) .append("path") .attr("class", "area") .attr("d", area) .attr("fill", "steelblue") .attr("fill-opacity","100%") .attr("stroke", "none") .attr("clip-path","url(#clip)"); var circles = svg.selectAll("circle") .data(Data) .enter() .append("circle"); circles.attr("cx", function(d) { return xScale(dateFormat.parse(d.YEAR)); }) .attr("cy", function(d) { return yScale(d.Netborexfin); }) .attr("r", 3) .attr("fill", "grey") .attr("fill-opacity","0%") .append("title") .text(function(d) { return d.YEAR + " : " + d.Netborexfin +"m"; }); //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); svg.append("text") .attr("class", "y label") .attr("text-anchor", "middle") .attr("x", 0) .attr("y", 0) .attr("dy",20) .attr("dx",-(w/2)+padding[0]+50) .attr("transform", "rotate(-90)") .text("Net borrowing ex. financial interventions"); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js