Page 2 of 2

Re: Lowest Monthly Rainfall record desired on website

Posted: Wed 10 May 2017 5:40 pm
by steve
This may not be the best Javascript, but it appears to work.

Change your monthlyrecordT.htm as follows:

After the HTML table row for highest monthly rainfall, add:

Code: Select all

<tr class="td_rainfall_data">
    <td class="main_table_text_labels">Lowest&nbsp;Monthly&nbsp;Rainfall</td>
    <td class="right_align_indented"><span id="MonthlyRainL">---</span>&nbsp;<#rainunit></td>
    <td class="right_align_indented"><span id="MonthlyRainLT">---</span></td>
 </tr>
Before the line "function changeData(month) {" add the following

Code: Select all

var rainyears = new Array();
var raintotals = new Array(13);
var lowmonthlyrain = new Array();
var lowmonthlyrainT = new Array();

loaddata();

function loaddata() {
    var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == XMLHttpRequest.DONE ) {
           if (xmlhttp.status == 200) {
               processdata(xmlhttp.responseText);
           }
           else {
               alert('error loading rainfall.txt');
           }
        }
    };

    xmlhttp.open("GET", "rainfall.txt", true);
    xmlhttp.send();
}

function processdata(data) {
	for (var i = 1; i < 13; i++) {
		raintotals[i] = new Array();
	}
	// split each line using comma delimeter
	var arrayOfLines = data.match(/[^\r\n]+/g);
	// process each line, ignoring first line of headers
	for (var i = 1; i < arrayOfLines.length; i++) {
		var dataForYear = arrayOfLines[i].split(',');
		// add the year to the list
		rainyears.push(dataForYear[0]);
		
		// add the totals to the lists
		for (var j = 1; j < 13; j++) {
			if (dataForYear[j] == '') {
				// empty value, use a high value to exclude
				raintotals[j].push(999);
			} 
			else {
				raintotals[j].push(parseFloat(dataForYear[j]));
			}
		}
	}
	
	// find minimum for each month
	for (var month = 1; month < 13; month++) {
		var minvalue = Math.min.apply(null,raintotals[month]);
		var minyear = rainyears[raintotals[month].indexOf(minvalue)];
		
		// add value and year to corresponding arrays
		lowmonthlyrain.push(minvalue);
		lowmonthlyrainT.push(minyear);
	}
	
	var d = new Date();
	changeData(d.getMonth());

}
After the line "document.getElementById('MonthlyRainHT').innerHTML = monthlyrainT[month];", add:

Code: Select all

	document.getElementById('MonthlyRainL').innerHTML = lowmonthlyrain[month];
	document.getElementById('MonthlyRainLT').innerHTML = lowmonthlyrainT[month];
Remove the following code from near the end of the file:

Code: Select all

function init() {
    var d = new Date();
    changeData(d.getMonth());
  }
  window.onload = init;
Put your database result in a file called rainfall.txt and upload it to your site in the same place as the html pages.

Re: Lowest Monthly Rainfall record desired on website

Posted: Wed 10 May 2017 6:25 pm
by steve
I've just noticed that the HTML table on your site doesn't match the code I've posted - hopefully you can work out what you need to do.

Re: Lowest Monthly Rainfall record desired on website

Posted: Wed 10 May 2017 6:44 pm
by RayProudfoot
Steve,

Greatly appreciate you taking the time to write this for me. So far so good but I have one query.

You wrote...

After the line "document.getElementById('MonthlyRainHT').innerHTML = monthlyrainT[month];", add:

But I can't see a line with that in it. I do have one that says...

document.getElementById('MonthlyRainHY').innerHTML = monthlyrainY[month];

I'm guessing I paste your code after that line. But should lowmonthlyrainT be lowmonthlyrainY to match the high rainfall record?

Re: Lowest Monthly Rainfall record desired on website

Posted: Wed 10 May 2017 6:45 pm
by RayProudfoot
steve wrote:I've just noticed that the HTML table on your site doesn't match the code I've posted - hopefully you can work out what you need to do.
I think so. I added some formatting to make alternate lines a different background colour.

Re: Lowest Monthly Rainfall record desired on website

Posted: Wed 10 May 2017 6:51 pm
by steve
RayProudfoot wrote:After the line "document.getElementById('MonthlyRainHT').innerHTML = monthlyrainT[month];", add:

But I can't see a line with that in it. I do have one that says...

document.getElementById('MonthlyRainHY').innerHTML = monthlyrainY[month];

I'm guessing I paste your code after that line. But should lowmonthlyrainT be lowmonthlyrainY to match the high rainfall record?
Ah, right, you've named yours with a Y as it's just the year. My extra code will work as it is, or you can use Y instead of T if you want, as long as you change all occurrences. The names don't affect the way the code works.

Re: Lowest Monthly Rainfall record desired on website

Posted: Wed 10 May 2017 6:58 pm
by RayProudfoot
Thanks Steve. I changed all the instances and pleased to report it's pulling back the right rainfall for each month.

http://www.cheadlehulmeweather.co.uk/monthlyrecord.htm

But it's not showing which year it occurred in. Have I missed something or have you?

I can sort the formatting easy enough. I may even be able to use your code as the template for other monthly records. Much appreciated, thank you. :clap:

Re: Lowest Monthly Rainfall record desired on website

Posted: Wed 10 May 2017 7:04 pm
by steve
In the HTML you have

<td class="right_align_indented"><span id="MonthlyRainLT">---</span></td>

and in the JavaScript

document.getElementById('MonthlyRainLY').innerHTML = lowmonthlyrainY[month];

The one in the HTML needs changing to match the JavaScript (MonthlyRainLY)

Re: Lowest Monthly Rainfall record desired on website

Posted: Wed 10 May 2017 7:19 pm
by RayProudfoot
Thanks! I knew it would be something I did. :lol: Almost there. Just need to tidy up the formatting.

24 hours from tentative enquiry to supplied code. You're not losing your touch Steve! :D

Re: Lowest Monthly Rainfall record desired on website

Posted: Wed 10 May 2017 7:37 pm
by RayProudfoot
All done. Formatting tidied up and looking good. Thanks Steve! :clap:

Re: Lowest Monthly Rainfall record desired on website

Posted: Thu 11 May 2017 9:17 am
by steve
Looks good.

Re: Lowest Monthly Rainfall record desired on website

Posted: Tue 26 Jun 2018 1:02 pm
by RayProudfoot
I'm revisiting this discussion from last year as it's very likely that not only will the June low rainfall record be broken but the all-time monthly one too.

Am I right in assuming the all-time record light will not flash as there is no all-time low rainfall record flag set in the core Cumulus program? Pity I can't set the light manually. :(

Re: Lowest Monthly Rainfall record desired on website

Posted: Tue 26 Jun 2018 1:23 pm
by steve
You're correct, lowest monthly rainfall is not a record tracked by Cumulus, sorry.

Re: Lowest Monthly Rainfall record desired on website

Posted: Tue 26 Jun 2018 2:33 pm
by RayProudfoot
No worries Steve. I shall have to find a way of drawing people’s attention to it.