awful.widget.graph: Add a max_value option

Signed-off-by: Uli Schlachter <psychon@znc.in>
Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Uli Schlachter 2009-06-08 19:08:49 +02:00 committed by Julien Danjou
parent aceab7a39a
commit 26f1904bc7
1 changed files with 19 additions and 12 deletions

View File

@ -50,9 +50,15 @@ local data = setmetatable({}, { __mode = "k" })
-- @param graph The graph. -- @param graph The graph.
-- @param color The graph background color. -- @param color The graph background color.
--- Set the maximum value the graph should handle.
-- @name set_max_value
-- @class function
-- @param graph The graph.
-- @param value The value.
local properties = { "width", "height", "border_color", local properties = { "width", "height", "border_color",
"gradient_colors", "gradient_angle", "color", "gradient_colors", "gradient_angle", "color",
"background_color" } "background_color", "max_value" }
local function update(graph) local function update(graph)
-- Create new empty image -- Create new empty image
@ -67,6 +73,7 @@ local function update(graph)
end end
local values = data[graph].values local values = data[graph].values
local max_value = data[graph].max_value
-- Draw background -- Draw background
-- Draw full gradient -- Draw full gradient
@ -87,15 +94,14 @@ local function update(graph)
if #values ~= 0 then if #values ~= 0 then
-- Draw reverse -- Draw reverse
for i = 0, #values - 1 do for i = 0, #values - 1 do
if values[#values - i] > 0 then local value = values[#values - i]
-- Do not draw a pixel if the value is 1 if value > 0 then
if values[#values - i] ~= 1 then value = value / max_value
img:draw_line(data[graph].width - border_width - i - 1, img:draw_line(data[graph].width - border_width - i - 1,
border_width + ((data[graph].height - 2 * border_width) * (1 - values[#values - i])), border_width + ((data[graph].height - 2 * border_width) * (1 - value)),
data[graph].width - border_width - i - 1, data[graph].width - border_width - i - 1,
border_width, border_width,
data[graph].background_color or "#000000aa") data[graph].background_color or "#000000aa")
end
end end
end end
end end
@ -121,7 +127,8 @@ local function add_value(graph, value)
if not graph then return end if not graph then return end
local value = value or 0 local value = value or 0
value = math.min(1, math.max(0, value)) local max_value = data[graph].max_value
value = math.min(max_value, math.max(0, value))
table.insert(data[graph].values, value) table.insert(data[graph].values, value)
@ -183,7 +190,7 @@ function new(args)
local graph = {} local graph = {}
graph.widget = capi.widget(args) graph.widget = capi.widget(args)
data[graph] = { width = width, height = height, values = {} } data[graph] = { width = width, height = height, values = {}, max_value = 1 }
-- Set methods -- Set methods
graph.add_value = add_value graph.add_value = add_value