Tk::Chart::Areas - Extension of Canvas widget to create area lines graph. |
Tk::Chart::Areas - Extension of Canvas widget to create area lines graph.
#!/usr/bin/perl use strict; use warnings; use Tk; use Tk::Chart::Areas;
my $mw = MainWindow->new( -title => 'Tk::Chart::Areas example', -background => 'white', );
my $chart = $mw->Areas( -title => 'My Area graph title', -xlabel => 'X Label', -ylabel => 'Y Label', -linewidth => 1,
)->pack(qw / -fill both -expand 1 /);
my @data = ( ['1st','2nd','3rd','4th','5th','6th','7th', '8th', '9th'], [ 5, 12, 24, 33, 19, 8, 6, 15, 21], [ -1, -2, -5, -6, -3, 1.5, 1, 1.3, 2] );
# Add a legend to the graph my @Legends = ( 'legend 1', 'legend 2', ); $chart->set_legend( -title => 'Title legend', -data => \@Legends, -titlecolors => 'blue', );
# Add help identification $chart->set_balloon();
# Create the graph $chart->plot( \@data );
MainLoop();
Tk::Chart::Areas is an extension of the Canvas widget. It is an easy way to build an interactive Area line graph into your Perl Tk widget. The module is written entirely in Perl/Tk.
You can set a background gradient color.
You can change the color, font of title, labels (x and y) of the graph. You can set an interactive legend. The axes can be automatically scaled or set by the code. With this module it is possible to plot quantitative variables according to qualitative variables.
When the mouse cursor passes over a plotted line or its entry in the legend, the line and its entry will be turned to a color (that you can change) to help identify it.
You can set a background gradient color by using all methods of the Tk::Canvas::GradientColor manpage. By default, it is not enabled.
To enabled background gradient color the first time, you firstly have to call enabled_gradientcolor method and configure your color and type of gradient with set_gradientcolor.
$chart->enabled_gradientcolor(); $chart->set_gradientcolor( -start_color => '#6585ED', -end_color => '#FFFFFF', );
Please, read WIDGET-SPECIFIC METHODS in the Tk::Canvas::GradientColor manpage documentation to know all available configurations.
-background -borderwidth -closeenough -confine -cursor -height -highlightbackground -highlightcolor -highlightthickness -insertbackground -insertborderwidth -insertofftime -insertontime -insertwidth -relief -scrollregion -selectbackground -selectborderwidth -selectforeground -takefocus -width -xscrollcommand -xscrollincrement -yscrollcommand -yscrollincrement
-viewsection => 0, # 0 or 1
Default : 1
-outlinearea => 'blue',
Default : 'black'
See WIDGET-SPECIFIC OPTIONS in the Tk::Chart::Lines manpage
The Canvas method creates a widget object. This object supports the configure and cget methods described in Tk::options which can be used to enquire and modify the options described above.
Fill an array of arrays with the values of the datasets (\@data). Make sure that every array has the same size, otherwise Tk::Chart::Lines will complain and refuse to compile the graph.
my @NewData = (1,10,12,5,4); $chart->add_data(\@NewData);
If your last graph has a legend, you have to add a legend entry for the new dataset. Otherwise, the legend graph will not be display (see below).
$legendmy @NewData = (1,10,12,5,4); my $legend = 'New data set'; $chart->add_data(\@NewData, $legend);
$chart->disabled_automatic_redraw;
my @data_point_value = ( [ 9, 2, 5, 6, 3, 1, 1, 3, 4 ], # The first line data undef, # The second line data [ 'A', 'B', undef, 'D', 'E', 'F', 'G', 'H', undef ], # The third line data ); $chart->display_values( \@data_point_value );
In this example, values are added above each point of the first and third lines. The second line is undef, no values are printed in the graph. B value is printed above the second point of the third line data.
$chart->enabled_automatic_redraw;
Fill an array of arrays with the x values and the values of the datasets (\@data). Make sure that every array have the same size, otherwise Tk::Chart::Areas will complain and refuse to compile the graph.
my @data = ( [ '1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th', '9th' ], [ 1, 2, 5, 6, 3, 1.5, 1, 3, 4 ], [ 4, 2, 5, 2, 3, 5.5, 7, 9, 4 ], [ 1, 2, 52, 6, 3, 17.5, 1, 43, 10 ] );
@data have to contain a least two arrays, the x values and the values of the datasets.
If you don't have a value for a point in a dataset, you can use undef, and the point will be skipped.
[ 1, undef, 5, 6, 3, 1.5, undef, 3, 4 ]-substitutionvalue => real number,
If you have a no real number value in a dataset, it will be replaced by a constant value.
Default : 0
my @data = ( [ '1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th', '9th' ], [ 1, '--', 5, 6, 3, 1.5, 1, 3, 4 ], [ 'mistake', 2, 5, 2, 3, 'NA', 7, 9, 4 ], [ 1, 2, 52, 6, 3, 17.5, 1, 43, 4 ], ); $chart->plot( \@data, -substitutionvalue => '12', ); # mistake, -- and NA will be replace by 12
-substitutionvalue have to be a real number (ex : 12, .25, 02.25, 5.2e+11, etc ...)
Fill an array of arrays with the values of the data to display (\@data_to_display).
my @data_to_display = ( [ 10, 30, 20, 30, 5, 41, 1, 23 ], [ 'A', 'B', 'C', 'D', 'E', 5.5, 'F', 'G', 4 ], [ 4, 'CPAN', 52, 6, 3, 17.5, 1, 43, 10 ] );
If you does not want to display a value for a point, you can use undef, and the point value will be skipped.
[ 1, undef, 5, 6, 3, 1.5, undef, 3, 4 ]-foreground => color,
Set color of text value.
Default : black
-font => string,Set the font to text value.
-font => 'Times 12 {bold}',
Redraw the graph.
If you have used clearchart for any reason, it is possible to redraw the graph. Tk::Chart::Areas supports the configure and cget methods described in the the Tk::options manpage manpage. If you use configure method to change a widget specific option, the modification will not be display. If the graph was already displayed and if you not resize the widget, call redraw method to resolv the bug.
... $mw->Button( -text => 'Change xlabel', -command => sub { $chart->configure(-xlabel => 'red'); }, )->pack; ... # xlabel will be changed but not displayed if you not resize the widget. ... $mw->Button( -text => 'Change xlabel', -command => sub { $chart->configure(-xlabel => 'red'); $chart->redraw; } )->pack; ... # OK, xlabel will be changed and displayed without resize the widget.
Set a background color for the balloon.
-background => 'red',
Default : snow
-colordatamouse => Array referenceSpecify an array reference wich contains 2 colors. The first color specifies the color of the line when mouse cursor passes over an entry in the legend. If the line has the same color, the second color will be used.
-colordatamouse => ['blue', 'green'],
Default : -colordatamouse => [ '#7F9010', '#CB89D3' ]
-morepixelselected => integerWhen the mouse cursor passes over an entry in the legend, the line width increase.
-morepixelselected => 5,
Default : 2
Set a title legend.
-title => 'My title',
Default : undef
-titlecolors => stringSet a color to legend text.
-titlecolors => 'red',
Default : black
-titlefont => stringSet the font to legend title text.
-titlefont => '{Arial} 8 {normal}',
Default : {Times} 8 {bold}
-legendcolor => colorColor of legend text.
-legendcolor => 'white',
Default : 'black'
-legendfont => stringSet the font to legend text.
-legendfont => '{Arial} 8 {normal}',
Default : {Times} 8 {normal}
-box => booleanSet a box around all legend.
-box => 1, # or 0
Default : 0
-legendmarkerheight => integerChange the heigth of marker for each legend entry.
-legendmarkerheight => 5,
Default : 10
-legendmarkerwidth => integerChange the width of marker for each legend entry.
-legendmarkerwidth => 5,
Default : 10
-heighttitle => integerChange the height title legend space.
-heighttitle => 75,
Default : 30
$chart->zoom(integer);
Zoom the graph. The x-axis and y-axis will be zoomed. If your graph has a 300*300 size, after a zoom(200), the graph will have a 600*600 size.
$chart->zoom(50); # size divide by 2 => 150*150 ... $chart->zoom(200); # size multiplie by 2 => 600*600 ... $chart->zoom(120); # 20% add in each axis => 360*360 ... $chart->zoom(100); # original resize 300*300.
Zoom the graph the x-axis.
# original canvas size 300*300 $chart->zoomx(50); # new size : 150*300 ... $chart->zoom(100); # new size : 300*300
Zoom the graph the y-axis.
# original canvas size 300*300 $chart->zoomy(50); # new size : 300*150 ... $chart->zoom(100); # new size : 300*300
In the demo directory, you have a lot of script examples with their screenshot. See also the http://search.cpan.org/dist/Tk-Chart/MANIFEST web page of the Tk::Chart manpage.
See the Tk::Canvas manpage for details of the standard options.
See the Tk::Chart manpage, the Tk::Chart::FAQ manpage, the GD::Graph manpage.
Djibril Ousmanou, <djibel at cpan.org>
Please report any bugs or feature requests to bug-Tk-Chart at rt.cpan.org
, or through
the web interface at http://rt.cpan.org/NoAuth/ReportBug.html. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
You can find documentation for this module with the perldoc command.
perldoc Tk::Chart::Areas
You can also look for information at:
Copyright 2011 Djibril Ousmanou, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Tk::Chart::Areas - Extension of Canvas widget to create area lines graph. |