Add clear() function to awful.widget.graph and doc fixes (#847)

* awful.widget.graph: add clear() function.

* awful.widget.graph: doc fixes for add_value.
add_value did not show up in generated luadoc. And the value parameter does not need to be between 0 and 1.

* awful.widget.graph: local functions clear and add_value as methods of graph.
This commit is contained in:
Wolfgang Popp 2016-04-26 22:14:09 +02:00 committed by Daniel Hahler
parent ad304a2596
commit 76313263b6
1 changed files with 23 additions and 18 deletions

View File

@ -161,42 +161,46 @@ function graph.fit(_graph)
end end
--- Add a value to the graph --- Add a value to the graph
-- @param _graph The graph. --
-- @param value The value between 0 and 1. -- @param value The value to be added to the graph
-- @param group The stack color group index. -- @param group The stack color group index.
local function add_value(_graph, value, group) function graph:add_value(value, group)
if not _graph then return end
value = value or 0 value = value or 0
local values = _graph._data.values local values = self._data.values
local max_value = _graph._data.max_value local max_value = self._data.max_value
value = math.max(0, value) value = math.max(0, value)
if not _graph._data.scale then if not self._data.scale then
value = math.min(max_value, value) value = math.min(max_value, value)
end end
if _graph._data.stack and group then if self._data.stack and group then
if not _graph._data.values[group] if not self._data.values[group]
or type(_graph._data.values[group]) ~= "table" or type(self._data.values[group]) ~= "table"
then then
_graph._data.values[group] = {} self._data.values[group] = {}
end end
values = _graph._data.values[group] values = self._data.values[group]
end end
table.insert(values, value) table.insert(values, value)
local border_width = 0 local border_width = 0
if _graph._data.border_color then border_width = 2 end if self._data.border_color then border_width = 2 end
-- Ensure we never have more data than we can draw -- Ensure we never have more data than we can draw
while #values > _graph._data.width - border_width do while #values > self._data.width - border_width do
table.remove(values, 1) table.remove(values, 1)
end end
_graph:emit_signal("widget::redraw_needed") self:emit_signal("widget::redraw_needed")
return _graph return self
end end
--- Clear the graph.
function graph:clear()
self._data.values = {}
self:emit_signal("widget::redraw_needed")
return self
end
--- Set the graph height. --- Set the graph height.
-- @param height The height to set. -- @param height The height to set.
@ -253,7 +257,8 @@ function graph.new(args)
_graph._data = { width = width, height = height, values = {}, max_value = 1 } _graph._data = { width = width, height = height, values = {}, max_value = 1 }
-- Set methods -- Set methods
_graph.add_value = add_value _graph.add_value = graph["add_value"]
_graph.clear = graph["clear"]
_graph.draw = graph.draw _graph.draw = graph.draw
_graph.fit = graph.fit _graph.fit = graph.fit