full_range_seasonal.gnuplot: fix calculating number of seasons
[experiments/gnuplot_seasonal_plot.git] / full_range_seasonal.gnuplot
index cfeb338..f92cf07 100755 (executable)
@@ -70,6 +70,12 @@ first_month_of_season(x) = ((int(x) * 3) + 11) % 12
 # Winter starts the previous year if the months are January (0) of February (1)
 start_of_season(x) = strptime("%Y-%m-%d", "" . (int(tm_year(x)) - (int(tm_mon(x)) < 2)) . "-" . int(first_month_of_season(season(tm_mon(x))) + 1) . "-21")
 
+# Calculate the difference between two dates.
+months_between_dates(a, b) = int((tm_year(b) - tm_year(a)) * 12 + tm_mon(b) - tm_mon(a))
+
+# One season is 3 months.
+seasons_between_dates(a, b) = int(months_between_dates(a, b) / 3)
+
 # Dummy plot to gather some stats from the dataset
 set terminal unknown
 plot 'data.dat' using 1:2
@@ -83,12 +89,9 @@ set xrange [xmin:xmax]
 set yrange [ymin:ymax]
 
 # Calculate the x sampling rate for the '+' plot below.
+# Have one sample per season, the +1 is to include the season of xmax.
 # This way we draw only the strictly needed boxxys for the background.
-#
-# Divide by three_months in order to have intervals of three months each and
-# round x_samples to the next multiple of 3 because the samples will be
-# divided by 3 in the season() function to set the season color.
-x_samples = ceil((xmax - xmin) / three_months / 3) * 3
+x_samples = seasons_between_dates(xmin, xmax) + 1
 set samples x_samples, 100
 
 set xtics xmin,three_months