File Coverage

blib/lib/CGI/Graph.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package CGI::Graph;
2              
3 1     1   8839 use Data::Table;
  1         32544  
  1         36  
4 1     1   736 use CGI::Graph::Plot::points::numerical;
  0            
  0            
5             use CGI::Graph::Plot::points::string;
6             use CGI::Graph::Plot::bars::numerical;
7             use CGI::Graph::Plot::bars::string;
8              
9             $VERSION = "0.93";
10             my $directory = ".";
11              
12             sub new {
13             my $vars = pop(@_);
14              
15             my $table = Data::Table::fromCSV($vars->{source});
16             my @header = $table->header;
17              
18             if (!($vars->{X})) {
19             $vars->{X} = $header[0];
20             }
21              
22             if ($vars->{graph_type} eq 'bar') {
23             if ($vars->{X} =~ /^[ir]_/) {
24             return new CGI::Graph::Plot::bars::numerical($vars);
25             }
26             return new CGI::Graph::Plot::bars::string($vars);
27             }
28              
29             #elsif ($vars->{graph_type} eq 'points') {
30             else {
31             if ($vars->{X} =~ /^[ir]_/) {
32             return new CGI::Graph::Plot::points::numerical($vars);
33             }
34             return new CGI::Graph::Plot::points::string($vars);
35             }
36             }
37              
38             sub newGraph {
39             my ($vars) = @_;
40             return new($vars);
41             }
42              
43             sub table {
44             my ($source,$myFile,$X) = @_;
45            
46             my $table = Data::Table::fromCSV($source);
47             open INPUT, "$directory/$myFile";
48             my $select = ;
49             close INPUT;
50             chomp $select;
51             my @select = split ("",$select);
52              
53             $displayTable = $table->rowMask(\@select, 0);
54             $displayTable->sort($X, !($X=~/^[ir]_/), 0);
55            
56             return $displayTable;
57             }
58              
59             1;
60              
61              
62             =head1 NAME
63              
64             CGI::Graph - Create interactive CGI-based graphs
65              
66             =head1 DESCRIPTION
67              
68             This module creates CGI graphs which allow the user to visualize spreadsheet
69             data using scatter plots, bar plots, histograms, etc. It provides features for
70             easy interactions such as panning, zooming, element selection, and axis
71             selection.
72              
73             An input file in CSV format is used. Any column names should be preceded by
74             a character identifying the type of that column and an underscore. For example,
75             a set of integers called "age" should be in a column named "i_age". Currently,
76             the only prefixes that are used are "i_" for integers and "r_" for real numbers.
77             If the column is prefixed by another character, it is assumed to be a
78             non-numerical data set.
79              
80             An output file is used for storing selection values. This file contains a
81             binary string, with each position in the string corresponding to a row from
82             the input file. 0 is used for unselected elements, 1 for selected elements.
83              
84             Within the package, there are 2 different types of modules: Plot and Layout.
85             The Layout module only provides functions relating to the placement of form
86             elements and icons. Classes derived from the Plot module are used to create
87             the various point and bar graphs.
88              
89             For Plot modules, the only required values are the name of the input and output
90             files. Other values such as the zoom factor or axes values are obtained from
91             the form elements of the CGI. See the field summary section for more details.
92             To create a new Plot object, a reference to a hash containing certain values is
93             needed. The Vars() function (for CGI objects) can be used to easily obtain a
94             hash containing all of these values.
95              
96             Once an object is created, a string of parameters can be generated that can be
97             passed to a CGI in order to output an image. The string is in the form
98             key1=value1&key2=value2... and can easily be appended to the URL in an
99             tag. Separate images can be generated for a main view and a global (grid) view.
100             The main image can be augmented with an image map that allows selection of
101             elements. The global view is divided into a grid, which shows the main view in
102             relation to a default view displaying all elements. A user can click on a
103             section of the grid to move the center of the main view.
104              
105             Form elements provided by the Layout module allow the user to zoom in and out,
106             change axes, change graph types, and more.
107              
108             The CGI::Graph::Plot module acts as a parent class for all graph types. From
109             this, the points and bars modules are derived. Finally, from each of those
110             modules, numerical and string modules are derived.
111             CGI::Graph::Layout provides functions relating to the placement of form
112             elements and icons.
113              
114              
115             Plot Layout
116             / \ |
117             points bars |
118             / \ / \ |
119             numerical string numerical string |
120             | \ / | |
121             +-------+-----------+ |
122             | |
123             CGI::Graph ----------> Driver.cgi <--+
124             |
125             +-> Draw.cgi
126             +-> selectTable.cgi
127              
128            
129             Driver.cgi
130             / | \
131             [graph & grid parameters] [image maps] [selection info]
132             | |
133             +-> Draw.cgi +-> selectTable.cgi
134             | |
135             +-> graph image +-> selected elements
136             +-> grid image in html table
137              
138             =over 4
139              
140             =item B
141              
142             a point graph using a numerical X axis.
143              
144             =item B
145              
146             a point graph using a non-numerical X axis.
147              
148             =item B
149              
150             a bar graph using a numerical X axis.
151              
152             =item B
153              
154             a bar graph using a non-numerical X axis.
155              
156             =back
157              
158             =head1 SYNOPSIS
159              
160             Sample Driver.cgi:
161              
162             use CGI::Graph;
163             use CGI;
164              
165             $q = new CGI;
166             $q->param('source','table.csv'); # assign specific parameters
167             $q->param('myFile','select.dat');
168             %hash = $q->Vars(); # get hash from CGI object
169              
170             $plot = new CGI::Graph(\%hash); # create new CGI::Graph object
171              
172             # specify dimensions for images and maps
173             $graph_x_size = 500;
174             $graph_y_size = 400;
175             $grid_x_size = 250;
176             $grid_y_size = 200;
177              
178             # obtain parameters to pass to cgi in order to generate graph image
179             $graphParams = $plot->graphParams($graph_x_size,$graph_y_size);
180              
181             # print image source tag, appending graph parameters to URL
182             print "\n";
183              
184             # output graph image map
185             print $plot->graphMap($graph_x_size,$graph_y_size,$CGI_name);
186              
187             # obtain parameters to pass to cgi in order to generate grid image
188             $gridParams = $plot->gridParams($grid_x_size,$grid_y_size);
189              
190             # print image source tag, appending grid parameters to URL
191             print "\n";
192              
193             # output grid image map
194             print $plot->gridMap($grid_x_size,$grid_y_size,$CGI_name);
195              
196             Sample Draw.cgi
197              
198             use CGI::Graph;
199             use CGI;
200             use GD;
201              
202             $q = new CGI;
203              
204             %hash = $q->Vars; # get hash from CGI object
205             my $plot = new CGI::Graph(\%hash); # create new CGI::Graph object
206              
207             if ($q->param('grid')) {
208             $gd = $plot->drawGrid(); # create a gd object that represents the grid image
209             }
210              
211             else {
212             $gd = $plot->drawGraph(); # create a gd object that represents the graph image
213             }
214              
215             print $q->header('image/png'); # output image header
216             binmode STDOUT; # make sure that STDOUT is in binary mode
217             print STDOUT $gd->png; # output gd object in png format
218              
219             =head1 REQUIRES
220              
221             Perl 5.004 or greater, CGI, GD, GD::Graph, Data::Table
222              
223             =head1 FIELD SUMMARY
224              
225             =over 4
226              
227             =item source I
228              
229             name of a CSV file used for input.
230              
231             =item myFile I
232              
233             filename used for storing selection data.
234              
235             =item X,Y I
236              
237             names of X and Y labels, which should correspond to columns of the table.
238              
239             =item center I
240              
241             the current center value of the graph, in x,y format.
242              
243             =item zoom I
244              
245             a value indicating the size of the current view window, 1 = full view,
246             5 = maximum zoom.
247              
248             =item divisions I
249              
250             the grid image is divided into divisions x divisions rectangles.
251              
252             =item selected I
253              
254             each digit of the string corresponds to the selection value of the table row
255             at each index.
256              
257             =item table I
258              
259             a reference to the Data::Table object read in from the source field.
260              
261             =item width,height I
262              
263             width and height of main graph image and map
264              
265             =item grid_width,grid_height I
266              
267             width and height of grid image and map
268              
269             =item graph_type I
270              
271             indicates the type of graph. Currently, only "points" and "bar" are available.
272              
273             =item zoom_type I
274              
275             indicates the type of zoom used for bar graphs. The default is "vertical lock",
276             where the vertical bounds are fixed regardless of zoom factor. Both "unlocked"
277             modes allow the user to change the upper bound, but the "unlocked int" mode
278             restricts the upper bound to an integer value.
279              
280             =item histogram_type I
281              
282             indicates the type of histogram zoom, either "fixed" or "variable". Fixed zoom
283             functions similarly to the zoom style of other graph types. Variable zoom will
284             re-calculate the histogram of the smaller set of data which roughly corresponds
285             to the area in the view window.
286              
287             =item dataColor,selectColor,lineColor,windowColor I<[red,green,blue], name, or hexadecimal>
288              
289             indicates the color for data, selected values, grid lines, and the grid window. The value
290             is passed from the object as a hexadecimal value, but can be inputted as an array reference,
291             specific color name(see GD::Graph::colours), or hexadecimal value.
292              
293             =item refresh_display I
294              
295             the refresh_display value is stored so that it can be incoporated into the
296             mapInfo method.
297              
298             =item x_min,x_max,y_min,y_max I
299              
300             minimum and maximum values for the current image or image map. Either indicates
301             a fractional value for elements to include or an actual mimimum or maximum.
302              
303             =back
304              
305             =head1 METHODS
306              
307             =head2 Public Methods
308              
309             =over 4
310              
311             =item I CGI::Graph::Plot::classname::new($vars)
312              
313             creates a new Plot. $vars is a hash reference that should contain the
314             keys source and myFile. The source value is used as an input and should be a
315             CSV file. The myFile value is used to store selection values. Optional keys are
316             the X and Y axes names, divisions, center, and zoom.
317              
318             =item I CGI::Graph::new($vars)
319              
320             creates a new Plot. Similar to the method above, but will automatically
321             choose an appropriate graph type.
322              
323             =item I Plot::all_values($filename,$size,$value)
324              
325             $value is written $size number of times to the filename specified. (used to
326             change all selection values to 0 or 1)
327              
328             =item I Plot->graphParams()
329              
330             returns a string of the form key1=value1&key2=value2... used to pass values to
331             the CGI which draws the graph.
332              
333             =item I Plot->gridParams()
334              
335             returns a string of the form key1=value1&key2=value2... used to pass values to
336             the CGI which draws the grid.
337              
338             =item I Plot->graphMap(width,height,name,mapname,info)
339              
340             returns an image map which should correspond to the image generated by
341             drawGraph. The dimensions width and height should be the same as those used by
342             drawGraph. The name parameter should be the name of a textbox and will be used
343             to display the attributes of the element which the user moves the mouse over.
344             Mapname is the name of the image map, which should match the "usemap" parameter
345             in the tag. Info is an optional parameter which will select a data set
346             to pop up over an element (point graphs only).
347              
348             =item I Plot->gridMap()
349              
350             return grid image map. Each area in the map contains the parameters used by the
351             current Plot, except that a different center is specified.
352              
353             =item I Plot->drawGraph()
354              
355             Return graph image as a GD::Image object.
356              
357             =item I Plot->drawGrid()
358              
359             Return grid image as a GD::Image object.
360              
361             =back
362              
363             =head2 Private Methods
364              
365             =over 4
366              
367             =item I Plot::getValues($vars,@keys)
368              
369             retrieves the values corresponding to @keys from the hash reference $vars. The
370             keys and values are returned in a string of the form key1=value1&key2=value2...
371              
372             =item I<(int,int)> Plot::bounds(@numericalData)
373              
374             given an array of numerical data, bounds will return a "nice" upper and lower
375             bound.
376              
377             =item I Plot->mapInfo()
378              
379             mapInfo returns a string which represents the attributes of the Plot object.
380              
381             =item I Plot->write_selected()
382              
383             writes the selection values of the Plot object to the file specified in the
384             Plot object's myFile field.
385              
386             =item I<(int,int,int)> Plot->resize()
387              
388             returns the center and span of the current view, relative to the number of
389             divisions.
390              
391             =item I Plot->gridLines($image)
392              
393             will add vertical and horizontal lines to an image, as well as a center marker
394             and a rectangle which acts as a view window.
395              
396             =item I Plot->count(varies)
397              
398             determines numerical X and Y data to be used in plotting the graph. Parameters
399             and return values differ for each of the different plot types. For example,
400             in a non-numerical bar graph, the occurences of each X value will be counted
401             and used as the Y value.
402              
403             =item I Plot->valuesInRange(varies)
404              
405             determines which X and Y elements should actually be plotted on the graph by
406             using the object's center and zoom values to calculate what should be in range.
407              
408             =back
409              
410             =head2 Layout Methods
411              
412             =over 4
413              
414             =item I Layout::new(CGI object)
415              
416             creates a new Layout object. The CGI object must contain two parameters: source
417             and myFile. These two parameters should be the same as the ones contained in
418             the CGI::Graph::Plot object.
419              
420             =item I Layout->header_bars()
421              
422             returns html to create two scrolling lists that contain the x and y column
423             names.
424              
425             =item I Layout->zoom_bar()
426              
427             returns html to create a scrolling list with numbers 1-5.
428              
429             =item I Layout->select_bars()
430              
431             returns html to create scrolling lists for select and unselect options.
432              
433             =item I Layout->zoom_type()
434              
435             returns html for a radio group controlling zoom type for bar graphs.
436              
437             =item I Layout->histogram_type()
438              
439             returns html for a radio group controlling the type of histogram for numerical
440             bar graphs.
441              
442             =item I Layout->textbox()
443              
444             returns html for a textarea for displaying the attributes of elements.
445              
446             =item I Layout->icons($bar,$points)
447              
448             returns html for image buttons used to determine graph type or saving data.
449             $bar and $points are optional filenames for alternate images.
450              
451             =item I Layout->view_button($windowName,$width,$height)
452              
453             returns html for a javascript button that will open a window displaying
454             selected elements in an html table. $windowName is the name of the window
455             to be opened. $width and $height are its dimensions.
456              
457             =item I Layout->refresh_selected()
458              
459             returns html for a checkbox which determines if the pop-up window displaying
460             selected elements is refreshed after each submission.
461              
462             =item I Layout->select_window($ref)
463              
464             returns html containing javascript which opens a window displaying selected
465             elements in an html table. $ref must contain
466              
467             =back
468              
469             =head1 AUTHORS
470              
471             Max Chang Echang@gnf.orgE and Yingyao Zhou Ezhou@gnf.orgE
472              
473             =head1 SEE ALSO
474              
475             CGI, GD, GD::Graph, Data::Table
476              
477             =cut