Ready for 7.10.29 development.

This commit is contained in:
Colin Kuskie 2013-03-20 21:38:23 -07:00
commit c806f99b7b
4236 changed files with 1217679 additions and 0 deletions

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,711 @@
package WebGUI::Image::Graph::XYGraph;
use strict;
use WebGUI::Image::Graph;
use WebGUI::International;
use List::Util;
use POSIX;
our @ISA = qw(WebGUI::Image::Graph);
=head1 NAME
Package WebGUI::Image::Graph::XYGraph
=head1 DESCRIPTION
Base class for flat xy charts.
=head1 SYNOPSIS
XY charts are graphs that have a x and a y coordinate. Examples are Line and Bar
graphs.
This package provides basics needs for such graphs like methods for drawing
axis, labels, rulers and the likes. Also it has methods to set parameters
belonging to xy charts in general such as setting chart width.
=head1 METHODS
These methods are available from this class:
=cut
#-------------------------------------------------------------------
=head2 configurationForm ( )
The configuration form part for this object. See WebGUI::Image::Graph for
documentation.
=cut
sub configurationForm {
my ($configForms, $f);
my $self = shift;
my $i18n = WebGUI::International->new($self->session, 'Image_Graph_XYGraph');
$configForms = $self->SUPER::configurationForm;
$f = WebGUI::HTMLForm->new($self->session);
$f->trClass('Graph_XYGraph');
$f->integer(
name => 'xyGraph_chartWidth',
value => $self->getChartWidth,
label => $i18n->get('chart width'),
hoverHelp => $i18n->get('chart width description'),
);
$f->integer(
name => 'xyGraph_chartHeight',
value => $self->getChartHeight,
label => $i18n->get('chart height'),
hoverHelp => $i18n->get('chart height description'),
);
$f->yesNo(
name => 'xyGraph_drawLabels',
value => $self->showLabels,
label => $i18n->get('draw labels'),
hoverHelp => $i18n->get('draw labels description'),
);
$f->yesNo(
name => 'xyGraph_drawAxis',
value => $self->showAxis,
label => $i18n->get('draw axis'),
hoverHelp => $i18n->get('draw axis description'),
);
$f->color(
name => 'xyGraph_axisColor',
value => $self->getAxisColor,
label => $i18n->get('axis color'),
hoverHelp => $i18n->get('axis color description'),
);
$f->yesNo(
name => 'xyGraph_drawRulers',
value => $self->showRulers,
label => $i18n->get('draw rulers'),
hoverHelp => $i18n->get('draw rulers description'),
);
$f->color(
name => 'xyGraph_rulerColor',
value => $self->getRulerColor,
label => $i18n->get('ruler color'),
hoverHelp => $i18n->get('ruler color description'),
);
$f->selectBox(
name => 'xyGraph_drawMode',
value => [ $self->getDrawMode ],
label => $i18n->get('draw mode'),
hoverHelp => $i18n->get('draw mode description'),
multiple=> 0,
options => {
sideBySide => 'Side by side',
stacked => 'Stacked (cumulative',
},
);
$f->float(
name => 'xyGraph_yGranularity',
value => $self->getYGranularity,
label => $i18n->get('y granularity'),
hoverHelp => $i18n->get('y granularity description'),
);
$configForms->{'graph_xygraph'} = $f->printRowsOnly;
return $configForms;
}
#-------------------------------------------------------------------
=head2 draw ( )
Draws the graph.
=cut
sub draw {
my $self = shift;
# Automagically set the chart offset.
my $maxYLabelWidth = List::Util::max(map {$self->getLabelDimensions($_)->{width}} @{$self->getYLabels});
$self->setChartOffset({
x=> $maxYLabelWidth + 2*$self->getLabelOffset,
y=> $self->getLabelOffset
});
$self->drawRulers if ($self->showRulers);
$self->drawGraph;
$self->drawAxis if ($self->showAxis);
$self->drawLabels if ($self->showLabels);
}
#-------------------------------------------------------------------
=head2 drawAxis ( )
Draws the axis.
=cut
sub drawAxis {
my $self = shift;
my $chartOffset = $self->getChartOffset;
$self->image->Draw(
primitive => 'Path',
stroke => $self->getAxisColor,
points =>
" M ".$chartOffset->{x}.",".$chartOffset->{y}.
" L ".$chartOffset->{x}.",".($self->getChartHeight + $chartOffset->{y}).
" L ".($self->getChartWidth + $chartOffset->{x}).",".($self->getChartHeight + $chartOffset->{y}),
fill => 'none',
);
}
#-------------------------------------------------------------------
=head2 drawLabels ( )
Draws the labels.
=cut
sub drawLabels {
my $self = shift;
my $location = shift;
my %anchorPoint = %{$self->getFirstAnchorLocation};
# Draw x-axis labels
foreach my $text (@{$self->getLabel}) {
$self->drawLabel($text, (
alignVertical => 'top',
align => 'left',
rotate => 90,
x => $anchorPoint{x},
y => $anchorPoint{y},
));
$anchorPoint{x} += $self->getAnchorSpacing->{x};
$anchorPoint{y} += $self->getAnchorSpacing->{y};
}
# Draw y-axis labels
$anchorPoint{x} = $self->getChartOffset->{x} - $self->getLabelOffset;
$anchorPoint{y} = $self->getChartOffset->{y} + $self->getChartHeight;
foreach (@{$self->getYLabels}) {
$self->drawLabel($_, (
alignHorizontal => 'right',
alignVertical => 'center',
x => $anchorPoint{x},
y => $anchorPoint{y},
));
$anchorPoint{y} -= $self->getPixelsPerUnit * $self->getYGranularity
}
}
#-------------------------------------------------------------------
=head2 drawRulers ( )
Draws the rulers.
=cut
sub drawRulers {
my $self = shift;
my $chartOffset = $self->getChartOffset;
my $dist = $self->getLabelOffset;
for (1 .. $self->getYRange / $self->getYGranularity) {
$self->image->Draw(
primitive => 'Path',
stroke => $self->getRulerColor,
points =>
" M ".$chartOffset->{x}.",".($chartOffset->{y}+$self->getChartHeight - $self->getPixelsPerUnit * $_*$self->getYGranularity).
" L ".($chartOffset->{x}+$self->getChartWidth).",".($chartOffset->{y}+$self->getChartHeight - $self->getPixelsPerUnit * $_*$self->getYGranularity)
);
}
}
#-------------------------------------------------------------------
=head2 formNamespace ( )
Extends the form namespace for this object. See WebGUI::Image::Graph for
documentation.
=cut
sub formNamespace {
my $self = shift;
return $self->SUPER::formNamespace.'_XYGraph';
}
#-------------------------------------------------------------------
=head2 getAnchorSpacing ()
This method MUST be overridden by all sub classes.
=cut
sub getAnchorSpacing {
die "You were supposed to override this method in the sub class.";
}
#-------------------------------------------------------------------
=head2 getAxisColor ( )
Returns the color triplet for the axis. Defaults to '#222222'.
=cut
sub getAxisColor {
my $self = shift;
return $self->{_axisProperties}->{axisColor} || '#222222';
}
#-------------------------------------------------------------------
=head2 getChartHeight ( )
Returns the height of the chart. Defaults to 200.
=cut
sub getChartHeight {
my $self = shift;
return $self->{_properties}->{chartHeight} || 200;
}
#-------------------------------------------------------------------
=head2 getChartOffset ( )
Returns the coordinates of the top-left corner of the chart. he coordinates are
contained in a hasref with keys 'x' and 'y'.
=cut
sub getChartOffset {
my $self = shift;
return $self->{_properties}->{chartOffset} || { x=>0, y=>0 }
}
#-------------------------------------------------------------------
=head2 getChartWidth ( )
Returns the width of the chart. Defaults to 200.
=cut
sub getChartWidth {
my $self = shift;
return $self->{_properties}->{chartWidth} || 200;
}
#-------------------------------------------------------------------
=head2 getConfiguration ( )
Returns a configuration hashref. See WebGUI::Image::Graph for documentation.
=cut
sub getConfiguration {
my $self = shift;
my $config = $self->SUPER::getConfiguration;
$config->{xyGraph_chartWidth} = $self->getChartWidth;
$config->{xyGraph_chartHeight} = $self->getChartHeight;
$config->{xyGraph_drawLabels} = $self->showLabels;
$config->{xyGraph_drawAxis} = $self->showAxis;
$config->{xyGraph_drawRulers} = $self->showRulers;
$config->{xyGraph_drawMode} = $self->getDrawMode;
$config->{xyGraph_yGranularity} = $self->getYGranularity;
return $config;
}
#-------------------------------------------------------------------
=head2 getDrawMode ( )
Returns the drawmode. Currently supported are 'stacked' and 'sideBySide'.
Defaults to 'sideBySide'.
=cut
sub getDrawMode {
my $self = shift;
return $self->{_barProperties}->{drawMode} || 'sideBySide';
}
#-------------------------------------------------------------------
=head2 getPixelsPerUnit ( )
Returns the number of pixels that correspond with one unit of the dataset
values.
=cut
sub getPixelsPerUnit {
my $self = shift;
return $self->getChartHeight / $self->getYRange;
}
#-------------------------------------------------------------------
=head2 getRulerColor ( )
Returns the color triplet of the rulers in the graph. Defaults to '#777777'.
=cut
sub getRulerColor {
my $self = shift;
return $self->{_axisProperties}->{rulerColor} || '#777777';
}
#-------------------------------------------------------------------
=head2 getYGranularity ( )
Returns the granularity of the labels and rulers in the Y direction. Defaults to
10. This is value is in terms of the values in the dataset and has no direct
relation to pixels.
=cut
sub getYGranularity {
my $self = shift;
return $self->{_properties}->{yGranularity} || 10;
}
#-------------------------------------------------------------------
=head2 getYLabels ( )
Returns an arrayref containing the labels for the Y axis.
=cut
sub getYLabels {
my $self = shift;
my @yLabels;
for (0 .. $self->getYRange / $self->getYGranularity) {
push(@yLabels, $_ * $self->getYGranularity);
}
return \@yLabels;
}
#-------------------------------------------------------------------
=head2 getYRange ( )
Returns the maxmimal value of the range that contains a whole number of times
the y granularity and is bigger than the maximum value in the dataset.
=cut
sub getYRange {
my $self = shift;
return $self->getYGranularity*ceil($self->getMaxValueFromDataset / $self->getYGranularity) || 1;
}
#-------------------------------------------------------------------
=head2 setAxisColor ( color )
Sets the color of the axis to the supplied value.
=head3 color
The triplet of the color you want to set the axis to. Must have the following
form: #ffffff.
=cut
sub setAxisColor {
my $self = shift;
my $color = shift;
$self->{_axisProperties}->{axisColor} = $color;
}
#-------------------------------------------------------------------
=head2 setChartHeight ( height )
Sets the height of the chart to the specified value.
=head3 height
The desired height in pixels.
=cut
sub setChartHeight {
my $self = shift;
my $height = shift;
$self->{_properties}->{chartHeight} = $height;
}
#-------------------------------------------------------------------
=head2 setChartOffset ( location )
Sets the location of the top-left corner of the graph within the image.
=head3 location
A hashref containing the desired location. Use the 'x' and 'y' as keys for the x
and y coordinate respectively.
=cut
sub setChartOffset {
my $self = shift;
my $point = shift;
$self->{_properties}->{chartOffset} = {%$point};
}
#-------------------------------------------------------------------
=head2 setChartWidth ( width )
Sets the width of the chart to the specified value.
=head3 width
The desired width in pixels.
=cut
sub setChartWidth {
my $self = shift;
my $width = shift;
$self->{_properties}->{chartWidth} =$width;
}
#-------------------------------------------------------------------
=head2 setConfiguration ( config )
Applies the settings in the given configuration hash. See WebGUI::Image::Graph
for more information.
=head3 config
A configuration hash.
=cut
sub setConfiguration {
my $self = shift;
my $config = shift;
$self->SUPER::setConfiguration($config);
$self->setChartWidth($config->{xyGraph_chartWidth});
$self->setChartHeight($config->{xyGraph_chartHeight});
$self->setShowLabels($config->{xyGraph_drawLabels});
$self->setShowAxis($config->{xyGraph_drawAxis});
$self->setShowRulers($config->{xyGraph_drawRulers});
$self->setDrawMode($config->{xyGraph_drawMode});
$self->setYGranularity($config->{xyGraph_yGranularity});
$self->setAxisColor($config->{xyGraph_axisColor});
$self->setRulerColor($config->{xyGraph_rulerColor});
return $config;
}
#-------------------------------------------------------------------
=head2 setDrawMode ( mode )
Set the way the datasets are drawn. Currently supported are 'stacked' and
'sideBySide' which correspond to respectivly cumulative drawing and normal
processing.
=head3 mode
The desired mode. Can be 'sideBySide' or 'stacked'.
=cut
sub setDrawMode {
my $self = shift;
my $mode = shift;
if ($mode eq 'stacked' || $mode eq 'sideBySide') {
$self->{_barProperties}->{drawMode} = $mode;
} else {
$self->{_barProperties}->{drawMode} = 'sideBySide';
}
}
#-------------------------------------------------------------------
=head2 setRulerColor ( color )
Set the color of the rulers.
=head3 color
The triplet of the desired ruler color. Must be in the following format:
'#ffffff'.
=cut
sub setRulerColor {
my $self = shift;
my $color = shift;
$self->{_axisProperties}->{rulerColor} = $color;
}
#-------------------------------------------------------------------
=head2 setShowAxis ( boolean )
Set whether or not to draw the axis.
=head3 boolean
If set to false the axis won't be drawn.
=cut
sub setShowAxis {
my $self = shift;
my $yesNo = shift;
$self->{_properties}->{showAxis} = $yesNo;
}
#-------------------------------------------------------------------
=head2 setShowLabels ( boolean )
Set whether or not to draw the labels.
=head3 boolean
If set to false the labels won't be drawn.
=cut
sub setShowLabels {
my $self = shift;
my $yesNo = shift;
$self->{_properties}->{showLabels} = $yesNo;
}
#-------------------------------------------------------------------
=head2 setShowRulers ( boolean )
Set whether or not to draw the rulers.
=head3 boolean
If set to false the rulers won't be drawn.
=cut
sub setShowRulers {
my $self = shift;
my $yesNo = shift;
$self->{_properties}->{showRulers} = $yesNo;
}
#-------------------------------------------------------------------
=head2 setYGranularity ( value )
Sets the y granularity. See getYGranularity for explanation of this concept.
=head3 value
The granularity in dataset units, not pixels.
=cut
sub setYGranularity {
my $self = shift;
my $granularity = shift;
$self->{_properties}->{yGranularity} = $granularity;
}
#-------------------------------------------------------------------
=head2 showAxis ( )
Returns a boolean indicating whether to draw the axis.
=cut
sub showAxis {
my $self = shift;
return 1 unless (defined $self->{_properties}->{showAxis});
return $self->{_properties}->{showAxis};
}
#-------------------------------------------------------------------
=head2 showLabels ( )
Returns a boolean indicating whether to draw the labels.
=cut
sub showLabels {
my $self = shift;
return 1 unless (defined $self->{_properties}->{showLabels});
return $self->{_properties}->{showLabels};
}
#-------------------------------------------------------------------
=head2 showRulers ( )
Returns a boolean indicating whether to draw the rulers.
=cut
sub showRulers {
my $self = shift;
return 1 unless (defined $self->{_properties}->{showRulers});
return $self->{_properties}->{showRulers};
}
1;

