NAME

Tk::Chart::Boxplots - Extension of Canvas widget to create boxplots graph.

SYNOPSIS

#!/usr/bin/perl
use strict;
use warnings;
use Tk;
use Tk::Chart::Boxplots;

my $mw = MainWindow->new(
  -title      => 'Tk::Chart::Boxplots example',
  -background => 'white',
);

my $chart = $mw->Boxplots(
  -title      => 'My graph title',
  -xlabel     => 'X Label',
  -ylabel     => 'Y Label',
  -background => 'snow',
)->pack(qw / -fill both -expand 1 /);

my $one   = [ 210 .. 275 ];
my $two   = [ 180, 190, 200, 220, 235, 245 ];
my $three = [ 40, 140 .. 150, 160 .. 180, 250 ];
my $four  = [ 100 .. 125, 136 .. 140 ];
my $five  = [ 10 .. 50, 100, 180 ];

my @data = (
  [ '1st', '2nd', '3rd',  '4th', '5th' ],
  [ $one,  $two,  $three, $four, $five ],
  [ [-25, 1..15], [-45, 25..45, 100], [70, 42..125], undef, [180..250] ],
  # ...
);

# Add a legend to the graph
my @legends = ( 'legend 1', 'legend 2' );
$chart->set_legend(
  -title       => 'Title legend',
  -data        => \@legends,
);

# Add help identification
$chart->set_balloon();

# Create the graph
$chart->plot( \@data );

MainLoop();

DESCRIPTION

Tk::Chart::Boxplots is an extension of the Canvas widget. It is an easy way to build interactive boxplots (also known as a box-and-whisker diagram or plot) 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.

When the mouse cursor passes over a boxplot, its outlier or its entry in the legend, the boxplot and its entry will be turned to a color (that you can change) to help identify it.

You can use 3 methods to zoom (vertically, horizontally or both).

BACKGROUND GRADIENT COLOR

You can set a background gradient color by using all methods of Tk::Canvas::GradientColor. 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 Tk::Canvas::GradientColor documentation to know all available configurations.

STANDARD OPTIONS

-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

WIDGET-SPECIFIC OPTIONS

Name: BoxplotLinescolor
Class: BoxplotLinesColor
Switch: -boxplotlinescolor

Color of lines of boxplots.

-boxplotlinescolor => 'red',

Default : black

Name: Spacingbar
Class: SpacingBar
Switch: -spacingbar

Set this to 1 to display remove space between each boxplot.

-spacingbar => 0, # 0 or 1

Default : 1

WIDGET-SPECIFIC OPTIONS like Tk::Chart::Lines

Many options that allow you to configure your graph as you want, but you can skip the default configuration.

Name: Title
Class: Title
Switch: -title

Title of your graph.

-title => 'My graph title',

Default : undef

Name: Titleposition
Class: TitlePosition
Switch: -titleposition

Position of title : center, left or right

-titleposition => 'left',

Default : center

Name: Titlecolor
Class: TitleColor
Switch: -titlecolor

Title color of your graph.

-titlecolor => 'red',

Default : black

Name: Titlefont
Class: TitleFont
Switch: -titlefont

Set the font for the title text. See also textfont option.

-titlefont => 'Times 15 {normal}',

Default : {Times} 12 {bold}

Name: Titleheight
Class: TitleHeight
Switch: -titleheight

Height for title graph space.

-titleheight => 100,

Default : 40

Name: Xlabel
Class: XLabel
Switch: -xlabel

The label to be printed just below the x-axis.

-xlabel => 'X label',

Default : undef

Name: Xlabelcolor
Class: XLabelColor
Switch: -xlabelcolor

Set x label color. See also textcolor option.

-xlabelcolor => 'red',

Default : black

Name: Xlabelfont
Class: XLabelFont
Switch: -xlabelfont

Set the font for the x label text. See also textfont option.

-xlabelfont => 'Times 15 {normal}',

