Fix pango API misuse, add pie-chart cache
This commit is contained in:
parent
698ca53762
commit
75e560995c
|
@ -13,7 +13,7 @@ local function draw_label(cr,angle,radius,center_x,center_y,text)
|
|||
cr:line_to(center_x+(1.5*radius)*math.cos(angle),center_y+(1.5*radius)*math.sin(angle))
|
||||
local x,y = cr:get_current_point()
|
||||
cr:line_to(x+(x>center_x and radius/2 or -radius/2),y)
|
||||
cr:move_to(x+(x>center_x and radius/2 + 5 or (-radius/2 - cr:text_extents(text).width - 5)),y+(cr:font_extents().height/4))
|
||||
cr:move_to(x+(x>center_x and radius/2 + 5 or (-radius/2 - cr:text_extents(text).width - 5)),y+(cr.font_extents.height/4))
|
||||
cr:show_text(text)
|
||||
cr:stroke()
|
||||
cr:arc(center_x+(radius/2)*math.cos(angle),center_y+(radius/2)*math.sin(angle),2,0,2*math.pi)
|
||||
|
@ -40,17 +40,35 @@ end
|
|||
|
||||
local function draw(self, w, cr, width, height)
|
||||
if not self._data then return end
|
||||
|
||||
-- Load from cache
|
||||
local cached = self._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 sum,start,count = compute_sum(self._data),0,0
|
||||
for k,v in pairs(self._data) do
|
||||
local end_angle = start + 2*math.pi*(v/sum)
|
||||
draw_pie(cr,start,end_angle,radius,colors[math.mod(count,4)+1],width/2,height/2)
|
||||
draw_label(cr,start+(end_angle-start)/2,radius,width/2,height/2,k)
|
||||
draw_pie(cr2,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)
|
||||
start,count = end_angle,count+1
|
||||
end
|
||||
self._cache[width*123+height*89] = img
|
||||
|
||||
--Paint on the drawable
|
||||
cr:set_source_surface(img)
|
||||
cr:paint()
|
||||
end
|
||||
|
||||
local function set_data(self,data)
|
||||
self._cached = {}
|
||||
self._data = data
|
||||
self:emit_signal("widget::updated")
|
||||
end
|
||||
|
@ -59,6 +77,7 @@ 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
|
||||
local im = wibox.widget.imagebox()
|
||||
im._data,im.draw,im.set_data = dummy_dataset,draw,set_data
|
||||
im._cache = {}
|
||||
return im
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue