From c09ce2eae1bee66b26d6ed4ccb362dc9ea483370 Mon Sep 17 00:00:00 2001 From: Jyrki Niemi Date: Tue, 25 May 2021 16:14:41 +0300 Subject: [PATCH] Fix #38: Correct trend diagram labels for decades <=1900 results.js: view.GraphResults.renderGraph: - Render correct x-axis decade labels in the trend diagram for decades 1900 and before by passing to Rickshaw.Graph.Axis.Time constructor the existing fixed time.ceil function with special handling of decades. (Previously, labels for decades 1900 and before were shifted back by one (1890 instead of 1900 and so on), probably resulting from Rickshaw's approximate handling of leap years.) - Modify the fixed time.ceil to work for decades 1800 and before. --- app/scripts/results.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/app/scripts/results.js b/app/scripts/results.js index b015df35e..e59b61998 100644 --- a/app/scripts/results.js +++ b/app/scripts/results.js @@ -2156,13 +2156,21 @@ view.GraphResults = class GraphResults extends BaseResults { const toDate = (sec) => moment(sec * 1000).toDate() const time = new Rickshaw.Fixtures.Time() + // Fix time.ceil for decades: Rickshaw.Fixtures.Time.ceil + // returns one decade too small values for 1900 and before. + // (The root cause may be Rickshaw's approximate handling of + // leap years: 1900 was not a leap year.) const old_ceil = time.ceil time.ceil = (time, unit) => { if (unit.name === "decade") { const out = Math.ceil(time / unit.seconds) * unit.seconds const mom = moment(out * 1000) - if (mom.date() === 31) { - mom.add("day", 1) + const monthDay = mom.date() + // If the day of the month is not 1, it is within the + // previous month (December), so add enough days to + // move the date to the expected month (January). + if (monthDay !== 1) { + mom.add(32 - monthDay, "day") } return mom.unix() } else { @@ -2172,9 +2180,10 @@ view.GraphResults = class GraphResults extends BaseResults { const xAxis = new Rickshaw.Graph.Axis.Time({ graph, + // Use the fixed .ceil for decades + timeFixture: time, + // timeUnit: time.unit("month") # TODO: bring back decade }) - // timeUnit: time.unit("month") # TODO: bring back decade - // timeFixture: new Rickshaw.Fixtures.Time() this.preview = new Rickshaw.Graph.RangeSlider.Preview({ graph,