From 8ee6f9d77d9d1cf28b8e436873c84aecc8205569 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 27 Sep 2018 21:19:25 +0200 Subject: Round timascale up to complete timescale_division units. --- room-schedule.rb | 51 +++++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/room-schedule.rb b/room-schedule.rb index 910a742..84864cc 100644 --- a/room-schedule.rb +++ b/room-schedule.rb @@ -169,12 +169,37 @@ EOL end private + + def timescale_unit + if timescale_divisions + timescale_divisions =~ /(\d+) ?(days|weeks|hours|minutes|seconds)?/ + division_units = $2 ? $2 : "days" + amount = $1.to_i + unit = nil + case division_units + when "weeks" + unit = 7 + when "days" + unit = 1 + when "hours" + unit = (1.0 / 24) + when "minutes" + unit = (1.0 / 1440) + when "seconds" + unit = (1.0 / 86400) + end + end + [ amount, unit ] + end + def x_range max_value = @data.map {|room| room[:data].map {|e| e.end_time}.max}.max min_value = @data.map {|room| room[:data].map {|e| e.start_time}.min}.min + amount, unit = timescale_unit + max_value += unit * (amount - (max_value.min % amount)) range = max_value - min_value - right_pad = range == 0 ? 10 : range / 20.0 + right_pad = 0 #range == 0 ? 10 : range / 20.0 scale_range = (max_value + right_pad) - min_value scale_division = scale_x_divisions || (scale_range / 10.0) @@ -190,27 +215,9 @@ EOL rv = [] min, max, scale_division = x_range if timescale_divisions - timescale_divisions =~ /(\d+) ?(days|weeks|hours|minutes|seconds)?/ - division_units = $2 ? $2 : "days" - amount = $1.to_i - if amount - step = nil - case division_units - when "weeks" - step = 7 * amount - when "days" - step = amount - when "hours" - step = (1.0 / 24) * amount - when "minutes" - step = (1.0 / 1440) * amount - when "seconds" - step = (1.0 / 86400) * amount - end - min.step( max, step ) {|v| rv << v} if step - - return rv - end + amount, unit = timescale_unit + min.step( max, amount * unit) {|v| rv << v} if amount + return rv end min.step(max, scale_division) {|v| rv << v} return rv -- cgit v1.2.3