Skip to content

Commit

Permalink
Merge pull request #176 from CSCfi/fix-38-trend-diagram-decade-labels
Browse files Browse the repository at this point in the history
Fix #38: Trend diagram: Correct x-axis decade labels for decades 1900 and before
  • Loading branch information
majsan authored Jun 22, 2021
2 parents faff967 + c09ce2e commit 8aacf36
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions app/scripts/results.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down

0 comments on commit 8aacf36

Please sign in to comment.