View file

@ -0,0 +1,426 @@
package WebGUI::Image::Graph::XYGraph::Bar;
use strict;
use WebGUI::Image::Graph::XYGraph;
use List::Util;
use POSIX;
use WebGUI::Utility;
our @ISA = qw(WebGUI::Image::Graph::XYGraph);
=head1 NAME
Package WebGUI::Image::Graph::XYGraph::Bar
=head1 DESCRIPTION
Package for creating bar graphs.
=head1 SYNOPSIS
This package privides the logic for drawing 2d bar graphs, 3d bars are in the
pipeline but not yet ready for prime time.
This module can draw bar graph in two forms: Stacked and Side by Side. The
diffrence is noticable only if more multiple dataset is used, the behaviour is
thus identical in case of one dataset.
Stacked graphs place the bars belonging the same index within diffrent datasets
on top of each other given a grand total for all datasets.
Sid by side graphs place bars with the same index next to each other, grouped by
index. This displays a better comaprison between datasets.
=head1 METHODS
These methods are available from this class:
=cut
#-------------------------------------------------------------------
=head2 configurationForm
Creates the configuration form for this plugin. See WebGUI::Image::Graph for
more information.
=cut
sub configurationForm {
my $self = shift;
my $i18n = WebGUI::International->new($self->session, 'Image_Graph_XYGraph_Bar');
my $configForms = $self->SUPER::configurationForm;
my $f = WebGUI::HTMLForm->new($self->session);
$f->trClass('Graph_XYGraph_Bar');
$f->float(
name => 'xyGraph_bar_barSpacing',
value => $self->getBarSpacing,
label => $i18n->get('bar spacing'),
hoverHelp => $i18n->get('bar spacing description'),
);
$f->float(
name => 'xyGraph_bar_groupSpacing',
value => $self->getGroupSpacing,
label => $i18n->get('group spacing'),
hoverHelp => $i18n->get('group spacing description'),
);
$configForms->{'graph_xygraph_bar'} = $f->printRowsOnly;
return $configForms;
}
#-------------------------------------------------------------------
=head2 drawBar ( bar, location, barWidth )
Draws a bar defined by bar and with width barWidth at location.
=head3 bar
A hashref defining the bar. Must contain keys 'height', 'strokeColor' and
'fillColor'.
=head3 location
A hashref containing the location of the bottom-left corner of the bar. Keys 'x'
and 'y' must specify the x- and y-coordinates respectively.
=head3 barWidth
The width of the bar in pixels.
=cut
sub drawBar {
my $self = shift;
my $bar = shift;
my $location = shift;
my $barWidth = shift;
my $barHeight = $bar->{height} * $self->getPixelsPerUnit;
$self->image->Draw(
primitive => 'Path',
stroke => $bar->{strokeColor},
points =>
" M ".$location->{x}.",".$location->{y}.
" L ".$location->{x}.",".($location->{y}-$barHeight).
" L ".($location->{x}+$barWidth).",".($location->{y}-$barHeight).
" L ".($location->{x}+$barWidth).",".$location->{y},
fill => $bar->{fillColor},
);
}
#-------------------------------------------------------------------
=head2 drawGraph
Draws all the bars.
=cut
sub drawGraph {
my %location;
my $self = shift;
$self->processDataSet;
my $numberOfGroups = List::Util::max(map {scalar @$_} @{$self->{_datasets}});
my $numberOfDatasets = scalar @{$self->{_datasets}};
my $groupWidth = ($self->getChartWidth - ($numberOfGroups-1) * $self->getGroupSpacing) / $numberOfGroups;
my $barWidth = $groupWidth;
$barWidth = ($groupWidth - ($numberOfDatasets - 1) * $self->getBarSpacing) / $numberOfDatasets if ($self->getDrawMode eq 'sideBySide');
$location{x} = $self->getChartOffset->{x} ;
$location{y} = $self->getChartOffset->{y} + $self->getChartHeight;
foreach my $currentBar (@{$self->{_bars}}) {
if ($self->getDrawMode eq 'stacked') {
$self->drawStackedBar($currentBar, \%location, $barWidth);
} else {
$self->drawSideBySideBar($currentBar, \%location, $barWidth);
}
$location{x} += $groupWidth + $self->getGroupSpacing;
}
}
#-------------------------------------------------------------------
=head2 drawSideBySideBar ( bars, location, barWidth )
Draws the bars in side by side mode. Meaning that per datsetindex the bars
representing a single dataset are grouped.
=head3 bars
An arrayref containing all the bar description hashrefs as described in drawBar.
=head3 location
Hashref containing the initial coordinates of the lower-left corner of the
chart. Pass coords in keys 'x' and 'y'.
=head3 barWidth
The width of each bar in pixels.
=cut
sub drawSideBySideBar {
my $self = shift;
my $bars = shift;
my $location = shift;
my $barWidth = shift;
my %thisLocation = %$location;
foreach (@$bars) {
$self->drawBar($_, \%thisLocation, $barWidth);
$thisLocation{x} += $barWidth + $self->getBarSpacing;
}
}
#-------------------------------------------------------------------
=head2 drawStackedBar ( bars, location, barWidth )
Draws the bars in side by side mode. Meaning that per datset-index the bars
representing a single dataset are stacked on top of each other.
=head3 bars
An arrayref containing all the bar description hashrefs as described in drawBar.
=head3 location
Hashref containing the initial coordinates of the lower-left corner of the
chart. Pass coords in keys 'x' and 'y'.
=head3 barWidth
The width of each bar in pixels.
=cut
sub drawStackedBar {
my $self = shift;
my $bars = shift;
my $location = shift;
my $barWidth = shift;
my %thisLocation = %$location;
foreach (@$bars) {
$self->drawBar($_, \%thisLocation, $barWidth);
$thisLocation{y} -= $_->{height} * $self->getPixelsPerUnit;
}
}
#-------------------------------------------------------------------
=head2 formNamespace
Returns the form namespace of this plugin. See WegBUI::Image::Graph for
more elaborate information.
=cut
sub formNamespace {
my $self = shift;
return $self->SUPER::formNamespace.'_Bar';
}
#-------------------------------------------------------------------
=head2 getAnchorSpacing
Returns the distance in pixels between two anchors on the x axis that define teh
placement of bars and labels.
=cut
sub getAnchorSpacing {
my $self = shift;
my $numberOfGroups = List::Util::max(map {scalar @$_} @{$self->getDataset});
my $spacing = round(($self->getChartWidth - ($numberOfGroups-1) * $self->getGroupSpacing) / $numberOfGroups + $self->getGroupSpacing);
return {
x => $spacing,
y => 0,
};
}
#-------------------------------------------------------------------
=head2 getBarSpacing
Returns the width of the gap between two bars within a group in pixels.
=cut
sub getBarSpacing {
my $self = shift;
return $self->{_barProperties}->{barSpacing} || 0;
}
#-------------------------------------------------------------------
=head2 getConfiguration
Returns the configuration hashref for this plugin. Refer to WebGUI::IMage::Graph
for a more detailed description.
=cut
sub getConfiguration {
my $self = shift;
my $config = $self->SUPER::getConfiguration;
$config->{xyGraph_bar_barSpacing} = $self->getBarSpacing;
$config->{xyGraph_bar_groupSpacing} = $self->getGroupSpacing;
return $config;
}
#-------------------------------------------------------------------
=head2 getGroupSpacing
Returns the width of the gap between two groups of bars in pixels.
=cut
sub getGroupSpacing {
my $self = shift;
return $self->{_barProperties}->{groupSpacing} || $self->getBarSpacing;
}
#-------------------------------------------------------------------
=head2 getFirstAnchorLocation
Returns a hashref containing the location of the leftmost x-axis anchor.
Location coordinates are encoded in keys 'x' and 'y'.
=cut
sub getFirstAnchorLocation {
my $self = shift;
return {
x => round($self->getChartOffset->{x} + ($self->getAnchorSpacing->{x} - $self->getGroupSpacing) / 2),
y => $self->getChartOffset->{y} + $self->getChartHeight
}
}
#-------------------------------------------------------------------
=head2 processDataSet
Processes the dataset. Used by drawGraph.
=cut
sub processDataSet {
my ($barProperties);
my $self = shift;
my $palette = $self->getPalette;
my $maxElements = List::Util::max(map {scalar @$_} @{$self->{_datasets}});
my $numberOfDatasets = scalar @{$self->{_datasets}};
for my $currentElement (0 .. $maxElements-1) {
my @thisSet = ();
for my $currentDataset (0 .. $numberOfDatasets - 1) {
my $color = $palette->getColor($currentDataset);
if ($numberOfDatasets == 1) {
$color = $palette->getNextColor;
}
push(@thisSet, {
height => $self->{_datasets}->[$currentDataset]->[$currentElement] || 0,
fillColor => $color->getFillColor,
strokeColor => $color->getStrokeColor,
});
}
push(@{$self->{_bars}}, [ @thisSet ]);
}
}
#-------------------------------------------------------------------
=head2 setBarSpacing ( gap )
Sets the distance between two bars in a group in pixels.
=head3 gap
The distance in pixels.
=cut
sub setBarSpacing {
my $self = shift;
my $gap = shift;
$self->{_barProperties}->{barSpacing} = $gap;
}
#-------------------------------------------------------------------
=head2 setConfiguration ( config )
Applies the given configuration hash to this plugin. See WebGUI::Image::Graph
for more info.
=head3 config
The configuration hash.
=cut
sub setConfiguration {
my $self = shift;
my $config = shift;
$self->SUPER::setConfiguration($config);
$self->setBarSpacing($config->{xyGraph_bar_barSpacing});
$self->setGroupSpacing($config->{xyGraph_bar_groupSpacing});
return $config;
}
#-------------------------------------------------------------------
=head2 setGroupSpacing ( gap )
Sets the distance between two groups of bars in pixels.
=head3 gap
The distance in pixels.
=cut
sub setGroupSpacing {
my $self = shift;
my $gap = shift;
$self->{_barProperties}->{groupSpacing} = $gap;
}
1;

