#!/usr/local/bin/perl use strict; use warnings; use CGI; use CGI::Carp qw(fatalsToBrowser); use Chart::Gnuplot; my $q = CGI->new(); $q = { map { ($_, $q->param($_)) } $q->param() }; my $tempfile = "/tmp/webchart-$$.png"; $ENV{PATH} .= ':/usr/local/bin'; # so macbookpro can find gs (my $plot = $q->{plot}) =~ s/ /+/g; my $scale = $q->{scale} || 0.5; $scale = 1 if($scale > 1 || $scale < 0.1); my $title = $q->{title}; ($title = $plot) =~ s/(\||,)/, /g unless($title); $title = '' if($title eq ' '); my $chart = Chart::Gnuplot->new( output => $tempfile, title => $title, # xlabel => 'X', # ylabel => 'Y', imagesize => join(', ', map { sprintf('%03f', $_) } $scale, $scale * 720/504), grid => { type => 'dash', width => 1, }, gnuplot => (grep { -f $_ } (qw(/sw/bin/gnuplot /usr/bin/gnuplot /usr/local/bin/gnuplot)))[0], convert => (grep { -f $_ } (qw(/sw/bin/convert /usr/bin/convert /usr/local/bin/convert)))[0], (map { exists($q->{$_}) ? ($_ => [split(/,\s*/, $q->{$_})]) : () } qw(trange xrange yrange)), ) || die("Can't create chart"); my @datasets = (); foreach my $plot (split(/\|/, $plot)) { my $func; if($plot =~ /,/) { my($x, $xfunc, $y, $yfunc) = split(/[=,]/, $plot); $func = { $x => gnuplotise($xfunc), $y => gnuplotise($yfunc), }; # use Data::Dumper; # die(Dumper($func)); } elsif($plot =~ /^(([xy])\s*=\s*)/) { my($x, $y) = split(/=/, $plot); ($x, $y) = ($y, $x) if($2 eq 'x'); ($x, $y) = map { s/[xy]/t/g; gnuplotise($_); } ($x, $y); $func = { x => $x, y => $y }; } else { die("$plot: You what?"); } push @datasets, Chart::Gnuplot::DataSet->new( func => $func, style => 'lines', # color => 'red', ) if($func); } if($q->{extradata}) { push @datasets, Chart::Gnuplot::DataSet->new( xdata => do { my $i = 0; [grep { $i++ % 2 == 0 } split(/,/, $q->{extradata})] }, ydata => do { my $i = 0; [grep { $i++ % 2 == 1 } split(/,/, $q->{extradata})] }, style => 'lines', # color => 'blue', ); } $chart->plot2d(@datasets) || die("Can't plot"); open(my $fh, $tempfile) || die("Can't read $tempfile: $!"); local $/; print "Content-type: image/png\n\n".<$fh>; close($fh); unlink($tempfile); sub gnuplotise { my $term = shift; $term =~ s/\^/**/g; $term =~ s/(\d)([a-z])/$1*$2/g; return $term; }