Create a Chart in Java

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 Java (JSP) wrapper for FusionCharts. The FusionCharts server-side JSP wrapper lets you easily add rich and interactive charts to any JSP 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 chart using the FusionCharts server-side JSP wrapper.

Installation

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

The FusionCharts JSP wrapper requires JAVA 6 or higher.

  • Copy and paste the fusioncharts.java file from integrations > java > 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.


// 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>

That completes the installation of FusionCharts and the jsp-fusioncharts component.

Create Your First Chart

Let's create a Column 2D chart using the jsp-fusioncharts wrapper showing the "Countries With Most Oil Reserves".

FusionCharts Suite has 95+ chart types for you to explore. Find the complete list of chart types here .

FusionCharts will load here..

To understand the chart components, click here.

The data for the above chart is shown in the table below:

Country No. of Oil Reserves
Venezuela 290
Saudi 260
Canada 180
Iran 140
Russia 115
UAE 100
US 30
China 30

Convert Tabular Data into JSON 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": "Countries With Most Oil Reserves [2017-18]",
        "subCaption": "In MMbbl = One Million barrels",
        "xAxisName": "Country",
        "yAxisName": "Reserves (MMbbl)",
        "numberSuffix": "K",
        "theme": "fusion"
    },
    "data": [{
        "label": "Venezuela",
        "value": "290"
    }, {
        "label": "Saudi",
        "value": "260"
    }, {
        "label": "Canada",
        "value": "180"
    }, {
        "label": "Iran",
        "value": "140"
    }, {
        "label": "Russia",
        "value": "115"
    }, {
        "label": "UAE",
        "value": "100"
    }, {
        "label": "US",
        "value": "30"
    }, {
        "label": "China",
        "value": "30"
    }]
}

Different types of charts in FusionCharts expect different JSON formats, based on their grouping. Explore different JSON formats, for example, single-series , multi-series , and combination charts.

In the above JSON data:

  • Create the chart object to define the elements of the chart.

  • Specify the label and value of each column within the data array.

Both the chart object and the data array contain a set of key-value pairs known as attributes. These attributes are used to set the functional and cosmetic properties of the chart.

Now that you have converted the tabular data to JSON format, let's see how to render the chart.

Render the Chart

To render the chart, follow the steps below:

  1. Include the cFusionCharts JSP wrapper in your project.

  2. Include the fusioncharts library.

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

  4. Store label-value pairs in an object.

  5. Set the JSON data as the data source for the chart.

  6. Store the chart configurations in an object.

  7. Convert the final chart configuration to JSON string.

  8. Create the chart instance and set the following:

    • Set the chart type as column2d. Each chart type is represented with a unique chart alias. For Column 2D chart, the alias is column2d. Find the complete list of chart types with their respective alias here.

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

  9. Finally, use a container to render the chart.

The consolidated code is shown below:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@page import="java.util.*" %>
<%@page import="fusioncharts.FusionCharts" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>FusionCharts | My First Chart</title>

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

    // Include FusionCharts Theme File
    <script src="http://cdn.fusioncharts.com/fusioncharts/latest/themes/fusioncharts.theme.fusion.js"></script>
</head>
<body>
        <div id="chart"></div>
        <%
            // store chart config name-config value pair
            Map<String, String> chartConfig = new HashMap<String, String>();
            chartConfig.put("caption", "Countries With Most Oil Reserves [2017-18]");
            chartConfig.put("subCaption", "In MMbbl = One Million barrels");
            chartConfig.put("xAxisName", "Country");
            chartConfig.put("yAxisName", "Reserves (MMbbl)");
            chartConfig.put("numberSuffix", "k");
            chartConfig.put("theme", "fusion");

            //store label-value pair
            Map<String, Integer> dataValuePair = new HashMap<String, Integer>();
            dataValuePair.put("Venezuela", 290);
            dataValuePair.put("Saudi", 260);
            dataValuePair.put("Canada", 180);
            dataValuePair.put("Iran", 140);
            dataValuePair.put("Russia", 115);
            dataValuePair.put("UAE", 100);
            dataValuePair.put("US", 30);
            dataValuePair.put("China", 30);

            StringBuilder jsonData = new StringBuilder();
            StringBuilder data = new StringBuilder();

            // json data to use as chart data source
            jsonData.append("{'chart':{");
            for(Map.Entry conf:chartConfig.entrySet())
            {
                jsonData.append("'" + conf.getKey()+"':'"+conf.getValue() + "',");
            }

            jsonData.replace(jsonData.length() - 1, jsonData.length() ,"},");

            // build  data object from label-value pair
            data.append("'data':[");

            for(Map.Entry pair:dataValuePair.entrySet())
            {
                data.append("{'label':'" + pair.getKey() + "','value':'" + pair.getValue() +"'},");
            }
            data.replace(data.length() - 1, data.length(),"]");

            jsonData.append(data.toString());
            jsonData.append("}");


            // Create chart instance
            // charttype, chartID, width, height,containerid, data format, data
            FusionCharts firstChart = new FusionCharts(
                "column2d", 
                "first_chart", 
                "700",
                "400", 
                "chart",
                "json", 
                jsonData.toString()
            );
        %>
        <%= firstChart.render() %>
    </body>
</html>


<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@page import="java.util.*" %>
<%@page import="fusioncharts.FusionCharts" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Insert title here</title>
        <script src="path/to/local/fusioncharts.js"></script>
        <script src="path/to/local/themes/fusioncharts.theme.fusion.js"></script>
    </head>
    <body>
        <div id="chart"></div>
        <%
            // store chart config name-config value pair
            Map<String, String> chartConfig = new HashMap<String, String>();
            chartConfig.put("caption", "Countries With Most Oil Reserves [2017-18]");
            chartConfig.put("subCaption", "In MMbbl = One Million barrels");
            chartConfig.put("xAxisName", "Country");
            chartConfig.put("yAxisName", "Reserves (MMbbl)");
            chartConfig.put("numberSuffix", "k");
            chartConfig.put("theme", "fusion");

            //store label-value pair
            Map<String, Integer> dataValuePair = new HashMap<String, Integer>();
            dataValuePair.put("Venezuela", 290);
            dataValuePair.put("Saudi", 260);
            dataValuePair.put("Canada", 180);
            dataValuePair.put("Iran", 140);
            dataValuePair.put("Russia", 115);
            dataValuePair.put("UAE", 100);
            dataValuePair.put("US", 30);
            dataValuePair.put("China", 30);

            StringBuilder jsonData = new StringBuilder();
            StringBuilder data = new StringBuilder();

            // json data to use as chart data source
            jsonData.append("{'chart':{");
            for(Map.Entry conf:chartConfig.entrySet())
            {
                jsonData.append("'" + conf.getKey()+"':'"+conf.getValue() + "',");
            }

            jsonData.replace(jsonData.length() - 1, jsonData.length() ,"},");

            // build  data object from label-value pair
            data.append("'data':[");

            for(Map.Entry pair:dataValuePair.entrySet())
            {
                data.append("{'label':'" + pair.getKey() + "','value':'" + pair.getValue() +"'},");
            }
            data.replace(data.length() - 1, data.length(),"]");

            jsonData.append(data.toString());
            jsonData.append("}");


            // Create chart instance
            // charttype, chartID, width, height,containerid, data format, data
            FusionCharts firstChart = new FusionCharts(
                "column2d", 
                "first_chart", 
                "700",
                "400", 
                "chart",
                "json", 
                jsonData.toString()
            );
        %>
        <%= firstChart.render() %>
    </body>
</html>

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