++ed by:
1 non-PAUSE user
Author image Frank Herrmann


Tk::Graph - A graphical Chartmaker at Canvas (Realtime).


   use Tk;
   use Tk::Graph;

   $mw = MainWindow->new;

   my $data = {
        Sleep   => 51,
        Work    => 135,
        Access  => 124,
        mySQL   => 5

   my $ca = $mw->Graph(
                -type  => 'BARS',
                -expand => 1,
                -fill => 'both',

   $ca->configure(-variable => $data);     # bind to data

   # or ...

   $ca->set($data);        # set data



A graphical Chartmaker at Canvas (Realtime). This is a real Canvas widget, so you can draw with the standard routines in the Canvas object. For example, you can draw a line with $chart->line(x,y,...). This is useful for you when you will add a logo or write some text in your created Chart.


-debug [0|1]

This is the switch for debug output at the normal console (STDOUT)

-type (Automatic, Line, Bars, HBars, Circle)

This is the type of Graph to display the data.

Automatic - analyze the datahash and choose a Chart:

 Hash with values -> PieChart
 Hash with keys with hashes or values (not all) -> Barchart per Key
 Hash with keys with arrays -> Linechart per Key
 Array -> Linechart

Line - Linechart,

Bars - Barchart with vertical Bars,

HBars - Barchart with horizontal bars,

Circle - PieChart

-foreground (black)

Color for the Axis, Legend and Labels.

-title -titlecolor (brown)

Message at the top of the Widget.

-headroom (20)

The headroom in percent. This is a clean area at the top of the widget. When a value is in this area, the graph is redrawn to preserve this headroom.

-threed (undef)

This switch a three dimensional Display on. The Value is deep in Pixel.

-light ([10,5,0])

How many percent is the color in top, side and front (in this direction) lighter or darker in 3d?


Maximum Value for the axis. If this set, the axis is not dynamically redrawn to the next maximum value from the data. Only used in Lines and Bars!

-sortnames ('alpha' | 'num') -sortreverse (0, 1)

sort the keys from the data hash.

-config (\%cfghash)

A config hash with optional added parameters for more flexibility. The first is the name of the key from your data hash, followed by a config hash with parameters. example:

        -config         => {
                'fr' => {
                        -title => 'Free',
                        -color => 'green',
                        -range => {
                                'red'   => [0, 50],
                                'yellow'=> [50, 100],
                                'green' => [100, 200],
                'sl' => {
                        -title => 'Sleep',
                        -color => 'yellow',


Here you can write another Name to display.


Key name displayed in this color.


A range to display the values in variable colors. You can say values from 0 to 50 display in green or above in red. if value not in something range, then this draw in original color. This is only use in LINE (ToDo: BARS and HBARS!)

-fill ('both')

The same as in perl/tk pack. Redraw only in x,y or both direction(s).

-xlabel -ylabel (text)

This displays a description for x and y axis.

-xtick -ytick (5)

Number of ticks at the x or y axis.

-xformat ('%s') -yformat ('%g')

This if the sprintf format for display of the value or key for the axis. example:

        -xformat => '%d%%'      # This will eg. Display '50%'
        -yformat => '%s host'   # This will eg. Display 'first host'

-padding ([15,20,20,50])

Margin display from the Widget border, in this order top, right, bottom, left.

-linewidth (1)

The weight of the border for the dots, circle and lines.


This is the sprintf format and switch for display of the value.


Draw max/average/min value lines in Bars and Line charts

-legend [0|1]

Switch on/off the legend in Circle or Lines

-colors (red, green, ...)

A comma-separated list with the allowed colors.

-shadow ('gray50') -shadowdepth (0)

You can add a shadow to all Charts, the switch is -shadowdepth. This is also the depth in Pixels for the shadow. -shadow is the color for the shadow. This Option is autoaticly switch off when use 3d.

-wire ('white')

Switch on/off a wire grid in background from line and bars chart.

-reference ('name', 'value')

This give a Reference value for the keys in datahash. I.e. the data values are displayed relative to this reference value.


        -reference      => 'Free, 1024',        # Free space at host

-look (10)

The number of values to display in a line chart. When you refresh the data hash (maybe with the methods set or variable), then this will display eg. the last 50 values.


        -look   => 50,  # 50 values to display pro key

-dots ('width')

The width and switch for the dots in line chart.

-barwidth (30)

The width for bars in bar charts.

-balloon (0|1)

Switch on/off ballon help for segements or lines. The text format is used from the -printvalue option.

-font ('-*-Helvetica-Medium-R-Normal--*-100-*-*-*-*-*-*')

Draw text in this font.

-lineheight (15)

The line height in pixels for text in the legend.


Here come the methods that you can use with this Widget.


Set the data hash to display.


Bind the data hash to display the data, write to $data will redraw the widget.


Set the data hash to register. When you have data for Linegraph then can you this register with method register. This data is the registered for the following linegraph, when you set new datas with 'set' or 'variable' then if this startet at the end from this data. if you call register without data the you get the actual datacache.

  my $to_register = {
        'one'  => [0,5,4,8,6,8],
        'two' => [2,5,9,4,6,2],
        'three' => [0,5,6,8,6,8],


Redraw chart


Clear the canvas.


Please see for examples in 'demos' directory in this distribution.


Frank Herrmann xpix@netzwert.ag http://www.netzwert.ag


Tk, Tk::Trace, Tk::Canvas,


Tk, Tk::Trace, Tk::Canvas,