Create a Gauge Using Django

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 Django wrapper for FusionCharts. The FusionCharts server-side Django wrapper lets you easily add rich and interactive charts to any Django project. Using the wrapper, you can create gauges 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 server-side Django wrapper.

Installation

In this section, we will show you how to install FusionCharts Suite XT and the FusionCharts Django wrapper and all the other dependencies on your system.

The FusionCharts Django wrapper requires Python 2.7 or higher.

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

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

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

The consolidated given below:


{% load static %}
// 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>


{% load static %}
// 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 Django 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:

FusionCharts will load here..

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 and maxValue within the color array under the colorRange 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 under dials 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

  1. Import Render from django.shortcuts.

  2. Import HttpResponse from django.http.

  3. Import OrderedDict from collections.

  4. Include the fusioncharts.py file.

  5. Load dial indicator values from a simple string array.

  6. Pass widget data to the dataSource parameter, as dict, in the form of key-value pairs.

  7. Configure the widgetConfig dict, which contains key-value pair data for the widget attribute.

  8. Configure the colorData dict, which contains key-value pairs of data for ColorRange of dial.

  9. Convert the data in dialData array into a format supported by FusionCharts.

  10. Enter the data for the gauge as an array, where each element is a JSON object, with label and value as keys.

  11. Iterate through the data in dialValues and insert into the dialData["dial"] list.

  12. Enter data for the dial using an array, wherein each element is a JSON object having the value as keys.

  13. Create the gauge instance and set the following:

    • Set the chart type as angulargauge. Find the complete list of gauge types with their respective alias here .

    • Set the gauge 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.

    • Pass the gauge data to the dataSource parameter.

  14. Finally, use a container using <div> to render the gauge.

The full code for the above sample is:

from django.shortcuts import render
from django.http import HttpResponse
from collections import OrderedDict

# Include the `fusioncharts.py` file that contains functions to embed the widget.
from fusioncharts import FusionCharts

def myFirstWidget(request):

#Load dial indicator values from simple string array# e.g.dialValues = ["52", "10", "81", "95"]
    dialValues = ["81"]

    # widget data is passed to the `dataSource` parameter, as dict, in the form of key-value pairs.
    dataSource = OrderedDict()

    # The `widgetConfig` dict contains key-value pairs of data for widget attribute
    widgetConfig = OrderedDict()
    widgetConfig["caption"] = "Nordstrom's Customer Satisfaction Score for 2017"
    widgetConfig["lowerLimit"] = "0"
    widgetConfig["upperLimit"] = "100"
    widgetConfig["showValue"] = "1"
    widgetConfig["numberSuffix"] = "%"
    widgetConfig["theme"] = "fusion"
    widgetConfig["showToolTip"] = "0"

    # The `colorData` dict contains key-value pairs of data for ColorRange of dial
    colorRangeData = OrderedDict()
    colorRangeData["color"] = [{
            "minValue": "0",
            "maxValue": "50",
            "code": "#F2726F"
        },
        {
            "minValue": "50",
            "maxValue": "75",
            "code": "#FFC533"
        },
        {
            "minValue": "75",
            "maxValue": "100",
            "code": "#62B58F"
        }
    ]

    # Convert the data in the `dialData` array into a format that can be consumed by FusionCharts.
    dialData = OrderedDict()
    dialData["dial"] = []

    dataSource["chart"] = widgetConfig
    dataSource["colorRange"] = colorRangeData
    dataSource["dials"] = dialData

    # Iterate through the data in `dialValues` and insert into the `dialData["dial"]` list.
    # The data for the `dial`should be in an array wherein each element of the
    # array is a JSON object# having the `value` as keys.
    for i in range(len(dialValues)):
        dialData["dial"].append({
        "value": dialValues[i]
    })
   # Create an object for the angular-gauge using the FusionCharts class constructor
   # The widget data is passed to the `dataSource` parameter.
   angulargaugeWidget = FusionCharts("angulargauge", "myFirstWidget", "100%", "200", "myFirstwidget-container", "json", dataSource)

   # returning complete JavaScript and HTML code, which is used to generate widget in the browsers.
   return render(request, 'index.html', {'output' : angulargaugeWidget.render()})

The HTML template of the above sample is shown below:


<!-- Filename: app_name/templates/index.html -->
<!DOCTYPE html>
<html>

<head>
<title>FC-python wrapper</title>
{% load static %}
<script type="text/javascript" src="{% static "https://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.js" %}"></script>
<script type="text/javascript" src="{% static "https://cdn.fusioncharts.com/fusioncharts/latest/themes/fusioncharts.theme.fusion.js" %}"></script>
</head>

<body>
<div id="myFirstchart-container">{{ output|safe }}</div>
</body>

</html>



<!-- Filename: app_name/templates/index.html -->
<!DOCTYPE html>
<html>

<head>
<title>FC-python wrapper</title>
{% load static %}
<script type="text/javascript" src="{% static "path/to/local/fusioncharts.js" %}"></script>
<script type="text/javascript" src="{% static "path/to/local/themes/fusioncharts.theme.fusion.js" %}"></script>
</head>

<body>
<div id="myFirstchart-container">{{ output|safe }}</div>
</body>

</html>


That's it! Your first gauge using the FusionCharts Django 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 <> 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.