PlotDataset - An extended version of the canvas widget for plotting 2D line graphs. Plots have a legend, zooming capabilities and the option to display error bars. |
PlotDataset - An extended version of the canvas widget for plotting 2D line graphs. Plots have a legend, zooming capabilities and the option to display error bars.
use Tk; use Tk::PlotDataset; use Tk::LineGraphDataset;
my $main_window = MainWindow -> new;
my @data1 = (0..25); my @errors1 = map { rand(2) } ( 0..25 ); my $dataset1 = LineGraphDataset -> new ( -name => 'Data Set One', -yData => \@data1, -yError => \@errors1, -yAxis => 'Y', -color => 'purple' );
my @data2x = (0..25); my @data2y = (); foreach my $xValue (@data2x) { push (@data2y, $xValue ** 2); } my $dataset2 = LineGraphDataset -> new ( -name => 'Data Set Two', -xData => \@data2x, -yData => \@data2y, -yAxis => 'Y1', -color => 'blue' );
my $graph = $main_window -> PlotDataset ( -width => 500, -height => 500, -background => 'snow' ) -> pack(-fill => 'both', -expand => 1);
$graph -> addDatasets($dataset1, $dataset2);
$graph -> plot;
MainLoop;
-background -highlightthickness -takefocus -selectborderwidth -borderwidth -insertbackground -relief -tile -cursor -insertborderwidth -selectbackground -xscrollcommand -insertwidth -highlightbackground -insertofftime -yscrollcommand -state -highlightcolor -insertontime -selectforeground
In addition to all the Canvas options, the following option/value pairs are
supported. All of these options can be set with the new()
method when the
PlotDataset object is created or by using configure():
This option only has an effect when datasets are plotted and therefore changing the array will not change the colour of the plots already on the graph. To change existing plots the colour must be set in the LineGraphDataset object or the dataset re-added to the graph.
Like the -colors, this option only has an effect when datasets are plotted and therefore changing the array will not change the point shapes of the plots already on the graph.
An axis can be reversed by swapping its minimum and maximum values around.
$graph -> configure ( -fonts => [ 'Times 8 bold', 'Courier 8 italic', 'Arial 12 bold', 'Arial 10' ] );
The format for each font string is; the name of the font, followed by its size and then whether it should be in bold, italic or underlined.
$graph -> configure ( -redraw => sub { my $button = $graph -> Button(-text => 'Button'); $graph -> createWindow ( $graph -> cget(-width) - 8, $graph -> cget(-height) - 8, -anchor => 'se', -height => 18, -width => 100, -window => $button ); } );
In addition to the standard options of the LineGraphDataset module, it is also possible to use additional options for use with PlotDataset. Please note that these options will only have an effect on PlotDataset and no other module and hence are not documented in LineGraphDataset.
PlotDataset is a quick and easy way to build an interactive plot widget into a Perl application. The module is written entirely in Perl/Tk.
The widget is an extension of the Canvas widget that will plot LineGraphDataset objects as lines onto a 2D graph. The axes can be automatically scaled or set by the code. The axes can have linear or logarithmic scales and there is also an option of an additional y-axis (y1).
By default, plots for datasets which contain error data will include error bars.
When the mouse cursor passes over a plotted line or its entry in the legend, the line and its entry will turn red to help identify it. Holding the cursor over a point on the graph will display the point's coordinates in a help balloon (unless disabled). Individual points are not shown when the number of points in the plot is greater than the value set by the -maxPoints option. The default number of points is 20.
By default, the left button (button-1) is used to zoom a graph. Move the cursor to one of the corners of the box into which you want the graph to zoom. Hold down the mouse button and move to the opposite corner. Release the mouse button and the graph will zoom into the box. To undo one level of zoom click the mouse button without moving the cursor.
The PlotDataset (or new) method creates a widget object. This object supports the configure and cget methods described in the Tk::options manpage, which can be used to enquire and modify the options described above (except -colors and -pointShapes). The widget also inherits all the methods provided by the Tk::Canvas class.
In addition, the module provides its own methods, described below:
plot()
method afterwards
to see the newly added datasets.
plot()
method afterwards to
clear the graph.
'never' to never rescale plot.
'not_zoomed' to only rescale when the plot is not zoomed in.
Note: Changes to the graph's configuration or datasets will also be applied when the graph is rescaled when zooming in or out.
This Tk widget is based on the Tk::LineGraph module by Tom Clifford. Due to trouble with overriding methods that call methods using SUPER:: LineGraph could not be used as a base class.
The main difference between this module and the original is that the graph is created as a widget and not in a separate window. It therefore does not have the drop down menus used to configure the graph in the original.
Other additions/alterations are:
- Running the cursor over a line name in the legend will highlight the curve on the graph.
- Added a clearDatasets method for removing all datasets from a plot.
- Added support for a -noLegend option for datasets, allowing them to be excluded from the legend.
- Added support for the -pointSize, -pointStyle, -lineStyle and -fillPoint LineGraphDataset options.
- Added -redraw option to allow a callback to be added to draw additional items onto the canvas when it is redrawn.
- Option for a logarithmic scale on the x-axis (previously this was only available on the y-axis).
- Changed the legend so that it displays an example line and point. This legend can be either at the bottom or side of the chart.
- Added -xTickFormat, -yTickFormat and -y1TickFormat options to configure the format of the number labels on each axis.
- Removed all bindings to the mouse buttons except for zooming. The mouse button used for zooming can be configured.
- Support for plotting y-error bars added by Thomas Pissulla.
A number of bugs in the original code have also been found and fixed:
- If less than ten colours were provided, then the colour usage failed to cycle and caused an error.
- If the user zooms beyond a range of approximately 1e-15, then it hangs.
- Scale values of 0 were frequently displayed as very small numbers (approximately 1e-17).
- Small grey boxes were sometimes left behind when zooming out.
- In places, -tags was passed a string instead of an array reference, which caused problems especially in the legends method.
- Corrected an issue with the positioning of the y1 axis label.
- Corrected a divide by zero error occurring when a vertical data line passes through a zoomed plot.
- Fixed a memory leak that occurred when the value passed to the configure method was an array reference.
Currently there are no known bugs, but there are a couple of the limitations to the module:
- In the case where the number of points in the x and y axes are different the points with missing values are not plotted.
- Currently, if zero or negative numbers are plotted on a logarithmic scale their values are set to the value of -logMin. This can produce strange looking graphs when using mixed type axes. A future improvement would be to provide an option to omit non-valid points from the graph.
- The widget does not work with the Tk::Scrolled module.
Copyright 2013 I.T. Dev Ltd.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Any code from the original Tk::LineGraph module is the copyright of Tom Clifford.
Andy Culmer, Tim Culmer and Stephen Spain. Contact via website - http://www.itdev.co.uk
Original code for the Tk::LineGraph module by Tom Clifford.
Y-Error Bars by Thomas Pissulla. Contact via website - http://www.ikp.uni-koeln.de/~pissulla
Tk::LineGraph Tk::LineGraphDataset
Plot 2D Axis
PlotDataset - An extended version of the canvas widget for plotting 2D line graphs. Plots have a legend, zooming capabilities and the option to display error bars. |