Advertisement
Pinkishu

graph

Jan 19th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.50 KB | None | 0 0
  1. dofile("guiUtils")
  2. dofile("bar")
  3.  
  4. graph = {}
  5.  
  6. function graph.new()
  7.     local newGraph = {}
  8.     newGraph.dataList = {}
  9.     newGraph.w = 0
  10.     newGraph.h = 0
  11.     newGraph.x = 0
  12.     newGraph.y = 0
  13.     newGraph.barWidth = 1
  14.     newGraph.dirtyBars = true
  15.     newGraph.highValue = 0
  16.     newGraph.bars = {}
  17.     newGraph.monitor = term
  18.     newGraph.barColor = colors.white
  19.     setmetatable(newGraph,{__index=graph})
  20.     return newGraph
  21. end
  22.  
  23. function graph:getMonitor()
  24.     return self.monitor
  25. end
  26.  
  27. function graph:setHighValue(hVal)
  28.     self.highValue = hVal
  29. end
  30.  
  31. function graph:getHighValue()
  32.     return self.highValue
  33. end
  34.  
  35. function graph:setMonitor(newMonitor)
  36.     self.monitor = newMonitor
  37. end
  38.  
  39. function graph:getDisplayRect()
  40.     return {x=self.x,y=self.y,w=self.w,h=self.h}
  41. end
  42.  
  43. function graph:setSize(w,h)
  44.     self.w = w
  45.     self.h = h
  46.     self.dirtyBars = true
  47. end
  48.  
  49. function graph:setPos(x,y)
  50.     self.x = x
  51.     self.y = y
  52.     self.dirtyBars = true
  53. end
  54.  
  55. function graph:getSize()
  56.     return self.w,self.h
  57. end
  58.  
  59. function graph:getPos()
  60.     return self.x,self.y
  61. end
  62.  
  63. function graph:setBarWidth(newSize)
  64.     self.barWidth = newSize
  65. end
  66.  
  67. function graph:getBarWidth()
  68.     return self.barWidth
  69. end
  70.  
  71. function graph:setBarColor(barColor)
  72.     self.barColor = barColor
  73.     self.dirtyBars = true
  74. end
  75.  
  76. function graph:calcBars()
  77.    
  78.     self.bars = {}
  79.     local barCount = math.floor(self.w / self.barWidth)
  80.     for i=1,barCount,1 do
  81.         local newBar = bar.new()
  82.         newBar:setPos((i-1)*self.barWidth,0)
  83.         newBar:setSize(self:getBarWidth(),self.h)
  84.         newBar:setParent(self)
  85.         newBar:setColor(self.barColor)
  86.         table.insert(self.bars,newBar)
  87.     end
  88.  
  89.     self.dirtyBars = false
  90. end
  91.  
  92. function graph:updateBars()
  93.  
  94.  
  95.     for i=1,#self.bars,1 do
  96.         local barNum = #self.bars-i+1
  97.         self.bars[barNum]:setHighValue(self:getHighValue())
  98.         if self.dataList[#self.dataList-i+1] ~= nil then
  99.             self.bars[barNum]:setValue(self.dataList[#self.dataList-i+1].value)
  100.  
  101.         else
  102.             self.bars[barNum]:setValue(0)
  103.         end
  104.     end
  105. end
  106.  
  107. function graph:draw()
  108.     if self.dirtyBars then self:calcBars() end
  109.     guiUtils.clearArea(self:getMonitor(),self:getDisplayRect())
  110.     self:updateBars()
  111.     for k,v in pairs(self.bars) do
  112.         v:draw()
  113.     end
  114. end
  115.  
  116. function graph:setData(dataList)
  117.     self.dataList = dataList
  118. end
  119.  
  120. function graph:addData(dataValue)
  121.     table.insert(self.dataList,{value=dataValue})
  122. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement