Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dofile("guiUtils")
- dofile("bar")
- graph = {}
- function graph.new()
- local newGraph = {}
- newGraph.dataList = {}
- newGraph.w = 0
- newGraph.h = 0
- newGraph.x = 0
- newGraph.y = 0
- newGraph.barWidth = 1
- newGraph.dirtyBars = true
- newGraph.highValue = 0
- newGraph.bars = {}
- newGraph.monitor = term
- newGraph.barColor = colors.white
- setmetatable(newGraph,{__index=graph})
- return newGraph
- end
- function graph:getMonitor()
- return self.monitor
- end
- function graph:setHighValue(hVal)
- self.highValue = hVal
- end
- function graph:getHighValue()
- return self.highValue
- end
- function graph:setMonitor(newMonitor)
- self.monitor = newMonitor
- end
- function graph:getDisplayRect()
- return {x=self.x,y=self.y,w=self.w,h=self.h}
- end
- function graph:setSize(w,h)
- self.w = w
- self.h = h
- self.dirtyBars = true
- end
- function graph:setPos(x,y)
- self.x = x
- self.y = y
- self.dirtyBars = true
- end
- function graph:getSize()
- return self.w,self.h
- end
- function graph:getPos()
- return self.x,self.y
- end
- function graph:setBarWidth(newSize)
- self.barWidth = newSize
- end
- function graph:getBarWidth()
- return self.barWidth
- end
- function graph:setBarColor(barColor)
- self.barColor = barColor
- self.dirtyBars = true
- end
- function graph:calcBars()
- self.bars = {}
- local barCount = math.floor(self.w / self.barWidth)
- for i=1,barCount,1 do
- local newBar = bar.new()
- newBar:setPos((i-1)*self.barWidth,0)
- newBar:setSize(self:getBarWidth(),self.h)
- newBar:setParent(self)
- newBar:setColor(self.barColor)
- table.insert(self.bars,newBar)
- end
- self.dirtyBars = false
- end
- function graph:updateBars()
- for i=1,#self.bars,1 do
- local barNum = #self.bars-i+1
- self.bars[barNum]:setHighValue(self:getHighValue())
- if self.dataList[#self.dataList-i+1] ~= nil then
- self.bars[barNum]:setValue(self.dataList[#self.dataList-i+1].value)
- else
- self.bars[barNum]:setValue(0)
- end
- end
- end
- function graph:draw()
- if self.dirtyBars then self:calcBars() end
- guiUtils.clearArea(self:getMonitor(),self:getDisplayRect())
- self:updateBars()
- for k,v in pairs(self.bars) do
- v:draw()
- end
- end
- function graph:setData(dataList)
- self.dataList = dataList
- end
- function graph:addData(dataValue)
- table.insert(self.dataList,{value=dataValue})
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement