Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Here be nightmares
- -- Seriously, don't look
- local util = require("gab.util")
- local gfx = require("gab.gfx")
- local bigfont = require("bigfont")
- local Plot = {}
- local HEIGHT_MAP = {"\144", "\148", "\149"}
- local X_TICK_FREQUENCY = 8
- local Y_TICK_FREQUENCY = 2
- function Plot.new(t, x, y, w, h, title, colour)
- local plot = {
- t = t,
- x = x,
- y = y,
- w = w,
- h = h,
- title = title,
- colour = colour,
- min = math.huge,
- max = 1,
- scaleHeight = (h - 2) * 3
- }
- function plot:addData(data)
- self.data = data
- for _, datum in pairs(data) do
- local v = datum.value
- if v > self.max then self.max = v end
- if v < self.min then self.min = v end
- end
- end
- function plot:scalePoint(value)
- return math.ceil((value / self.max) * self.scaleHeight)
- end
- function plot:drawTick(x, maxPoints, now)
- local time = now - (24 * 60 * 60 * (maxPoints - x))
- local date = os.date("%m/%d", time)
- t.setCursorPos(self.x + x, self.y + self.h + 3)
- t.write("|")
- t.setCursorPos(self.x + x - 2, self.y + self.h + 4)
- t.write(date)
- end
- function plot:draw()
- local now = math.floor(os.epoch("utc") / 1000)
- t.setTextColour(colour)
- bigfont.writeOn(t, 1, title, x, y)
- gfx.drawBox(t, x, y + 3, w, h)
- t.setTextColour(colours.lightGrey)
- local maxPoints = math.min(w - 2, #self.data)
- local start = #self.data - maxPoints
- for i = 1, maxPoints do
- local datum = self.data[i + start]
- local scaled = self:scalePoint(datum.value)
- local y = (self.h - math.floor(scaled / 3))
- local barHeight = self.h - y
- local barStart = self.y + 2 + y
- for j = barStart, barStart + barHeight do
- t.setCursorPos(self.x + i, j)
- if j == barStart then
- t.write(HEIGHT_MAP[((scaled - 1) % 3) + 1])
- else
- t.write("\149")
- end
- end
- end
- -- x-axis ticks
- for i = 1, maxPoints, X_TICK_FREQUENCY do
- plot:drawTick(i, maxPoints, now)
- end
- plot:drawTick(maxPoints, maxPoints, now)
- -- y-axis ticks
- for i = 1, self.h, Y_TICK_FREQUENCY do
- local val = math.floor((((self.max - self.min) / self.h) * i))
- if i == 1 then val = self.min
- elseif i == self.h then val = self.max end
- --print(val, self.h, self.min, self.max)
- t.setCursorPos(self.x + self.w + 1, self.y + self.h - i + 3)
- t.write(util.formatCommas(val))
- end
- end
- return plot
- end
- return Plot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement