Create a Gauge Using FusionCharts
FusionCharts Suite XT — the industry's most comprehensive JavaScript charting solution — is all about easing the whole process of data visualization through charts.
In this page, we'll see how to install FusionCharts library and all the other dependencies on your system and render a gauge using Plain JavaScript.
Installation
Install FusionCharts using any of the following steps:
fusioncharts
package via npm run the command below:
$ npm install fusioncharts
- Include the FusionCharts JavaScript files from CDN.
- Include the theme file.
<head>
<!-- Step 1 - Include the fusioncharts core library -->
<script type="text/javascript" src="https://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.js"></script>
<!-- Step 2 - Include the fusion theme -->
<script type="text/javascript" src="https://cdn.fusioncharts.com/fusioncharts/latest/themes/fusioncharts.theme.fusion.js"></script>
</head>
- Include the FusionCharts JavaScript files, which can be downloaded from here.
- Include the FusionCharts theme file to apply style to the charts.
<head>
<!-- Step 1 - Include the fusioncharts core library -->
<script type="text/javascript" src="path/to/local/fusioncharts.js"></script>
<!-- Step 2 - Include the fusion theme -->
<script type="text/javascript" src="path/to/local/themes/fusioncharts.theme.fusion.js"></script>
</head>
That completes the installation of FusionCharts Suite.
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 are 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 shown in red color. Any score between 50 and 75 is average and is shown in yellow color. Any score above 75 means good and is shown in green color.
FusionCharts accepts data in JSON format. The 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 render the gauge.
Render the Gauge
To render the gauge, follow the steps below:
Include the
fusioncharts
library.Include the gauge type.
Include the FusionCharts theme file to apply style to the charts.
Add the gauge and the theme as dependencies to the core.
Store the chart configurations in a JSON object. In this JSON object:
- Set the chart type as
angulargauge
. Each chart type is represented with a unique chart alias. For Angular Gauge, the alias isangulargauge
. Find the complete list of gauge types with their respective aliases here. - Set the width and height (in pixels).
- Set the
dataFormat
as JSON. - Embed the json data as the value of the
dataSource
.
- Set the chart type as
Add a container (instance) for the gauge.
The consolidated code is shown below:
fusioncharts
package for npm
can now be used in two different ways:- FusionCharts ES module
- FusionCharts CJS module
ES6
// Include the core fusioncharts file from core -
import FusionCharts from 'fusioncharts/core';
// Include the gauge from viz folder
// E.g. - import ChartType from fusioncharts/viz/[ChartType]
import AngularGauge from 'fusioncharts/viz/angulargauge';
// Include the fusion theme
import FusionTheme from 'fusioncharts/themes/es/fusioncharts.theme.fusion';
// Add the gauge and theme as dependency
// E.g. FusionCharts.addDep(ChartType)
FusionCharts.addDep(AngularGauge);
FusionCharts.addDep(FusionTheme);
// Create an Instance with chart options
var gaugeInstance = new FusionCharts({
type: 'angulargauge', // The gauge type
width: '450', // Width of the gauge
height: '250', // Height of the gauge
dataFormat: 'json', // Data type
renderAt:'chart-container', //Container where the chart will render
dataSource: {
// Gauge 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"
}]
}
}
});
// Render
gaugeInstance.render();
CJS
var FusionCharts = require('fusioncharts');
// Require widgets from fusioncharts
var Widgets = require('fusioncharts/fusioncharts.widgets');
// Require theme from fusioncharts
var FusionTheme = require('fusioncharts/themes/fusioncharts.theme.fusion');
// Add widgets and themes as dependency
Widgets(FusionCharts);
FusionTheme(FusionCharts);
// Create an Instance with chart options
var gaugeInstance = new FusionCharts({
type: 'angulargauge', // The gauge type
width: '450', // Width of the gauge
height: '250', // Height of the gauge
dataFormat: 'json', // Data type
renderAt:'chart-container', //Container where the gauge will render
dataSource: {
// Gauge 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"
}]
}
}
});
// Render
gaugeInstance.render();
<html>
<head>
<title>My first gauge using FusionWidgets XT</title>
<!-- Include fusioncharts core library -->
<script type="text/javascript" src="https://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.js"></script>
<!-- Include fusion theme -->
<script type="text/javascript" src="https://cdn.fusioncharts.com/fusioncharts/latest/themes/fusioncharts.theme.fusion.js"></script>
<script type="text/javascript">
FusionCharts.ready(function() {
var csatGauge = new FusionCharts({
"type": "angulargauge",
"renderAt": "chart-container",
"width": "450",
"height": "250",
"dataFormat": "json",
"dataSource": {
// 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"
}]
}
}
});
csatGauge.render();
});
</script>
</head>
<body>
<div id="chart-container">An angular gauge will load here!</div>
</body>
</html>
<html>
<head>
<title>My first gauge using FusionWidgets XT</title>
<!-- Include fusioncharts core library -->
<script type="text/javascript" src="path/to/local/fusioncharts.js"></script>
<!-- Include fusion theme -->
<script type="text/javascript" src="path/to/local/themes/fusioncharts.theme.fusion.js"></script>
<script type="text/javascript">
FusionCharts.ready(function() {
var csatGauge = new FusionCharts({
"type": "angulargauge",
"renderAt": "chart-container",
"width": "450",
"height": "250",
"dataFormat": "json",
"dataSource": {
// 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"
}]
}
}
});
csatGauge.render();
});
</script>
</head>
<body>
<div id="chart-container">An angular gauge will load here!</div>
</body>
</html>
That's it! Your first gauge using Plain JavaScript 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.