Default : {Times} 10 {bold}

Name: Xlabelheight
Class: XLabelHeight
Switch: -xlabelheight

Height for x label space.

-xlabelheight => 50,

Default : 30

Name: Xlabelskip
Class: XLabelSkip
Switch: -xlabelskip

Print every xlabelskip number under the tick on the x-axis. If you have a dataset wich contain many points, the tick and x values will be overwrite on the graph. This option can help you to clarify your graph. Eg:

# ['leg1', 'leg2', ...'leg1000', 'leg1001', ... 'leg2000'] => There are 2000 ticks and text values on x-axis.
-xlabelskip => 1 => ['leg1', 'leg3', 'leg5', ...]        # => 1000 ticks will be display.

See also -xvaluesregex option.

-xlabelskip => 2,

Default : 0

Name: Xvaluecolor
Class: XValueColor
Switch: -xvaluecolor

Set x values colors. See also textcolor option.

-xvaluecolor => 'red',

Default : black

Name: Xvaluespace
Class: XValueSpace
Switch: -xvaluespace

Width for x values space.

-xvaluespace => 50,

Default : 30

Name: Xvalueview
Class: XvalueView
Switch: -xvalueview

View values on x-axis.

-xvalueview => 0, # 0 or 1

Default : 1

Name: Xvaluesregex
Class: XValuesRegex
Switch: -xvaluesregex

View the x values which will match with regex. It allows you to display tick on x-axis and values that you want. You can combine it with -xlabelskip to display many dataset.

...
['leg1', 'leg2', 'data1', 'data2', 'symb1', 'symb2']
...

-xvaluesregex => qr/leg/i,

On the graph, just leg1 and leg2 will be display.

Default : qr/.+/

Name: Ylabel
Class: YLabel
Switch: -ylabel

The label to be printed next to y-axis.

-ylabel => 'Y label',

Default : undef

Name: Ylabelcolor
Class: YLabelColor
Switch: -ylabelcolor

Set the color of y label. See also textcolor option.

-ylabelcolor => 'red',

Default : black

Name: Ylabelfont
Class: YLabelFont
Switch: -ylabelfont

Set the font for the y label text. See also textfont option.

-ylabelfont => 'Times 15 {normal}',

Default : {Times} 10 {bold}

Name: Ylabelwidth
Class: YLabelWidth
Switch: -ylabelwidth

Width of space for y label.

-ylabelwidth => 30,

Default : 5

Name: Yvaluecolor
Class: YValueColor
Switch: -yvaluecolor

Set the color of y values. See also valuecolor option.

-yvaluecolor => 'red',

Default : black

Name: Yvalueview
Class: YvalueView
Switch: -yvalueview

View values on y-axis.

-yvalueview => 0, # 0 or 1

Default : 1

Name: Yminvalue
Class: YMinValue
Switch: -yminvalue

Minimum value displayed on the y-axis. See also -interval option.

-yminvalue => 10.12,

Default : 0

Name: Ymaxvalue
Class: YMaxValue
Switch: -ymaxvalue

Maximum value displayed on the y-axis. See also -interval option.

-ymaxvalue => 5,

Default : Computed from data sets

Name: interval
Class: Interval
Switch: -interval

If set to a true value, -yminvalue and -ymaxvalue will be fixed to minimum and maximum values of data sets. It overwrites -yminvalue and -ymaxvalue options.

-interval => 1, # 0 or 1

Default : 0

Name: Labelscolor
Class: LabelsColor
Switch: -labelscolor

Combine xlabelcolor and ylabelcolor options. See also textcolor option.

-labelscolor => 'red',

Default : undef

Name: Valuescolor
Class: ValuesColor
Switch: -valuescolor

Set the color of x, y values in axes. It combines xvaluecolor and yvaluecolor options.

-valuescolor => 'red',

Default : undef

Name: Textcolor
Class: TextColor
Switch: -textcolor

