Simplify the graph drawing code

Instead of having to handle the border width all the time, let's make cairo
worry about that.

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2012-06-13 18:06:05 +02:00
parent 7a1acd4a98
commit f702c4ea04
1 changed files with 20 additions and 19 deletions

View File

@ -70,19 +70,18 @@ function draw(graph, wibox, cr, width, height)
local max_value = data[graph].max_value
local values = data[graph].values
local border_width = 0
if data[graph].border_color then
border_width = 1
end
cr:set_line_width(1)
-- Draw the background first
cr:rectangle(border_width, border_width,
width - (2 * border_width),
height - (2 * border_width))
cr:set_source(color(data[graph].background_color or "#000000aa"))
cr:fill()
cr:paint()
-- Account for the border width
cr:save()
if data[graph].border_color then
cr:translate(1, 1)
width, height = width - 2, height - 2
end
-- Draw a stacked graph
if data[graph].stack then
@ -97,19 +96,17 @@ function draw(graph, wibox, cr, width, height)
end
end
for i = 0, width - (2 * border_width) do
for i = 0, width do
local rel_i = 0
local rel_x = border_width + i + 0.5
local rel_x = i + 0.5
if data[graph].stack_colors then
for idx, col in ipairs(data[graph].stack_colors) do
local stack_values = values[idx]
if stack_values and i < #stack_values then
local value = stack_values[#stack_values - i] + rel_i
cr:move_to(rel_x, border_width +
(height - 2 * border_width) * (1 - (rel_i / max_value)))
cr:line_to(rel_x, border_width +
(height - 2 * border_width) * (1 - (value / max_value)))
cr:move_to(rel_x, height * (1 - (rel_i / max_value)))
cr:line_to(rel_x, height * (1 - (value / max_value)))
cr:set_source(color(col or "#ff0000"))
cr:stroke()
rel_i = value
@ -133,10 +130,8 @@ function draw(graph, wibox, cr, width, height)
local value = values[#values - i]
if value >= 0 then
value = value / max_value
cr:move_to(border_width + i + 0.5, border_width +
(height - 2 * border_width) * (1 - value))
cr:line_to(border_width + i + 0.5, border_width +
(height - 2 * border_width))
cr:move_to(i + 0.5, height * (1 - value))
cr:line_to(i + 0.5, height)
end
end
cr:set_source(color(data[graph].color or "#ff0000"))
@ -145,8 +140,14 @@ function draw(graph, wibox, cr, width, height)
end
-- Undo the cr:translate() for the border
cr:restore()
-- Draw the border last so that it overlaps already drawn values
if data[graph].border_color then
-- We decremented these by two above
width, height = width + 2, height + 2
-- Draw the border
cr:rectangle(0.5, 0.5, width - 1, height - 1)
cr:set_source(color(data[graph].border_color or "#ffffff"))