index.html
<!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-size","11px")
.text(function(d) { return d.score; });
});
</script>
data.csv
order,score
1,19.76
2,21.37
3,21.37
4,19.58
5,16.67
6,32.25
7,45.01
8,47.45
9,44.86
10,39.02
11,45.36
12,39.89
13,16.72
14,38.01
15,42.37
16,46.8
17,44.45
18,35
19,40.32
20,16.58
21,37.14
22,33.87
23,49.29