| 1 | |||
| 2 | =head1 NAME | ||
| 3 | |||
| 4 | App::Basis::ConvertText2::Plugin::Gnuplot | ||
| 5 | |||
| 6 | =head1 SYNOPSIS | ||
| 7 | |||
| 8 | my $content = "# | ||
| 9 | # $Id: surface1.dem,v 1.11 2004/09/17 05:01:12 sfeam Exp $ | ||
| 10 | # | ||
| 11 | set samples 21 | ||
| 12 | set isosample 11 | ||
| 13 | set xlabel "X axis" offset -3,-2 | ||
| 14 | set ylabel "Y axis" offset 3,-2 | ||
| 15 | set zlabel "Z axis" offset -5 | ||
| 16 | set title "3D gnuplot demo" | ||
| 17 | set label 1 "This is the surface boundary" at -10,-5,150 center | ||
| 18 | set arrow 1 from -10,-5,120 to -10,0,0 nohead | ||
| 19 | set arrow 2 from -10,-5,120 to 10,0,0 nohead | ||
| 20 | set arrow 3 from -10,-5,120 to 0,10,0 nohead | ||
| 21 | set arrow 4 from -10,-5,120 to 0,-10,0 nohead | ||
| 22 | set xrange [-10:10] | ||
| 23 | set yrange [-10:10] | ||
| 24 | splot x*y | ||
| 25 | " ; | ||
| 26 | my $params = {} ; | ||
| 27 | my $obj = App::Basis::ConvertText2::Plugin::Gnuplot->new() ; | ||
| 28 | my $out = $obj->process( 'gnuplot', $content, $params) ; | ||
| 29 | |||
| 30 | =head1 DESCRIPTION | ||
| 31 | |||
| 32 | convert a gnuplot text string into a PNG, requires gnuplot program http://gnuplot.sourceforge.net/ | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | # ---------------------------------------------------------------------------- | ||
| 37 | |||
| 38 | package App::Basis::ConvertText2::Plugin::Gnuplot; | ||
| 39 | $App::Basis::ConvertText2::Plugin::Gnuplot::VERSION = '0.4'; | ||
| 40 | use 5.10.0; | ||
| 41 | use strict; | ||
| 42 | use warnings; | ||
| 43 | use Path::Tiny; | ||
| 44 | use Moo; | ||
| 45 | use Image::Resize ; | ||
| 46 | use App::Basis; | ||
| 47 | use App::Basis::ConvertText2::Support; | ||
| 48 | use namespace::autoclean; | ||
| 49 | |||
| 50 | has handles => ( | ||
| 51 | is => 'ro', | ||
| 52 | init_arg => undef, | ||
| 53 | default => sub {[qw{gnuplot}]} | ||
| 54 | ); | ||
| 55 | |||
| 56 | # gnuplot is a script to run plantgnuplot basically does java -jar plantgnuplot.jar | ||
| 57 | use constant GNUPLOT => "gnuplot"; | ||
| 58 | |||
| 59 | # ---------------------------------------------------------------------------- | ||
| 60 | |||
| 61 | =item gnuplot | ||
| 62 | |||
| 63 | create a simple gnuplot image | ||
| 64 | |||
| 65 | parameters | ||
| 66 | data - gnuplot text | ||
| 67 | filename - filename to save the created image as | ||
| 68 | |||
| 69 | hashref params of | ||
| 70 | title - title to use for image alt attribute | ||
| 71 | size - size of image, widthxheight - optional, default 720x512 | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | sub process { | ||
| 76 | my $self = shift; | ||
| 77 | my ( $tag, $content, $params, $cachedir ) = @_; | ||
| 78 | $params->{size} ||= "720x512"; | ||
| 79 | my ( $x, $y ) = ( $params->{size} =~ /^\s*(\d+)\s*x\s*(\d+)\s*$/ ); | ||
| 80 | |||
| 81 | # strip any ending linefeed | ||
| 82 | chomp $content; | ||
| 83 | return "" if ( !$content ); | ||
| 84 | |||
| 85 | # we can use the cache or process everything ourselves | ||
| 86 | my $sig = create_sig( $content, $params ); | ||
| 87 | my $filename = cachefile( $cachedir, "$sig.png" ); | ||
| 88 | if ( !-f $filename ) { | ||
| 89 | my $gnuplotfile = Path::Tiny->tempfile("gnuplotXXXXXXXX"); | ||
| 90 | |||
| 91 | # make sure the output file is not theirs | ||
| 92 | $content =~ s/set output.*$//gsmi ; | ||
| 93 | # set out filename | ||
| 94 | $content = "set output '$filename'\n$content" ; | ||
| 95 | # we want to set the size | ||
| 96 | # strip any size in the data | ||
| 97 | $content =~ s/set term png size.*$//gsmi ; | ||
| 98 | $content = "set term png size $x, $y\n$content" ; | ||
| 99 | |||
| 100 | path($gnuplotfile)->spew_utf8($content); | ||
| 101 | |||
| 102 | my $cmd = GNUPLOT . " $gnuplotfile"; | ||
| 103 | my ( $exit, $stdout, $stderr ) = run_cmd($cmd); | ||
| 104 | if( $exit) { | ||
| 105 | warn "Could not run script " . GNUPLOT . " get it from http://gnuplot.sourceforge.net/" ; | ||
| 106 | } | ||
| 107 | # if we want to force the size of the graph | ||
| 108 | # if ( -f $filename && $x && $y ) { | ||
| 109 | # my $image = Image::Resize->new($filename); | ||
| 110 | # my $gd = $image->resize( $x, $y ); | ||
| 111 | |||
| 112 | # # overwrite original file with resized version | ||
| 113 | # if ($gd) { | ||
| 114 | # path($filename)->spew_raw( $gd->png ); | ||
| 115 | # } | ||
| 116 | # } | ||
| 117 | } | ||
| 118 | my $out; | ||
| 119 | if ( -f $filename ) { | ||
| 120 | |||
| 121 | # create something suitable for the HTML | ||
| 122 | $out = create_img_src( $filename, $params->{title} ); | ||
| 123 | } | ||
| 124 | |||
| 125 | return $out; | ||
| 126 | } | ||
| 127 | |||
| 128 | # ---------------------------------------------------------------------------- | ||
| 129 | |||
| 130 | 1; |