Create a Gauge in Vue Using 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 and lightweight Vue component which provides bindings for FusionCharts. The vue-fusioncharts component allows you to easily add rich and interactive charts to any Vue project.
In this page, we'll see how to install FusionCharts and render a gauge using the vue-fusionCharts component.
Installation
Install FusionCharts and the vue-fusioncharts component using any of the following methods:
vue-fusioncharts component via npm follow the steps below:vue-fusioncharts component
$ npm install vue-fusioncharts --save
fusioncharts package
$ npm install fusioncharts --save
vue-fusioncharts component follow the steps below:vue-fusioncharts component.
<head>
<!-- Step 1 - Including vue -->
<script type="text/javascript" src="https://unpkg.com/[email protected]"></script>
<!-- Step 2 - Including vue-fusioncharts component -->
<script type="text/javascript" src="https://unpkg.com/vue-fusioncharts/dist/vue-fusioncharts.min.js"></script>
<!-- Step 3 - Including the fusioncharts core library -->
<script type="text/javascript" src="https://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.js"></script>
<!-- Step 4 - Including the fusion theme -->
<script type="text/javascript" src="https://cdn.fusioncharts.com/fusioncharts/latest/themes/fusioncharts.theme.fusion.js"></script>
</head>
vue-fusioncharts component follow the steps below:vue-fusioncharts component.
<head>
<!-- Step 1 - Including vue -->
<script type="text/javascript" src="path/to/local/vue.js"></script>
<!-- Step 2 - Including vue-fusioncharts component -->
<script type="text/javascript" src="path/to/local/vue-fusioncharts.js"></script>
<!-- Step 3 - Including the fusioncharts core library -->
<script type="text/javascript" src="path/to/local/fusioncharts.js"></script>
<!-- Step 4 - Including the fusion theme -->
<script type="text/javascript" src="path/to/local/themes/fusioncharts.theme.fusion.js"></script>
</head>
That completes the installation of FusionCharts and the vue-fusioncharts component.
Create your First Gauge
Gauges are powerful tools that can showcase information using a radial scale to display data.
To start with, we'll build a simple angular gauge showcasing Nordstrom's Customer Satisfaction Score as shown below.
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
chartobject to define the elements of the gauge.Create the
colorRangeobject to set the color associated with the specific range of values.Specify
minValueandmaxValuewithin thecolorarray under thecolorRangeobject.Set the
codeattribute to specify the hex color of respective ranges.Create the
dialsobject to represent the customer satisfaction score.Create the
dialobject underdialsobject 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 gauge
To render the gauge, open App.vue file and follow the steps below:
Include the
vuecomponent.Include
vue-fusionchartscomponent.Include
fusionchartscore library.Include the chart type.
Include the FusionCharts theme file to apply style to the charts.
Register the
vue-fusionChartscomponent.Store the chart configurations in a JSON object. In this JSON object:
- Set the chart type as
angulargauge. For Angular Gauge, the alias isangulargauge. Find the complete list of gauge types with their respective alias here. - Set the width and height (in pixels).
- Set the
dataFormatas JSON. - Embed the json data as the value of the
dataSource.
- Set the chart type as
<script>
import Vue from 'vue';
import VueFusionCharts from 'vue-fusioncharts';
import FusionCharts from 'fusioncharts';
import AngularGauge from 'fusioncharts/fusioncharts.widgets';
import FusionTheme from 'fusioncharts/themes/fusioncharts.theme.fusion';
Vue.use(VueFusionCharts, FusionCharts, AngularGauge, FusionTheme);
export default {
name: 'app',
data () {
return {
"type": "angulargauge",
"renderAt": "chart-container",
"width": "550",
"height": "350",
"dataFormat": "json",
"datasource": {
"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"
}]
}
}
}
}
}
</script>
<template>
<div id="app">
<div id="chart-container">
<fusioncharts
:type="type"
:width="width"
:height="height"
:dataformat="dataformat"
:datasource="datasource"
>
</fusioncharts>
</div>
</div>
</template>
<html>
<head>
<!-- Including vue -->
<script type="text/javascript" src=" https://unpkg.com/[email protected]"></script>
<!-- Including vue-fusioncharts component -->
<script type="text/javascript" src=" https://unpkg.com/vue-fusioncharts/dist/vue-fusioncharts.min.js"></script>
<!-- Including the fusioncharts core library -->
<script type="text/javascript" src=" https://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.js"></script>
<script type="text/javascript" src=" https://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.widgets.js"></script>
<!-- Including the fusion theme -->
<script type="text/javascript" src=" https://cdn.fusioncharts.com/fusioncharts/latest/themes/fusioncharts.theme.fusion.js"></script>
</head>
<body>
<div id="app">
<fusioncharts :type="type" :width="width" :height="height" :dataFormat="dataFormat" :dataSource="dataSource"></fusioncharts>
</div>
<script type="text/javascript">
// Use VueFusionCharts component by calling the Vue.use() method:
Vue.use(VueFusionCharts, FusionCharts);
var app = new Vue({
el: '#app',
data: {
width: '450',
height: '250',
type: 'angulargauge',
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"
}]
}
}
}
});
</script>
</body>
</html>
<html>
<head>
<!-- Including vue -->
<script type="text/javascript" src="https://unpkg.com/[email protected]"></script>
<!-- Including vue-fusioncharts component -->
<script type="text/javascript" src="path/to/local/vue-fusioncharts.min.js"></script>
<!-- Including the fusioncharts core library -->
<script type="text/javascript" src="path/to/local/fusioncharts.js"></script>
<script type="text/javascript" src="path/to/local/fusioncharts.widgets.js"></script>
<!-- Including the fusion theme -->
<script type="text/javascript" src="path/to/local/themes/fusioncharts.theme.fusion.js"></script>
</head>
<body>
<div id="app">
<fusioncharts :type="type" :width="width" :height="height" :dataFormat="dataFormat" :dataSource="dataSource"></fusioncharts>
</div>
<script type="text/javascript">
// Use VueFusionCharts component by calling the Vue.use() method:
Vue.use(VueFusionCharts, FusionCharts);
var app = new Vue({
el: '#app',
data: {
width: '450',
height: '250',
type: 'angulargauge',
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"
}]
}
}
}
});
</script>
</body>
</html>
That's it! Your first gauge using vue-fusioncharts 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.jswas 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.