Advertisement
infiniteblock

Untitled

Apr 14th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. -- Here be nightmares
  2. -- Seriously, don't look
  3.  
  4. local util = require("covid19.util")
  5. local gfx = require("covid19.gfx")
  6. local bigfont = require("bigfont")
  7.  
  8. local Plot = {}
  9.  
  10. local HEIGHT_MAP = {"\144", "\148", "\149"}
  11. local X_TICK_FREQUENCY = 8
  12. local Y_TICK_FREQUENCY = 2
  13.  
  14. function Plot.new(t, x, y, w, h, title, colour)
  15. local plot = {
  16. t = t,
  17. x = x,
  18. y = y,
  19. w = w,
  20. h = h,
  21. title = title,
  22. colour = colour,
  23. min = math.huge,
  24. max = 1,
  25. scaleHeight = (h - 2) * 3
  26. }
  27.  
  28. function plot:addData(data)
  29. self.data = data
  30.  
  31. for _, datum in pairs(data) do
  32. local v = datum.value
  33. if v > self.max then self.max = v end
  34. if v < self.min then self.min = v end
  35. end
  36. end
  37.  
  38. function plot:scalePoint(value)
  39. return math.ceil((value / self.max) * self.scaleHeight)
  40. end
  41.  
  42. function plot:drawTick(x, maxPoints, now)
  43. local time = now - (24 * 60 * 60 * (maxPoints - x))
  44. local date = os.date("%m/%d", time)
  45.  
  46. t.setCursorPos(self.x + x, self.y + self.h + 3)
  47. t.write("|")
  48. t.setCursorPos(self.x + x - 2, self.y + self.h + 4)
  49. t.write(date)
  50. end
  51.  
  52. function plot:draw()
  53. local now = math.floor(os.epoch("utc") / 1000)
  54.  
  55. t.setTextColour(colour)
  56. bigfont.writeOn(t, 1, title, x, y)
  57. gfx.drawBox(t, x, y + 3, w, h)
  58.  
  59. t.setTextColour(colours.lightGrey)
  60.  
  61. local maxPoints = math.min(w - 2, #self.data)
  62. local start = #self.data - maxPoints
  63. for i = 1, maxPoints do
  64. local datum = self.data[i + start]
  65. local scaled = self:scalePoint(datum.value)
  66. local y = (self.h - math.floor(scaled / 3))
  67. local barHeight = self.h - y
  68. local barStart = self.y + 2 + y
  69.  
  70. for j = barStart, barStart + barHeight do
  71. t.setCursorPos(self.x + i, j)
  72.  
  73. if j == barStart then
  74. t.write(HEIGHT_MAP[((scaled - 1) % 3) + 1])
  75. else
  76. t.write("\149")
  77. end
  78. end
  79. end
  80.  
  81. -- x-axis ticks
  82. for i = 1, maxPoints, X_TICK_FREQUENCY do
  83. plot:drawTick(i, maxPoints, now)
  84. end
  85. plot:drawTick(maxPoints, maxPoints, now)
  86.  
  87. -- y-axis ticks
  88. for i = 1, self.h, Y_TICK_FREQUENCY do
  89. local val = math.floor((((self.max - self.min) / self.h) * i))
  90. if i == 1 then val = self.min
  91. elseif i == self.h then val = self.max end
  92.  
  93. --print(val, self.h, self.min, self.max)
  94.  
  95. t.setCursorPos(self.x + self.w + 1, self.y + self.h - i + 3)
  96. t.write(util.formatCommas(val))
  97. end
  98. end
  99.  
  100. return plot
  101. end
  102.  
  103. return Plot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement