Chart-Graph

 view release on metacpan or  search on metacpan

Graph/Gnuplot.pm  view on Meta::CPAN

	    $plot_file = _make_tmpfile("plot", "png");
	    print $handle "set output \"$plot_file\"\n";
	}
	print $handle "set terminal png small\n";
    } elsif ($output_type eq "tgif") {
	if (defined $output_file) {
	    $plot_file = _make_tmpfile("plot", "obj");
	    print $handle "set output \"$plot_file\"\n";
	}
	print $handle "set terminal tgif\n";
    } elsif ($output_type eq 'svg') {
	if (defined $output_file) {
	    $plot_file = _make_tmpfile("plot", "svg");
	    print $handle "set output \"$plot_file\"\n";
	}
	print $handle "set terminal svg\n";
    }

    # process data sets
    print $handle "plot ";
    while (@data_sets) {
	
	$data_set_ref = shift @data_sets;
	
	if (ref($data_set_ref) ne "ARRAY") {
	    carp "Data set must be an array";
	    $handle->close();
	    _cleanup_tmpdir();
	    return 0;
	}
	
	if (not _gnuplot_data_set($handle, @{$data_set_ref})) {
	    ## already printed error message
	    $handle->close();
	    _cleanup_tmpdir();
	    return 0;
	}
	
	if (@data_sets) {
	    print $handle ", ";
	}
    }	
    
    $handle->close();
	
    # gnuplot and convert pbm file to gif
    if (not _exec_gnuplot($command_file)) {
	_cleanup_tmpdir();
	return 0;
    }
    
    if ($output_type eq "gif") {
	if(not _exec_pbmtogif($plot_file, $output_file)) {
	    _cleanup_tmpdir();
	    return 0;
	}
    } elsif (defined $output_file && $output_type =~ /^(pbm|eps(?: .*)?|png|tgif)$/) {
	#try to get rid of the ugly warnings when moving a file on freebsd
	if ($^O eq 'freebsd') {
	    eval { #this is opportunistic, if it fails it doesn't really matter
		my $gid = `/usr/bin/id -g`;
		chomp($gid);
		system('/usr/bin/chgrp',$gid,$plot_file);
	    };
	}

	my $status = system("mv", "$plot_file", "$output_file");

        if (not _chk_status($status)) {
	    if ($Chart::Graph::debug) {
		print STDERR "Couldn't mv $plot_file to $output_file: $!\n";
	    }
	    _cleanup_tmpdir();
	    return 0;
	}
    } 
    _cleanup_tmpdir();
    return 1;
}

#
#
# Subroutine: gnuplot_data_set()
# 
# Description: this functions processes the X number
#              of data sets that a user gives as 
#              arguments to gnuplot(). Again, please
#              see http://www.caida.org/Tools/Graph/
#              for the format of the dataset.
#


 
sub _gnuplot_data_set {
    my ($handle, $user_data_opts_ref, @data) = @_;
    my (%data_opts);
    
    # set these values with empty string because we print them out later 
    # we don't want perl to complain of uninitialized value. 
    my ($title, $style, $axes, $ranges, $type,) = ("", "", "", "", "");
    my ($using) = ("");
    my $result;
    my $filename = _make_tmpfile("data");
    
    ## check first arg for hash
    if (ref($user_data_opts_ref) ne "HASH") {
	carp "Data options must be a hash.";
	return 0;
    }

    # call to combine user options with default options   
    %data_opts = _mesh_opts($user_data_opts_ref, \%def_gnu_data_opts);

    # write data options to command file
    while (my ($key, $value) = each %data_opts) {
	
        if ($key eq "using") {
            $using = "using $value";
        }

	if ($key eq "title") {
	    $title = "title \"$value\"";
	}



( run in 0.884 second using v1.01-cache-2.11-cpan-5735350b133 )