# Advanced Graphs and Charts for PHP

- Graph Home
- Pricing / License Options
- Online Demos
- Documentation
- Tutorials

## Plotting Data from a Database

In this example the graph is configured to read and plot data directly from a database.

This example requires that the 'Sample Application' has been set up. Setting up the Sample Application will create a database populated with data for use by this example. If you have not yet set up the Sample Application then you can do so here.

The graph above is produced using the "Database Info" method with the following IMG tag:-

<img src="../graph/vertical-bar-graph.php? dbinfo=../sampleApplication/dataQueries/salesByMonthDB.php& config=../sampleApplication/graphConfig/salesByMonthConfig.txt" width="700" height="400" />

The graph data is read directly from the database using the information contained in the file:-

/sampleApplication/dataQueries/salesByMonthDB.php // Data Queries $jpDatabase["data"] = array(); // Blue Robots $jpDatabase["data"][0]["query"] = "SELECT SUM(amount) AS monthAmount FROM salesByMonth WHERE saleYear=2008 AND product_id = '1' GROUP BY saleMonth ORDER BY saleMonth "; $jpDatabase["data"][0]["valueField"] = "monthAmount"; // Green Robots $jpDatabase["data"][1]["query"] = "SELECT SUM(amount) AS monthAmount FROM salesByMonth WHERE saleYear=2008 AND product_id = '2' GROUP BY saleMonth ORDER BY saleMonth "; $jpDatabase["data"][1]["valueField"] = "monthAmount"; // Red Robots $jpDatabase["data"][2]["query"] = "SELECT SUM(amount) AS monthAmount FROM salesByMonth WHERE saleYear=2008 AND product_id = '3' GROUP BY saleMonth ORDER BY saleMonth "; $jpDatabase["data"][2]["valueField"] = "monthAmount"; // Yellow Robots $jpDatabase["data"][3]["query"] = "SELECT SUM(amount) AS monthAmount FROM salesByMonth WHERE saleYear=2008 AND product_id = '4' GROUP BY saleMonth ORDER BY saleMonth "; $jpDatabase["data"][3]["valueField"] = "monthAmount";

With this method queries for each series of data are entered into the $jpDatabase["data"] array. At page view time the graphing software will dynamically include this file and execute the queries to produce the graph in real-time.

The settings and styles are set from the information contained in the file:-

/sampleApplication/graphConfig/salesByMonthConfig.txt

If the graph above does not display then see the Troubleshooting guide for details on how to resolve the problem.

If the graph above does display but no data is shown then the most likely cause is that the graph is having trouble connecting to the database server. Ensure that the 'Sample Application' has been set up with the correct DB User credentials.

If you have not yet set up the Sample Application then you can do so here.

Package Index

Documentation

Troubleshooting Guide

Support

#### Examples

- Graphing Data from files
- Data from external scripts
- Custom data function
- Utilising request parameters
- Plotting data from a database
- Database interface scripts
- Multiple Scales, text and images
- Graphing CSV files

#### Basic Chart Styles

- Pie Charts
- X-Y Scatter Graphs
- Bubble Charts
- Area Graphs
- Line Graphs

#### Bar Graphs

- Vertical Bar Graphs
- Stacked Vertical Bar Graphs
- Horizontal Bar Graphs
- Stacked Horizontal Bar Graphs

#### Cylinder Charts

- Vertical Cylinder Graphs
- Stacked Vertical Cylinder Graphs
- Horizontal Cylinder Graphs
- Stacked Horizontal Cylinder Graphs

#### Combination Charts

- Area and Stacked Vertical Bar Graph
- Area and Vertical Bar Graph
- Line and Stacked Vertical Bar Graph
- Line and Vertical Bar Graph