Set the color of x, y labels and title text. It combines titlecolor, xlabelcolor and ylabelcolor options.

-textcolor => 'red',

Default : undef

Name: Textfont
Class: TextFont
Switch: -textfont

Set the font of x, y labels and title text. It combines titlefont, xlabelfont and ylabelfont options.

-textfont => 'Times 15 {normal}',

Default : undef

Name: Longticks
Class: LongTicks
Switch: -longticks

If longticks is a true value, x and y ticks will be drawn with the same length as the axes. See also -xlongticks and -ylongticks options.

-longticks => 1, #  0 or 1

Default : 0

Name: Longtickscolor
Class: LongTicksColor
Switch: -longtickscolor

Set the color of x and y ticks that will be drawn with the same length as the axes. See also -xlongtickscolor and -ylongtickscolor options.

-longtickscolor => 'red',

Default : undef

Name: XLongticks
Class: XLongTicks
Switch: -xlongticks

If xlongticks is a true value, x ticks will be drawn with the same length as the x-axis. See also -longticks.

-xlongticks => 1, #  0 or 1

Default : 0

Name: YLongticks
Class: YLongTicks
Switch: -ylongticks

If ylongticks is a true value, y ticks will be drawn with the same length as the axes. See also -longticks.

-ylongticks => 1, #  0 or 1

Default : 0

Name: XLongtickscolor
Class: XLongTicksColor
Switch: -xlongtickscolor

Set the color of xlongticks. See also -xlongtickscolor.

-xlongtickscolor => 'red',

Default : #B3B3B3

Name: YLongtickscolor
Class: YLongTicksColor
Switch: -ylongtickscolor

Set the color of ylongticks. See also -ylongtickscolor.

-ylongtickscolor => 'red',

Default : #B3B3B3

Name: Boxaxis
Class: BoxAxis
Switch: -boxaxis

Draw the axes as a box.

-boxaxis => 1, #  0 or 1

Default : 0

Name: Noaxis
Class: NoAxis
Switch: -noaxis

Hide the axes with ticks and values ticks.

-noaxis => 1, # 0 or 1

Default : 0

Name: Zeroaxis
Class: ZeroAxis
Switch: -zeroaxis

If set to a true value, the axes for y values will only be drawn. This might be useful in case your graph contains negative values, but you want it to be clear where the zero value is (see also zeroaxisonly and boxaxis).

-zeroaxis => 1, # 0 or 1

Default : 0

Name: Zeroaxisonly
Class: ZeroAxisOnly
Switch: -zeroaxisonly

If set to a true value, the zero x-axis will be drawn and no axes at the bottom of the graph will be drawn. The labels for X values will be placed on the zero x-axis. This works if there is at least one negative value in dataset.

-zeroaxisonly => 1, # 0 or 1

Default : 0

Name: Axiscolor
Class: AxisColor
Switch: -axiscolor

Color of the axes.

-axiscolor => 'red',

Default : black

Name: Xtickheight
Class: XTickHeight
Switch: -xtickheight

Set height of all x ticks.

-xtickheight => 10,

Default : 5

Name: Xtickview
Class: XTickView
Switch: -xtickview

View x ticks of graph.

-xtickview => 0, # 0 or 1

Default : 1

Name: Yticknumber
Class: YTickNumber
Switch: -yticknumber

Number of ticks to print for the y-axis.

-yticknumber => 10,

Default : 4

Name: Ytickwidth
Class: YtickWidth
Switch: -ytickwidth

Set width of all y ticks.

-ytickwidth => 10,

Default : 5

Name: Ytickview
Class: YTickView
Switch: -ytickview

View y ticks of graph.

-ytickview => 0, # 0 or 1

Default : 1

Name: Alltickview
Class: AllTickView
Switch: -alltickview

View all ticks of graph. Combines xtickview and ytickview options.

-alltickview => 0, # 0 or 1

Default : undef

