|
<!DOCTYPE html> |
|
<meta charset="utf-8"> |
|
<style> |
|
body { font: 12px Arial;} |
|
|
|
path { |
|
stroke: black; |
|
stroke-width: 0; |
|
fill: none; |
|
} |
|
|
|
/* styling for each area chart */ |
|
.area { |
|
fill: lightsteelblue; |
|
stroke-width: 0.5; |
|
opacity: 0.5; |
|
} |
|
|
|
.area2 { |
|
fill: none; |
|
stroke-width: 0; |
|
opacity: 0.5; |
|
} |
|
|
|
.area3 { |
|
fill: none; |
|
stroke-width: 0; |
|
} |
|
.area4 { |
|
fill: none; |
|
stroke-width: 0; |
|
opacity: 0.5; |
|
} |
|
|
|
|
|
/* grid line styling */ |
|
.grid .tick { |
|
stroke: lightgrey; |
|
opacity: 0.5; |
|
} |
|
.grid path { |
|
stroke-width: 0; |
|
|
|
} |
|
|
|
.axis path, |
|
.axis line { |
|
fill: none; |
|
stroke: grey; |
|
stroke-width: 1.5; |
|
shape-rendering: crispEdges; |
|
} |
|
|
|
.overlay { |
|
fill: none; |
|
pointer-events: all; |
|
} |
|
|
|
</style> |
|
|
|
<input class="filter_button1" id="button" type="checkbox">Projected</input> |
|
<input class="filter_button2" id="button" type="checkbox">Other Channel</input> |
|
<input class="filter_button3" id="button" type="checkbox">Difference</input><br> |
|
|
|
<body> |
|
|
|
<script type='text/javascript' src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> |
|
|
|
<script> |
|
var margin = {top: 30, right: 20, bottom: 40, left: 50}, |
|
width = 900 - margin.left - margin.right, |
|
height = 370 - margin.top - margin.bottom; |
|
|
|
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").ticks(10).tickFormat(d3.format("0f")); |
|
|
|
var yAxis = d3.svg.axis().scale(y) |
|
.orient("left").ticks(6); |
|
|
|
var area = d3.svg.area() |
|
.x(function(d,i) { return x(i); }) |
|
.y0(height) |
|
.y1(function(d) { return y(d.landings); }); |
|
|
|
var area2 = d3.svg.area() |
|
.x(function(d,i) { return x(i); }) |
|
.y0(height) |
|
.y1(function(d) { return y(d.projected); }); |
|
|
|
var area3 = d3.svg.area() |
|
.x(function(d,i) { return x(i); }) |
|
.y0(height) |
|
.y1(function(d) { return y(d.remove); }); |
|
|
|
var area4 = d3.svg.area() |
|
.x(function(d,i) { return x(i); }) |
|
.y0(height) |
|
.y1(function(d) { return y(d.diff); }); |
|
|
|
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 + ")"); |
|
|
|
// Get the data |
|
// read in all the data, but no need to use it all |
|
d3.csv("tv/data.csv", function(error, data) { |
|
data.forEach(function(d) { |
|
d.ind = d.ind; |
|
d.landings = +d.landings; |
|
d.projected = +d.projected; |
|
d.remove = +d.remove; |
|
d.diff = +d.diff; |
|
}); |
|
|
|
// Scale the range of the data |
|
x.domain([0, d3.max(data, function(d,i) { return i; })]); |
|
y.domain([0, d3.max(data, function(d) { return d.landings; })]); |
|
|
|
|
|
// functions to create grid lines |
|
function make_y_axis() { |
|
return d3.svg.axis() |
|
.scale(y) |
|
.orient("left") |
|
.ticks(9) |
|
} |
|
|
|
// function for the title |
|
function title(svg) { |
|
svg.append("text") |
|
.attr("x", (width / 2)) |
|
.attr("y", 0 - (margin.top / 2)) |
|
.attr("text-anchor", "middle") |
|
.style("font-size", "16px") |
|
.style("text-decoration", "underline") |
|
.text("Landings per unit time"); |
|
} |
|
|
|
// Add gridlines, make sure to put this before the area charts |
|
// so gridlines don't go over |
|
svg.append("g") |
|
.attr("class", "grid") |
|
.call(make_y_axis() |
|
.tickSize(-width, 0, 0) |
|
.tickFormat("")); |
|
|
|
svg.append("path") |
|
.datum(data) |
|
.attr("class", "area") |
|
.attr("d", area); |
|
|
|
svg.append("path") |
|
.datum(data) |
|
.attr("class", "area2") |
|
.attr("d", area2); |
|
|
|
svg.append("path") |
|
.datum(data) |
|
.attr("class", "area3") |
|
.attr("d", area3); |
|
|
|
svg.append("path") |
|
.datum(data) |
|
.attr("class", "area4") |
|
.attr("d", area4); |
|
|
|
// Add X and Y axis |
|
svg.append("g") |
|
.attr("class", "x axis") |
|
.attr("transform", "translate(0," + height + ")") |
|
.call(xAxis) |
|
.append("text") |
|
.attr("x", width / 2) |
|
.attr("y", height/10 ) |
|
.style("text-anchor", "middle") |
|
.text("Time Index"); |
|
|
|
svg.append("g") |
|
.attr("class", "y axis") |
|
.call(yAxis) |
|
.append("text") |
|
.attr("transform", "rotate(-90)") |
|
.attr("y", 0 - margin.left + 5 ) |
|
.attr("x", 0 - height/2 ) |
|
.style("font-size","12px") |
|
.style("text-anchor", "left") |
|
.text("Landings"); |
|
|
|
title(svg) |
|
|
|
d3.selectAll(".filter_button1").on("change", function() { |
|
display = this.checked ? "darkred" : "none"; |
|
|
|
svg.selectAll(".area2") |
|
.style("fill", display); |
|
|
|
}); |
|
|
|
d3.selectAll(".filter_button2").on("change", function() { |
|
display = this.checked ? "darkgreen" : "none"; |
|
|
|
svg.selectAll(".area3") |
|
.style("fill", display); |
|
}); |
|
|
|
d3.selectAll(".filter_button3").on("change", function() { |
|
display = this.checked ? "darkblue" : "none"; |
|
|
|
svg.selectAll(".area4") |
|
.style("fill", display); |
|
}); |
|
}); |
|
|
|
</script> |
|
</body> |