Skip to content

Instantly share code, notes, and snippets.

@pinsterdev
Last active November 8, 2016 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pinsterdev/b07872ba5b213b23644e48ed260c6d74 to your computer and use it in GitHub Desktop.
Save pinsterdev/b07872ba5b213b23644e48ed260c6d74 to your computer and use it in GitHub Desktop.
CSO RPPI (old)

CSO Residential Property Price Index (old style, to June 2016).

Superseded by new CSO Index in September 2016. Informational purposes only.
Select statistic and change type. Float over lines for more info. Raw data here. Source data here. (v0.2)

<html>
<meta charset="utf-8">
<head>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<br><br>
<div id="chart" style="float: left;"></div>
<div style="float: left; margin: 120px 0px 0px 40px;">
<div>
<strong>Statistic</strong>
<form name="selS" action="">
<input type="radio" id="radio11" name="selS" onclick="selStat(0)" checked>National - all resi properties<br>
<input type="radio" id="radio12" name="selS" onclick="selStat(1)">National - houses<br>
<input type="radio" id="radio13" name="selS" onclick="selStat(2)">National - apartments<br>
<input type="radio" id="radio17" name="selS" onclick="selStat(3)">ex-Dublin - all resi properties<br>
<input type="radio" id="radio18" name="selS" onclick="selStat(4)">ex-Dublin - houses<br>
<input type="radio" id="radio14" name="selS" onclick="selStat(5)">Dublin - all resi properties<br>
<input type="radio" id="radio15" name="selS" onclick="selStat(6)">Dublin - houses<br>
<input type="radio" id="radio16" name="selS" onclick="selStat(7)">Dublin - apartments<br>
</form>
<br>
<strong>Change</strong>
<form name="selC" action="">
<input type="radio" id="radio21" name="selC" onclick="selChange(0)" checked>Yearly<br>
<input type="radio" id="radio22" name="selC" onclick="selChange(1)">Quarterly<br>
<input type="radio" id="radio23" name="selC" onclick="selChange(2)">Monthly<br>
</form>
</div>
</div>
<br>
<div style="clear: both"></div>
<div id="readme"></div>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
<script src="//cdn.rawgit.com/showdownjs/showdown/1.3.0/dist/showdown.min.js"></script>
<script src="/pinsterdev/raw/a2fcc47fae884342e190/util.js"></script>
<script src="https://api.github.com/gists/0f82dc8380acfd77299724369184a01d?callback=gistDataLoaded"></script>
<script src="main.js"></script>
</body>
</html>
body {
font: 12px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
line.axis2 {
display: none;
}
path.line {
fill: none;
stroke-width: 3;
}
.tick line{
stroke: lightgrey;
opacity: 0.4;
}
.ysep {
stroke:black;
stroke-width:2;
}
.ysep2 {
stroke:darkgrey;
stroke-width:2;
stroke-dasharray: 10,10;
}
.overlay {
fill: none;
pointer-events: all;
}
.focus circle {
fill: none;
stroke: steelblue;
}
#readme {
width: 960px;
}
#chart {
width: 960px;
height: 500px;
}
readme(document.getElementById("readme"));
var chartElem = d3.select("body #chart");
var yAxisLabel = "Index (Jan 2005 = 100)";
var yAxis2Label = ["YoY %", "QoQ %", "MoM %"];
var margin = {
top : 20,
right : 80,
bottom : 50,
left : 40
},
width = 960 - margin.left - margin.right,
height2 = 150,
height = 480 - margin.top - margin.bottom - height2;
// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
var y2 = d3.scale.linear().range([height2, 0]);
var y2domains;
var bisectDate = d3.bisector(function(d) { return d.date; }).left
// Define the axes
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(6).tickFormat(d3.time.format("%b"))
.tickSize(-(height+height2), 0, 0);
var xAxis2 = d3.svg.axis().scale(x)
.orient("bottom").ticks(d3.time.years, 1)
.tickFormat(d3.time.format("%Y")).tickSize(5,0);
var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(5)
.tickSize(-width, 0, 0);
var yAxis2 = d3.svg.axis().scale(y2)
.orient("left").ticks(5)
.tickSize(-width, 0, 0);
var data, chgData, tableNames;
mungeData();
var svg, focus;
var mouseDateFormat = d3.time.format("%b-%Y");
var mouseNumFormat = d3.format(".1f");
var color = d3.scale.category20();
colorDomain = []; tableNames.forEach(function (d){
colorDomain.push(d);
colorDomain.push(d+"c");
});
color.domain(colorDomain);
// Define the lines
var valueline = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d[selectedStat]); });
var valueline2 = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y2(d[selectedStat]) + height; });
var selectedStatNum = parseParamWithDefault(
's',['T1','T2','T3','T4','T5','T6','T7','T8']);
var selectedStat = tableNames[selectedStatNum];
var selectedChg = parseParamWithDefault('c',['yoy','qoq','mom']);
document.selS.selS[selectedStatNum].checked = true;
document.selC.selC[selectedChg].checked = true;
doSvg();
/**
* Selection changed - remove and redo graph
*/
function selStat(s) {
selectedStat = tableNames[s];
d3.select("svg").remove();
doSvg();
}
function selChange(s) {
selectedChg = s;
d3.select("svg").remove();
doSvg();
}
function doSvg() {
y2.domain(y2domains[selectedChg]);
// Adds the svg canvas
svg = chartElem.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + height2 + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// Add the X Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (height + height2) + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "start")
.attr("dx", "0em")
.attr("dy", "1.15em")
.attr("transform", "rotate(-0)" );
svg.append("g")
.attr("class", "x axis2")
.attr("transform", "translate(0," + (height+height2+25) + ")")
.call(xAxis2)
.selectAll("text")
.style("text-anchor", "start");
// Add the Y Axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis).append("text")
.attr("transform", "rotate(-90)").attr("y", 6).attr("dy",
".71em").style("text-anchor", "end").text(yAxisLabel);
// Add separator line
var sepLines = svg.append("g")
.attr("transform", "translate(0," + (height) + ")");
sepLines.append("line").attr("x1",0).attr("x2",width).attr("class","ysep");
sepLines.append("line").attr("x1",0).attr("x2",width).attr("class","ysep2")
.attr("transform", "translate(0," + (y2(0)) + ")");
// Add the Y2 Axis
svg.append("g")
.attr("class", "y axis2")
.attr("transform", "translate(0," + (height) + ")")
.call(yAxis2).append("text")
.attr("transform", "rotate(-90)").attr("y", 6).attr("dy",
".71em").style("text-anchor", "end").text(yAxis2Label[selectedChg]);
var series = svg.selectAll(".series").data([data]).enter()
.append("g").attr("class", "series");
series.append("path")
.attr("class", "line")
.attr("d", function(d) { return valueline(d); })
.style({stroke: function(d) {return color(selectedStat);}});
var series = svg.selectAll(".cseries").data([chgData[selectedChg]]).enter()
.append("g").attr("class", "cseries");
series.append("path")
.attr("class", "line")
.attr("d", function(d) { return valueline2(d); })
.style({stroke: function(d) {return color(selectedStat + "c");}});
// Add focus circle and overlays
focus = svg.append("g")
.attr("class", "focus")
.style("display", "none");
focus.append("circle")
.attr("r", 4.5);
focus.append("text")
.attr("x", 9)
.attr("dy", ".35em");
svg.append("rect")
.attr("class", "overlay")
.attr("width", width)
.attr("height", height)
.on("mouseover", function() { focus.style("display", null); })
.on("mouseout", function() { focus.style("display", "none"); })
.on("mousemove", mousemove);
svg.append("rect")
.attr("class", "overlay")
.attr("width", width)
.attr("height", height2)
.attr("transform", "translate(0," + (height) + ")")
.on("mouseover", function() { focus.style("display", null); })
.on("mouseout", function() { focus.style("display", "none"); })
.on("mousemove", mousemove2);
}
function mousemove() {
mouse(data, y, 0);
}
function mousemove2() {
mouse(chgData[selectedChg], y2, height);
}
function mouse(dataset, scale, yoffset) {
var x0 = x.invert(d3.mouse(svg[0][0])[0]),
i = bisectDate(dataset, x0, 1),
d0 = dataset[i - 1],
d1 = dataset[i],
d = x0 - d0.date > d1.date - x0 ? d1 : d0;
focus.attr("transform", "translate(" + x(d.date) + "," + (scale(d[selectedStat])+yoffset) + ")");
var str = mouseNumFormat(d[selectedStat]) + " (" + mouseDateFormat(d.date) + ")";
focus.select("text").text(str);
}
function mungeData() {
var sheet = extractGistData(gistData["CSO_RPPI_old.csv"].content);
data = sheet.data;
chgData = [[],[],[]];
chgIntervals = [12, 4, 1];
tableNames = sheet.colNames.filter(function (d) {return d.startsWith("T")});
var minMaxDate = [], minMaxIndex = [], minMaxChg = [[],[],[]];
data.forEach(function (d,i) {
d.date = new Date(+d.Year, +d.Month - 1, 01);
minMaxDate.push(d.date);
chgIntervals.forEach(function (ci, n) {chgData[n].push({date: d.date})});
tableNames.forEach(function (t) {
d[t] = +d[t];
minMaxIndex.push(d[t]);
chgIntervals.forEach(function (ci, n) {
chgData[n][i][t] = (i < ci)? 0 : d[t] / data[i - ci][t] * 100 - 100;
minMaxChg[n].push(chgData[n][i][t]);
});
});
});
chgIntervals.forEach(function (ci, i) {
chgData[i] = chgData[i].filter(function (cd, j) {
return (ci - 1) < j;
});
});
// Scale the range of the data
x.domain(d3.extent(minMaxDate));
y.domain(d3.extent(minMaxIndex));
y2domains = [];
chgIntervals.forEach(function (ci, i) {
y2domains[i] = (d3.extent(minMaxChg[i]));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment