2009-05-04 18:24:35 +02:00
|
|
|
---------------------------------------------------------------------------
|
2014-05-20 13:02:39 +02:00
|
|
|
--- A graph widget.
|
|
|
|
--
|
2009-05-04 18:24:35 +02:00
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
|
|
|
-- @copyright 2009 Julien Danjou
|
|
|
|
-- @release @AWESOME_VERSION@
|
2014-05-20 13:02:39 +02:00
|
|
|
-- @classmod awful.widget.graph
|
2009-05-04 18:24:35 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
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
|
|
|
|
2012-06-14 01:08:27 +02:00
|
|
|
local graph = { mt = {} }
|
2009-05-04 18:24:35 +02:00
|
|
|
|
|
|
|
local data = setmetatable({}, { __mode = "k" })
|
|
|
|
|
|
|
|
--- Set the graph border color.
|
|
|
|
-- If the value is nil, no border will be drawn.
|
2014-05-20 13:02:39 +02:00
|
|
|
--
|
|
|
|
-- @function set_border_color
|
2009-05-04 18:24:35 +02:00
|
|
|
-- @param graph The graph.
|
|
|
|
-- @param color The border color to set.
|
|
|
|
|
|
|
|
--- Set the graph foreground color.
|
2014-05-20 13:02:39 +02:00
|
|
|
--
|
|
|
|
-- @function set_color
|
2009-05-04 18:24:35 +02:00
|
|
|
-- @param graph The graph.
|
|
|
|
-- @param color The graph color.
|
|
|
|
|
|
|
|
--- Set the graph background color.
|
2014-05-20 13:02:39 +02:00
|
|
|
--
|
|
|
|
-- @function set_background_color
|
2009-05-04 18:24:35 +02:00
|
|
|
-- @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.
|
2014-05-20 13:02:39 +02:00
|
|
|
--
|
|
|
|
-- @function set_max_value
|
2009-06-08 19:08:49 +02:00
|
|
|
-- @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.
|
2014-05-20 13:02:39 +02:00
|
|
|
--
|
|
|
|
-- @function set_scale
|
2009-06-08 19:13:44 +02:00
|
|
|
-- @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.
|
2014-05-20 13:02:39 +02:00
|
|
|
--
|
|
|
|
-- @function set_stack
|
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.
|
2014-05-20 13:02:39 +02:00
|
|
|
--
|
|
|
|
-- @function set_stack_colors
|
2010-03-25 05:24:10 +01:00
|
|
|
-- @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
|
|
|
|
2012-06-14 01:08:27 +02:00
|
|
|
function graph.draw(_graph, wibox, cr, width, height)
|
|
|
|
local max_value = data[_graph].max_value
|
|
|
|
local values = data[_graph].values
|
2009-05-04 18:24:35 +02:00
|
|
|
|
2010-09-29 18:57:52 +02:00
|
|
|
cr:set_line_width(1)
|
|
|
|
|
2012-05-15 00:47:21 +02:00
|
|
|
-- Draw the background first
|
2012-06-14 01:08:27 +02:00
|
|
|
cr:set_source(color(data[_graph].background_color or "#000000aa"))
|
2012-06-13 18:06:05 +02:00
|
|
|
cr:paint()
|
|
|
|
|
|
|
|
-- Account for the border width
|
|
|
|
cr:save()
|
2012-06-14 01:08:27 +02:00
|
|
|
if data[_graph].border_color then
|
2012-06-13 18:06:05 +02:00
|
|
|
cr:translate(1, 1)
|
|
|
|
width, height = width - 2, height - 2
|
|
|
|
end
|
2012-05-15 00:47:21 +02:00
|
|
|
|
2010-03-25 05:24:10 +01:00
|
|
|
-- Draw a stacked graph
|
2012-06-14 01:08:27 +02:00
|
|
|
if data[_graph].stack then
|
2010-07-25 13:48:14 +02:00
|
|
|
|
2012-06-14 01:08:27 +02:00
|
|
|
if data[_graph].scale then
|
2010-07-25 13:48:14 +02:00
|
|
|
for _, v in ipairs(values) do
|
|
|
|
for __, sv in ipairs(v) do
|
|
|
|
if sv > max_value then
|
|
|
|
max_value = sv
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-13 18:06:05 +02:00
|
|
|
for i = 0, width do
|
2010-03-25 05:24:10 +01:00
|
|
|
local rel_i = 0
|
2012-06-13 18:06:05 +02:00
|
|
|
local rel_x = i + 0.5
|
2010-03-25 05:24:10 +01:00
|
|
|
|
2012-06-14 01:08:27 +02:00
|
|
|
if data[_graph].stack_colors then
|
|
|
|
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
|
2012-06-13 18:06:05 +02:00
|
|
|
cr:move_to(rel_x, height * (1 - (rel_i / max_value)))
|
|
|
|
cr:line_to(rel_x, height * (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
|
2012-06-14 01:08:27 +02:00
|
|
|
if data[_graph].scale then
|
2010-03-25 05:24:10 +01:00
|
|
|
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-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
|
2012-06-13 18:06:05 +02:00
|
|
|
cr:move_to(i + 0.5, height * (1 - value))
|
|
|
|
cr:line_to(i + 0.5, height)
|
2010-03-25 05:24:10 +01:00
|
|
|
end
|
2009-05-04 18:24:35 +02:00
|
|
|
end
|
2012-06-14 01:08:27 +02:00
|
|
|
cr:set_source(color(data[_graph].color or "#ff0000"))
|
2010-09-29 18:57:52 +02:00
|
|
|
cr:stroke()
|
2009-05-04 18:24:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-06-13 18:06:05 +02:00
|
|
|
-- Undo the cr:translate() for the border
|
|
|
|
cr:restore()
|
|
|
|
|
2010-03-25 05:24:10 +01:00
|
|
|
-- Draw the border last so that it overlaps already drawn values
|
2012-06-14 01:08:27 +02:00
|
|
|
if data[_graph].border_color then
|
2012-06-13 18:06:05 +02:00
|
|
|
-- We decremented these by two above
|
|
|
|
width, height = width + 2, height + 2
|
|
|
|
|
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)
|
2012-06-14 01:08:27 +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
|
|
|
|
2012-06-14 01:08:27 +02:00
|
|
|
function graph.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
|
2012-11-19 14:09:10 +01:00
|
|
|
-- @param _graph The graph.
|
2009-05-04 18:24:35 +02:00
|
|
|
-- @param value The value between 0 and 1.
|
2010-03-25 05:24:10 +01:00
|
|
|
-- @param group The stack color group index.
|
2012-06-14 01:08:27 +02:00
|
|
|
local function add_value(_graph, value, group)
|
|
|
|
if not _graph then return end
|
2009-05-04 18:24:35 +02:00
|
|
|
|
|
|
|
local value = value or 0
|
2012-06-14 01:08:27 +02:00
|
|
|
local values = data[_graph].values
|
|
|
|
local max_value = data[_graph].max_value
|
2009-06-08 19:13:44 +02:00
|
|
|
value = math.max(0, value)
|
2012-06-14 01:08:27 +02:00
|
|
|
if not data[_graph].scale then
|
2009-06-08 19:13:44 +02:00
|
|
|
value = math.min(max_value, value)
|
|
|
|
end
|
2009-05-04 18:24:35 +02:00
|
|
|
|
2012-06-14 01:08:27 +02:00
|
|
|
if data[_graph].stack and group then
|
|
|
|
if not data[_graph].values[group]
|
|
|
|
or type(data[_graph].values[group]) ~= "table"
|
2010-03-25 05:24:10 +01:00
|
|
|
then
|
2012-06-14 01:08:27 +02:00
|
|
|
data[_graph].values[group] = {}
|
2010-03-25 05:24:10 +01:00
|
|
|
end
|
2012-06-14 01:08:27 +02:00
|
|
|
values = data[_graph].values[group]
|
2010-03-25 05:24:10 +01:00
|
|
|
end
|
|
|
|
table.insert(values, value)
|
2009-05-04 18:24:35 +02:00
|
|
|
|
|
|
|
local border_width = 0
|
2012-06-14 01:08:27 +02:00
|
|
|
if data[_graph].border_color then border_width = 2 end
|
2009-05-04 18:24:35 +02:00
|
|
|
|
|
|
|
-- Ensure we never have more data than we can draw
|
2012-06-14 01:08:27 +02:00
|
|
|
while #values > data[_graph].width - border_width do
|
2010-03-25 05:24:10 +01:00
|
|
|
table.remove(values, 1)
|
2009-05-04 18:24:35 +02:00
|
|
|
end
|
|
|
|
|
2012-06-14 01:08:27 +02:00
|
|
|
_graph:emit_signal("widget::updated")
|
|
|
|
return _graph
|
2009-05-04 18:24:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--- Set the graph height.
|
|
|
|
-- @param height The height to set.
|
2012-11-19 14:09:10 +01:00
|
|
|
function graph:set_height(height)
|
2009-05-04 18:24:35 +02:00
|
|
|
if height >= 5 then
|
2012-11-19 14:09:10 +01:00
|
|
|
data[self].height = height
|
|
|
|
self:emit_signal("widget::updated")
|
2009-05-04 18:24:35 +02:00
|
|
|
end
|
2012-11-19 14:09:10 +01:00
|
|
|
return self
|
2009-05-04 18:24:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Set the graph width.
|
|
|
|
-- @param width The width to set.
|
2012-11-19 14:09:10 +01:00
|
|
|
function graph:set_width(width)
|
2009-05-04 18:24:35 +02:00
|
|
|
if width >= 5 then
|
2012-11-19 14:09:10 +01:00
|
|
|
data[self].width = width
|
|
|
|
self:emit_signal("widget::updated")
|
2009-05-04 18:24:35 +02:00
|
|
|
end
|
2012-11-19 14:09:10 +01:00
|
|
|
return self
|
2009-05-04 18:24:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Build properties function
|
|
|
|
for _, prop in ipairs(properties) do
|
2012-06-14 01:08:27 +02:00
|
|
|
if not graph["set_" .. prop] then
|
|
|
|
graph["set_" .. prop] = function(_graph, value)
|
|
|
|
data[_graph][prop] = value
|
|
|
|
_graph:emit_signal("widget::updated")
|
|
|
|
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.
|
2012-06-14 01:08:27 +02:00
|
|
|
function graph.new(args)
|
2009-05-04 18:24:35 +02:00
|
|
|
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
|
|
|
|
|
2012-06-14 01:08:27 +02:00
|
|
|
local _graph = base.make_widget()
|
2009-05-04 18:24:35 +02:00
|
|
|
|
2012-06-14 01:08:27 +02:00
|
|
|
data[_graph] = { width = width, height = height, values = {}, max_value = 1 }
|
2009-05-04 18:24:35 +02:00
|
|
|
|
|
|
|
-- Set methods
|
2012-06-14 01:08:27 +02:00
|
|
|
_graph.add_value = add_value
|
|
|
|
_graph.draw = graph.draw
|
|
|
|
_graph.fit = graph.fit
|
2009-05-04 18:24:35 +02:00
|
|
|
|
|
|
|
for _, prop in ipairs(properties) do
|
2012-06-16 21:12:26 +02:00
|
|
|
_graph["set_" .. prop] = graph["set_" .. prop]
|
2009-05-04 18:24:35 +02:00
|
|
|
end
|
|
|
|
|
2012-06-14 01:08:27 +02:00
|
|
|
return _graph
|
2009-05-04 18:24:35 +02:00
|
|
|
end
|
|
|
|
|
2012-06-14 01:08:27 +02:00
|
|
|
function graph.mt:__call(...)
|
|
|
|
return graph.new(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(graph, graph.mt)
|
2009-05-04 18:24:35 +02:00
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|