Skip to content

Commit

Permalink
Fix spraakbanken#38: Correct trend diagram labels for decades <1900
Browse files Browse the repository at this point in the history
Render correct x-axis decade labels for decades before 1900 by passing
to Rickshaw.Graph.Axis.Time constructor the existing fixed
Rickshaw.Fixtures.Time.ceil. Also modify the fixed .ceil to work for
decades before 1800.
  • Loading branch information
janiemi committed May 25, 2021
1 parent d62f3e3 commit d0d1fe9
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions app/scripts/results.js
Original file line number Diff line number Diff line change
Expand Up @@ -2156,13 +2156,22 @@ 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 before 1900. (The root
// cause may be Rickshaw's simplistic 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).
// (Should we check the month, too?)
if (monthDay !== 1) {
mom.add(32 - monthDay, "day")
}
return mom.unix()
} else {
Expand All @@ -2172,6 +2181,8 @@ 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
// timeFixture: new Rickshaw.Fixtures.Time()
Expand Down

0 comments on commit d0d1fe9

Please sign in to comment.