awful.widget: add graph stack property
Initial implementation of stacked graphs. Adds two new methods, set_stack (false by default) and set_stack_colors (i.e. {"red", "white", "blue"}). The order of the colors matters, because the add_value method now accepts an (optional) last argument, an index of a color from your stack color group. Signed-off-by: Adrian C. (anrxc) <anrxc@sysphere.org> Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
parent
167770d06f
commit
dc5b09d3b0
|
@ -8,6 +8,7 @@ local setmetatable = setmetatable
|
||||||
local ipairs = ipairs
|
local ipairs = ipairs
|
||||||
local math = math
|
local math = math
|
||||||
local table = table
|
local table = table
|
||||||
|
local type = type
|
||||||
local capi = { image = image,
|
local capi = { image = image,
|
||||||
widget = widget }
|
widget = widget }
|
||||||
local layout = require("awful.widget.layout")
|
local layout = require("awful.widget.layout")
|
||||||
|
@ -65,9 +66,21 @@ local data = setmetatable({}, { __mode = "k" })
|
||||||
-- @param graph The graph.
|
-- @param graph The graph.
|
||||||
-- @param scale A boolean value
|
-- @param scale A boolean value
|
||||||
|
|
||||||
local properties = { "width", "height", "border_color",
|
--- Set the graph to draw stacks. Default is false.
|
||||||
"gradient_colors", "gradient_angle", "color",
|
-- @name set_stack
|
||||||
"background_color", "max_value", "scale" }
|
-- @class function
|
||||||
|
-- @param progressbar The graph.
|
||||||
|
-- @param stack A boolean value.
|
||||||
|
|
||||||
|
--- Set the graph stacking colors. Order matters.
|
||||||
|
-- @name set_stack_colors
|
||||||
|
-- @class function
|
||||||
|
-- @param graph The graph.
|
||||||
|
-- @param stack_colors A table with stacking colors.
|
||||||
|
|
||||||
|
local properties = { "width", "height", "border_color", "stack",
|
||||||
|
"stack_colors", "gradient_colors", "gradient_angle",
|
||||||
|
"color", "background_color", "max_value", "scale" }
|
||||||
|
|
||||||
local function update(graph)
|
local function update(graph)
|
||||||
-- Create new empty image
|
-- Create new empty image
|
||||||
|
@ -78,61 +91,90 @@ local function update(graph)
|
||||||
border_width = 1
|
border_width = 1
|
||||||
end
|
end
|
||||||
|
|
||||||
local values = data[graph].values
|
-- Draw a stacked graph
|
||||||
local max_value = data[graph].max_value
|
if data[graph].stack then
|
||||||
|
-- Draw the background first
|
||||||
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
|
|
||||||
img:draw_rectangle_gradient(border_width, border_width,
|
|
||||||
data[graph].width - (2 * border_width),
|
|
||||||
data[graph].height - (2 * border_width),
|
|
||||||
data[graph].gradient_colors,
|
|
||||||
data[graph].gradient_angle or 270)
|
|
||||||
else
|
|
||||||
img:draw_rectangle(border_width, border_width,
|
img:draw_rectangle(border_width, border_width,
|
||||||
data[graph].width - (2 * border_width),
|
data[graph].width - (2 * border_width),
|
||||||
data[graph].height - (2 * border_width),
|
data[graph].height,
|
||||||
true, data[graph].color or "red")
|
true, data[graph].background_color or "#000000aa")
|
||||||
end
|
|
||||||
|
|
||||||
-- No value? Draw nothing.
|
for i = 0, data[graph].width - (2 * border_width) do
|
||||||
if #values ~= 0 then
|
local rel_i = 0
|
||||||
-- Draw reverse
|
local rel_x = data[graph].width - border_width - i - 1
|
||||||
for i = 0, #values - 1 do
|
|
||||||
local value = values[#values - i]
|
if data[graph].stack_colors then
|
||||||
if value >= 0 then
|
for idx, color in ipairs(data[graph].stack_colors) do
|
||||||
value = value / max_value
|
local values = data[graph].values[idx]
|
||||||
img:draw_line(data[graph].width - border_width - i - 1,
|
if values and i < #values then
|
||||||
border_width - 1 +
|
local value = values[#values - i] + rel_i
|
||||||
math.floor(((data[graph].height - 2 * border_width) * (1 - value)) + 0.5),
|
|
||||||
data[graph].width - border_width - i - 1,
|
img:draw_line(rel_x, border_width - 1 +
|
||||||
border_width - 1,
|
math.ceil((data[graph].height - 2 * border_width) * (1 - rel_i)),
|
||||||
data[graph].background_color or "#000000aa")
|
rel_x, border_width - 1 +
|
||||||
|
math.ceil((data[graph].height - 2 * border_width) * (1 - value)),
|
||||||
|
color or "red")
|
||||||
|
rel_i = value
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
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 full gradient
|
||||||
|
if data[graph].gradient_colors then
|
||||||
|
img:draw_rectangle_gradient(border_width, border_width,
|
||||||
|
data[graph].width - (2 * border_width),
|
||||||
|
data[graph].height - (2 * border_width),
|
||||||
|
data[graph].gradient_colors,
|
||||||
|
data[graph].gradient_angle or 270)
|
||||||
|
else
|
||||||
|
img:draw_rectangle(border_width, border_width,
|
||||||
|
data[graph].width - (2 * border_width),
|
||||||
|
data[graph].height - (2 * border_width),
|
||||||
|
true, data[graph].color or "red")
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Draw the background on no value
|
||||||
|
if #values ~= 0 then
|
||||||
|
-- Draw reverse
|
||||||
|
for i = 0, #values - 1 do
|
||||||
|
local value = values[#values - i]
|
||||||
|
if value >= 0 then
|
||||||
|
value = value / max_value
|
||||||
|
img:draw_line(data[graph].width - border_width - i - 1,
|
||||||
|
border_width - 1 +
|
||||||
|
math.ceil((data[graph].height - 2 * border_width) * (1 - value)),
|
||||||
|
data[graph].width - border_width - i - 1,
|
||||||
|
border_width - 1,
|
||||||
|
data[graph].background_color or "#000000aa")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- If we didn't draw values in full length, draw a square
|
||||||
|
-- over the last, left, part to reset everything to 0
|
||||||
|
if #values < data[graph].width - (2 * border_width) then
|
||||||
|
img:draw_rectangle(border_width, border_width,
|
||||||
|
data[graph].width - (2 * border_width) - #values,
|
||||||
|
data[graph].height - (2 * border_width),
|
||||||
|
true, data[graph].background_color or "#000000aa")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- If we did not draw values everywhere, draw a square over the last left
|
-- Draw the border last so that it overlaps already drawn values
|
||||||
-- part to set everything to 0 :-)
|
|
||||||
if #values < data[graph].width - (2 * border_width) then
|
|
||||||
img:draw_rectangle(border_width, border_width,
|
|
||||||
data[graph].width - (2 * border_width) - #values,
|
|
||||||
data[graph].height - (2 * border_width),
|
|
||||||
true, data[graph].background_color or "#000000aa")
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Draw the border last so that it overlaps other stuff
|
|
||||||
if data[graph].border_color then
|
if data[graph].border_color then
|
||||||
-- Draw border
|
-- Draw the border
|
||||||
img:draw_rectangle(0, 0, data[graph].width, data[graph].height,
|
img:draw_rectangle(0, 0, data[graph].width, data[graph].height,
|
||||||
false, data[graph].border_color or "white")
|
false, data[graph].border_color or "white")
|
||||||
end
|
end
|
||||||
|
@ -144,24 +186,34 @@ end
|
||||||
--- Add a value to the graph
|
--- Add a value to the graph
|
||||||
-- @param graph The graph.
|
-- @param graph The graph.
|
||||||
-- @param value The value between 0 and 1.
|
-- @param value The value between 0 and 1.
|
||||||
local function add_value(graph, value)
|
-- @param group The stack color group index.
|
||||||
|
local function add_value(graph, value, group)
|
||||||
if not graph then return end
|
if not graph then return end
|
||||||
|
|
||||||
local value = value or 0
|
local value = value or 0
|
||||||
|
local values = data[graph].values
|
||||||
local max_value = data[graph].max_value
|
local max_value = data[graph].max_value
|
||||||
value = math.max(0, value)
|
value = math.max(0, value)
|
||||||
if not data[graph].scale then
|
if not data[graph].scale then
|
||||||
value = math.min(max_value, value)
|
value = math.min(max_value, value)
|
||||||
end
|
end
|
||||||
|
|
||||||
table.insert(data[graph].values, value)
|
if data[graph].stack and group then
|
||||||
|
if not data[graph].values[group]
|
||||||
|
or type(data[graph].values[group]) ~= "table"
|
||||||
|
then
|
||||||
|
data[graph].values[group] = {}
|
||||||
|
end
|
||||||
|
values = data[graph].values[group]
|
||||||
|
end
|
||||||
|
table.insert(values, value)
|
||||||
|
|
||||||
local border_width = 0
|
local border_width = 0
|
||||||
if data[graph].border then border_width = 2 end
|
if data[graph].border 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 #data[graph].values > data[graph].width - border_width do
|
while #values > data[graph].width - border_width do
|
||||||
table.remove(data[graph].values, 1)
|
table.remove(values, 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
update(graph)
|
update(graph)
|
||||||
|
|
Loading…
Reference in New Issue