Giter Site home page Giter Site logo

gnuplot's Introduction

GnuPlot

A PHP Library for using GnuPlot

WARNING: This invoke the gnuplot command line as back-end, which can lead to arbitrary code execution. Be careful if you intend to use this library with user-provided information. Have a look at this post for more information.

This is the output of the demo/write.php:

gnuplot

Requirements

You need to have a server with gnuplot installed and the safe mode disabled (to be able to run proc_open())

Usage

There is examples in the demo/ directory.

You can create a graph and populate it like this:

<?php

use Gregwar\GnuPlot\GnuPlot;

$plot = new GnuPlot;

// Setting the main graph title
$plot->setGraphTitle('Demo graph');

// Adding three points to the first curve
$plot
    ->setTitle(0, 'The first curve')
    ->push(0, 4)
    ->push(1, 5)
    ->push(2, 6)
    ;

// Adding three points on the other curve
// (with index 1)
$plot
    ->setTitle(1, 'The first curve')
    ->push(0, 8, 1)
    ->push(1, 9, 1)
    ->push(2, 10, 2)
    ;

You can then save it to a file, have a look to write.php for example:

<?php

// Write the graph to out.png
$plot->writePng('out.png');

Or render it directly into a browser, you can try out.php for example:

<?php

header('Content-type: image/png');
echo $plot->get();

Or display it on the screen (useful with CLI scripts), run the demo.php script for example:

<?php

$plot->display();

Or display it, and re-feed it in real time (with CLI scripts), you can run realTime.php for example:

<?php

$plot->refresh();

API

  • push($x, $y, $index=0), add a point to the $index-nth curve
  • display(), renders the graph on the screen (asuming you are using it as a CLI with an X Server
  • refresh(), same as display(), but will replot the graph after the first call
  • get(), gets the PNG data for your image
  • writePng($filename), write the data to the output file
  • setTitle($index, $title), sets the title of the $index-nt curve
  • setGraphTitle($title), sets the main title for the graph
  • setXTimeFormat($format), sets the X axis as a time axis and specify data format
  • setXTimeFormatString($format), specify the X axis time presentation format
  • setXLabel($text), sets the label for the X axis
  • setYLabel($text), sets the label for the Y axis
  • setYFormat($format), sets Y axis formatting
  • setXRange($min, $max), set the X min & max
  • setYRange($min, $max), set the Y min & max
  • setWidth($width), sets the width of the graph
  • setHeight($height), sets the width of the graph
  • addLabel($x, $y, $text), add some label at a point

License

Gregwar\GnuPlot is under MIT license

gnuplot's People

Contributors

bobemoe avatar breart avatar gianmarcotoso avatar gregwar avatar martinlindhe avatar mre avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

gnuplot's Issues

get function has wrong variable for height

$this->sendCommand("set terminal $format size {$this->width}{$this->unit}, {$this->width}{$this->unit}");
Should be
$this->sendCommand("set terminal $format size {$this->width}{$this->unit}, {$this->height}{$this->unit}");

Add ability for gnuplot to display/send back errors

Hi,
While trying out GnuPlot, I faced following issue:

  • gnuplot was not correctly installed. However there was no error thrown. I looked in logs, even enabled display_errors, yet no output.
  • Finally I got gnuplot working and using php write.php from command line worked, but running it via web failed. Again, I got no output from script to let me know there was an error somewhere.

Could errors from running gnuplot be passed back to the user, please?

Empty file download

I've noticed that if you send a file created through the write method as the payload of a response, it's returned empty. This is because the gnuplot process is still running and has not yet closed the file descriptor (at least on my configuration, Ubuntu 14.04 running on VirtualBox).

I've solved this by moving what's in the __construct and __destruct methods inside two new methods, and calling them at the end of the write function (as well as in the constructor and the destructor). Like this:

public function write($terminal, $file)
    {
        $this->sendInit();
        $this->sendCommand("set terminal $terminal size {$this->width}{$this->unit}, {$this->height}{$this->unit}");
        $this->sendCommand('set output "'.$file.'"');
        $this->plot();

                // Cleanup and re-init
        $this->cleanup();
        $this->initialize();
    }

Of course, this means that all the commands need to be sent again after each write, so I'm checking in before making a pull request to make sure this is ok.

On a sidenote, I've already added line width support for individual lines and I'll be pushing it alongside this :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.