Create a Gauge Using Ruby on Rails

Overview

FusionCharts Suite XT includes the FusionCharts server-side RoR wrapper that lets you create interactive, data-driven charts. Using the wrapper, you can create charts in your browsers without writing any JavaScript code. The required JavaScript and HTML code is generated as a string in the server and inserted in the web page to generate charts.

In this article, we will show you how to install and render a gauge using the FusionCharts Rails gem wrapper.

Installation

In this article, we will show you how to download and install the FusionCharts Rails gem wrapper and all the other dependencies on your system.

  • Copy the 'fusionCharts-rails.rbfromintegrations > rubyonrails > 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 gauge.


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

// Include FusionCharts Theme file
<script type="text/javascript" src="http://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>

  • Add the FusionCharts Rail wrapper:

    • Using RubyGems
    • Manually
  • Include the FusionCharts Rails wrapper.

To add the FusionCharts Rails wrapper, use any of the following processes:

Add this line to your application’s Gemfile:

    gem ‘fusioncharts-rails’

RubyGems contain package information along with the files to install. On the command line prompt, execute the following command:

    $bundle

This command will automatically install the fusioncharts-rails gem. You can also install the gem directly from the command line prompt, without making any edits to the Gemfile. To do this, use the code line given below:

    $gem install fusioncharts-rails

Step 1: Copy all files from fusioncharts-suite-xt > integrations > rubyonrails > fusioncharts-wrapper folder.
Step 2: Paste the copied files to the lib folder of your application

That completes the installation of FusionCharts Suite and the Rails 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 Nordstorm'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..

The thresholds for the above sample have been defined using the following range.

Range Color Hex Code
0-50 Red #F2726F
50-75 Yellow #FFC533
75-100 Green #62B58F

So, any score less than 50 (considered bad) is presented in red. Any score between 50 and 75 (considered average) is presented in yellow. Any score above 75 (considered good) is presented in green.

Convert Tabular Data into JSON/XML Format

Now that you have the tabular data ready, it's time to convert it into JSON format, as FusionCharts accepts data in JSON or XML format. In this example, we will use the JSON format, as shown below:

{
    "chart": {
        "caption": "Nordstorm's Customer Satisfaction Score for 2017",
        "lowerLimit": "0",
        "upperLimit": "100",
        "showValue": "1",
        "numberSuffix": "%",
        "theme": "fusion",
        "showToolTip": "0"
    },
    "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 array to set the color associated with the specific range of values.

  • Specify minValue and maxValue within the color array under the colorRange array.

  • Specify the hex code of the color within the color array.

  • Create the dials object to represent the customer satisfaction score.

  • Create the dial object under dials object to set the value of the dial in an array.

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 converted the tabular data to JSON format, let's learn how to render the gauge.

Render the Gauge

To render the gauge, follow the steps below:

  1. Include the FusionCharts Rails wrapper in your project.

  2. Include the fusioncharts library.

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

  4. Set the gauge appearance configuration to display the data in the gauge.

  5. Create an array named chartDataObj to save the color range data of the gauge.

  6. Create the gauge dial data in the array format (Multiple values can be separated by comma).

  7. Set te chart data as JSON string.

  8. Create the gauge instance and set the following:

    • Set the chart type as angulargauge. Each gauge is represented with a unique alias. For Angular Gauge, the alias is angulargauge. Find the complete list of gauges with their respective alias here.

    • 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.

  9. Create a container using <div> to render the chart.

The full code for the above sample is:

require 'json'

class FirstWidget

    # Widget rendering
    def self.getWidget

        # Widget appearance configuration
        widgetAppearancesConfigObj = {
            "caption" => "Nordstorm's Customer Satisfaction Score for 2017",
            "lowerLimit" => "0",
            "upperLimit" => "100",
            "showValue" => "1",
            "numberSuffix" => "%",
            "theme" => "fusion",
            "showToolTip" => "0"
        }

        # Widget color range data
        colorDataObj = {"color" => [
                {"minValue" => "0", "maxValue" => "50", "code" => "#F2726F"}, 
                {"minValue" => "50", "maxValue" => "75", "code" => "#FFC533"},
                {"minValue" => "75", "maxValue" => "100", "code" => "#62B58F"}
            ]
        }

        # Widget dial data in array format, multiple values can be separated by comma e.g. ["81", "23", "45",...]
        widgetDialDataArray = ["81"]

        # Dial value in JSON format
        widgetDialDataStr = ""

        # Template for dial value
        widgetDialDataTemplate = "{ \"value\": \"%s\" },"

        # Iterates dial data array and converts them proper data format 
        widgetDialDataArray.each {|item|
            data = widgetDialDataTemplate % [item]
            widgetDialDataStr.concat(data)
        }

        # Removing trailing comma
        widgetDialDataStr = widgetDialDataStr.chop

        # Formats dial value(s)
        widgetDialTemplate = "{ \"dial\": [%s]}"
        widgetDialStr = ""
        widgetDialStr = widgetDialTemplate % [widgetDialDataStr]

        # Final Widget JSON template
        widgetJSONTemplate = "{ \"chart\": %s, \"colorRange\": %s,  \"dials\": %s}"

        # Final Widget JSON data from template
        widgetJSONStr = widgetJSONTemplate % [widgetAppearancesConfigObj.to_json, colorDataObj.to_json, widgetDialStr]

        # Rendering the widget
        widget = Fusioncharts::Chart.new({
            width: "450",
            height: "250",
            type: "angulargauge",
            renderAt: "widgetContainer",
            dataSource: widgetJSONStr
        })
    end
end

The template of the above sample is shown below:

<<!-- Filename: app/views/examples/firstwidget.html.erb -->
<h3>My Widget</h3>
<div id="widget-container"></div>
<%[email protected]() %>

That's it! Your first gauge using FusionCharts Rails wrapper is ready. When you run this HTML page now, you should see a gauge representing your data.

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.