Change Chart Type at Runtime

FusionCharts Suite XT includes advanced features that let you add more context to your chart and make data visualization simpler. These features include chart updates, update chart type at runtime, and events.

This article focuses on how you can change the chart type of the chart at runtime using react-native-fusioncharts component. The chart types used in the sample is:

  • Column 2D
  • Bar 2D
  • Pie 2D

A chart configured to change the chart type, is shown below:

FusionCharts will load here..
{
    "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"
        }
    ]
}

The full code of the above sample is given below:

import React, { Component } from 'react';
import { Platform, StyleSheet, Text, View, Button, Alert } from 'react-native';
import FusionCharts from 'react-native-fusioncharts';

export default class ChartRunTime extends Component {
    constructor(props) {
        super(props);
        this.apiCaller = null;
        this.state = {
            type: 'column2d',
            width: '700',
            height: '400',
            dataFormat: 'json',
            chartType: '',
            dataSource: {
                type: "column2d", // The chart types
                width: "700", // Width of the chart
                height: "400", // Height of the chart
                dataFormat: "json", // Data type
                dataSource: { 
                    // Chart Configuration 
                    "chart": {
                        "caption": "Countries With Most Oil Reserves [2017-18]",
                        "subCaption": "In MMbbl = One Million barrels",
                        "xAxisName": "Country",
                        "yAxisName": "Reserves (MMbbl)",
                        "numberSuffix": "K",
                        "theme": "fusion",
                    },
                    // Chart Data
                    "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"
                    }]
                }
            };
            this.libraryPath = Platform.select({
                // Specify fusioncharts.html file location
                android: { uri: 'file:///android_asset/fusioncharts.html' },
                ios: require('./assets/fusioncharts.html')
            });
        }

        changeType(type) {
            this.setState({ chartType: type }, () => {
                this.apiCaller(`window.chartObj.chartType('${type}')`);
            });
        }

        render() {
          return (
            <View style={styles.container}>
            <Text style={styles.header}>Change chart type at runtime</Text>
            <View style={styles.chartContainer}>
              <FusionCharts
                type={this.state.type}
                width={this.state.width}
                height={this.state.height}
                dataFormat={this.state.dataFormat}
                dataSource={this.state.dataSource}
                libraryPath={this.libraryPath} // set the libraryPath property
                onInitialized={caller => {
                  this.apiCaller = caller;
                  if (this.state.chartType === '')
                    this.setState({ chartType: this.state.type });
                }}
              />
            </View>
            <Text style={styles.info}>Press button to change chart type</Text>
            <View style={styles.buttonContainer}>
              <Button
                disabled={
                  this.state.chartType === '' || this.state.chartType == 'column2d'
                }
                style={{ margin: 8 }}
                title="Column2D"
                onPress={() => this.changeType('column2d')}
              />
              <Button
                disabled={
                  this.state.chartType === '' || this.state.chartType == 'pie2d'
                }
                title="Pie2D"
                onPress={() => this.changeType('pie2d')}
              />
              <Button
                disabled={
                  this.state.chartType === '' || this.state.chartType == 'bar2d'
                }
                title="Bar2D"
                onPress={() => this.changeType('bar2d')}
              />
            </View>
          </View>
        );
    }
}
const styles = StyleSheet.create({
    container: {
        flex: 1,
        padding: 10
    },
    header: {
        fontWeight: 'bold',
        fontSize: 20,
        textAlign: 'center',
        paddingBottom: 10
    },
    chartContainer: {
        height: 400,
        borderColor: '#000',
        borderWidth: 1
    },
    buttonContainer: {
        padding: 10,
        display: 'flex',
        justifyContent: 'space-around',
        flexDirection: 'row',
        alignItems: 'center'
    },
    info: {
        fontSize: 16,
        textAlign: 'center',
        marginTop: 5
    }
});

The above chart has been rendered using the following steps:

  1. Include the necessary libraries and components using import. For example, react-native-fusioncharts, fusioncharts, etc.

  2. Define the chart configuration in a JSON object. In the JSON object:

    • Set the chart type as column2d. Find the complete list of chart types with their respective alias here.
    • Set the width and height of the chart in pixels.
    • Set the dataFormat as JSON.
    • Embed the json data as the value of dataSource.
  3. Specify the location of fusioncharts.html for Android and iOS.

  4. Create a function to update the chart type at runtime.

  5. Add the render() function to create the radio buttons.

  6. Add style to the container of the chart.

The HTML template(fusioncharts.html) of the above sample is:


<!DOCTYPE html>
<html>

<head>
    <title>FusionCharts</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />

    <style type="text/css">
        body,
        html {
            margin: 0;
            padding: 0;
            overflow: hidden;
            font-size: 13px;
        }

        #chart-container {
            width: 100%;
            height: 100%;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            position: absolute;
            user-select: none;
            -webkit-user-select: none;
            overflow: hidden;
        }

        #loading-text {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            -webkit-transform: translate(-50%, -50%);
            user-select: none;
            -webkit-user-select: none;
        }
    </style>
</head>

<body>
    <div id="chart-container">
        <div id="loading-text">
            Chart is loading...
        </div>
    </div>

    <script type='text/javascript'>
        "use strict";
        (function() {
            var a = Promise.resolve(),
                b = {},
                c = {};
            (function d() {
                var f = function() {
                    function g() {
                        return Math.floor(65536 * (1 + Math.random())).toString(16).substring(1)
                    }
                    return g() + g() + "-" + g() + "-" + g() + "-" + g() + "-" + g() + g() + g()
                };
                window.webViewBridge = {
                    send: function send(g, h, i, j) {
                        i = i || function() {}, j = j || function() {};
                        var k = {
                                targetFunc: g,
                                data: h || {},
                                msgId: f()
                            },
                            l = JSON.stringify(k);
                        a = a.then(function() {
                            return new Promise(function(m, n) {
                                b[k.msgId] = {
                                    resolve: m,
                                    reject: n
                                }, c[k.msgId] = {
                                    onsuccess: i,
                                    onerror: j
                                }, window.postMessage(l)
                            })
                        }).catch(function() {})
                    }
                }, window.document.addEventListener("message", function(g) {
                    var h;
                    try {
                        h = JSON.parse(g.data)
                    } catch (i) {
                        return
                    }
                    b[h.msgId] && (b[h.msgId].resolve(), delete b[h.msgId]), h.args && c[h.msgId] && (h.isSuccessfull ? c[h.msgId].onsuccess.apply(null, h.args) : c[h.msgId].onerror.apply(null, h.args), delete c[h.msgId])
                })
            })()
        })();
    </script>

    <!-- Include the required FusionCharts modules -->
    <script type='text/javascript' src="fusioncharts/fusioncharts.js"></script>
    <script type='text/javascript' src="fusioncharts/fusioncharts.charts.js"></script>
    <script type='text/javascript' src="fusioncharts/themes/fusioncharts.theme.fusion.js"></script>

</body>

</html>


<!DOCTYPE html>
<html>

<head>
    <!-- Include the required FusionCharts modules -->
    <script type='text/javascript' src="fusioncharts/fusioncharts.js"></script>
    <script type='text/javascript' src="fusioncharts/fusioncharts.charts.js"></script>
    <script type='text/javascript' src="fusioncharts/themes/fusioncharts.theme.fusion.js"></script>
</head>

<body></body>

</html>