Site icon FusionBrew – The FusionCharts Blog

Rendering FusionCharts in an Android Application

Google’s Android is a Linux-based mobile operating system primarily designed for smartphones and tablets. Since its debut in 2007, it has been the best-selling operating system for tablets and smartphones. Furthermore, Android gives you access to a plethora of useful libraries and tools that can be used to create rich applications. In this post, we’ll walk through the process of integrating FusionCharts into an Android-native app. FusionCharts is available for web and mobile projects, making it simple to create stunning dashboards and Data Charts for your Android app. Because of its extensive documentation, cross-browser support, and consistent API, it is now easier than ever to add interactive graphs and responsive charts to your website. Without further ado, let’s get started with the tutorial!

Core Requirements

Before we start with the steps on embedding FusionCharts in an Android app, we need to make sure that we have the following components downloaded and installed on our local machine:

Embedding FusionCharts and Creating Charts in an Android App

For a demonstration, we will embed FusionCharts in an Android app and create the following three types of charts: Follow the steps given below to embed FusionCharts and create charts in an Android app:

Step 1

Step 2

Step 3

The image below shows the project structure created so far:

Step 4

< ?xml version="1.0" encoding="utf-8"?>

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/activity_main"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context="com.fusioncharts.app.fusiondemo.MainActivity"
   android:orientation="vertical"
   android:baselineAligned="false">

   <tablerow android:layout_width="350dp"
       android:layout_height="wrap_content"
       android:orientation="horizontal">
       <button android:text="Column"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/button_column"
           android:layout_gravity="left"
           android:onClick="showcolumn"></button>

       <button android:text="Pie"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/button_pie"
           android:onClick="showpie"></button>

       <button android:text="Drilldown"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/button_drilldown"
           android:layout_gravity="right"
           android:onClick="showdrilldown"></button>
   </tablerow>
   <webview android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/webView"></webview>
</linearlayout>
The image below shows the layout of the application created when the above code is executed. This layout can be further customized as required.

Step 5

package com.fusioncharts.app.fusiondemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
   }
   public void showcolumn(View a) {
       if (a.getId() == R.id.button_column) {
           WebView webView= (WebView)findViewById(R.id.webView);
           webView.getSettings().setJavaScriptEnabled(true);
           webView.setWebChromeClient(new WebChromeClient());
           webView.loadUrl("file:///android_asset/index.html");
       }
   }
   public void showpie(View a) {
       if (a.getId() == R.id.button_pie) {
           WebView webView= (WebView)findViewById(R.id.webView);
           webView.getSettings().setJavaScriptEnabled(true);
           webView.setWebChromeClient(new WebChromeClient());
           webView.loadUrl("file:///android_asset/pie.html");
       }
   }
   public void showdrilldown(View a) {
       if (a.getId() == R.id.button_drilldown) {
           WebView webView= (WebView)findViewById(R.id.webView);
           webView.getSettings().setJavaScriptEnabled(true);
           webView.setWebChromeClient(new WebChromeClient());
           webView.loadUrl("file:///android_asset/drilldown.html");
       }
   }
}
After adding the above code in the MainActivity.java file, you’ll notice that each button-click loads an HTML page inside the webview. The subsequent steps talk about how these HTML pages are included.

Step 6

From the app folder, select New > Folder > Assets Folder, as shown in the image below: The New Android Component window opens.

Step 7

Step 8

Step 9

<html>
<head>
	<title>My first chart using FusionCharts Suite XT</title>
	<script type="text/javascript" src="fusioncharts.js"></script>
	<script type="text/javascript" src="fusioncharts.charts.js"></script>
	<script type="text/javascript" src="fusioncharts.theme.fint.js"></script>
	<script type="text/javascript">
	FusionCharts.ready(function(){
    var revenueChart = new FusionCharts({
        "type": "column2d",
        "renderAt": "chartContainer",
        "width": "260",
        "height": "300",
        "dataFormat": "json",
        "dataSource": {
            "chart": {
                "caption": "Quarterly revenue for last year",
                "subCaption": "Harry's SuperMart",
                "palettecolors": "#2874A6",
                "useplotgradientcolor": "0",
                "yAxisName": "Revenues (In USD)",
                "theme": "fint",
                "showvalues": "0",
                "yaxismaxvalue": "40000",
                "yaxisminvalue": "0"
            },
            "data": [{
                    "label": "Q1",
                    "value": "16000"
                },
                {
                    "label": "Q2",
                    "value": "25300"
                },
                {
                    "label": "Q3",
                    "value": "10140"
                },
                {
                    "label": "Q4",
                    "value": "19140"
                }
            ]
        }
    });
    revenueChart.render();
	})
	</script>
