Bind Event Listener
Events are signals that let you execute specific actions—such as manipulating the DOM, sending data to the server, and so on—using JavaScript, in response to any interactions/updates for a chart. Events can be used to trigger action(s) when a chart renders successfully, when data completes loading, when a data plot is clicked, when the mouse pointer is hovered over a data plot, and so on.
Events can be used for applications like monitoring the state of a system or a business. For example, you can listen to an event to observe the temperature of a deep freezer and display an alert message if the temperature falls below the minimum value.
Take a look at the Column 2D chart shown below:
Roll the mouse pointer over any one data plot and see how the text (the data label and the value) rendered below the chart changes.
For example, if you roll the mouse pointer over the Canada data plot, the following text is displayed below the chart:
You are currently hovering over Canada whose value is 180
The full code of the above sample is given below:
//Including react
import React, { Component } from 'react';
import { Platform, StyleSheet, Text, View, Alert } from 'react-native';
import FusionCharts from 'react-native-fusioncharts';
export default class ListenEvents extends Component {
constructor(props) {
super(props);
this.state = {
type: 'column2d',
width: '700',
height: '400',
dataFormat: 'json',
dataSource: {
"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"
}]
};
this.libraryPath = Platform.select({
// Specify fusioncharts.html file location
android: { uri: 'file:///android_asset/fusioncharts.html' },
ios: require('./assets/fusioncharts.html')
});
}
render() {
return (
<View style={styles.container}>
<Text style={styles.header}>Listen to events from chart</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}
events={this.state.events}
libraryPath={this.libraryPath} // set the libraryPath property
/>
</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
}
});
The above chart is rendered using the following steps:
Include the necessary libraries and components using
import
. For example,react-native-fusioncharts
,fusioncharts
, etc.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
.
- Set the chart type as
Specify the location of
fusioncharts.html
for Android and iOS.Add the
render()
function to call the events which will be triggered when the mouse pointer is rolled over a data plot.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>