rgraph logophp-RGraph is an implementation to use RGraph via php-interface. Not only for php-developers it eases the use of RGraph and extends RGraph with all the features php can offer. The interfaces even allows writing pure php-code to create attractive charts without the need having skills for javascript programming.

 

Main purpose of charts is visualization of data. With php-RGraph you get all the functions to retrieve and use data from mySql-databases, from local files, data from remote or local server, from csv-files and even from Google's sheets. To visualize this data right now php-RGraph "knows" about 450 different chart properties for all charts RGraph has offered and uses this knowledge to build a stable interface to RGraph's javascript libraries.

 

php_RGraph Howto Guide

How to use php-RGraph

This guide is should help to understand the structure of php-RGraph and give you some advice howto use php-RGraph on your website.

This Guide has three sections
  1. structure of php-RGraph charts
  2. Using templates
  3. Using php-RGraph on your website

With RGraph you build charts by writing javascript code where as with php-Graph you code your chart objects purely with php. We will now go step by step showing what to do to make php-RGraph run on your site.

structure of php-RGraph charts

Installation of php-RGraph

Installation is straight forward. Once you have downloaded installation file "php-rgraph.zip", unzip it and upload all folders and files to your website into an accessible directory (or if you are using XAMPP, copy the folders and files into accessible directory). "Accessible" means that you may access all files via webserver (e.g. Apache) and browser. Please keep directory structure and naming of php-RGraph (do not rename any folders).

Then check if you can access php-RGraph's documentation using url: "http://your-host/your-install-directory/RGraph/index.html". If this is OK, you may read RGraph's reference and try out the provided samples.

Structure of php-RGraph chart scripts

To give you an idea how all works, you should check some of the provided samples. (please use url "http://your-host/your-install-directory/RGraph/doc/samples.html"). The samples page gives you the option to display a sample and see the php-code for creating the sample. This code illustrates the structur of php-RGraph. A php chart script always has this components:
  1. Define environment
  2. Define chart layout
  3. Define chart's data
  4. Define RGraph chart properties
  5. Build json object for RGraph
  6. Use template for rendering the chart (html creation)

Define environment

Every chart script must know, where resources for charts are located. This is done with embedding (including) script "environment.php". If this script does not work correctly charts cannot be created. See the layout of "environment.php", which shows what information is needed:
<?php
// php-Rgraph root directory
$rgraph_root = "/your-install-directory/RGraph";

// php-Rgraph library
$lib = $_SERVER['DOCUMENT_ROOT'] . $rgraph_root . "/lib/rgraph_chart.php";
if (!file_exists($lib))
 die ("Error: $lib not found");
require_once ($lib);

// directory for templates
$templates =  $_SERVER['DOCUMENT_ROOT'] . $rgraph_root . "/templates/";
if (!file_exists($templates))
    die ("Error: $templates not found");

// directory for RGraphs's javascript library
$rgraph_libraries = $rgraph_root . "/libraries/";

// (optional) directory for Utilities
$utility =   $_SERVER['DOCUMENT_ROOT'] . $rgraph_root . '/lib/util.php';
Note: Variable naming is important for using templates.

Define chart layout

With four lines of code you define major layout of you chart:
$template = "default.php";
$draw_option = "draw();";
$width = "500";
$height = "350";
You define width ("$width") and ("height") of the canvas and what method RGraph should use to draw the chart ("$draw_option"). And you must specify what template is used for rendering the chart.

Define chart's data

You may define chart's data coming from various sources:
  • define an array of numerical data direcly within your chart script
  • retrieve data from file
  • retrieve data from local or remote server
  • retrieve data from MySql database
  • retrieve data from csv file
  • retrieve data from Google sheets
  • generate data via math formula
php-class rgraph_chart has various methods to support you in providing data for your chart. For more information see the reference guide or have a look at the samples.

Define RGraph chart properties

First step is to define a rgraph_chart object:
$chart = new rgraph_chart($id, $data, $chart_type);
  • "$id" is required and will define the canvas id (e.g. "cvs").
  • "$data" defines the data to be used for charts
  • "$chart_type" sets the desired chart type (e.g. "Pie")
Once you have created a rgraph_chart object you may create a rgraph_options object and define chart properties.
 $options = new rgraph_options($ini-file);
 $options->set_option("title", "My first chart);
  ...
With optional parameter "$ini_file" you may specify an ini-file with pre-defined properties (e.g. font-sizes, colors, font-families, ...etc).

php-RGraph "knows" about 450 different chart properties for almost all charts RGraph has offered. php-class rgraph_options has various methods to support you in providing chart properties, for more information see the reference guide, have a look at the samples or or visit the home of RGraph.

Build json object for RGraph

Once you have created the chart object and its properties you first define the properties ($options object) to the chart object and then create a json object to be used with RGraph:
$chart->set_options($options);
$rgraph_json = $chart->toPrettyString();

Use template for rendering the chart

This is an easy task as the template's directory has been defined with "environment.php" and the template name has been defined already. So your code looks like:
 include_once ($templates . $template);

 

Download php-RGraph Package

php-RGraph is free software - however you have to adhere to the GPL-license conditions to use it. Please learn more about GPL and free software at GNU Org.

To download php-RGraph please click the download button:

Version 0.0.1 (Build Level 0.0.1.0 - May 13th 2016)

 download php-RGraph Package

 

Acknowledgements

Without the excellent work done by the developers of RGraph my development of php-RGraph would not have been possible. Therefore many thanks to the RGraph development team! - they have produced an outstanding piece of work.