Name: Linewidth
Class: LineWidth
Switch: -linewidth

Set width of all lines graph of dataset.

-linewidth => 10,

Default : 1

Name: Colordata
Class: ColorData
Switch: -colordata

This controls the colors of the lines. This should be a reference to an array of color names.

-colordata => [ qw(green pink blue cyan) ],

Default :

[ 'red',     'green',   'blue',    'yellow',  'purple',  'cyan',
  '#996600', '#99A6CC', '#669933', '#929292', '#006600', '#FFE100',
  '#00A6FF', '#009060', '#B000E0', '#A08000', 'orange',  'brown',
  'black',   '#FFCCFF', '#99CCFF', '#FF00CC', '#FF8000', '#006090',
],

The default array contains 24 colors. If you have more than 24 samples, the next line will have the color of the first array case (red).

Name: verbose
Class: Verbose
Switch: -verbose

Warning will be print if necessary.

-verbose => 0,

Default : 1

WIDGET METHODS

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.

add_data

$chart->add_data(\@newdata, ?$legend)

This method allows you to add data in your graph. If you have already plot data using plot method and if you want to add new data, you can use this method. Your graph will be updade.

  • Data array reference

    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 $one     = [ 210 .. 275 ];
    my $two     = [ 180, 190, 200, 220, 235, 245 ];
    my $three   = [ 40, 140 .. 150, 160 .. 180, 250 ];
    my $four    = [ 100 .. 125, 136 .. 140 ];
    my $five    = [ 10 .. 50, 100, 180 ];
    my @newdata = ( $one, $two, $three, $four, $five );
    $chart->add_data( \@newdata, 'new legend' );

    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).

  • $legend

    my $legend = 'New data set';
    $chart->add_data(\@newdata, $legend);

boxplot_information

$chart->boxplot_information

Use this method if you want to get the informations about all boxplots (25th percentile (Q1), 75th percentile (Q3), smallest non-outlier, largest non-outlier, median and mean). This method returns an array reference. The informations are stored in a hash reference.

my $ref_array_information = $chart->boxplot_information();

# Print information of boxplot @{$data[2][3]} (2th sample, 4th data )
print "Boxplot @{$data[2][3]} (2th sample, 4th data )\n";
print "Outliers : @{$ref_array_information->[1][3]->{outliers}}\n";
print '25th percentile (Q1) : ', $ref_array_information->[1][3]->{Q1}, "\n";
print '75th percentile (Q3) :',  $ref_array_information->[1][3]->{Q3}, "\n";
print 'Smallest non-outlier : ',
  $ref_array_information->[1][3]->{smallest_non_outlier}, "\n";
print 'Largest non-outlier :', $ref_array_information->[1][3]->{largest_non_outlier},
  "\n";
print 'Median : ', $ref_array_information->[1][3]->{median}, "\n";
print 'Mean : ',   $ref_array_information->[1][3]->{mean},   "\n";

if you have this data :

my @data = (
    [ '1st', '2nd', '3rd',  '4th', '5th' ],
    [ [ list data00 ],  [list data01],  [list data02], ],
    [ [ list data10 ],  [list data11],  [list data12], ],
    [ [ list data20 ],  [list data21],  [list data22], ],
    #...
  );

To get the informations about boxplot list data21, you have to read hash reference like this :

$ref_hash_information = $ref_array_information->[2][1];
# 25th percentile (Q1)
print $ref_hash_information->{Q1};
# Smallest non-outlier
print $ref_hash_information->{smallest_non_outlier};

The quantile is calculated with the same algorithm as Excel and type 7 quantile R package.

clearchart

$chart->clearchart

This method allows you to clear the graph. The canvas will not be destroy. It's possible to redraw your last graph using the redraw method.

delete_balloon

$chart->delete_balloon

If you call this method, you disable help identification which has been enabled with set_balloon method.

disabled_automatic_redraw

$chart->disabled_automatic_redraw

