piechart: Drop cache mechanism and update indentation
This commit is contained in:
parent
38f589903b
commit
facf681a04
|
@ -1,7 +1,6 @@
|
||||||
local setmetatable = setmetatable
|
local setmetatable = setmetatable
|
||||||
local math,pairs = math,pairs
|
local math,pairs = math,pairs
|
||||||
local color = require( "gears.color" )
|
local color = require( "gears.color" )
|
||||||
local cairo = require( "lgi" ).cairo
|
|
||||||
local wibox = require( "wibox" )
|
local wibox = require( "wibox" )
|
||||||
local beautiful = require( "beautiful" )
|
local beautiful = require( "beautiful" )
|
||||||
|
|
||||||
|
@ -36,40 +35,26 @@ end
|
||||||
local function compute_sum(data)
|
local function compute_sum(data)
|
||||||
local ret = 0
|
local ret = 0
|
||||||
for k,v in pairs(data) do ret = ret + v end
|
for k,v in pairs(data) do ret = ret + v end
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
end
|
end
|
||||||
|
|
||||||
local function draw(self, context, cr, width, height)
|
local function draw(self, context, cr, width, height)
|
||||||
if not self._private.data then return end
|
if not self._private.data then return end
|
||||||
|
|
||||||
-- Load from cache
|
|
||||||
local cached = self._private.cache[width*123+height*89]
|
|
||||||
if cached then
|
|
||||||
cr:set_source_surface(cached)
|
|
||||||
cr:paint()
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local img = cairo.ImageSurface.create(cairo.Format.ARGB32, width, height)
|
|
||||||
local cr2 = cairo.Context(img)
|
|
||||||
|
|
||||||
local radius = (height > width and width or height) / 4
|
local radius = (height > width and width or height) / 4
|
||||||
local sum,start,count = compute_sum(self._private.data),0,0
|
local sum, start, count = compute_sum(self._private.data),0,0
|
||||||
|
|
||||||
for k,v in pairs(self._private.data) do
|
for k,v in pairs(self._private.data) do
|
||||||
local end_angle = start + 2*math.pi*(v/sum)
|
local end_angle = start + 2*math.pi*(v/sum)
|
||||||
draw_pie(cr2,start,end_angle,radius,colors[math.mod(count,4)+1],width/2,height/2)
|
draw_pie(cr,start,end_angle,radius,colors[math.mod(count,4)+1],width/2,height/2)
|
||||||
draw_label(cr2,start+(end_angle-start)/2,radius,width/2,height/2,k)
|
draw_label(cr,start+(end_angle-start)/2,radius,width/2,height/2,k)
|
||||||
start,count = end_angle,count+1
|
start,count = end_angle,count+1
|
||||||
end
|
end
|
||||||
self._private.cache[width*123+height*89] = img
|
|
||||||
|
|
||||||
--Paint on the drawable
|
|
||||||
cr:set_source_surface(img)
|
|
||||||
cr:paint()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function set_data(self,data)
|
local function set_data(self,data)
|
||||||
self._private.cache = {}
|
|
||||||
self._private.data = data
|
self._private.data = data
|
||||||
self:emit_signal("widget::redraw_needed")
|
self:emit_signal("widget::redraw_needed")
|
||||||
end
|
end
|
||||||
|
@ -78,9 +63,9 @@ local function new(data)
|
||||||
if not colors then colors = {color(beautiful.fg_normal),color(beautiful.bg_alternate),color(beautiful.fg_focus),color(beautiful.bg_highlight)} end
|
if not colors then colors = {color(beautiful.fg_normal),color(beautiful.bg_alternate),color(beautiful.fg_focus),color(beautiful.bg_highlight)} end
|
||||||
local im = wibox.widget.imagebox()
|
local im = wibox.widget.imagebox()
|
||||||
im.draw,im.set_data = draw,set_data
|
im.draw,im.set_data = draw,set_data
|
||||||
im._private.cache = {}
|
|
||||||
return im
|
return im
|
||||||
end
|
end
|
||||||
|
|
||||||
return setmetatable(module, { __call = function(_, ...) return new(...) end })
|
return setmetatable(module, { __call = function(_, ...) return new(...) end })
|
||||||
-- kate: space-indent on; indent-width 2; replace-tabs on;
|
-- kate: space-indent on; indent-width 4; replace-tabs on;
|
||||||
|
|
Loading…
Reference in New Issue