Create a Gauge 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 gauge using the FusionCharts PHP
wrapper.
Installation
In this section, we will show you how to install FusionCharts Suite XT and the FusionCharts PHP
wrapper and all the other dependencies on your system.
The FusionCharts PHP server-side wrapper requires PHP 5.6 or higher.
To install the FusionCharts Suite, follow the steps below:
Include the FusionCharts JavaScript files, which can be downloaded from here.
Copy and paste the
fusioncharts.php
file fromintegrations > 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 gauge
Gauges are powerful tools that can showcase information using a radial or linear scale to display data.
To start with, we'll build a simple angular gauge showcasing Nordstrom's Customer Satisfaction Score as shown below.
FusionCharts Suite has 95+ chart types for you to explore. Find the complete list of chart types here .
The angular gauge is shown below:
Chart data
The thresholds for the above sample is shown in the table below:
Range | Color | Hex Code |
---|---|---|
0-50 | Red | #F2726F |
50-75 | Yellow | #FFC533 |
75-100 | Green | #62B58F |
So, any score less than 50 is bad and is red. Any score between 50 and 75 is average and is yellow. Any score above 75 means good and are green.
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": "Nordstrom's Customer Satisfaction Score for 2017",
"lowerLimit": "0",
"upperLimit": "100",
"showValue": "1",
"numberSuffix": "%",
"theme": "fusion",
"showToolTip": "0"
},
// Chart Data
"colorRange": {
"color": [
{
"minValue": "0",
"maxValue": "50",
"code": "#F2726F"
},
{
"minValue": "50",
"maxValue": "75",
"code": "#FFC533"
},
{
"minValue": "75",
"maxValue": "100",
"code": "#62B58F"
}
]
},
"dials": {
"dial": [
{
"value": "81"
}
]
}
}
In the above JSON:
Create the
chart
object to define the elements of the gauge.Create the
colorRange
object to set the color associated with the specific range of values.Specify
minValue
andmaxValue
within thecolor
array under thecolorRange
object.Set the
code
attribute to specify the hex color of respective ranges.Create the
dials
object to represent the customer satisfaction score.Create the
dial
object underdials
object to set the value of customer satisfaction score.
The chart object and the respective arrays contain a set of key-value pairs known as attributes
. These attributes are used to set the functional and cosmetic properties of the gauge.
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:
Include the FusionCharts PHP wrapper in your project.
Include the
fusioncharts
library.Include the FusionCharts theme file to apply the style to the charts.
Store the chart configurations in an associative array.
Store the color range in a multidimensional array.
Store the value of the
dial
in an array.Create a data object within the chart configurations to assign the associative data array to it.
Convert the final chart configuration to JSON string.
Create the chart instance and set the following:
Set the chart type as
angulargauge
. Each gauge is represented with a unique alias. For Angular Gauge, the alias isangulargauge
. Find the complete list of gauges with their respective alias here.Set the gauge
id
.Set the
width
andheight
(in pixels).Set the container for the gauge.
Set the
dataFormat
as JSON.Embed the
json
data as the value of thedataSource
.
Finally, use a container using
<div>
to render the gauge.
The full code for the above sample is given 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 Widget</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
// Widget appearance configuration
\$arrChartConfig = array(
"chart" => array(
"caption" => "Nordstrom's Customer Satisfaction Score for 2017",
"lowerLimit" => "0",
"upperLimit" => "100",
"showValue" => "1",
"numberSuffix" => "%",
"theme" => "fusion",
"showToolTip" => "0"
)
);
// Widget color range data
$colorDataObj = array("color" => array(
["minValue" => "0", "maxValue" => "50", "code" => "#F2726F"],
["minValue" => "50", "maxValue" => "75", "code" => "#FFC533"],
["minValue" => "75", "maxValue" => "100", "code" => "#62B58F"]
));
// Dial array
$dial = array();
// Widget dial data in array format, multiple values can be separated by comma e.g. ["81", "23", "45",...]
$widgetDialDataArray = array("81");
for($i = 0; $i < count($widgetDialDataArray); $i++) {
array_push($dial,array("value" => $widgetDialDataArray[$i]));
}
$arrChartConfig["colorRange"] = $colorDataObj;
$arrChartConfig["dials"] = array( "dial" => $dial);
// JSON Encode the data to retrieve the string containing the JSON representation of the data in the array.
$jsonEncodedData = json_encode($arrChartConfig);
// Widget object
$Widget = new FusionCharts("angulargauge", "MyFirstWidget" , "400", "250", "widget-container", "json", $jsonEncodedData);
// Render the Widget
$Widget->render();
?>
<center>
<div id="widget-container">Widget 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 Widget</title>
// Include FusionCharts core file
<script src="path/to/local/fusioncharts.js"></script>
// Include FusionCharts Theme file
<script src="path/to/local/themes/fusioncharts.theme.fusion.js"></script>
</head>
<body>
<?php
// Widget appearance configuration
\$arrChartConfig = array(
"chart" => array(
"caption" => "Nordstrom's Customer Satisfaction Score for 2017",
"lowerLimit" => "0",
"upperLimit" => "100",
"showValue" => "1",
"numberSuffix" => "%",
"theme" => "fusion",
"showToolTip" => "0"
)
);
// Widget color range data
$colorDataObj = array("color" => array(
["minValue" => "0", "maxValue" => "50", "code" => "#F2726F"],
["minValue" => "50", "maxValue" => "75", "code" => "#FFC533"],
["minValue" => "75", "maxValue" => "100", "code" => "#62B58F"]
));
// Dial array
$dial = array();
// Widget dial data in array format, multiple values can be separated by comma e.g. ["81", "23", "45",...]
$widgetDialDataArray = array("81");
for($i = 0; $i < count($widgetDialDataArray); $i++) {
array_push($dial,array("value" => $widgetDialDataArray[$i]));
}
$arrChartConfig["colorRange"] = $colorDataObj;
$arrChartConfig["dials"] = array( "dial" => $dial);
// JSON Encode the data to retrieve the string containing the JSON representation of the data in the array.
$jsonEncodedData = json_encode($arrChartConfig);
// Widget object
$Widget = new FusionCharts("angulargauge", "MyFirstWidget" , "400", "250", "widget-container", "json", $jsonEncodedData);
// Render the Widget
$Widget->render();
?>
<center>
<div id="widget-container">Widget 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.