Gtk2-Ex-Graph-GD

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

0.02 May 17, 2005
    - right-click menu to switch to lines, pie
    
0.03 May 18, 2005
    - mouse-over tooltip for line and linespoints charts
    - right-click menu to switch to linespoints, to cumulate
    
0.04 October 13 2005
    - fixed a bug with mouse-over
    - added call-backs for 'mouse-over' and 'clicked'
    - fixed a bug with lines legend
    - cleaned up code
    

examples/graph-gd-demo.pl  view on Meta::CPAN

$graph->set (
	title           => 'Mice, Fish and Lobsters',
	x_labels_vertical => TRUE,
	bar_spacing     => 1,
	shadowclr       => 'dred',
	transparent     => 0,
	# cumulate		=> TRUE,
	type => ['lisen', 'bars', 'bars'],
);

my @legend_keys = ('Field Mice Population', 'Fish Population', 'Lobster Growth in millions');
$graph->set_legend(@legend_keys);

my $data = GD::Graph::Data->new([
    [ 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,],
    [  1,  2,  5, 8,  3, 4.5,  1, 3,  4],
    [1.4,  4, 15, 6, 13, 1.5, 11, 3,  4],
    [11.4,  14, 22, 16, 1.3, 15, 1, 13,  14],
]) or die GD::Graph::Data->error;

$graph->signal_connect ('mouse-over' =>
	sub {

lib/Gtk2/Ex/Graph/GD.pm  view on Meta::CPAN

	} elsif ($type eq 'pie') {
		$graph = GD::Graph::pie->new($width, $height);
	}
	$self->{graph} = undef;
	$self->{graph} = $graph;
}

sub _refresh {
	my ($self) = @_;
	$self->{graph}->set(%{$self->{graphhash}}) if $self->{graphhash};
	$self->set_legend(@{$self->{graphlegend}}) if $#{@{$self->{graphlegend}}} >= 0;
	$self->get_image($self->{graphdata});
}

sub _init_tooltip {
	my ($self) = @_;
	my $tooltip_label = Gtk2::Label->new;
	my $tooltip = Gtk2::Window->new('popup');
	$tooltip->set_decorated(0);
	$tooltip->set_position('mouse'); # We'll choose this to start with.
	$tooltip->modify_bg ('normal', Gtk2::Gdk::Color->parse('yellow')); # The obligatory yellow
	$tooltip->add($tooltip_label);
	$self->{tooltip}->{window} = $tooltip;
	$self->{tooltip}->{displayed} = FALSE;
	$self->{tooltip}->{label} = $tooltip_label;	
}

sub set_legend {
	my ($self, @legend_keys) = @_;
	return if ($self->{graphtype} eq 'pie');
	$self->{graph}->set_legend(@legend_keys);
	$self->{graphlegend} = \@legend_keys;
}

sub get_image {
	my ($self, $data) = @_;
	$self->{graphdata} = $data;
	my $graph = $self->{graph};
	$graph->plot($data) or warn $graph->error;
	my $loader = Gtk2::Gdk::PixbufLoader->new;
	$loader->write ($graph->gd->png);
	$loader->close;

lib/Gtk2/Ex/Graph/GD.pm  view on Meta::CPAN

	my $hotspotlist = $self->{hotspotlist};
	foreach my $datameasure (@$hotspotlist){
		my $j=0;
		foreach my $hotspot (@$datameasure) {
			my ($name, @coords) = @$hotspot;
			if (_on_the_line($x, $y, @coords)) {
				my $xvalue0 = $self->{graphdata}->[0]->[$j-1];
				my $yvalue0 = $self->{graphdata}->[$i+1]->[$j-1];
				my $xvalue1 = $self->{graphdata}->[0]->[$j];
				my $yvalue1 = $self->{graphdata}->[$i+1]->[$j];
				my $measure = $self->{graphlegend}->[$i];
				return [$measure, $xvalue0, $yvalue0, $xvalue1, $yvalue1];
			}
			$j++;
		} 
		$i++;	
	}
	$self->{tooltip}->{window}->hide;
	$self->{tooltip}->{displayed} = FALSE;
}

lib/Gtk2/Ex/Graph/GD.pm  view on Meta::CPAN

	my ($self, $x, $y) = @_;
	my $i=0;
	my $hotspotlist = $self->{hotspotlist};
	foreach my $datameasure (@$hotspotlist){
		my $j=0;
		foreach my $hotspot (@$datameasure) {			
			my ($name, @coords) = @$hotspot;
			if ($x >= $coords[0] && $x <= $coords[2] && $y >= $coords[1] && $y <= $coords[3]) {
				my $xvalue = $self->{graphdata}->[0]->[$j];
				my $yvalue = $self->{graphdata}->[$i+1]->[$j];
				my $measure = $self->{graphlegend}->[$i];
				return [$measure, $xvalue, $yvalue];
			} 
			$j++;	
		}
		$i++;
	}
	$self->{tooltip}->{window}->hide;
	$self->{tooltip}->{displayed} = FALSE;
}

lib/Gtk2/Ex/Graph/GD.pm  view on Meta::CPAN


	$graph->set (
		x_label         => 'X Label',
		y_label         => 'Y label',
		title           => 'A Simple Bar Chart',
		bar_spacing     => 1,
		shadowclr       => 'dred',
		transparent     => 0,
	);

