D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
ebendennis
Full window
Github gist
LFC Seasons by Week
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v3.min.js"></script> <style> body { margin:0; position:fixed; top:0; right:0; bottom:0; left:0; font: 10px sans-serif; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .dot { stroke: rgba(0,0,0,.25); } .tooltip { position: absolute; width: 45px; height: 24px; padding: 2px; background: white; pointer-events: none; } </style> </head> <body> <script> var margin = {top: 20, right: 20, bottom: 30, left: 40}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var x = d3.scale.linear() .range([0, width]); var y = d3.scale.linear() .range([height, 0]); var color = d3.scale.category10(); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left"); // Define the div for the tooltip var div = d3.select("body").append("div") .attr("class", "tooltip") .style("opacity", 0); 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 + ")"); d3.csv("seasonData.csv", function(error, data) { if (error) throw error; x.domain([1,42]); y.domain([0,90]); var nestData = d3.nest() .key(function(d) { return +d.week; }) .rollup(function(values) { return { first: d3.quantile(values.map(function(d) { return +d.points;}).sort(d3.ascending),.20), avg: d3.mean(values, function(d) {return +d.points; }), last: d3.quantile(values.map(function(d) { return +d.points;}).sort(d3.ascending),.80), avgWeek: d3.mean(values, function(d) {return +d.week; }) }; }) .entries(data); console.log(nestData); //Draw line var subjectPath = d3.svg.line() .interpolate("monotone") .x(function(d) { return x(d.values.avgWeek); }) .y(function(d) { return y(d.values.avg); }) var area = d3.svg.area() .interpolate("monotone") .x(function(d) { return x(d.values.avgWeek); }) .y0(function(d) { return y(d.values.first); }) .y1(function(d) { return y(d.values.last); }); svg.append('path') .style("fill", "#eaeaea") // Pass the second level of the nested array to subjectPath to generate x/y co-ords .attr("d", area(nestData)); svg.append('path') .style("stroke", "#bababa") .style("stroke-width", 1.5) .style("fill", "none") // Pass the second level of the nested array to subjectPath to generate x/y co-ords .attr("d", subjectPath(nestData)); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis) .append("text") .attr("class", "label") .attr("x", width) .attr("y", -6) .style("text-anchor", "end") .text("Week"); svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("class", "label") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("Points") }); d3.csv("seasonData.csv", function(error, data) { if (error) throw error; data.forEach(function(d) { d.week = +d.week; d.points = +d.points; }); svg.selectAll(".dot") .data(data) .enter().append("circle") .attr("class", "dot") .attr("r", 3.5) .attr("cx", function(d) { return x(d.week); }) .attr("cy", function(d) { return y(d.points); }) .style("fill", function(d) {if (d.season == "16-17") {return "#ac0000";} else {return "rgba(0,0,0,0)";}; }) .on("mouseover", function(d) { div.transition() .duration(200) .style("opacity", .9); div.html("Week " + d.week + "<br/>" + d.season + ": " + d.points) .style("left", (d3.event.pageX + 10) + "px") .style("top", (d3.event.pageY - 10) + "px"); }) .on("mouseout", function(d) { div.transition() .duration(500) .style("opacity", 0); });; }); </script> </body>
https://d3js.org/d3.v3.min.js