D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
bricedev
Full window
Github gist
Hackerrank score
<!DOCTYPE html> <meta charset="utf-8"> <style> body { font: 10px sans-serif; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .line { fill: none; stroke: #22B84C; stroke-width: 2px; } </style> <body> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> <script> var margin = {top: 20, right: 40, bottom: 20, left: 25}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var formatValue = d3.format(",.2f"); var x = d3.scale.linear() .range([0, width]); var y = d3.scale.linear() .range([height, 0]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left"); var line = d3.svg.line() .x(function(d) { return x(d.order); }) .y(function(d) { return y(d.score); }); 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("data.csv", function(error, data) { if (error) throw error; data.forEach(function(d) { d.order = +d.order; d.score = +d.score; }); x.domain(d3.extent(data, function(d) { return d.order; })); y.domain([0,60]); svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("x", 0) .attr("y", 10) .attr("transform", "rotate(-90)") .style("text-anchor", "end") .style("font-weight","bold") .text("Hackerrank Score"); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis) .append("text") .attr("x", 0) .attr("y", -4) .attr("transform", "translate(" + width + ",0)") .style("text-anchor", "end") .style("font-weight","bold") .text("Attempts"); svg.append("path") .datum(data) .attr("class", "line") .attr("d", line); var dot = svg.selectAll(".dot") .data(data) .enter().append("g") .attr("class","dot"); dot.append("circle") .attr("class", "dot") .attr("r", 5) .attr("cx", function(d) { return x(d.order); }) .attr("cy", function(d) { return y(d.score); }) .style("stroke-width","1px") .style("stroke","white") .style("fill","#22B84C"); dot.append("text") .attr("class", "dot") .attr("x", function(d) { return x(d.order) + 5; }) .attr("y", function(d) { return y(d.score) - 5; }) // .style("font-weight","bold") .style("font-size","11px") .text(function(d) { return d.score; }); }); </script>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js