Some of my scripts for daily use on a linux desktop

Friday, April 9, 2010

Pie Chart & Ring Widget



Edit: v1.1 15/05/2010, widget can now draw a ring, see at bottom.

Here is another widget for Conky... it's a pie-chart fully customizable. For example, for the circle in the above picture : it's a file system (yes it is!), each sector is proportional to the size of a disk, and two colors are used to draw a sector (one for used space, and one for free space).

It can be downloaded from Ubuntu forums here.

You need Conky 1.8.0 to use it.
To call it in a Lua script, you have to set parameters in a table:
pie_settings= { table1, table2 ...}

Each table has is own parameters, only one is mandatory, it's tableV, a table containing the values to display.

Example of tableV, of course, in real life, table is created by the script :
 -- table of labels and values 
{{label1,conky_variable,conky_argument,convert to Go-Mo-Ko units (true/false) or unit}, ...}
-- all the values are at maximum values in this example
-- last parameter set to true means that the script
will convert the values in Go, Mo ...
local k=1024*1024*1024
tableV={{"/","", k*50, k*50,true},
{"/home","", k*500, k*500,true},
{"/data","", k*1000, k*1000,true},
{"stuff","", k*10, k*10,true}
},

The Lua file contains a script which use the output of df command, because this widget was made with the disks spaces in mind, but it can be use with any other value, like this (but it's not the purpose of the script, so we won't use it):
 tableV={{"cpu0","cpu", "cpu0",100,"%"},
{"cpu1","cpu", "cpu1",100,"%"},
{"fan","", "${exec sensors | grep 'CPU Fan' | cut -c13-16}", 2000," rpm"},
{"sys","", "${exec sensors | grep 'Sys Temp' | cut -c15-17}", 100,"°C"}
},

Examples of tablebg and tablefg (background and foreground colors):
 tablebg={{0xFFFFFF,0.5},
{0xFFFFFF,0.5},
{0xFFFFFF,0.5},
{0xFFFFFF,0.5}
},
tablefg={{0xFF0000,1},
{0x00FF00,1},
{0x0000FF,1},
{0xFFFF00,1}
},

With the above parameters and these ones :
 xc=conky_window.width/2,
yc=conky_window.height/2,
int_radius=0,
radius=100,
first_angle=0,
last_angle=360,
proportional=true,
gradient_effect=false,
show_text=true,
line_lgth=30,
line_space=20,
line_width=1,
extend_line=true,
txt_font="FreeSans",
font_size=12,
font_color=nil,
font_alpha=1,
txt_offset=1,
txt_format="&m",
nb_decimals=0,

we obtain this simple kind of chart


If the number of values in a colors table is less than the number of sectors, like that :
 tablefg={{0xFF0000,1},
{0xFFFF00,1}},

colors restart at the end of the table :

Now, if we change the second parameter of tableV, the value for each sector, we change the way the graph is displayed.
 local k=1024*1024*1024
tableV={{"/", .25*k*50, k*50,true},
{"/home", .50*k*500, k*500,true},
{"/data", .75*k*1000, k*1000,true},
{"stuff", .90*k*10, k*10,true}
},


This is what I wanted to display on my desktop : the amount of used space on my external or internal drives and the proportion of each drive in the full file system. Like the big arc on first picture, I have to clean some disks ...



Now, have a look to more interestings parameters ;-)
With int_radius=50, (internal radius)


For the angles, (O° is North and angles go clockwise),
 first_angle=-45
last_angle=180


Back to full circle and proportional parameter:
 first_angle=0,
last_angle=360,
proportional=false,


Back to proportional=true and adding some gradient and hiding text
 proportional=true,
gradient_effect=true,
show_text=false,




By the way, here is an interesting script to toggle the text (or others flags) when the conky is running.
In the Lua script :
 local file = io.open("/tmp/flag-conky","r")
io.close()
show_text=(file == nil)

And a bash script associated with a keyboard shortcut:
 #!/bin/bash
flag="/tmp/flag-conky"
if [ -f $flag ]; then
rm $flag
else
echo > $flag
fi



The lines options
 line_lgth=100
line_width=4


line_space can be usefull when labels are too close:
--for the second arc (yellow and blue labels are separated)
first_angle=0,
last_angle=90,
line_space=20,


Last parameter for lines is extend_line which will extend the horizontal line if the text is too long, with extend_line=true on the second arc :

The text options
txt_font and font_size are obvious.
font_color and font_alpha create a linear gradient. To keep text color the same color of the line, set font_color=nil
txt_offset is the space between the line and the text
nb_decimals is the number of decimals for the numbers ;-)
With
 txt_font="FreeSans",
font_size=12,
font_color=0xFFFFFF,
font_alpha=0.75,
txt_offset=1,
nb_decimals=1,



The last parameter txt_format is the string for formating texts. Some tags can be used :

-- &l for label (the first value of tableV)
-- &v for value (the second value of tableV)
-- &m for max value (the third value of tableV)
-- &o for occupied percentage (ie tableV[2]/tableV[3])
-- &f for free percentage (ie tableV[3] - tableV[2]/tableV[3])
-- &n for free value (non-occupied) (ie tableV[3] - tableV[2])

With
 txt_format="&l &m, free &f",
nb_decimals=0,



That's all ;-)



Edit : a last example (code is here ):



Edit for version 1.1, I added the parameter type_arc. If type arc is "r" (the default value) values are displayed on a radial way as before :

but if type_arc="l" (l for linear) then values are dispayed like rings :

1 comment:

  1. Hey there,

    great work! Tried to use it on my system, but for some reasons it works fine sometimes, other times my cpu just goes to 100% and the windows doesn't show up. Have you ever experienced this problem? If I comment out the lua script part, the problem doesn't appear.

    Thanks!

    ReplyDelete