2009-05-04 18:24:35 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
|
|
|
-- @copyright 2009 Julien Danjou
|
|
|
|
-- @release @AWESOME_VERSION@
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local ipairs = ipairs
|
|
|
|
local math = math
|
|
|
|
local table = table
|
2010-03-25 05:24:10 +01:00
|
|
|
local type = type
|
2010-09-29 18:57:52 +02:00
|
|
|
local color = require("gears.color")
|
2010-10-06 14:25:59 +02:00
|
|
|
local base = require("wibox.widget.base")
|
2009-05-04 18:24:35 +02:00
|
|
|
|
|
|
|
--- A graph widget.
|
|
|
|
module("awful.widget.graph")
|
|
|
|
|
|
|
|
local data = setmetatable({}, { __mode = "k" })
|
|
|
|
|
|
|
|
--- Set the graph border color.
|
|
|
|
-- If the value is nil, no border will be drawn.
|
|
|
|
-- @name set_border_color
|
|
|
|
-- @class function
|
|
|
|
-- @param graph The graph.
|
|
|
|
-- @param color The border color to set.
|
|
|
|
|
|
|
|
--- Set the graph foreground color.
|
|
|
|
-- @name set_color
|
|
|
|
-- @class function
|
|
|
|
-- @param graph The graph.
|
|
|
|
-- @param color The graph color.
|
|
|
|
|
|
|
|
--- Set the graph background color.
|
|
|
|
-- @name set_background_color
|
|
|
|
-- @class function
|
|
|
|
-- @param graph The graph.
|
|
|
|
-- @param color The graph background color.
|
|
|
|
|
2009-06-08 19:08:49 +02:00
|
|
|
--- Set the maximum value the graph should handle.
|
2009-06-08 19:13:44 +02:00
|
|
|
-- If "scale" is also set, the graph never scales up below this value, but it
|
|
|
|
-- automatically scales down to make all data fit.
|
2009-06-08 19:08:49 +02:00
|
|
|
-- @name set_max_value
|
|
|
|
-- @class function
|
|
|
|
-- @param graph The graph.
|
|
|
|
-- @param value The value.
|
|
|
|
|
2009-06-08 19:13:44 +02:00
|
|
|
--- 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
|
|
|
|
|
2010-03-25 05:24:10 +01:00
|
|
|
--- Set the graph to draw stacks. Default is false.
|
|
|
|
-- @name set_stack
|
|
|
|
-- @class function
|
2011-05-07 16:04:04 +02:00
|
|
|
-- @param graph The graph.
|
2010-03-25 05:24:10 +01:00
|
|
|
-- @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",
|
2010-09-29 19:02:03 +02:00
|
|
|
"stack_colors", "color", "background_color",
|
|
|
|
"max_value", "scale" }
|
2009-05-04 18:24:35 +02:00
|
|
|
|
2010-10-06 14:25:59 +02:00
|
|
|
function draw(graph, wibox, cr, width, height)
|
2010-07-25 13:48:14 +02:00
|
|
|
local max_value = data[graph].max_value
|
|
|
|
local values = data[graph].values
|
2009-05-04 18:24:35 +02:00
|
|
|
|
|
|
|
local border_width = 0
|
|
|
|
if data[graph].border_color then
|
|
|
|
border_width = 1
|
|
|
|
end
|
|
|
|
|
2010-09-29 18:57:52 +02:00
|
|
|
cr:set_line_width(1)
|
|
|
|
|
2010-03-25 05:24:10 +01:00
|
|
|
-- Draw a stacked graph
|
|
|
|
if data[graph].stack then
|
2010-07-25 13:48:14 +02:00
|
|
|
|
|
|
|
if data[graph].scale then
|
|
|
|
for _, v in ipairs(values) do
|
|
|
|
for __, sv in ipairs(v) do
|
|
|
|
if sv > max_value then
|
|
|
|
max_value = sv
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-25 05:24:10 +01:00
|
|
|
-- Draw the background first
|
2010-09-29 18:57:52 +02:00
|
|
|
cr:rectangle(border_width, border_width,
|
2010-10-07 13:50:44 +02:00
|
|
|
width - (2 * border_width),
|
|
|
|
height)
|
2010-09-29 18:57:52 +02:00
|
|
|
cr:set_source(color(data[graph].background_color or "#000000aa"))
|
|
|
|
cr:fill()
|
2009-05-04 18:24:35 +02:00
|
|
|
|
2010-10-06 14:25:59 +02:00
|
|
|
for i = 0, width - (2 * border_width) do
|
2010-03-25 05:24:10 +01:00
|
|
|
local rel_i = 0
|
2010-10-06 14:25:59 +02:00
|
|
|
local rel_x = width - border_width - i - 0.5
|
2010-03-25 05:24:10 +01:00
|
|
|
|
|
|
|
if data[graph].stack_colors then
|
2010-09-29 18:57:52 +02:00
|
|
|
for idx, col in ipairs(data[graph].stack_colors) do
|
2010-07-25 13:48:14 +02:00
|
|
|
local stack_values = values[idx]
|
|
|
|
if stack_values and i < #stack_values then
|
|
|
|
local value = stack_values[#stack_values - i] + rel_i
|
2010-03-25 05:24:10 +01:00
|
|
|
|
2010-09-29 18:57:52 +02:00
|
|
|
cr:move_to(rel_x, border_width - 0.5 +
|
2010-10-07 13:50:44 +02:00
|
|
|
(height - 2 * border_width) * (1 - (rel_i / max_value)))
|
2010-09-29 18:57:52 +02:00
|
|
|
cr:line_to(rel_x, border_width - 0.5 +
|
2010-10-07 13:50:44 +02:00
|
|
|
(height - 2 * border_width) * (1 - (value / max_value)))
|
2010-09-29 18:57:52 +02:00
|
|
|
cr:set_source(color(col or "#ff0000"))
|
|
|
|
cr:stroke()
|
2010-03-25 05:24:10 +01:00
|
|
|
rel_i = value
|
|
|
|
end
|
|
|
|
end
|
2009-06-08 19:13:44 +02:00
|
|
|
end
|
|
|
|
end
|
2009-05-04 18:24:35 +02:00
|
|
|
else
|
2010-03-25 05:24:10 +01:00
|
|
|
if data[graph].scale then
|
|
|
|
for _, v in ipairs(values) do
|
|
|
|
if v > max_value then
|
|
|
|
max_value = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-05-04 18:24:35 +02:00
|
|
|
|
2010-09-29 19:02:03 +02:00
|
|
|
cr:rectangle(border_width, border_width,
|
2010-10-07 13:50:44 +02:00
|
|
|
width - (2 * border_width),
|
|
|
|
height - (2 * border_width))
|
2010-09-29 19:02:03 +02:00
|
|
|
cr:set_source(color(data[graph].color or "#ff0000"))
|
|
|
|
cr:fill()
|
2010-03-25 05:24:10 +01:00
|
|
|
|
|
|
|
-- 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
|
2010-10-06 14:25:59 +02:00
|
|
|
cr:move_to(width - border_width - i - 0.5,
|
2010-09-29 18:57:52 +02:00
|
|
|
border_width - 0.5 +
|
2010-10-07 13:50:44 +02:00
|
|
|
(height - 2 * border_width) * (1 - value))
|
|
|
|
cr:line_to(width - border_width - i - 0.5,
|
2010-09-29 18:57:52 +02:00
|
|
|
border_width - 1)
|
2010-03-25 05:24:10 +01:00
|
|
|
end
|
2009-05-04 18:24:35 +02:00
|
|
|
end
|
2010-09-29 18:57:52 +02:00
|
|
|
cr:set_source(color(data[graph].background_color or "#000000aa"))
|
|
|
|
cr:stroke()
|
2009-05-04 18:24:35 +02:00
|
|
|
end
|
|
|
|
|
2010-03-25 05:24:10 +01:00
|
|
|
-- If we didn't draw values in full length, draw a square
|
|
|
|
-- over the last, left, part to reset everything to 0
|
2010-10-06 14:25:59 +02:00
|
|
|
if #values < width - (2 * border_width) then
|
2010-09-29 18:57:52 +02:00
|
|
|
cr:rectangle(border_width, border_width,
|
2010-10-07 13:50:44 +02:00
|
|
|
width - (2 * border_width) - #values,
|
|
|
|
height - (2 * border_width))
|
2010-09-29 18:57:52 +02:00
|
|
|
cr:set_source(color(data[graph].background_color or "#000000aa"))
|
|
|
|
cr:fill()
|
2010-03-25 05:24:10 +01:00
|
|
|
end
|
2009-05-04 18:24:35 +02:00
|
|
|
end
|
|
|
|
|
2010-03-25 05:24:10 +01:00
|
|
|
-- Draw the border last so that it overlaps already drawn values
|
2009-07-03 17:29:18 +02:00
|
|
|
if data[graph].border_color then
|
2010-03-25 05:24:10 +01:00
|
|
|
-- Draw the border
|
2010-10-07 13:50:44 +02:00
|
|
|
cr:rectangle(0.5, 0.5, width - 1, height - 1)
|
2010-10-06 14:25:59 +02:00
|
|
|
cr:set_source(color(data[graph].border_color or "#ffffff"))
|
2010-09-29 18:57:52 +02:00
|
|
|
cr:stroke()
|
2009-07-03 17:29:18 +02:00
|
|
|
end
|
2010-10-06 14:25:59 +02:00
|
|
|
end
|
2009-05-04 18:24:35 +02:00
|
|
|
|
2010-10-06 14:25:59 +02:00
|
|
|
function fit(graph, width, height)
|
|
|
|
return data[graph].width, data[graph].height
|
2009-05-04 18:24:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Add a value to the graph
|
|
|
|
-- @param graph The graph.
|
|
|
|
-- @param value The value between 0 and 1.
|
2010-03-25 05:24:10 +01:00
|
|
|
-- @param group The stack color group index.
|
|
|
|
local function add_value(graph, value, group)
|
2009-05-04 18:24:35 +02:00
|
|
|
if not graph then return end
|
|
|
|
|
|
|
|
local value = value or 0
|
2010-03-25 05:24:10 +01:00
|
|
|
local values = data[graph].values
|
2009-06-08 19:08:49 +02:00
|
|
|
local max_value = data[graph].max_value
|
2009-06-08 19:13:44 +02:00
|
|
|
value = math.max(0, value)
|
|
|
|
if not data[graph].scale then
|
|
|
|
value = math.min(max_value, value)
|
|
|
|
end
|
2009-05-04 18:24:35 +02:00
|
|
|
|
2010-03-25 05:24:10 +01:00
|
|
|
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)
|
2009-05-04 18:24:35 +02:00
|
|
|
|
|
|
|
local border_width = 0
|
|
|
|
if data[graph].border then border_width = 2 end
|
|
|
|
|
|
|
|
-- Ensure we never have more data than we can draw
|
2010-03-25 05:24:10 +01:00
|
|
|
while #values > data[graph].width - border_width do
|
|
|
|
table.remove(values, 1)
|
2009-05-04 18:24:35 +02:00
|
|
|
end
|
|
|
|
|
2010-10-06 14:25:59 +02:00
|
|
|
graph:emit_signal("widget::updated")
|
2009-08-08 15:47:00 +02:00
|
|
|
return graph
|
2009-05-04 18:24:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--- Set the graph height.
|
|
|
|
-- @param graph The graph.
|
|
|
|
-- @param height The height to set.
|
2009-08-26 21:04:42 +02:00
|
|
|
function set_height(graph, height)
|
2009-05-04 18:24:35 +02:00
|
|
|
if height >= 5 then
|
|
|
|
data[graph].height = height
|
2010-10-06 14:25:59 +02:00
|
|
|
graph:emit_signal("widget::updated")
|
2009-05-04 18:24:35 +02:00
|
|
|
end
|
2009-08-08 15:47:00 +02:00
|
|
|
return graph
|
2009-05-04 18:24:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Set the graph width.
|
|
|
|
-- @param graph The graph.
|
|
|
|
-- @param width The width to set.
|
2009-08-26 21:04:42 +02:00
|
|
|
function set_width(graph, width)
|
2009-05-04 18:24:35 +02:00
|
|
|
if width >= 5 then
|
|
|
|
data[graph].width = width
|
2010-10-06 14:25:59 +02:00
|
|
|
graph:emit_signal("widget::updated")
|
2009-05-04 18:24:35 +02:00
|
|
|
end
|
2009-08-08 15:47:00 +02:00
|
|
|
return graph
|
2009-05-04 18:24:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Build properties function
|
|
|
|
for _, prop in ipairs(properties) do
|
|
|
|
if not _M["set_" .. prop] then
|
|
|
|
_M["set_" .. prop] = function(graph, value)
|
|
|
|
data[graph][prop] = value
|
2010-10-06 14:25:59 +02:00
|
|
|
graph:emit_signal("widget::updated")
|
2009-08-08 15:47:00 +02:00
|
|
|
return graph
|
2009-05-04 18:24:35 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Create a graph widget.
|
|
|
|
-- @param args Standard widget() arguments. You should add width and height
|
|
|
|
-- key to set graph geometry.
|
|
|
|
-- @return A graph widget.
|
|
|
|
function new(args)
|
|
|
|
local args = args or {}
|
|
|
|
|
|
|
|
local width = args.width or 100
|
|
|
|
local height = args.height or 20
|
|
|
|
|
|
|
|
if width < 5 or height < 5 then return end
|
|
|
|
|
2010-10-06 14:25:59 +02:00
|
|
|
local graph = base.make_widget()
|
2009-05-04 18:24:35 +02:00
|
|
|
|
2009-06-08 19:08:49 +02:00
|
|
|
data[graph] = { width = width, height = height, values = {}, max_value = 1 }
|
2009-05-04 18:24:35 +02:00
|
|
|
|
|
|
|
-- Set methods
|
|
|
|
graph.add_value = add_value
|
2010-10-06 14:25:59 +02:00
|
|
|
graph.draw = draw
|
|
|
|
graph.fit = fit
|
2009-05-04 18:24:35 +02:00
|
|
|
|
|
|
|
for _, prop in ipairs(properties) do
|
|
|
|
graph["set_" .. prop] = _M["set_" .. prop]
|
|
|
|
end
|
|
|
|
|
|
|
|
return graph
|
|
|
|
end
|
|
|
|
|
|
|
|
setmetatable(_M, { __call = function(_, ...) return new(...) end })
|
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|