</head>
<body>
<div id="chartContainer">FusionCharts XT will load here!</div>
</body>
</html>
<html>
<head>
	<title>My first chart using FusionCharts Suite XT</title>
</head>
<body>
	<div id="chartContainer">FusionCharts XT will load here!</div>
	<script src="fusioncharts.js"></script>
	<script src="fusioncharts.charts.js"></script>
	<script type="text/javascript">
	FusionCharts.ready(function(){
    var ageGroupChart = new FusionCharts({
        type: 'pie2d',
        renderAt: 'chartContainer',
        width: '270',
        height: '290',
        dataFormat: 'json',
        dataSource: {
            "chart": {
                "caption": "Split of Visitors by Age Group",
                "subCaption": "Last year",
                "paletteColors": "#0075c2,#1aaf5d,#f2c500,#f45b00,#8e0000",
                "bgColor": "#ffffff",
                "showBorder": "0",
                "use3DLighting": "0",
                "showShadow": "0",
                "pieradius": "40",
                "enableSmartLabels": "1",
                "startingAngle": "0",
                "showPercentValues": "1",
                "showPercentInTooltip": "0",
                "decimals": "1",
                "captionFontSize": "14",
                "subcaptionFontSize": "14",
                "subcaptionFontBold": "0",
                "toolTipColor": "#ffffff",
                "toolTipBorderThickness": "0",
                "toolTipBgColor": "#000000",
                "toolTipBgAlpha": "80",
                "toolTipBorderRadius": "2",
                "toolTipPadding": "5",
                "showHoverEffect": "1",
                "showLegend": "1",
                "legendBgColor": "#ffffff",
                "legendBorderAlpha": '0',
                "legendShadow": '0',
                "legendItemFontSize": '10',
                "legendItemFontColor": '#666666'
            },
            "data": [{
                    "label": "Teenage",
                    "value": "1250400"
                },
                {
                    "label": "Adult",
                    "value": "1463300"
                },
                {
                    "label": "Mid-age",
                    "value": "1050700"
                },
                {
                    "label": "Senior",
                    "value": "491000"
                }
            ]
        }
    }).render();
});
	</script>
</body>
</html>
<html>
<head>
	 <title>My first chart using FusionCharts Suite XT</title>
	 <script type="text/javascript" src="fusioncharts.js"></script>
	 <script type="text/javascript" src="fusioncharts.charts.js"></script>
	 <script type="text/javascript">
FusionCharts.ready(function() {
    var salesChart = new FusionCharts({
        type: 'column2d',
        renderAt: 'chartContainer',
        width: '260',
        height: '290',
        dataFormat: 'json',
        dataSource: {
            "chart": {
                "caption": "Top 3 Juice Flavors",
                "subcaption": "Last year",
                "xaxisname": "Flavor",
                "yaxisname": "Amount (In USD)",
                "numberprefix": "$",
                "palettecolors": "#008ee4",
                "canvasbgalpha": "0",
                "canvasborderalpha": "0",
                "useplotgradientcolor": "0",
                "showplotborder": "0",
                "placevaluesinside": "1",
                "valuefontcolor": "#ffffff",
                "captionpadding": "20",
                "showaxislines": "1",
                "axislinealpha": "20",
                "divlinealpha": "20",
                "showborder": "1",
                "bgalpha": "0",
                "animation": "0"
            },
            "data": [{
                "label": "Apple",
                "value": "810000",
                "link": "newchart-xml-apple"
            }, {
                "label": "Cranberry",
                "value": "620000",
                "link": "newchart-xml-cranberry"
            }, {
                "label": "Grapes",
                "value": "350000",
                "link": "newchart-xml-grapes"
            }],
            "linkeddata": [{
                "id": "apple",
                "linkedchart": {
                    "chart": {
                        "caption": "Apple Juice - Quarterly Sales",
                        "subcaption": "Last year",
                        "xaxisname": "Quarter",
                        "yaxisname": "Amount (In USD)",
                        "numberprefix": "$",
                        "palettecolors": "#008ee4",
                        "canvasbgalpha": "0",
                        "canvasborderalpha": "0",
                        "useplotgradientcolor": "0",
                        "showplotborder": "0",
                        "placevaluesinside": "1",
                        "valuefontcolor": "#ffffff",
                        "captionpadding": "20",
                        "showaxislines": "1",
                        "axislinealpha": "20",
                        "divlinealpha": "20",
                        "showborder": "1",
                        "bgalpha": "0",
                        "animation": "0"
                    },
                    "data": [{
                        "label": "Q1",
                        "value": "157000"
                    }, {
                        "label": "Q2",
                        "value": "172000"
                    }, {
                        "label": "Q3",
                        "value": "206000"
                    }, {
                        "label": "Q4",
                        "value": "275000"
                    }]
                }
            }, {
                "id": "cranberry",
                "linkedchart": {
                    "chart": {
                        "caption": "Cranberry Juice - Quarterly Sales",
                        "subcaption": "Last year",
                        "xaxisname": "Quarter",
                        "yaxisname": "Amount (In USD)",
                        "numberprefix": "$",
                        "palettecolors": "#008ee4",
                        "canvasbgalpha": "0",
                        "canvasborderalpha": "0",
                        "useplotgradientcolor": "0",
                        "showplotborder": "0",
                        "placevaluesinside": "1",
                        "valuefontcolor": "#ffffff",
                        "captionpadding": "20",
                        "showaxislines": "1",
                        "axislinealpha": "20",
                        "divlinealpha": "20",
                        "showborder": "1",
                        "bgalpha": "0",
                        "animation": "0"
                    },
                    "data": [{
                        "label": "Q1",
                        "value": "102000"
                    }, {
                        "label": "Q2",
                        "value": "142000"
                    }, {
                        "label": "Q3",
                        "value": "187000"
                    }, {
                        "label": "Q4",
                        "value": "189000"
                    }]
                }
            }, {
                "id": "grapes",
                "linkedchart": {
                    "chart": {
                        "caption": "Grape Juice - Quarterly Sales",
                        "subcaption": "Last year",
                        "xaxisname": "Quarter",
                        "yaxisname": "Amount (In USD)",
                        "numberprefix": "$",
                        "palettecolors": "#008ee4",
                        "canvasbgalpha": "0",
                        "canvasborderalpha": "0",
                        "useplotgradientcolor": "0",
                        "showplotborder": "0",
                        "placevaluesinside": "1",
                        "valuefontcolor": "#ffffff",
                        "captionpadding": "20",
                        "showaxislines": "1",
                        "axislinealpha": "20",
                        "divlinealpha": "20",
                        "showborder": "1",
                        "bgalpha": "0",
                        "animation": "0"
                    },
                    "data": [{
                        "label": "Q1",
                        "value": "45000"
                    }, {
                        "label": "Q2",
                        "value": "72000"
                    }, {
                        "label": "Q3",
                        "value": "95000"
                    }, {
                        "label": "Q4",
                        "value": "108000"
                    }]
                }
            }]
        }
    });
    salesChart.render();
});

</script>
</head>
<body>
<div id="chartContainer">FusionCharts XT will load here!</div>
</body>
</html>

Step 10

Building Your Own .apk File

Follow the steps given below to build your own .apk file: If you find any difficulty in rendering the chart or you see any errors in your code, click here to download the complete source code of the sample project we have created for this tutorial.
Exit mobile version