Skip to content

Instantly share code, notes, and snippets.

@mattykuch
Created February 10, 2017 14:15
Show Gist options
  • Save mattykuch/3436092adda17c8a8ce25a19e5378753 to your computer and use it in GitHub Desktop.
Save mattykuch/3436092adda17c8a8ce25a19e5378753 to your computer and use it in GitHub Desktop.
UCE 2000-2015 Performance Bump Chart made with D3.js
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Top 20: UCE School Performance Bump Chart (2000 - 2015)</title>
</head>
<style>
body {
font-size:14px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-style: bold;
}
.x.axis path,.x.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.y.axis path {
fill: none;
}
.y.axis line {
fill: none;
stroke: #eeeeee;
shape-rendering: crispEdges;
}
h1{
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 26px;
font-style: bold;
}
p{
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 16px;
}
</style>
<body>
<h1>Top 20: UCE School Performance Bump Chart (2000 - 2015)</h1>
<h5> Hover over a School Name or Line to view how its done over the past decade</h5>
<p>
<FORM>
<INPUT TYPE="button" onClick="history.go(0)" VALUE="Refresh">
</FORM>
</p>
<!-- <script src="http://d3js.org/d3.v3.min.js"></script> -->
<script src="http://d3js.org/d3.v3.js"></script>
<script>
var margin = {top: 20, right: 250, bottom: 50, left: 30},
width = 1000 - margin.left - margin.right,
height = 600 - margin.top - margin.bottom;
var speed = 1000;
var valueFormat = d3.format("");
var x = d3.scale.linear()
.range([0,width]);
var y = d3.scale.ordinal()
.rangeRoundBands([height, 0], .1);
var xAxis = d3.svg.axis()
.scale(x)
.tickSize(0)
.ticks(13)
.orient("bottom")
.tickFormat(valueFormat)
;
var xAxis1 = d3.svg.axis()
.scale(x)
.tickSize(0)
.ticks(13)
.orient("top")
.tickFormat(valueFormat)
;
var yAxis = d3.svg.axis()
.scale(y)
.tickSize(-width)
.tickPadding(10)
.orient("left");
var line = d3.svg.line()
.x(function(d) { return x(d.year); })
.y(function(d) { return y(d.position) + y.rangeBand()/2; });
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 + ")");
var clip = svg.append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", 0)
.attr("height", height);
var year; // As a Global
d3.json("ugo.txt", function(error, data) {
y.domain(d3.range(1,d3.max(data.standing, function(school) { return d3.max(school.rank, function(d) { return d.position; }); }) + 1 ).reverse());
//xAxis.tickValues(data.standing[0].rank.map(function(d) { return d.year; }));
x.domain(d3.extent(data.standing[0].rank.map(function(d) { return d.year; })));
//x.domain([2000, 2015]);
//set y axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
//set bottom axis position
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(" + 0 + "," + 526 + ")")
.call(xAxis);
//set top axis
svg.append("g")
.attr("class", "x axis")
.call(xAxis1);
var school = svg.selectAll(".school")
.data(data.standing)
.enter().append("g")
.attr("class", "school");
var path = school.append("path")
.attr("class", "line")
.style("stroke", function(d) { return d.color; })
.style("stroke-width", 4)
.style("fill","none")
.attr("clip-path", function(d) { return "url(#clip)"; })
.attr("d", function(d) { return line(d.rank); })
.on("mouseover", function (d) {
school.style("opacity",0.2);
school.filter(function(path) {return path.schoolName === d.schoolName; }).style("opacity",1);
})
.on("mouseout", function (d) { school.style("opacity",1); })
;
var circleStart = school.append("circle")
.attr("cx", function(d) { return x(d.rank[0].year); })
.attr("cy", function(d) { return y(d.rank[0].position) + y.rangeBand()/2; })
// .style("fill", function(d) { return d.color; })
.attr("r", 6)
.style("stroke", function(d) { return d.color; })
.style("stroke-width", 4)
.style("fill", "white")
.on("mouseover", function (d) {
school.style("opacity",0.2);
school.filter(function(path) {return path.schoolName === d.schoolName; }).style("opacity",1);
})
.on("mouseout", function (d) { school.style("opacity",1); })
var circleEnd = school.append("circle")
.attr("cx", function(d) { return x(d.rank[0].year); })
.attr("cy", function(d) { return y(d.rank[0].position) + y.rangeBand()/2; })
// .style("fill", function(d) { return d.color; })
.attr("r", 6)
.style("stroke", function(d) { return d.color; })
.style("stroke-width", 4)
.style("fill", "white")
.on("mouseover", function (d) {
school.style("opacity",0.2);
school.filter(function(path) {return path.schoolName === d.schoolName; }).style("opacity",1);
})
.on("mouseout", function (d) { school.style("opacity",1); })
// text label for the x axis
svg.append("text")
.attr("x", width / 2 )
.attr("y", height + (margin.bottom/1.5))
.attr("style","font-size:16px;") // to bold title
.style("text-anchor", "middle")
.text("Year");
var label = school.append("text")
.attr("transform", function(d) { return "translate(" + x(d.rank[0].year) + "," + (y(d.rank[0].position) + y.rangeBand()/2) + ")"; })
.attr("x", 8)
.attr("dy", ".31em")
.on("mouseover", function (d) {
school.style("opacity",0.2);
school.filter(function(path) {return path.schoolName === d.schoolName; }).style("opacity",1);
})
.on("mouseout", function (d) { school.style("opacity",1); })
.style("cursor","pointer")
.style("fill", function(d) { return d.color; })
.style("font-weight", "bold")
.text(function(d) { return ""+ d.rank[0].position + " " + d.schoolName; });
// .text(function(d) { return ""+ d.rank[0].position + " " + d.schoolName; });
var year = 1;
var transition = d3.transition()
.duration(speed)
.each("start", function start() {
label.transition()
.duration(speed)
.ease('linear')
.attr("transform", function(d) { return "translate(" + x(d.rank[year].year) + "," + (y(d.rank[year].position) + y.rangeBand()/2) + ")"; })
.text(function(d) { return " " + " " + d.schoolName; });
circleEnd.transition()
.duration(speed)
.ease('linear')
.attr("cx", function(d) { return x(d.rank[year].year); })
.attr("cy", function(d) { return y(d.rank[year].position) + y.rangeBand()/2; });
clip.transition()
.duration(speed)
.ease('linear')
.attr("width", x((year*2000)+1))
.attr("height", height);
year+=1;
if (year !== data.standing[0].rank.length) transition = transition.transition().each("start", start);
});
});
</script>
<p></p>
</body>
{
"leagueCaption": "Uganda Olevel 1",
"standing": [
{"schoolName":"Mt. St. Mary's Namagunga","color":"#527a73",
"rank":[
{"year":2000,"position":3},
{"year":2001,"position":2},
{"year":2004,"position":2},
{"year":2005,"position":1},
{"year":2006,"position":6},
{"year":2007,"position":2},
{"year":2008,"position":2},
{"year":2010,"position":1},
{"year":2011,"position":2},
{"year":2012,"position":1},
{"year":2013,"position":1},
{"year":2014,"position":3},
{"year":2015,"position":1}
]},
{"schoolName":"St. Marys SS Kitende","color":"#527a5f",
"rank":[
{"year":2000,"position":19},
{"year":2001,"position":20},
{"year":2004,"position":7},
{"year":2005,"position":7},
{"year":2006,"position":5},
{"year":2007,"position":5},
{"year":2008,"position":4},
{"year":2010,"position":5},
{"year":2011,"position":6},
{"year":2012,"position":3},
{"year":2013,"position":3},
{"year":2014,"position":1},
{"year":2015,"position":2}
]},
{"schoolName":"St. Josephs SS Naggalama","color":"#7a5f52",
"rank":[
{"year":2000,"position":18},
{"year":2001,"position":14},
{"year":2004,"position":8},
{"year":2005,"position":2},
{"year":2006,"position":8},
{"year":2007,"position":11},
{"year":2008,"position":15},
{"year":2010,"position":9},
{"year":2011,"position":16},
{"year":2012,"position":12},
{"year":2013,"position":12},
{"year":2014,"position":16},
{"year":2015,"position":3}
]},
{"schoolName":"Uganda Martyrs SS Namugongo","color":"#526d7a",
"rank":[
{"year":2000,"position":1},
{"year":2001,"position":1},
{"year":2004,"position":1},
{"year":2005,"position":4},
{"year":2006,"position":1},
{"year":2007,"position":1},
{"year":2008,"position":3},
{"year":2010,"position":3},
{"year":2011,"position":5},
{"year":2012,"position":4},
{"year":2013,"position":2},
{"year":2014,"position":2},
{"year":2015,"position":4}
]},
{"schoolName":"St. Josephs Girls Nsambya","color":"#303b46",
"rank":[
{"year":2000,"position":15},
{"year":2001,"position":15},
{"year":2004,"position":16},
{"year":2005,"position":16},
{"year":2006,"position":11},
{"year":2007,"position":13},
{"year":2008,"position":9},
{"year":2010,"position":12},
{"year":2011,"position":11},
{"year":2012,"position":16},
{"year":2013,"position":13},
{"year":2014,"position":9},
{"year":2015,"position":5}
]},
{"schoolName":"Namilyango Col.","color":"#1d283b",
"rank":[
{"year":2000,"position":14},
{"year":2001,"position":5},
{"year":2004,"position":6},
{"year":2005,"position":11},
{"year":2006,"position":7},
{"year":2007,"position":8},
{"year":2008,"position":6},
{"year":2010,"position":8},
{"year":2011,"position":1},
{"year":2012,"position":7},
{"year":2013,"position":11},
{"year":2014,"position":13},
{"year":2015,"position":6}
]},
{"schoolName":"Seeta HS","color":"#3b301d",
"rank":[
{"year":2000,"position":20},
{"year":2001,"position":19},
{"year":2004,"position":20},
{"year":2005,"position":17},
{"year":2006,"position":12},
{"year":2007,"position":19},
{"year":2008,"position":19},
{"year":2010,"position":15},
{"year":2011,"position":18},
{"year":2012,"position":9},
{"year":2013,"position":8},
{"year":2014,"position":10},
{"year":2015,"position":7}
]},
{"schoolName":"St. Henrys Col. Kitovu","color":"#1d3b30",
"rank":[
{"year":2000,"position":10},
{"year":2001,"position":6},
{"year":2004,"position":3},
{"year":2005,"position":5},
{"year":2006,"position":4},
{"year":2007,"position":3},
{"year":2008,"position":7},
{"year":2010,"position":4},
{"year":2011,"position":10},
{"year":2012,"position":6},
{"year":2013,"position":6},
{"year":2014,"position":17},
{"year":2015,"position":8}
]},
{"schoolName":"St.Pauls Sem. Kabale","color":"#282736",
"rank":[
{"year":2000,"position":6},
{"year":2001,"position":17},
{"year":2004,"position":10},
{"year":2005,"position":8},
{"year":2006,"position":16},
{"year":2007,"position":15},
{"year":2008,"position":20},
{"year":2010,"position":18},
{"year":2011,"position":20},
{"year":2012,"position":20},
{"year":2013,"position":17},
{"year":2014,"position":5},
{"year":2015,"position":9}
]},
{"schoolName":"Trinity Col. Nabbingo","color":"#273628",
"rank":[
{"year":2000,"position":9},
{"year":2001,"position":11},
{"year":2004,"position":13},
{"year":2005,"position":12},
{"year":2006,"position":17},
{"year":2007,"position":14},
{"year":2008,"position":10},
{"year":2010,"position":17},
{"year":2011,"position":14},
{"year":2012,"position":15},
{"year":2013,"position":15},
{"year":2014,"position":19},
{"year":2015,"position":10}
]},
{"schoolName":"Kings Col. Budo","color":"#85b687",
"rank":[
{"year":2000,"position":11},
{"year":2001,"position":7},
{"year":2004,"position":5},
{"year":2005,"position":13},
{"year":2006,"position":14},
{"year":2007,"position":9},
{"year":2008,"position":12},
{"year":2010,"position":7},
{"year":2011,"position":7},
{"year":2012,"position":18},
{"year":2013,"position":9},
{"year":2014,"position":11},
{"year":2015,"position":11}
]},
{"schoolName":"Gayaza HS","color":"#0a6a6f",
"rank":[
{"year":2000,"position":4},
{"year":2001,"position":3},
{"year":2004,"position":9},
{"year":2005,"position":14},
{"year":2006,"position":10},
{"year":2007,"position":10},
{"year":2008,"position":13},
{"year":2010,"position":6},
{"year":2011,"position":3},
{"year":2012,"position":13},
{"year":2013,"position":4},
{"year":2014,"position":8},
{"year":2015,"position":12}
]},
{"schoolName":"Nabisunsa Girls Sch","color":"#0a6f42",
"rank":[
{"year":2000,"position":5},
{"year":2001,"position":10},
{"year":2004,"position":11},
{"year":2005,"position":6},
{"year":2006,"position":15},
{"year":2007,"position":6},
{"year":2008,"position":8},
{"year":2010,"position":10},
{"year":2011,"position":17},
{"year":2012,"position":14},
{"year":2013,"position":14},
{"year":2014,"position":15},
{"year":2015,"position":13}
]},
{"schoolName":"St. Marys Col. Kisubi","color":"#6f420a",
"rank":[
{"year":2000,"position":7},
{"year":2001,"position":4},
{"year":2004,"position":4},
{"year":2005,"position":10},
{"year":2006,"position":9},
{"year":2007,"position":4},
{"year":2008,"position":1},
{"year":2010,"position":2},
{"year":2011,"position":4},
{"year":2012,"position":2},
{"year":2013,"position":5},
{"year":2014,"position":4},
{"year":2015,"position":14}
]},
{"schoolName":"Maryhill HS","color":"#6f0f0a",
"rank":[
{"year":2000,"position":17},
{"year":2001,"position":9},
{"year":2004,"position":17},
{"year":2005,"position":3},
{"year":2006,"position":13},
{"year":2007,"position":12},
{"year":2008,"position":14},
{"year":2010,"position":13},
{"year":2011,"position":12},
{"year":2012,"position":17},
{"year":2013,"position":19},
{"year":2014,"position":7},
{"year":2015,"position":15}
]},
{"schoolName":"Immaculate Heart Girls","color":"#4b2e83",
"rank":[
{"year":2000,"position":13},
{"year":2001,"position":16},
{"year":2004,"position":15},
{"year":2005,"position":18},
{"year":2006,"position":20},
{"year":2007,"position":20},
{"year":2008,"position":18},
{"year":2010,"position":20},
{"year":2011,"position":19},
{"year":2012,"position":11},
{"year":2013,"position":7},
{"year":2014,"position":14},
{"year":2015,"position":16}
]},
{"schoolName":"Ntare Sch.","color":"#7a8cb7",
"rank":[
{"year":2000,"position":16},
{"year":2001,"position":13},
{"year":2004,"position":12},
{"year":2005,"position":9},
{"year":2006,"position":3},
{"year":2007,"position":7},
{"year":2008,"position":11},
{"year":2010,"position":11},
{"year":2011,"position":9},
{"year":2012,"position":5},
{"year":2013,"position":10},
{"year":2014,"position":12},
{"year":2015,"position":17}
]},
{"schoolName":"Ndejje SS","color":"#6a1415",
"rank":[
{"year":2000,"position":12},
{"year":2001,"position":8},
{"year":2004,"position":14},
{"year":2005,"position":15},
{"year":2006,"position":18},
{"year":2007,"position":17},
{"year":2008,"position":16},
{"year":2010,"position":16},
{"year":2011,"position":13},
{"year":2012,"position":10},
{"year":2013,"position":16},
{"year":2014,"position":20},
{"year":2015,"position":18}
]},
{"schoolName":"Kitabi Sem.","color":"#8cabec",
"rank":[
{"year":2000,"position":2},
{"year":2001,"position":12},
{"year":2004,"position":18},
{"year":2005,"position":20},
{"year":2006,"position":19},
{"year":2007,"position":18},
{"year":2008,"position":5},
{"year":2010,"position":19},
{"year":2011,"position":8},
{"year":2012,"position":19},
{"year":2013,"position":20},
{"year":2014,"position":6},
{"year":2015,"position":19}
]},
{"schoolName":"Kisubi Sem.","color":"#60223B",
"rank":[
{"year":2000,"position":8},
{"year":2001,"position":18},
{"year":2004,"position":19},
{"year":2005,"position":19},
{"year":2006,"position":2},
{"year":2007,"position":16},
{"year":2008,"position":17},
{"year":2010,"position":14},
{"year":2011,"position":15},
{"year":2012,"position":8},
{"year":2013,"position":18},
{"year":2014,"position":18},
{"year":2015,"position":20}
]} ]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment