Create Charts using PHP and MongoDB

PHP is one of the most widely used server side languages. We’ve already got detailed documentation on how PHP can be easily integrated with FusionCharts using the FusionCharts-PHP wrapper. We also had a recent blog post on how you can use the Zend Framework (a cluster of PHP packages) with FusionCharts.

PHP is fairly easy to work with, w.r.t its support for many database technologies. However, there is some work involved when using PHP with a NoSQL database.

However, before we move to talking about that, it is important that we brush up on what is NoSQL, for there will be many of us who aren’t familiar with this database technology.

Let’s first talk about:

  • What is NoSQL?
  • Why people use NoSQL?

Here goes the answers…

NoSQL also referred to as ‘NOT ONLY SQL’ or ‘non relational’ database, provides a data storage and retrieval mechanism that is modeled differently from the traditional tabular relations that are used in relational databases. NoSQL is used because it encompasses a wide variety of different database technologies that were developed in response to the demands presented in building modern applications. Top NoSQL databases used today include MongoDB, Redis, Cassandra, CouchDB HBase, Neo4j, and so on.

In this blog post, we’ll explore how to integrate PHP with MongoDB and create a chart using FusionCharts. MongoDB is an open-source, document-oriented database developed by MongoDB Inc. It works on the concept of document and collections, stores data in the JSON format, and can vary in structure as per the requirement. Query access is fast through the MongoDB Query Language. It uses dynamic schemas, meaning that you can create records without first defining the structure, such as the fields or the type of values.

Now that we have the basics sufficiently covered, let’s get started…

To get the code in this blog working, we need to install the following components:

Make sure that all the above mentioned components are present in the system and are working as expected.

Next, we move on to how you can create the database

Step 1: Execute the command “mongod” using the shell to run the mongodb server.

mongod

Step 2: Create a json file that contains the data that goes into the database and will used as the source data for the chart. We’ll name this file as fuel_price.json.

Step 3: Import the .json file created in Step 2 into mongodb using the command shown below.

mongoimport -d database_name -c collection_name –type json –file complete path for the json file –jsonArray

importData

Step 4: Next, open another shell to run the command “mongo”.

mongo

Now we need to verify if our data has been imported correctly into the database or no. To do this, you need to execute the following commands:

  • Use the “show dbs” command to see your database. A list of the databases present is shown.
  • From this list, select the required database using the “use database_name” command.
  • Next, use the “show collections” command, which shows the collections inside your database.
  • For seeing the contents of a specific collection, execute the “db.collection_name.find()” command .

mongoDatabase

With this, we are done with creating the database.

Now lets move to the coding part that is a three-step process with, multiple sub-steps.

STEP I (Configuration)

Create a file and save it as config.php inside the project folder.

  • Include the ‘autoload.php’ file that is required for using mongodb with php.
  • Next, we need to create the connection between php and mongoDB.

After completion of the configuration, the php code will look like this.

STEP II

  • Extract the FusionCharts Suite XT zip and keep all the script files in a new folder inside the project folder, as shown below. Here, we have named the new folder as fusioncharts.
  • fcLib

  • Extract the fusioncharts PHP wrapper and keep the fusioncharts.php file inside the same folder created in the previous step for keeping the script files (for our example, fusioncharts).

STEP III (Main Page)

  • Create a file multiseries_mongodb.php, because we will be creating a multi-series chart using mongodb. Include the config.php file inside this file, using the command shown below:
  • Include the ‘fusioncharts.php’ file that contains functions to embed fusioncharts in php.
  • Add the fusioncharts script file.
  • Retrieve data from the database: Include the php code within the body tag. First, retrieve the data from the database and store it in a variable. Here we are storing the data in a variable called $myObj.

    Here chartData is the collection name in the database myProject.
  • Form an associative array: As $myObj is a mongo cursor, we need to convert it to an associative array.

    Thereafter, sort the associative array in an ascending order (if required).
  • Next, we will create an array that will contain the dataset—the chart attributes and the data—fetched from the database.
  • The associative array created in the above step will now have to be encoded into a json format that is required to render the chart.
  • Now, we create the chart object and render the chart using render() function.
  • Next, we close the php tag and continue with the html part by defining the dom element, where the chart will render.
  • Finally, execute the php code using any browser.
    eg. http://localhost/phpMongodb/multiSeries_mongodb.php

    Your output should be like the image shown below:

msLineChart

If you see any errors in your code, click here to download the complete source code of the sample project we have created for this tutorial.

Leave a Comment