View file

@ -0,0 +1,184 @@
package WebGUI::Image::Graph::XYGraph::Line;
use strict;
use WebGUI::Image::Graph::XYGraph;
use List::Util;
use POSIX;
our @ISA = qw(WebGUI::Image::Graph::XYGraph);
=head1 NAME
Package WebGUI::Image::Graph::XYGraph::Line
=head1 DESCRIPTION
Package for creating line graphs.
=head1 SYNOPSIS
This package privides the logic for drawing 2d line graphs, 3d lines are in the
pipeline but not yet ready for prime time.
The possibilities are quite limited for now but will be enhanced upon in the future.
=head1 METHODS
These methods are available from this class:
=cut
#-------------------------------------------------------------------
=head2 drawGraph
Draws all the lines.
=cut
sub drawGraph {
my ($currentBar, %location);
my $self = shift;
$self->processDataSet;
my $numberOfGroups = List::Util::max(map {scalar @$_} @{$self->{_datasets}});
my $interval = $self->getChartWidth / ($numberOfGroups - 1);
%location = %{$self->getChartOffset};
$location{y} += $self->getChartHeight;
foreach (@{$self->{_lines}}) {
$self->drawLine($_, \%location, $interval);
}
}
#-------------------------------------------------------------------
=head2 drawLine ( line, location, interval )
Draws a bar defined by bar and with width barWidth at location.
=head3 line
A hashref defining the line. Must contain keys 'strokeColor' and
'dataset', the latter one being an arrayref containing all points of the line.
=head3 location
A hashref containing the location of the bottom-left corner of the line's
origin. Keys 'x' and 'y' must specify the x- and y-coordinates respectively.
=head3 interval
The distance between x-axis anchors in pixels.
=cut
sub drawLine {
my $self = shift;
my $line = shift;
my $location = shift;
my $interval = shift;
my %currentLocation = %$location;
my $dataCounter;
my $path;# = " M ".$currentLocation{x}.",".$currentLocation{y};
foreach (@{$line->{dataset}}) {
$path .= ($dataCounter++) ? " L " : " M ";
$path .= $currentLocation{x}.",".($currentLocation{y} - $_*$self->getPixelsPerUnit);
$currentLocation{x} += $interval;
}
$self->image->Draw(
primitive => 'Path',
stroke => $line->{strokeColor},
points => $path,
fill => 'none',
);
}
#-------------------------------------------------------------------
=head2 formNamespace
Returns the form namespace of this plugin. See WegBUI::Image::Graph for
more elaborate information.
=cut
sub formNamespace {
my $self = shift;
return $self->SUPER::formNamespace.'_Line';
}
#-------------------------------------------------------------------
=head2 getAnchorSpacing
Returns the distance in pixels between two anchors on the x axis that define teh
placement of bars and labels.
=cut
sub getAnchorSpacing {
my $self = shift;
my $numberOfGroups = List::Util::max(map {scalar @$_} @{$self->getDataset});
my $spacing = $self->getChartWidth / ($numberOfGroups - 1);
return {
x => $spacing,
y => 0,
};
}
#-------------------------------------------------------------------
=head2 getFirstAnchorLocation
Returns a hashref containing the location of the leftmost x-axis anchor.
Location coordinates are encoded in keys 'x' and 'y'.
=cut
sub getFirstAnchorLocation {
my $self = shift;
return {
x => $self->getChartOffset->{x},
y => $self->getChartOffset->{y} + $self->getChartHeight
}
}
#-------------------------------------------------------------------
=head2 processDataSet
Processes the dataset. Used by drawGraph.
=cut
sub processDataSet {
my ($barProperties);
my $self = shift;
my $palette = $self->getPalette;
foreach (@{$self->{_datasets}}) {
push (@{$self->{_lines}}, {
dataset => $_,
strokeColor => $palette->getColor->getStrokeColor,
});
$palette->getNextColor;
}
}
1;