Skip to content

Commit 3d8a031

Browse files
committed
Refactor months display to be anchored on the coordinate of a month, not the month of a coordinate
1 parent 77e5b69 commit 3d8a031

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

site/js/forecast.js

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -321,24 +321,39 @@ function repaint() {
321321
drawLine(x, axisY + TICK_SIZE, x, axisY + 1, AXIS_STYLE);
322322
}
323323
//months
324-
var month = Math.floor((xRange[0] % 100 - 1) / getNumWeeks(Math.floor(xRange[0] / 100)) * MONTHS.length);
325324
var on = true;
326-
for(var epiweek = xRange[0]; epiweek <= xRange[1]; epiweek = addEpiweeks(epiweek, 4.35)) {
327-
var label = MONTHS[month];
328-
if(month == 0) {
329-
label += '\'' + (Math.floor(epiweek / 100) % 100);
325+
var decStart = epiweekToDecimal(xRange[0]-1);
326+
var decEnd = epiweekToDecimal(xRange[1]);
327+
// run through the year twice so we get everybody no matter how long our current season goes on
328+
for (var si = 0; si<2; si++) {
329+
for (var li=0; li<MONTHS.length; li++) {
330+
var label = MONTHS[li];
331+
var epiDecimal = currentSeason + si + li/12.;
332+
333+
x1 = epiDecimal;
334+
x2 = epiDecimal + 1./12;
335+
336+
// skip months that won't show up
337+
if (x1 > decEnd || x2 < decStart) {
338+
continue;
339+
}
340+
x1 = decimalToFEpiweek(max(decStart,x1));
341+
x2 = decimalToFEpiweek(min(decEnd,x2));
342+
343+
// skip truncated months that are too short for their label
344+
if (x2-x1<1.5) { continue; }
345+
346+
x1 = getX(x1);
347+
x2 = getX(x2);
348+
y1 = canvas.height - row3 + row2/4;
349+
oldFillStyle=g.fillStyle;
350+
g.fillStyle = on ? '#eee' : '#fff'; on = !on;
351+
g.fillRect(x1, y1, x2-x1, row2/2);
352+
g.fillStyle = oldFillStyle;
353+
354+
drawText(g, label, 0.5*(x1 + x2), canvas.height - row2, 0, Align.center, Align.center);
330355
}
331-
oldFillStyle=g.fillStyle;
332-
g.fillStyle = on ? '#eee' : '#fff'; on = !on;
333-
x1 = max(xRange[0]-1, addEpiweeks(epiweek,-4.35/2));
334-
y1 = canvas.height - row3 + row2/4;
335-
x2 = min(addEpiweeks(x1, 4.35), xRange[1])
336-
g.fillRect(getX(x1), y1, getX(x2)-getX(x1), row2/2);
337-
g.fillStyle = oldFillStyle;
338-
339-
drawText(g, label, getX(epiweek), canvas.height - row2, 0, Align.center, Align.center);
340-
month = (month + 1) % MONTHS.length;
341-
}
356+
}
342357
//label
343358
drawText(g, LABEL_X, canvas.width / 2, canvas.height - row1, 0, Align.center, Align.center, 1.5, ['bold', 'Calibri']);
344359
}
@@ -513,6 +528,13 @@ function epiweekToDecimal(ew) {
513528
var week = ew % 100;
514529
return year + (week - 1) / getNumWeeks(year);
515530
}
531+
function decimalToFEpiweek(yr) {
532+
yr += 0.5 / 52;
533+
var year = Math.floor(yr);
534+
var wk = yr - year;
535+
var week = wk * getNumWeeks(year);
536+
return year * 100 + week;
537+
}
516538
function decimalToEpiweek(yr) {
517539
yr += 0.5 / 52;
518540
var year = Math.floor(yr);

0 commit comments

Comments
 (0)