xxxxxxxxxx
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
<style>
body {
margin:0;
position:fixed;
top:0;
right:0;
bottom:0;
left:0;
font: 10px sans-serif;
background-color: #7f7f7f;
}
.domain,
.tick line {
fill: none;
stroke: #eee;
shape-rendering: crispEdges;
}
.tick text {
fill: #eee;
}
.dot {
stroke-opacity: .25;
}
.tooltip {
position: absolute;
width: 45px;
height: 24px;
padding: 2px;
background: white;
pointer-events: none;
}
</style>
</head>
<body>
<input type="checkbox" name="v" value="1" checked>1st
<input type="checkbox" name="v" value="2" checked>2nd
<input type="checkbox" name="v" value="3" checked>3rd
<input type="checkbox" name="v" value="4" checked>4th
<input type="checkbox" name="v" value="5" checked>5th
<input type="checkbox" name="v" value="6" checked>6th
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scaleLinear()
.range([0, width]);
var y = d3.scaleLinear()
.range([height, 0]);
var color = d3.scaleSequential(d3.interpolateMagma).domain([6,1]);
// Define the div for the tooltip
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
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 + ")");
//Container for the gradients
var defs = svg.append("defs");
//Filter for the outside glow
var filter = defs.append("filter")
.attr("id","glow");
filter.append("feGaussianBlur")
.attr("stdDeviation","5")
.attr("result","coloredBlur");
var feMerge = filter.append("feMerge");
feMerge.append("feMergeNode")
.attr("in","coloredBlur");
feMerge.append("feMergeNode")
.attr("in","SourceGraphic");
// Call csv and add points
d3.csv("top6Data.csv", function(error, data) {
if (error) throw error;
data.forEach(function(d) {
d.week = +d.week;
d.points = +d.points;
});
x.domain([1,38]);
y.domain(d3.extent(data, function(d) { return d.points; })).nice();
var nestData = d3.nest()
.key(function(d) { return d.position; })
.key(function(d) { return +d.week; })
.rollup(function(values) { return {
avg: d3.mean(values, function(d) {return +d.points; }),
avgWeek: d3.mean(values, function(d) {return +d.week; })
};
})
.entries(data);
//Draw line
var subjectPath = d3.line()
.x(function(d) {
return x(d.value.avgWeek);
})
.y(function(d) {
return y(d.value.avg);
})
svg.selectAll(".subject-group")
.data(nestData)
.enter().append('g')
.attr('class', 'subject-group')
.append('path')
.style("stroke", function(d) { return color(d.key); })
.style("stroke-opacity", .5)
.style("fill", "none")
// Pass the second level of the nested array to subjectPath to generate x/y co-ords
.attr("d",function(d) {return subjectPath(d.values);});
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", 3.5)
.attr("cx", function(d) { return x(d.week); })
.attr("cy", function(d) { return y(d.points); })
.style("fill", function(d) { return color(d.position); })
.style("stroke", function(d) { return color(d.position); })
.style("fill-opacity", function(d) {if (d.season == "16-17") {return 1;} else {return 0;}; })
.on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", .9);
div.html("Position " + d.position + "<br/>" + d.season + ": " + d.points)
.style("left", (d3.event.pageX + 10) + "px")
.style("top", (d3.event.pageY - 10) + "px");
})
.on("mouseout", function(d) {
div.transition()
.duration(500)
.style("opacity", 0);
});
// Add the X Axis
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text("Week");
// Add the Y Axis
svg.append("g")
.call(d3.axisLeft(y))
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 3)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Points");
d3.selectAll(".subject-group")
.style("filter", "url(#glow)");
function type(d) {
d.value = +d.value;
return d;
}
});
d3.selectAll("[name=v]").on("change", function() {
var selected = this.value;
opacity = this.checked ? 1 : 0;
svg.selectAll(".dot")
.filter(function(d) {return selected == d.position;})
.style("opacity", opacity);
svg.selectAll(".subject-group")
.filter(function(d) {return selected == d.key;})
.style("opacity", opacity);
});
</script>
</body>
https://d3js.org/d3.v4.min.js
https://d3js.org/d3-scale-chromatic.v1.min.js