Skip to content

Instantly share code, notes, and snippets.

@pinsterdev
Last active June 19, 2020 12:12
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/e9ce2892326bd83e2be5 to your computer and use it in GitHub Desktop.
Save pinsterdev/e9ce2892326bd83e2be5 to your computer and use it in GitHub Desktop.
CSO RPPI

CSO Residential Property Price Index.

Select statistic and change type. Float over lines for more info. Raw data here. Source data here. (v0.4)

<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: 0px 0px 0px 40px;">
<div>
<strong>Statistic</strong>
<form name="selS" action="">
<input type="radio" id="radio101" name="selS" onclick="selStat(0)" checked>T1: National - all residential<br>
<input type="radio" id="radio102" name="selS" onclick="selStat(1)">T2: National - houses<br>
<input type="radio" id="radio103" name="selS" onclick="selStat(2)">T3: National - apartments<br>
<input type="radio" id="radio104" name="selS" onclick="selStat(3)">T4: ex-Dublin - all residential<br>
<input type="radio" id="radio105" name="selS" onclick="selStat(4)">T5: ex-Dublin - houses<br>
<input type="radio" id="radio106" name="selS" onclick="selStat(5)">T6: Dublin - all residential<br>
<input type="radio" id="radio107" name="selS" onclick="selStat(6)">T7: Dublin - houses<br>
<input type="radio" id="radio108" name="selS" onclick="selStat(7)">T8: Dublin - apartments<br>
<input type="radio" id="radio109" name="selS" onclick="selStat(8)">T9: Dublin City - houses<br>
<input type="radio" id="radio110" name="selS" onclick="selStat(9)">T10: Dún Laoghaire-Rathdown - houses<br>
<input type="radio" id="radio111" name="selS" onclick="selStat(10)">T11: Fingal - houses<br>
<input type="radio" id="radio112" name="selS" onclick="selStat(11)">T12: South Dublin - houses<br>
<input type="radio" id="radio113" name="selS" onclick="selStat(12)">T13: ex-Dublin - apartments<br>
<input type="radio" id="radio114" name="selS" onclick="selStat(13)">T14: Midland - houses<br>
<input type="radio" id="radio115" name="selS" onclick="selStat(14)">T15: West - houses<br>
<input type="radio" id="radio116" name="selS" onclick="selStat(15)">T16: South-West - houses<br>
<input type="radio" id="radio117" name="selS" onclick="selStat(16)">T17: Border - houses<br>
<input type="radio" id="radio118" name="selS" onclick="selStat(17)">T18: Mid-East - houses<br>
<input type="radio" id="radio119" name="selS" onclick="selStat(18)">T19: Mid-West - houses<br>
<input type="radio" id="radio120" name="selS" onclick="selStat(19)">T20: South-East - houses<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="//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 (2015 average = 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, filterData, filterData2;
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','T9','T10',
'T11','T12','T13','T14','T15','T16','T17','T18','T19','T20']);
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 blankFilter(d) {
var b = false;
return d.filter(function (di) {
if (di[selectedStat] != 0) {
b = true;
}
return b;
});
}
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]);
filterData = blankFilter(data);
var series = svg.selectAll(".series").data([filterData]).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);}});
filterData2 = blankFilter(chgData[selectedChg]);
var series = svg.selectAll(".cseries").data([filterData2]).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(filterData, y, 0);
}
function mousemove2() {
mouse(filterData2, 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.csv"].content);
data = sheet.data;
chgData = [[],[],[]];
chgIntervals = [12, 3, 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) {
var prev = (i < ci)? 0 : data[i - ci][t];
chgData[n][i][t] = (prev == 0)? 0 : d[t] / prev * 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