=head2 $graph->set_legend(@legend_keys)

This is just a thin wrapper on the C<GD::Graph->set_legend> method. However, this method
extracts the C<@legend_keys> and uses them in the mouse-over tooltip text.

	my @legend_keys = ('First', 'Second');
	$graph->set_legend(@legend_keys);

=head2 $graph->get_image($data)

The C<$data> object used here is a C<GD::Graph::Data> object. This method internally calls
the C<GD::Graph->plot($data)> and then exports the output into a png. The png is then wrapped
into a Gtk2::Image and then into a Gtk2::EventBox and returned here. You can go on and
pack this C<$image> into the window.

	my $image = $graph->get_image($data);

t/area.1.t  view on Meta::CPAN

$graph->set (
	title           	=> 'Mice, Fish and Lobsters',
	x_labels_vertical 	=> TRUE,
	bar_spacing     	=> 1,
	shadowclr       	=> 'dred',
	transparent     	=> 0,
	# cumulate			=> TRUE,
	type 				=> 	['area'],
);

my @legend_keys = ('Field Mice Population');
$graph->set_legend(@legend_keys);

my $data = GD::Graph::Data->new([
    [ 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,],
    [  1,  2,  5, 8,  3, 4.5,  1, 3,  4],
]) or die GD::Graph::Data->error;

$graph->signal_connect ('mouse-over' =>
	sub {
		#print Dumper @_;
	}

t/bars.1.t  view on Meta::CPAN

$graph->set (
	title           	=> 'Mice, Fish and Lobsters',
	x_labels_vertical 	=> TRUE,
	bar_spacing     	=> 1,
	shadowclr       	=> 'dred',
	transparent     	=> 0,
	# cumulate			=> TRUE,
	type 				=> 	['bars'],
);

my @legend_keys = ('Field Mice Population');
$graph->set_legend(@legend_keys);

my $data = GD::Graph::Data->new([
    [ 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,],
    [  1,  2,  5, 8,  3, 4.5,  1, 3,  4],
]) or die GD::Graph::Data->error;

$graph->signal_connect ('mouse-over' =>
	sub {
		#print Dumper @_;
	}

t/lines.1.t  view on Meta::CPAN

$graph->set (
	title           	=> 'Mice, Fish and Lobsters',
	x_labels_vertical 	=> TRUE,
	bar_spacing     	=> 1,
	shadowclr       	=> 'dred',
	transparent     	=> 0,
	# cumulate			=> TRUE,
	type 				=> 	['lines'],
);

my @legend_keys = ('Field Mice Population');
$graph->set_legend(@legend_keys);

my $data = GD::Graph::Data->new([
    [ 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,],
    [  1,  2,  5, 8,  3, 4.5,  1, 3,  4],
]) or die GD::Graph::Data->error;

$graph->signal_connect ('mouse-over' =>
	sub {
		#print Dumper @_;
	}

t/lines_points.1.t  view on Meta::CPAN

$graph->set (
	title           	=> 'Mice, Fish and Lobsters',
	x_labels_vertical 	=> TRUE,
	bar_spacing     	=> 1,
	shadowclr       	=> 'dred',
	transparent     	=> 0,
	# cumulate			=> TRUE,
	type 				=> 	['linespoints'],
);

my @legend_keys = ('Field Mice Population');
$graph->set_legend(@legend_keys);

my $data = GD::Graph::Data->new([
    [ 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,],
    [  1,  2,  5, 8,  3, 4.5,  1, 3,  4],
]) or die GD::Graph::Data->error;

$graph->signal_connect ('mouse-over' =>
	sub {
		#print Dumper @_;
	}

t/pie.1.t  view on Meta::CPAN

$graph->set (
	title           	=> 'Mice, Fish and Lobsters',
	x_labels_vertical 	=> TRUE,
	bar_spacing     	=> 1,
	shadowclr       	=> 'dred',
	transparent     	=> 0,
	# cumulate			=> TRUE,
	type 				=> 	['pie'],
);

my @legend_keys = ('Field Mice Population');
$graph->set_legend(@legend_keys);

my $data = GD::Graph::Data->new([
    [ 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,],
    [  1,  2,  5, 8,  3, 4.5,  1, 3,  4],
]) or die GD::Graph::Data->error;

$graph->signal_connect ('mouse-over' =>
	sub {
		#print Dumper @_;
	}

t/test.1.t  view on Meta::CPAN

$graph->set (
	title           => 'Mice, Fish and Lobsters',
	x_labels_vertical => TRUE,
	bar_spacing     => 1,
	shadowclr       => 'dred',
	transparent     => 0,
	# cumulate		=> TRUE,
	type => ['bars', 'bars', 'bars'],
);

my @legend_keys = ('Field Mice Population', 'Fish Population', 'Lobster Growth in millions');
$graph->set_legend(@legend_keys);

my $data = GD::Graph::Data->new([
    [ 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,],
    [  1,  2,  5, 8,  3, 4.5,  1, 3,  4],
    [1.4,  4, 15, 6, 13, 1.5, 11, 3,  4],
    [11.4,  14, 22, 16, 1.3, 15, 1, 13,  14],
]) or die GD::Graph::Data->error;

$graph->signal_connect ('mouse-over' =>
	sub {



( run in 1.409 second using v1.01-cache-2.11-cpan-49f99fa48dc )