Skip to content

Instantly share code, notes, and snippets.

@Ljfernando
Created February 12, 2017 01:31
Show Gist options
  • Save Ljfernando/f7db25f125b8723dfe7e6cab36067a70 to your computer and use it in GitHub Desktop.
Save Ljfernando/f7db25f125b8723dfe7e6cab36067a70 to your computer and use it in GitHub Desktop.
HW1 Chart3
license: mit
Hour Count
0 527
1 337
2 278
3 229
4 147
5 139
6 231
7 286
8 424
9 463
10 536
11 570
12 725
13 627
14 642
15 668
16 744
17 841
18 833
19 857
20 703
21 601
22 611
23 599
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.axis--x path {
display: none;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
.grid line {
stroke: lightgrey;
stroke-opacity: 0.7;
shape-rendering: crispEdges;
}
.grid path {
stroke-width: 0;
}
.area {
fill: lightsteelblue;
stroke-width: 0;
}
</style>
<svg width="960" height="500"></svg>
<script src="//d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
margin = {top: 50, right: 80, bottom: 40, left: 50},
width = svg.attr("width") - margin.left - margin.right,
height = svg.attr("height") - margin.top - margin.bottom,
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var x = d3.scaleLinear().range([0, width]),
y = d3.scaleLinear().range([height, 0]),
z = d3.scaleOrdinal(d3.schemeCategory10);
var line = d3.line()
.curve(d3.curveLinear)
.x(function(d) { return x(d.Hour); })
.y(function(d) { return y(d.Count); });
function make_x_gridlines() {
return d3.axisBottom(x)
.ticks(25)
}
function make_y_gridlines() {
return d3.axisLeft(y)
.ticks(7)
}
var area = d3.area()
.x(function(d) { return x(d.Hour); })
.y1(function(d) { return y(d.Count); });
d3.tsv("data.tsv", function(d){
console.log();
d.Count = +d.Count;
console.log(d.Count)
d.Hour = +d.Hour;
return d;
}, function(error, data) {
if (error) throw error;
x.domain(d3.extent(data, function(d) { return d.Hour; }));
y.domain([0, d3.max(data, function(d) {return d.Count; })]);
area.y0(y(0));
g.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x).ticks(25));
g.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y))
g.append("text")
.attr("y", 0- margin.left)
.attr("x", 0 - (height/2))
.attr("transform", "rotate(-90)")
.attr("dy", "0.71em")
.attr("fill", "#000")
.text("Number of Calls");
g.append("g")
.attr("class", "grid")
.call(make_x_gridlines()
.tickSize(height)
.tickFormat(""))
g.append("g")
.attr("class", "grid")
.call(make_y_gridlines()
.tickSize(-width)
.tickFormat(""))
g.append("path")
.datum(data)
.attr("fill", "steelblue")
.attr("d", area);
g.append("text")
.attr("x", (width / 2))
.attr("y", 0 - (margin.top / 2))
.attr("text-anchor", "middle")
.style("font-size", "20px")
.style("font-family", "Times New Roman")
.text("SF PD Calls Per Hour in December");
g.append("text") // text label for the x axis
.attr("x", (width / 2) )
.attr("y", height + margin.bottom )
.style("text-anchor", "middle")
.text("Hour");
g.append("path")
.datum(data)
.attr("class", "area")
.attr("d", area);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment