Create a Chart Using PHP in FusionCharts

Overview

FusionCharts is a JavaScript charting library that enables you to create interactive charts, gauges, maps and dashboards in JavaScript. We have built a simple server-side PHP wrapper for FusionCharts. The FusionCharts server-side PHP wrapper lets you easily add rich and interactive charts to any PHP project. Using the wrapper, you can create charts in your browsers, without writing any JavaScript code.

In this page, we'll see how to install FusionCharts and render a chart using the FusionCharts server-side PHP wrapper.

Installation

Install FusionCharts and the PHP wrapper component using any of the following methods:

The FusionCharts PHP server-side wrapper requires PHP 5.6 or higher.

  • Include the FusionCharts JavaScript files, which can be downloaded from here.

  • Copy and paste the fusioncharts.php file from integrations > php > fusioncharts-wrapper in your project folder.

  • Include the FusionCharts theme file to apply the style to the charts.


// Include FusionCharts core file
<script type="text/javascript" src="https://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.js"></script>

// Include FusionCharts Theme file
<script type="text/javascript" src="https://cdn.fusioncharts.com/fusioncharts/latest/themes/fusioncharts.theme.fusion.js"></script>


// Include FusionCharts core file
<script type="text/javascript" src="path/to/local/fusioncharts.js"></script>

// Include FusionCharts Theme file
<script type="text/javascript" src="path/to/local/themes/fusioncharts.theme.fusion.js"></script>

That completes the installation of FusionCharts Suite and the PHP wrapper.

Create Your First Chart

Let's create a Column 2D chart using the php-fusioncharts wrapper showing the "Countries With Most Oil Reserves".

FusionCharts Suite has 95+ chart types for you to explore. Find the complete list of chart types here.

FusionCharts will load here..

To understand the chart components, click here.

Chart data

The data to render the above chart is shown in the table below:

Country No. of Oil Reserves
Venezuela 290K
Saudi 260K
Canada 180K
Iran 140K
Russia 115K
UAE 100K
US 30K
China 30K

FusionCharts accepts data in JSON format. Following code is the JSON representation of the above table with the required attributes to render the above chart.

{
    // Chart Configuration
    "chart": {
        "caption": "Countries With Most Oil Reserves [2017-18]",
        "subCaption": "In MMbbl = One Million barrels",
        "xAxisName": "Country",
        "yAxisName": "Reserves (MMbbl)",
        "numberSuffix": "K",
        "theme": "fusion",
    },
    // Chart Data
    "data": [{
        "label": "Venezuela",
        "value": "290"
    }, {
        "label": "Saudi",
        "value": "260"
    }, {
        "label": "Canada",
        "value": "180"
    }, {
        "label": "Iran",
        "value": "140"
    }, {
        "label": "Russia",
        "value": "115"
    }, {
        "label": "UAE",
        "value": "100"
    }, {
        "label": "US",
        "value": "30"
    }, {
        "label": "China",
        "value": "30"
    }]
}

Different types of charts in FusionCharts expect different JSON formats, based on their grouping. Explore different JSON formats, for example, single-series,multi-series, combination charts.

In the above JSON data:

  • Create the chart object to define the elements of the chart.

  • Set the caption and subcaption of the chart.

  • Set the value of xAxisName attribute to Country(first column of the table).

  • Set the value of yAxisName attribute to Reserves(second column of the table).

  • In the data array, create objects for each row and specify the label attribute to represent the Country. For example, Venezuela.

  • Similarly, specify the value attribute to set the value of Oil Reserves in respective countries. For example, 290K for Venezuela.

  • Set the numberSuffix attribute to set the unit of the values.

  • Set the theme attribute to apply the predefines themes to the chart.

Both the chart object and the data array contain a set of key-value pairs known as attributes. These attributes are used to set the functional and cosmetic properties of the chart.

Now that you have the data in JSON format, let's see how to render the chart.

Render the Chart

To render the chart, follow the steps below:

  1. Include the FusionCharts PHP wrapper in your project.

  2. Include the fusioncharts library.

  3. Include the FusionCharts theme file to apply the style to the charts.

  4. Store the chart configurations in an associative array.

  5. Store the data in a multidimensional array.

  6. Create an associative array with label and values derived from the data array.

  7. Create a data object within the chart configurations to assign the associative data array to it.

  8. Convert the final chart configuration to JSON string.

  9. Create the chart instance and set the following:

    • Set the chart type as column2d. Each chart type is represented with a unique chart alias. For Column 2D chart, the alias is column2d. Find the complete list of chart types with their respective alias here.

    • Set the chart id.

    • Set the width and height (in pixels).

    • Set the container for the chart.

    • Set the dataFormat as JSON.

    • Embed the json data as the value of the dataSource.

  10. Finally, create a container using <div> to render the chart.

The consolidated code is shown below:


<?php

   /* Include the ../src/fusioncharts.php file that contains functions to embed the charts.*/
   include("includes/fusioncharts.php");
?>
<html>

<head>
    <title>FusionCharts | My First Chart</title>

    // Include FusionCharts core file
    <script src="https://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.js"></script>

    // Include FusionCharts Theme File
    <script src="https://cdn.fusioncharts.com/fusioncharts/latest/themes/fusioncharts.theme.fusion.js"></script>
</head>

<body>
    <?php
    // Chart Configuration stored in Associative Array
    $arrChartConfig = array(
        "chart" => array(
            "caption" => "Countries With Most Oil Reserves [2017-18]",
            "subCaption" => "In MMbbl = One Million barrels",
            "xAxisName" => "Country",
            "yAxisName" => "Reserves (MMbbl)",
            "numberSuffix" => "K",
            "theme" => "fusion"
        )
    );
    // An array of hash objects which stores data
    $arrChartData = array(
        ["Venezuela", "290"],
        ["Saudi", "260"],
        ["Canada", "180"],
        ["Iran", "140"],
        ["Russia", "115"],
        ["UAE", "100"],
        ["US", "30"],
        ["China", "30"]
    );

    $arrLabelValueData = array();

    // Pushing labels and values
    for($i = 0; $i < count($arrChartData); $i++) {
        array_push($arrLabelValueData, array(
            "label" => $arrChartData[$i][0], "value" => $arrChartData[$i][1]
        ));
    }

    $arrChartConfig["data"] = $arrLabelValueData;

    // JSON Encode the data to retrieve the string containing the JSON representation of the data in the array.
    $jsonEncodedData = json_encode($arrChartConfig);

    // chart object
    $Chart = new FusionCharts("column2d", "MyFirstChart" , "700", "400", "chart-container", "json", $jsonEncodedData);

    // Render the chart
    $Chart->render();
    ?>

    <center>
        <div id="chart-container">Chart will render here!</div>
    </center>
</body>
</html>


<?php

   /* Include the ../src/fusioncharts.php file that contains functions to embed the charts.*/
   include("includes/fusioncharts.php");
?>
<html>

<head>
    <title>FusionCharts | My First Chart</title>

    // Include the FusionCharts core file
    <script src="path/to/local/fusioncharts.js"></script>

    // Include the FusionCharts Theme file
    <script src="path/to/local/themes/fusioncharts.theme.fusion.js"></script>
</head>

<body>
    <?php
    $arrChartConfig = array(
        "chart" => array(
            "caption" => "Countries With Most Oil Reserves [2017-18]",
            "subCaption" => "In MMbbl = One Million barrels",
            "xAxisName" => "Country",
            "yAxisName" => "Reserves (MMbbl)",
            "numberSuffix" => "K",
            "theme" => "fusion"
        )
    );
    // An array of hash objects which stores data
    $arrChartData = array(
        ["Venezuela", "290"],
        ["Saudi", "260"],
        ["Canada", "180"],
        ["Iran", "140"],
        ["Russia", "115"],
        ["UAE", "100"],
        ["US", "30"],
        ["China", "30"]
    );

    $arrLabelValueData = array();

    // Pushing labels and values
    for($i = 0; $i < count($arrChartData); $i++) {
        array_push($arrLabelValueData, array(
            "label" => $arrChartData[$i][0], "value" => $arrChartData[$i][1]
        ));
    }

    $arrChartConfig["data"] = $arrLabelValueData;

    // JSON Encode the data to retrieve the string containing the JSON representation of the data in the array.
    $jsonEncodedData = json_encode($arrChartConfig);

    // chart object
    $Chart = new FusionCharts("column2d", "MyFirstChart" , "700", "400", "chart-container", "json", $jsonEncodedData);

    // Render the chart
    $Chart->render();
    ?>

    <center>
        <div id="chart-container">Chart will render here!</div>
    </center>
</body>
</html>

That's it! Your first chart using the FusionCharts PHP wrapper is ready.

Problem rendering the chart?

In case there is an error, and you are unable to see the chart, check for the following:

  • If you are getting a JavaScript error on your page, check your browser console for the exact error and fix accordingly. If you're unable to solve it, click here to get in touch with our support team.

  • If the chart does not show up at all, but there are no JavaScript errors, check if the FusionCharts Suite XT JavaScript library has loaded correctly. You can use developer tools within your browser to see if fusioncharts.js was loaded.

  • If you get a Loading Data or Error in loading data message, check whether your JSON data structure is correct, or there are conflicts related to quotation marks in your code.