When the graph is created and the widget size changes, the graph is automatically re-created. Call this method to avoid resizing.

$chart->disabled_automatic_redraw;  

enabled_automatic_redraw

$chart->enabled_automatic_redraw

Use this method to allow your graph to be recreated automatically when the widget size change. When the graph is created for the first time, this method is called.

$chart->enabled_automatic_redraw;  

plot

$chart->plot(\@data, ?arg)

To display your graph the first time, plot the graph by using this method.

  • \@data

    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::Boxplots will complain and refuse to compile the graph.

    my @data = (
      [ '1st', '2nd', '3rd',  '4th', '5th' ],
      [ [100 .. 125, 136 .. 140 ],  [22..89],  [12,54,88,10], [12,11,23,14..98,45], [0..55,11,12] ],
      [ [-25..-5, 1..15], [-45, 25..45, 100], [70, 42..125], [100, 30, 88, 95, 115, 155], [180..250] ],
      #...
    );

    @data have to contain a least two arrays, the x values and the values of the datasets. Each boxplot (array eg: [22..89]) have to contain at least 4 data.

    [22,1,14] => wrong

    If you don't have a value for a point in a dataset, you can use undef, and the point will be skipped.

    [ undef,  [22..89],  [12,54,88,10], undef, [0..55,11,12] ],
  • -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 (Eg : 12, .25, 02.25, 5.2e+11, ...)

redraw

Redraw the graph.

If you have used clearchart for any reason, it is possible to redraw the graph. Tk::Chart::Boxplots supports the configure and cget methods described in the Tk::options 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_balloon

$chart->set_balloon(? %options)

If you call this method, you enable help identification. When the mouse cursor passes over a plotted line or its entry in the legend, the line and its entry will be turn into a color (that you can change) to help the identification. set_legend method must be set if you want to enabled identification.

  • -background => string

    Set a background color for the balloon.

    -background => 'red',

    Default : snow

  • -colordatamouse => Array reference

    Specify an array reference wich contains 2 colors. The first color specifies the color of the line when mouse cursor passes over a 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 => integer

    When the mouse cursor passes over an entry in the legend, the line width increase.

    -morepixelselected => 5,

    Default : 1

set_legend

$chart->set_legend(? %options)

View a legend for the graph and allow to enabled identification help by using set_balloon method.

  • -title => string

    Set a title legend.

    -title => 'My title',

    Default : undef

  • -titlecolors => string

    Set a color to legend text.

    -titlecolors => 'red',

    Default : black

  • -titlefont => string

    Set the font to legend title text.

    -titlefont => '{Arial} 8 {normal}',

    Default : {Times} 8 {bold}

  • -legendcolor => color

    Color of legend text.

    -legendcolor => 'white',

    Default : 'black'

  • -legendfont => string

    Set the font to legend text.

    -legendfont => '{Arial} 8 {normal}',

    Default : {Times} 8 {normal}

  • -box => boolean

    Set a box around all legend.

    -box => 1, # or 0

    Default : 0

  • -legendmarkerheight => integer

    Change the heigth of marker for each legend entry.

    -legendmarkerheight => 5,

    Default : 10

  • -legendmarkerwidth => integer

    Change the width of marker for each legend entry.

    -legendmarkerwidth => 5,

    Default : 10

  • -heighttitle => integer

    Change the height title legend space.

    -heighttitle => 75,

    Default : 30

zoom

$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. 

zoomx

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

zoomy

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

EXAMPLES

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 Tk::Chart.

SEE ALSO

See Tk::Canvas for details of the standard options.

See Tk::Chart, Tk::Chart::FAQ, GD::Graph, Tk::Graph.

AUTHOR

Djibril Ousmanou, <djibel at cpan.org>

BUGS

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?Queue=Tk-Chart. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Tk::Chart::Boxplots

You can also look for information at:

ACKNOWLEDGEMENTS

COPYRIGHT & LICENSE

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.