|
<!DOCTYPE html> |
|
<html> |
|
<meta charset="utf-8"> |
|
<script src="https://d3js.org/d3.v5.min.js"></script> |
|
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script> <!-- for color scales --> |
|
|
|
<style> |
|
body {font-family: calibri;} |
|
.axis {font: 14px calibri;} |
|
.label {font: 16px calibri;} |
|
.line {fill: none;stroke: #ff7265;stroke-width: 5;} |
|
.dot {fill: #5900ff;stroke: #ff3d3d;} |
|
/*End quoted code */ |
|
</style> |
|
|
|
<body> |
|
<h2>Line Chart</h2> |
|
<div><svg id="chart2" width="644" height="400"></svg></div> |
|
|
|
<script> |
|
|
|
var svg2 = d3.select("#chart2"), |
|
margin = {top: 20, right: 20, bottom: 30, left: 40}, |
|
width = +svg2.attr("width") - margin.left - margin.right, |
|
height = +svg2.attr("height") - margin.top - margin.bottom; |
|
|
|
var x2 = d3.scaleBand().rangeRound([0, width]).padding(0.8), |
|
y2 = d3.scaleLinear().rangeRound([height, 0]); |
|
|
|
var g2 = svg2.append("g") |
|
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); |
|
|
|
// Set line generator based upon code by Gord Lea at https://bl.ocks.org/gordlea/27370d1eea8464b04538e6d8ced39e89 |
|
|
|
var line = d3.line() |
|
.x(function(d) { return x2(d.numStudents); }) |
|
// set the x values for the line generator |
|
.y(function(d) { return y2(parseInt(d.Faculty)); }) |
|
.curve(d3.curveMonotoneX); |
|
|
|
// load data |
|
//ScatterPlot2 numStudents in increasing order |
|
//ScatterPlot3 Faculty in increasing order |
|
d3.csv("ScatterPlot3.csv").then(function(data) { |
|
|
|
x2.domain(data.map(function(d) { return d.numStudents; })); |
|
y2.domain([0, d3.max(data, function(d) { return +d.Faculty; })]); |
|
|
|
// x-axis |
|
g2.append("g") |
|
.attr("class", "axis axis--x") |
|
.attr("transform", "translate(0," + height + ")") |
|
.call(d3.axisBottom(x2)); |
|
|
|
// x-axis label |
|
g2.append("text") |
|
.attr("class", "label") |
|
.attr("x", 335-margin.right) |
|
.attr("y", 410-margin.bottom) |
|
.style("text-anchor", "begin") |
|
.text ("Number of Students") |
|
|
|
// y-axis |
|
g2.append("g") |
|
.attr("class", "axis axis--y") |
|
.call(d3.axisLeft(y2).ticks(13, "#")); |
|
|
|
// y-axis label |
|
g2.append("text") |
|
.attr("class", "label") |
|
.attr("x", 0-margin.left) |
|
.attr("y", 0-margin.top/2) |
|
.style("text-anchor", "begin") |
|
.text ("Number of Faculty") |
|
|
|
g2.append("g").append("path") |
|
.data([data]) |
|
.attr("class", "line") |
|
.attr("d", line); |
|
|
|
g2.selectAll(".dot") |
|
.data(data) |
|
.enter().append("circle") |
|
.attr("class", "dot") // Assign a class for styling |
|
.attr("cx", function(d) { return x2(d.numStudents); }) |
|
.attr("cy", function(d) { return y2(d.Faculty); }) |
|
.attr("r", 4 ) |
|
|
|
; |
|
}); |
|
|
|
|
|
</script> |
|
</body> |
|
</html> |