1 --[[ GRAPH widget v1.0 by wlourf (31.10.2010)
2 this widget draws some graphs with some effects
3 http://u-scripts.blogspot.com/2010/10/graph-widget.html
5 To call the script in a conky, use, before TEXT
6 lua_load /path/to/the/script/graph.lua
7 lua_draw_hook_pre main_graph
8 and add one line (blank or not) after TEXT
12 3 parameters are mandatory
13 name - the name of the conky variable to display,
14 for example for {$cpu cpu0}, just write name="cpu"
15 arg - the argument of the above variable,
16 for example for {$cpu cpu1}, just write arg="cpu1"
17 arg can be a numerical value if name=""
18 max - the maximum value the above variable can reach,
19 for example for {$cpu cpu1}, just write max=100 or less or more
22 x,y - coordinates of the bottom-left corner of the graph,
23 relative to the top-left corner of the conky window
24 default = bottom-left corner of the conky window
25 width - width of the graph, default = 100 pixels
26 height - height of the graph, default = 20 pixels
27 nb_values - number of values to display in the graph, default=width
28 i.e. 1 pixel for 1 value
29 autoscale - if set to true, calculate the max valeu of the y axis and
30 doesn't use the max parameter above, default=false
31 skew_x - skew graph around x axis, défaut = 0
32 skew_y - skew graph around y axis, défaut = 0
33 angle - angle of rotation of the graph in degress, default = 0
34 i.e. a horizontal graph)
35 inverse - if set to true, graph are draw from right to left, default=false
36 background - if set to false, background is not drawn, default=true
37 foreground - if set to false, foreground is not drawn, default=true
38 foreground = plain graph
39 bg_bd_size - size of the border of the background, default=0=no border
40 fg_bd_size - size of the border of the foreground, default=0=no border
43 Colours tables below are defined into braces :
44 {position in the gradient (0 to 1), colour in hexadecimal, alpha (0 to 1)}
45 example for a single colour table :
46 {{0,0xFFAA00,1}} position parameter doesn't matter
47 example for a two-colours table :
48 {{0,0xFFAA00,1},{1,0x00AA00,1}} or {{0.5,0xFFAA00,1},{1,0x00AA00,1}}
49 example for a three-colours table :
50 {{0,0xFFAA00,1},{0.5,0xFF0000,1},{1,0x00AA00,1}}
52 bg_colour - colour table for background,
53 default = {{0,0x000000,.5},{1,0xFFFFFF,.5}}
54 fg_colour - colour table for foreground,
55 default = {{0,0x00FFFF,1},{1,0x0000FF,1}}
56 bg_bd_colour- colour table for background border,
57 default = {{1,0xFFFFFF,1}}
58 fg_bd_colour- colour table for foreground border,
59 default = {{1,0xFFFF00,1}}
61 bg_orientation, bg_bd_orientation, fg_orientation, fg_bd_orientation,
62 - "orientation" defines the starting point of the gradient,
64 there are 8 available starting points :
65 "nw","nn","ne","ee","se","ss","sw","ww"
66 (n for north, w for west ...)
67 theses 8 points are the 4 corners + the 4 middles of graph
68 so a gradient "nn" will go from "nn" to "ss"
69 a gradient "nw" will go from "nw" to "se"
72 v1.0 (31 Oct. 2010) original release
74 -- This program is free software; you can redistribute it and/or modify
75 -- it under the terms of the GNU General Public License as published by
76 -- the Free Software Foundation version 3 (GPLv3)
78 -- This program is distributed in the hope that it will be useful,
79 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
80 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
81 -- GNU General Public License for more details.
83 -- You should have received a copy of the GNU General Public License
84 -- along with this program; if not, write to the Free Software
85 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
86 -- MA 02110-1301, USA.
92 function set_settings()
105 bg_colour = {{0,0x007AF6,0},{1,0x000000,0}},
106 --fg_bd_colour = { {0,0xFF0000,1},{0.5,0xFFA209,1},{1,0xFFFF00,1}},
107 fg_bd_colour = { {0,0xFF0000,1},{0.5,0xFF3300,1},{1,0xFFFFFF,1}},
108 --fg_colour = { {0,0xFF0000,1},{0.5,0xFFA209,1},{1,0xFFFF00,1}},
109 fg_colour = { {0,0x00CCFF,0.7},{0.5,0x0000FF,0.7},{1,0x000000,0.1}},
112 bg_bd_orientation="nn",
126 bg_colour = {{0,0x007AF6,0},{1,0x000000,0}},
127 fg_bd_colour = { {0,0xFF0000,1},{0.5,0xFFA209,0.8},{1,0xFFFF00,0.6}},
128 fg_colour = { {0,0xFF0000,0.65},{0.5,0xFFA209,0.5},{1,0xFFFF00,0.3}},
131 DrawMe="${if_up wlan0}1${else}0$endif",
145 bg_colour = {{0,0x007AF6,0},{1,0x000000,0}},
146 fg_bd_colour = { {0,0xFF0000,1},{0.5,0xFFA209,0.8},{1,0xFFFF00,0.6}},
147 fg_colour = { {0,0xFF0000,0.65},{0.5,0xFFA209,0.5},{1,0xFFFF00,0.3}},
149 DrawMe="${if_up wlan0}1${else}0$endif",
163 bg_colour = {{0,0x007AF6,0},{1,0x000000,0}},
164 fg_bd_colour = { {0,0xFF0000,1},{0.5,0xFFA209,0.8},{1,0xFFFF00,0.6}},
165 fg_colour = { {0,0xFF0000,0.65},{0.5,0xFFA209,0.5},{1,0xFFFF00,0.3}},
168 DrawMe="${if_up gprs0}1${else}0$endif",
182 bg_colour = {{0,0x007AF6,0},{1,0x000000,0}},
183 fg_bd_colour = { {0,0xFF0000,1},{0.5,0xFFA209,0.8},{1,0xFFFF00,0.6}},
184 fg_colour = { {0,0xFF0000,0.65},{0.5,0xFFA209,0.5},{1,0xFFFF00,0.3}},
186 DrawMe="${if_up gprs0}1${else}0$endif",
200 bg_colour = {{0,0x667AF6,0},{1,0x000000,0}},
201 fg_bd_colour = { {0,0xdd0000,0.7},{0.5,0xdddd00,0.7},{1,0xFFFF00,0.7}},
202 fg_colour = { {0,0x000000,0.7},{0.5,0xdd0000,0.7},{1,0xff0000,0.7}},
206 name="to_bytes ${diskio",
216 bg_colour = {{0,0x667AF6,0},{1,0x000000,0}},
217 fg_bd_colour = { {0,0xdd0000,1},{0.5,0xdddd00,1},{1,0xFFFF00,1}},
218 fg_colour = { {1,0x111111,0.3},{0.5,0x33cc00,0.6},{0,0x00FF00,0.8}},
226 function check_settings(t)
227 --tables are check only when conky start
228 if t.name==nil and t.arg==nil then
229 print ("No input values ... use parameters 'name'" ..
230 " with 'arg' or only parameter 'arg' ")
234 print ("No maximum value defined, use 'max'")
235 print ("for name=" .. t.name .. " with arg=" .. t.arg)
238 if t.name==nil then t.name="" end
239 if t.arg==nil then t.arg="" end
243 function conky_main_graph()
244 if conky_window == nil then return end
245 local w=conky_window.width
246 local h=conky_window.height
247 local cs=cairo_xlib_surface_create(conky_window.display,
248 conky_window.drawable, conky_window.visual, w, h)
250 updates=tonumber(conky_parse('${updates}'))
251 --start drawing after "updates_gap" updates
252 --prevent segmentation error for cpu
257 for i in pairs(graph_settings) do
258 if graph_settings[i].width==nil then graph_settings[i].width=100 end
259 if graph_settings[i].nb_values==nil then
260 graph_settings[i].nb_values= graph_settings[i].width
262 --create an empty table to store values
263 graph_settings[i]["values"]={}
265 graph_settings[i].beg = graph_settings[i].nb_values
266 --graph_settings[i].beg = 0
267 for j =1, graph_settings[i].nb_values do
268 graph_settings[i].values[j]=0
270 graph_settings[i].flag_init=true
271 flagOK=flagOK + check_settings(graph_settings[i])
275 --abort script if error in one of the tables
276 print ("ERROR : Check the graph_setting table")
280 if updates > updates_gap then
281 for i in pairs(graph_settings) do
282 if (graph_settings[i].DrawMe==nil or conky_parse(graph_settings[i].DrawMe) == "1") then
283 --print(graph_settings[i].DrawMe.."= '"..conky_parse(graph_settings[i].DrawMe).."'")
285 --cancel fetch value from conky if interface down or whatever
286 local nb_values=graph_settings[i].nb_values
287 graph_settings[i].automax=0
288 for j =1, nb_values do
289 if graph_settings[i].values[j+1]==nil then
290 graph_settings[i].values[j+1]=0
292 graph_settings[i].values[j]=graph_settings[i].values[j+1]
295 if graph_settings[i].name=="" then
296 value=graph_settings[i].arg
298 value=tonumber(conky_parse('${' ..
299 graph_settings[i].name .. " " ..
300 graph_settings[i].arg ..'}'))
302 graph_settings[i].values[nb_values]=value
304 graph_settings[i].automax=math.max(graph_settings[i].automax,
305 graph_settings[i].values[j])
306 if graph_settings[i].automax == 0 then graph_settings[i].automax = 1 end --should stop weird glitches at beginning when no values reported yet for upspeed or diskio
308 draw_graph(graph_settings[i])
313 cairo_surface_destroy(cs)
318 function draw_graph(t)
320 local function rgb_to_r_g_b(colour)
321 return ((colour[2] / 0x10000) % 0x100) / 255., ((colour[2] / 0x100) % 0x100) / 255., (colour[2] % 0x100) / 255., colour[3]
324 local function linear_orientation(o,w,h)
325 --set gradient for bg and bg border
347 local function linear_orientation_inv(o,w,h)
348 --set gradient for fg and fg border
369 if t.DrawMe~=nil and conky_parse(t.DrawMe) ~= "1" then
370 --print(t.DrawMe.."= '"..conky_parse(t.DrawMe).."'")
374 end --cancel drawing for same reason as earlier
376 if t.height==nil then t.height=20 end
377 --checked in previous part : width and nb_values
378 if t.background==nil then t.background=true end
379 if t.bg_bd_size==nil then t.bg_bd_size=0 end
380 if t.x==nil then t.x=t.bg_bd_size end
381 if t.y==nil then t.y=conky_window.height -t.bg_bd_size end
382 if t.bg_colour==nil then t.bg_colour={{0,0x000000,.5},{1,0xFFFFFF,.5}} end
383 if t.bg_bd_colour==nil then t.bg_bd_colour={{1,0xFFFFFF,1}} end
384 if t.foreground==nil then t.foreground=true end
385 if t.fg_colour==nil then t.fg_colour={{0,0x00FFFF,1},{1,0x0000FF,1}} end
386 if t.fg_bd_size==nil then t.fg_bd_size=0 end
387 if t.fg_bd_colour==nil then t.fg_bd_colour={{1,0xFFFF00,1}} end
388 if t.autoscale==nil then t.autoscale=false end
389 if t.inverse==nil then t.inverse=false end
390 if t.angle==nil then t.angle=0 end
391 if t.bg_bd_orientation==nil then t.bg_bd_orientation="nn" end
392 if t.bg_orientation==nil then t.bg_orientation="nn" end
393 if t.fg_bd_orientation==nil then t.fg_bd_orientation="nn" end
394 if t.fg_orientation==nil then t.fg_orientation="nn" end
395 --check colours tables
396 for i=1, #t.fg_colour do
397 if #t.fg_colour[i]~=3 then
398 print ("error in fg_colour table")
399 t.fg_colour[i]={1,0x0000FF,1}
402 for i=1, #t.fg_bd_colour do
403 if #t.fg_bd_colour[i]~=3 then
404 print ("error in fg_bd_colour table")
405 t.fg_bd_colour[i]={1,0x00FF00,1}
408 for i=1, #t.bg_colour do
409 if #t.bg_colour[i]~=3 then
410 print ("error in background color table")
411 t.bg_colour[i]={1,0xFFFFFF,0.5}
414 for i=1, #t.bg_bd_colour do
415 if #t.bg_bd_colour[i]~=3 then
416 print ("error in background border color table")
417 t.bg_bd_colour[i]={1,0xFFFFFF,1}
420 --calculate skew parameters if needed
422 if t.skew_x == nil then
425 t.skew_x = math.pi*t.skew_x/180
427 if t.skew_y == nil then
430 t.skew_y = math.pi*t.skew_y/180
434 cairo_set_line_cap(cr,CAIRO_LINE_CAP_ROUND)
435 cairo_set_line_join(cr,CAIRO_LINE_JOIN_ROUND)
436 local matrix0 = cairo_matrix_t:create()
437 tolua.takeownership(matrix0)
439 cairo_matrix_init (matrix0, 1,t.skew_y,t.skew_x,1,0,0)
440 cairo_transform(cr,matrix0)
441 local ratio=t.width/t.nb_values
442 cairo_translate(cr,t.x,t.y)
443 cairo_rotate(cr,t.angle*math.pi/180)
447 local pts=linear_orientation(t.bg_orientation,t.width,t.height)
448 local pat = cairo_pattern_create_linear (pts[1],pts[2],pts[3],pts[4])
449 for i=1, #t.bg_colour do
450 --print ("i",i,t.colour[i][1], rgb_to_r_g_b(t.colour[i]))
451 cairo_pattern_add_color_stop_rgba (pat, t.bg_colour[i][1], rgb_to_r_g_b(t.bg_colour[i]))
453 cairo_set_source (cr, pat)
454 cairo_rectangle(cr,0,0,t.width,t.height)
456 cairo_pattern_destroy(pat)
463 local scale_x = t.width/(t.nb_values-1)
464 local scale_y = t.height/t.max
465 --define first point of the graph
466 if updates-updates_gap <t.nb_values then
468 --next line prevent segmentation error when conky window is redraw
469 --quicly when another window "fly" over it
470 if t.beg<0 then t.beg=0 end
474 if t.inverse then cairo_scale(cr,-1,1)
475 cairo_translate(cr,-t.width,0) end
478 local pts_fg=linear_orientation_inv(t.fg_orientation,t.width,t.height)
479 local pat = cairo_pattern_create_linear (pts_fg[1],pts_fg[2],pts_fg[3],pts_fg[4])
480 for i=1,#t.fg_colour,1 do
481 cairo_pattern_add_color_stop_rgba (pat, 1-t.fg_colour[i][1], rgb_to_r_g_b(t.fg_colour[i]))
483 cairo_set_source (cr, pat)
484 cairo_move_to(cr,t.beg*scale_x,0)
485 cairo_line_to(cr,t.beg*scale_x,t.values[t.beg+1]*scale_y)
486 for i=t.beg, t.nb_values-1 do
487 cairo_line_to(cr,i*scale_x,t.values[i+1]*scale_y)
489 cairo_line_to(cr,(t.nb_values-1)*scale_x,0)
492 cairo_pattern_destroy(pat)
495 if t.fg_bd_size>0 then
496 local pts=linear_orientation_inv(t.fg_bd_orientation,t.width,t.height)
497 local pat = cairo_pattern_create_linear (pts[1],pts[2],pts[3],pts[4])
498 for i=1,#t.fg_bd_colour,1 do
499 cairo_pattern_add_color_stop_rgba (pat, 1-t.fg_bd_colour[i][1], rgb_to_r_g_b(t.fg_bd_colour[i]))
501 cairo_set_source (cr, pat)
502 cairo_move_to(cr,t.beg*scale_x,t.values[t.beg+1]*scale_y)
503 for i=t.beg, t.nb_values-1 do
504 cairo_line_to(cr,i*scale_x,t.values[i+1]*scale_y)
506 cairo_set_line_width(cr,t.fg_bd_size)
508 cairo_pattern_destroy(pat)
512 if t.bg_bd_size>0 then
513 local pts=linear_orientation(t.bg_bd_orientation,t.width,t.height)
514 local pat = cairo_pattern_create_linear (pts[1],pts[2],pts[3],pts[4])
515 for i=1, #t.bg_bd_colour do
516 --print ("i",i,t.colour[i][1], rgb_to_r_g_b(t.colour[i]))
517 cairo_pattern_add_color_stop_rgba (pat, t.bg_bd_colour[i][1], rgb_to_r_g_b(t.bg_bd_colour[i]))
519 cairo_set_source (cr, pat)
520 cairo_rectangle(cr,0,0,t.width,t.height)
521 cairo_set_line_width(cr,t.bg_bd_size)
523 cairo_pattern_destroy(pat)