From 22ac877f461a829ddd1a9bd4ed2f4fb1a3bff49a Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Mon, 8 Jun 2009 19:13:44 +0200 Subject: [PATCH] awful.widget.graph: Add a "scale" property If this is set to true (default is false), then the graph widget automatically scales its content to make it fit exactly. If "max_value" is also set, this is the minimum "height" the graph will use. This can be useful for graphes which monitor things like network bandwidth which can vary a lot. Signed-off-by: Uli Schlachter Signed-off-by: Julien Danjou --- lib/awful/widget/graph.lua.in | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/awful/widget/graph.lua.in b/lib/awful/widget/graph.lua.in index 8691269ef..d3b5c109a 100644 --- a/lib/awful/widget/graph.lua.in +++ b/lib/awful/widget/graph.lua.in @@ -51,14 +51,22 @@ local data = setmetatable({}, { __mode = "k" }) -- @param color The graph background color. --- Set the maximum value the graph should handle. +-- If "scale" is also set, the graph never scales up below this value, but it +-- automatically scales down to make all data fit. -- @name set_max_value -- @class function -- @param graph The graph. -- @param value The value. +--- Set the graph to automatically scale its values. Default is false. +-- @name set_scale +-- @class function +-- @param graph The graph. +-- @param scale A boolean value + local properties = { "width", "height", "border_color", "gradient_colors", "gradient_angle", "color", - "background_color", "max_value" } + "background_color", "max_value", "scale" } local function update(graph) -- Create new empty image @@ -75,6 +83,14 @@ local function update(graph) local values = data[graph].values local max_value = data[graph].max_value + if data[graph].scale then + for _, v in ipairs(values) do + if v > max_value then + max_value = v + end + end + end + -- Draw background -- Draw full gradient if data[graph].gradient_colors then @@ -128,7 +144,10 @@ local function add_value(graph, value) local value = value or 0 local max_value = data[graph].max_value - value = math.min(max_value, math.max(0, value)) + value = math.max(0, value) + if not data[graph].scale then + value = math.min(max_value, value) + end table.insert(data[graph].values, value)