Skip to content

Commit 146c144

Browse files
committed
Fixed x offset for last forecast; Fixed curve stitching; Fixed spurious green dots
1 parent abfc3c3 commit 146c144

File tree

1 file changed

+34
-45
lines changed

1 file changed

+34
-45
lines changed

site/forecast.php

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ function getColor($regionID, $seasonID) {
220220
&nbsp;<i class="fa fa-angle-right"></i>&nbsp; You can edit any part of your forecast by redrawing just that part.<br />
221221
&nbsp;<i class="fa fa-angle-right"></i>&nbsp; You can adjust a single point by dragging it up or down.<br />
222222
The animation below demonstrates these actions.
223-
(If you don't see the animation, click <a target="_blank" href="images/tutorial.gif">here</a>.)
223+
(If you don't see the animation, click <a target="_blank" href="images/tutorial.gif">here</a>.)<!-- ' -->
224224
</p>
225225
<video width="1112" height="480" controls autoplay loop>
226226
<source src="images/tutorial.mp4" type="video/mp4">
@@ -323,6 +323,7 @@ function getColor($regionID, $seasonID) {
323323
</div></div><div id="box_canvas"><canvas id="canvas" width="800" height="400"></canvas></div>
324324
</div>
325325
<script>
326+
<!-- was: forecast.js -->
326327
var DRAW_POINTS = true;
327328
var TICK_SIZE = 5;
328329
var MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
@@ -427,8 +428,9 @@ function min(x1,x2) { if (x1<x2) { return x1; } return x2; }
427428
} // end $output['regions'] as $r
428429
?>
429430
var selectedSeasons = [];
430-
var showLastForecast = true;
431-
var lastForecast = [<?php foreach($lastForecast['wili'] as $v){printf('%.3f,', $v);} ?>];
431+
var showLastForecast = <?= $lastForecast['wili'] ? "true" : "false" ?>;
432+
var lastForecast = [<?php foreach($lastForecast['wili'] as $v){printf('%.3f,', $v);} ?>];
433+
var lastForecastEpiweek = <?= $lastForecast['date'] ? $lastForecast['date'][0] : $currentWeek ?>;
432434
var timeoutID;
433435
var lastDrag = null;
434436
var tooltip = null;
@@ -535,7 +537,7 @@ function drawPoints(xs, ys, start, end, style, g) {
535537
for(var i = start; i < end; i++) {
536538
if(ys[i] >= 0) {
537539
g.beginPath();
538-
var x = getX(xs[i]);
540+
var x = getX(modulusEpiweek(xs[i]));
539541
var y = getY(ys[i]);
540542
g.moveTo(x, y);
541543
g.lineTo(x + 1, y);
@@ -544,14 +546,17 @@ function drawPoints(xs, ys, start, end, style, g) {
544546
}
545547
}
546548

547-
function drawCurve(curve, start, end, epiweekOffset, style) {
549+
function drawCurve(curve, start, end, epiweekStart, style, do_drawPoints) {
550+
if (typeof do_drawPoints == "undefined") {
551+
do_drawPoints = DRAW_POINTS;
552+
}
548553
var g = getGraphics();
549554
g.strokeStyle = style.color;
550555
g.lineWidth = style.size * uiScale;
551556
g.setLineDash(style.dash);
552557
g.beginPath();
553558
var first = true;
554-
var epiweek = addEpiweeks(xRange[0], epiweekOffset);
559+
var epiweek = epiweekStart; //addEpiweeks(xRange[0], epiweekOffset);
555560
var xs = [];
556561
for(var i = start; i < end; i++) {
557562
if(curve[i] >= 0) {
@@ -569,10 +574,13 @@ function drawCurve(curve, start, end, epiweekOffset, style) {
569574
}
570575
g.stroke();
571576
g.setLineDash([]);
572-
if(DRAW_POINTS)
577+
if(do_drawPoints)
573578
drawPoints(xs, curve, start, end, style, g);
574579
}
575-
function drawCurveXY(xs, ys, start, end, style) {
580+
function drawCurveXY(xs, ys, start, end, style, do_drawPoints) {
581+
if (typeof do_drawPoints == "undefined") {
582+
do_drawPoints = DRAW_POINTS;
583+
}
576584
var g = getGraphics();
577585
g.strokeStyle = style.color;
578586
g.lineWidth = style.size * uiScale;
@@ -593,26 +601,27 @@ function drawCurveXY(xs, ys, start, end, style) {
593601
}
594602
g.stroke();
595603
g.setLineDash([]);
596-
if(DRAW_POINTS) drawPoints(xs, ys, start, end, style, g);
604+
if(do_drawPoints) drawPoints(xs, ys, start, end, style, g);
597605
}
598606
function stitchCurves(regionID, style, y2, xoffset) {
599-
if(forecast[regionID][0] < 0) {
600-
return;
601-
}
607+
if(forecast[regionID][0] < 0) {
608+
return;
609+
}
602610
if (typeof y2 == "undefined") {
603611
y2 = getY(forecast[regionID][0]);
604612
}
605613
if (typeof xoffset == "undefined") {
606614
xoffset = 1;
607-
608615
}
609616

610-
var seasonIndex = seasonIndices[<?= $currentYear ?>];
611-
var seasonLength = seasonOffsets[seasonIndex+1] - seasonOffsets[seasonIndex];
612-
var x1 = getX(addEpiweeks(xRange[0], seasonLength - 1));
613-
var y1 = getY(pastWili[regionID][seasonOffsets[seasonIndex + 1] - 1]);
614-
var x2 = getX(addEpiweeks(currentWeek, xoffset));
615-
drawLine(x1, y1, x2, y2, style);
617+
var seasonIndex = seasonIndices[<?= $currentYear ?>];
618+
var end = ((seasonIndex+1)<seasonOffsets.length) ? seasonOffsets[seasonIndex+1] : (pastWili[regionID].length-1);
619+
var seasonLength = end - seasonOffsets[seasonIndex];
620+
var x1 = (addEpiweeks(xRange[0], seasonLength));
621+
var y1 = getY(pastWili[regionID][end]);
622+
var x2 = (addEpiweeks(currentWeek, xoffset));
623+
//console.log("stitch curve:",seasonIndex,end,seasonLength,x1,y1,x2,y2);
624+
drawLine(getX(x1), y1, getX(x2), y2, style);
616625
}
617626
function drawTooltip(g, str) {
618627
str = ' ' + str;
@@ -736,7 +745,7 @@ function repaint() {
736745
g.fillStyle=oldFillStyle;
737746

738747
//other regions or past seasons
739-
function repaintSelection(r, s) {
748+
function repaintSelection(r, s, withPoints) {
740749
if (typeof s == "undefined") {
741750
i = r;
742751
var r = selectedSeasons[i][0];
@@ -747,49 +756,29 @@ function repaintSelection(r, s) {
747756
// var length = totalWeeks;
748757
<?php if(!$showPandemic) { ?>if(s == 2008) { length -= 12; }<?php } ?>
749758

750-
751-
// re the below: nah, we stored the dates for a reason.
752-
//if(start == 0) {
753-
// var nextStart = seasonOffsets[seasonIndices[s + 1]];
754-
// length = nextStart - start;
755-
// //todo: that -1 at the end should only be there if current season has 53 weeks and past season has 52 weeks
756-
// epiweekOffset = Math.max(0, totalWeeks - length -1);
757-
//}
758-
759759
var end = seasonIndices[s]+1 < seasonOffsets.length ? seasonOffsets[seasonIndices[s]+1] : pastWili[r].length;
760-
drawCurveXY(pastEpiweek[r], pastWili[r], start, end, style);
761-
762-
// forecast gets drawn later, no need to do it twice
763-
//if(s == <?= $currentYear ?>) {
764-
// style = {color: style.color, size: style.size, dash: DASH_STYLE};
765-
// drawCurve(forecast[r], 0, 52, numPastWeeks + 1, style);
766-
// stitchCurves(r, style);
767-
//}
760+
drawCurveXY(pastEpiweek[r], pastWili[r], start, end, style, withPoints);
768761
}
769762
for(var i = 0; i < selectedSeasons.length; i++) {
770763
var isCurrentSeason = (selectedSeasons[i][1] == <?= $currentYear ?>);
771764
if(selectedSeasons[i][0] == regionID && isCurrentSeason) {
772765
//Skip the current region's latest season
773766
continue;
774767
}
775-
repaintSelection(i);
768+
repaintSelection(i, undefined, false);
776769
}
777770

778771
//last forecast
779772
var lfStyle = {color: '#aaa', size: 2, dash: DASH_STYLE};
780773
if(showLastForecast) {
781-
// shift x axis by 30 weeks.
782-
drawCurve(lastForecast, 0, lastForecast.length, totalWeeks - lastForecast.length, lfStyle);
774+
drawCurve(lastForecast, 0, lastForecast.length, lastForecastEpiweek, lfStyle);
783775
stitchCurves(regionID, lfStyle, getY(lastForecast[0]), 0);
784776
}
785777

786778
//current region and latest season
787-
repaintSelection(regionID, <?= $currentYear ?>);
779+
repaintSelection(regionID, <?= $currentYear ?>, true);
788780
var style = {color: '#000', size: 2, dash: DASH_STYLE};
789-
//var start = seasonOffsets[seasonOffsets.length - 1];
790-
//var end = Math.min(pastWili[regionID].length, start + totalWeeks);
791-
//drawCurve(pastWili[regionID], start, end, 0, style);
792-
drawCurve(forecast[regionID], 0, 52, numPastWeeks + 1, style);
781+
drawCurve(forecast[regionID], 0, 52, currentWeek+1, style);
793782
stitchCurves(regionID, style);
794783

795784
//nowcast

0 commit comments

Comments
 (0)