Converted draft code to Coffeescript, and added calculation for number of weeks in the year

This commit is contained in:
Mark Abbott 2012-03-04 16:11:48 +00:00
parent ceab688216
commit 6be7b1904c
4 changed files with 65 additions and 4 deletions

View File

@ -59,6 +59,49 @@
}); });
}); });
</script> </script>
<h3>Formatting Dates With Weeks</h3>
<div id="graph-weeks" class="graph"></div>
<script>
$(function () {
// data stolen from http://howmanyleft.co.uk/vehicle/jaguar_'e'_type
var week_data = [
{"period": "2011 W27", "licensed": 3407, "sorned": 660},
{"period": "2011 W26", "licensed": 3351, "sorned": 629},
{"period": "2011 W25", "licensed": 3269, "sorned": 618},
{"period": "2011 W24", "licensed": 3246, "sorned": 661},
{"period": "2011 W23", "licensed": 3257, "sorned": 667},
{"period": "2011 W22", "licensed": 3248, "sorned": 627},
{"period": "2011 W21", "licensed": 3171, "sorned": 660},
{"period": "2011 W20", "licensed": 3171, "sorned": 676},
{"period": "2011 W19", "licensed": 3201, "sorned": 656},
{"period": "2011 W18", "licensed": 3215, "sorned": 622},
{"period": "2011 W17", "licensed": 3148, "sorned": 632},
{"period": "2011 W16", "licensed": 3155, "sorned": 681},
{"period": "2011 W15", "licensed": 3190, "sorned": 667},
{"period": "2011 W14", "licensed": 3226, "sorned": 620},
{"period": "2011 W13", "licensed": 3245, "sorned": 0},
{"period": "2011 W12", "licensed": 3289, "sorned": 0},
{"period": "2011 W11", "licensed": 3263, "sorned": 0},
{"period": "2011 W10", "licensed": 3189, "sorned": 0},
{"period": "2011 W09", "licensed": 3079, "sorned": 0},
{"period": "2011 W08", "licensed": 3085, "sorned": 0},
{"period": "2011 W07", "licensed": 3055, "sorned": 0},
{"period": "2011 W06", "licensed": 3063, "sorned": 0},
{"period": "2011 W05", "licensed": 2943, "sorned": 0},
{"period": "2011 W04", "licensed": 2806, "sorned": 0},
{"period": "2011 W03", "licensed": 2674, "sorned": 0},
{"period": "2011 W02", "licensed": 1702, "sorned": 0},
{"period": "2011 W01", "licensed": 1732, "sorned": 0}
];
Morris.Line({
element: 'graph-weeks',
data: week_data,
xkey: 'period',
ykeys: ['licensed', 'sorned'],
labels: ['Licensed', 'SORN']
});
});
</script>
<h3>Formatting Dates with YYYY-MM</h3> <h3>Formatting Dates with YYYY-MM</h3>
<div id="graph-yyyy-mm" class="graph"></div> <div id="graph-yyyy-mm" class="graph"></div>

View File

@ -288,8 +288,20 @@ class Morris.Line
m = s.match /^(\d+) Q(\d)$/ m = s.match /^(\d+) Q(\d)$/
n = s.match /^(\d+)-(\d+)$/ n = s.match /^(\d+)-(\d+)$/
o = s.match /^(\d+)-(\d+)-(\d+)$/ o = s.match /^(\d+)-(\d+)-(\d+)$/
p = s.match /^(\d+) W(\d+)$/
if m if m
parseInt(m[1], 10) + (parseInt(m[2], 10) * 3 - 1) / 12 parseInt(m[1], 10) + (parseInt(m[2], 10) * 3 - 1) / 12
else if p
# calculate number of weeks in year given
year = parseInt(p[1], 10);
y1 = new Date(year, 0, 1);
y2 = new Date(year+1, 0, 1);
if y1.getDay() isnt 4
y1.setMonth(0, 1 + ((4 - y1.getDay()) + 7) % 7);
if y2.getDay() isnt 4
y2.setMonth(0, 1 + ((4 - y2.getDay()) + 7) % 7);
weeks = 1 + Math.ceil((y2 - y1) / 604800000);
parseInt(p[1], 10) + (parseInt(p[2], 10) - 1) / weeks;
else if n else if n
parseInt(n[1], 10) + (parseInt(n[2], 10) - 1) / 12 parseInt(n[1], 10) + (parseInt(n[2], 10) - 1) / 12
else if o else if o

View File

@ -314,16 +314,22 @@
}; };
Line.prototype.parseYear = function(date) { Line.prototype.parseYear = function(date) {
var day, m, month, n, o, s, timestamp, y1, y2, year; var day, m, month, n, o, p, s, timestamp, weeks, y1, y2, year;
s = date.toString(); s = date.toString();
m = s.match(/^(\d+) Q(\d)$/); m = s.match(/^(\d+) Q(\d)$/);
n = s.match(/^(\d+)-(\d+)$/); n = s.match(/^(\d+)-(\d+)$/);
o = s.match(/^(\d+)-(\d+)-(\d+)$/); o = s.match(/^(\d+)-(\d+)-(\d+)$/);
p = s.match(/^(\d+) W(\d)$/); p = s.match(/^(\d+) W(\d+)$/);
if (m) { if (m) {
return parseInt(m[1], 10) + (parseInt(m[2], 10) * 3 - 1) / 12; return parseInt(m[1], 10) + (parseInt(m[2], 10) * 3 - 1) / 12;
} else if (p) { } else if (p) {
return parseInt(p[1], 10) + (parseInt(p[2], 10) - 1) / 53; year = parseInt(p[1], 10);
y1 = new Date(year, 0, 1);
y2 = new Date(year + 1, 0, 1);
if (y1.getDay() !== 4) y1.setMonth(0, 1 + ((4 - y1.getDay()) + 7) % 7);
if (y2.getDay() !== 4) y2.setMonth(0, 1 + ((4 - y2.getDay()) + 7) % 7);
weeks = 1 + Math.ceil((y2 - y1) / 604800000);
return parseInt(p[1], 10) + (parseInt(p[2], 10) - 1) / weeks;
} else if (n) { } else if (n) {
return parseInt(n[1], 10) + (parseInt(n[2], 10) - 1) / 12; return parseInt(n[1], 10) + (parseInt(n[2], 10) - 1) / 12;
} else if (o) { } else if (o) {

2
morris.min.js vendored

File diff suppressed because one or more lines are too long