RCHART for Php, User Guide

Copyright 2005, J4L Components (http://www.java4less.com)
Go bak to contents


The API

Chart creation process

We recommend you to create charts using the parameters however if you need it you can create charts using the api (see documentation in the api subdirectory of your installation).

The process is:

1. Reference RChart classes

[Php]
require("Chart.inc");

2. Create the title [Php]
$title=new Title("Sales (thousands $)");
3. Create the axis you need (depends on the chart)

[Php]
$XAxis=new Axis(HORIZONTAL,new Scale());
$YAxis=new Axis(VERTICAL,new Scale());
$XAxis->scale->min=0;
$YAxis->scale->min=0;
$XAxis->scale->max=5;
$YAxis->scale->max=5;
.....

4. Create the legend [Php]
$legend=new Legend();
$legend->addItem("Products",new FillStyle("BLUE"));
$legend->addItem("Services",new FillStyle("RED"));
$legend->background=new FillStyle("WHITE");
$legend->border=new LineStyle(1,"BLACK",LINE_NORMAL);

5. Create the axis labels

[Php]
$XLabel= new HAxisLabel("Month","BLUE",new ChartFont("Default-3",14));

$YLabel= new VAxisLabel("Brutto","BLACK",new ChartFont("Default-3",14));

 

6. create the plotter (or plotters if you combine lines and bars) [Php]
$plot=new LinePlotter3D();
7. create the chart and set properties (labels,legend and size) [Php]
$chart=new Chart($title,$plot,$XAxis,$YAxis);
$chart->XLabel=$XLabel;
$chart->YLabel=$YLabel;
$chart->legend=$legend;
$chart->height=200;
$chart->width=200;
8. create the data series [Php]
$d1=Array(1,1,3,3.5,5,4,2);
$data1= new LineDataSerie(new LineStyle(1,"BLUE",LINE_NORMAL));
$data1->Create2($d1);
9. add serie to chart [Php]
$chart->addSerie($data1);
10. paint chart on an image and output to the browser [Php]

// create image
$g = new ChartGraphics;
$g->create(500,500);

// paint chart
$chart->setSize(500,500);
$chart->paint($g);

// output to the browser
$g->setType("png");
$path="";
$g->outputImage( $path);

 

 

The chartLoader

You can use the ChartLoader to create your charts using the parameters:

[Php]

require("chartLoader.inc");
require("Chart.inc");

// define variable
$chartLoader= new ChartLoader();
// set chart parameters
chartLoader->setParameter("TITLECHART","Sales 2002");
chartLoader->setParameter("XLABEL","Month");
chartLoader->setParameter("YLABEL","Million $");
chartLoader->setParameter("XSCALE_MIN","0");
chartLoader->setParameter("XSCALE_MAX","5.5");
chartLoader->setParameter("YSCALE_MIN","-15");
chartLoader->setParameter("BIG_TICK_INTERVALX","1");
chartLoader->setParameter("BIG_TICK_INTERVALY","10");
chartLoader->setParameter("XAXIS_LABELS","June|July|Aug.|Sept.|Oct.|Nov.|Dec.");
chartLoader->setParameter("CERO_XAXIS","LINE");
chartLoader->setParameter("YAXIS_INTEGER","TRUE");
chartLoader->setParameter("SERIE_1","Products");
chartLoader->setParameter("SERIE_2","Services");
chartLoader->setParameter("SERIE_TYPE_1","BAR");
chartLoader->setParameter("SERIE_TYPE_2","BAR");
chartLoader->setParameter("SERIE_FONT_1","Arial|8");
chartLoader->setParameter("SERIE_FONT_2","Arial|8");
chartLoader->setParameter("SERIE_DATA_1","12|43|50|45|30");
chartLoader->setParameter("SERIE_DATA_2","-10|41|48|39|36");
chartLoader->setParameter("SERIE_BORDER_TYPE_1","RAISED");
chartLoader->setParameter("SERIE_BORDER_TYPE_2","RAISED");
chartLoader->setParameter("SERIE_BAR_STYLE_1","#00ff00");
chartLoader->setParameter("SERIE_BAR_STYLE_2","#0000ff");
chartLoader->setParameter("BARCHART_BARSPACE","1");
chartLoader->setParameter("LEFT_MARGIN","0.15");
chartLoader->setParameter("CHART_FILL","#ffcc00");
chartLoader->setParameter("SERIE_NEGATIVE_STYLE_2","RED");
chartLoader->setParameter("YLABEL_VERTICAL","TRUE");
// create chart
$chart=chartLoader->buildChart();

// create image
$g = new ChartGraphics();
$g->create(500,500);

// paint chart
$chart->setSize(500,500);
$chart->paint($g);

// output to the browser
$g->setType("png");
$path="";
$g->outputImage( $path);

 

You can also read the parameters directly from a file or url:

[php]

// define variable
$chartLoader= new ChartLoader();
// set chart parameters
$chartLoader->loadFromFile("params.txt",true); // true means, clear existing parameters
// create chart
$chart=chartLoader->buildChart();

 

In all cases the steps are:

  1. Create a chart loader..
  2. Set the parameters or load them from file
  3. build the chart
  4. paint chart and output it to the browser

 

Exporting the chart to gif, png or jpg

RChart can create the following image files: JPG, PNG and GIF. This can be done by executing chartGraphics->setFilePath() and chartGraphics->outputImage(filename) after the chart has been painted:

[php]

// define variable
$chartLoader= new ChartLoader();
// set chart parameters
chartLoader->loadFromFile("params.txt",true);
// create chart
$chart=chartLoader->buildChart();


// create image
$g = new ChartGraphics();
$g->create(500,500);

// paint chart
$chart->setSize(500,500);
$chart->paint($g);

// output to file
$filename="chart.png";
$fp="c:\\directory\\";
$g->setFilePath( $fp );
$g->outputImage( $filename );