Here's a snippet from something I did with GD::Graph that read from a flat file:
foreach (@lines)
{
# Lines in the file look like:
# Sun Jul 07 2002 10:03:03 Sensor 0 [100D973000080069] C: 25.1 F:
+77.1
chomp;
my ($day, $mon, $date, $year, $time, undef, $sensor, undef, undef,
+ $c, undef, $f) = split;
push (@{$data[0]}, $day);
push (@{$data[$sensor + 1]}, $f);
$y_max = $y_max > $f ? $y_max : $f;
$y_min = $y_min < $f ? $y_min : $f;
}
my $y_axis_max = ceil($y_max / 10) * 10;
my $y_axis_min = floor($y_min / 10) * 10;
my $y_ticks = ($y_axis_max - $y_axis_min) / 10;
my $graph = new GD::Graph::lines();
$graph->set(
x_label => 'Day',
y_label => 'Temperature (F)',
title => $title,
y_max_value => $y_axis_max,
y_min_value => $y_axis_min,
y_tick_number => 16,
y_label_skip => 2,
box_axis => 1,
line_width => 3,
y_label_position => 1,
x_label_skip => 48,
x_tick_offset => 48,
x_long_ticks => 1,
transparent => 0,
);
$graph->set_legend(@legend_labels);
$graph->plot($data);
save_chart($graph, "$img_path/$img_file");
Note that all arrays passed to plot() should be the same size. If they're not, pad the beginning of any short arrays with undef.
BTW, I've since switched from using a flat file to a MySQL database, and from GD::Graph to Chart::Graph::Gnuplot. I like Chart::Graph::Gnuplot because of the amount of control Gnuplot gives over the graph. If you've ever created Gnuplot input files yourself, you'll appreciate this module. Of course, some may consider it a drawback that it relies on an external program.
|