Create a chart in React
FusionTime is a JavaScript charting library that helps you visualize, and explore time-series data. In FusionTime, you get lots of out-of-the-box interactivity, like, time navigator, date range selectors, tooltips with crosslines, interactive legend and more features which enhances the experience of exploring and understanding time series data.
We have built a simple react-fusioncharts
component which provides bindings for FusionTime. The react-fusioncharts
component allows you to easily add interactive time-series charts to any React project.
In this page, we'll see how to install FusionTime and render an interactive time-series chart using the react-fusioncharts
component.
Installation
Since FusionTime is distributed along with FusionCharts Suite, download/install the FusionCharts package to get access to FusionTime and other chart types of the FusionCharts Suite. To install follow the steps below:
react-fusioncharts
component via npm follow the steps below:react-fusioncharts
component
$ npm install react-fusioncharts --save
fusioncharts
package
$ npm install fusioncharts --save
react-fusioncharts
component follow the steps below:
<head>
<!-- Including react -->
<script type="text/javascript" src="https://unpkg.com/react@16/umd/react.development.js"></script>
<!-- Including react-dom -->
<script type="text/javascript" src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<!-- Including babel -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>
<!-- Including the fusioncharts core library -->
<script type="text/javascript" src="http://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.js"></script>
<!-- Including the fusiontime file to render charts -->
<script type="text/javascript" src="http://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.timeseries.js"></script>
<!-- Including react-fusioncharts component -->
<script type="text/javascript" src=" https://unpkg.com/[email protected]/dist/react-fusioncharts.js"></script>
</head>
react-fusioncharts
component follow the steps below:
<head>
<!-- Step 1 - Including react -->
<script type="text/javascript" src="path/to/local/react.js"></script>
<script type="text/javascript" src="path/to/local/react-dom.js"></script>
<!-- Step 2 - Including Babel for JSX transpiling -->
<script type="text/javascript" src="path/to/local/babel-core.js"></script>
<!-- Step 3 - Including the fusioncharts core library -->
<script type="text/javascript" src="path/to/local/fusioncharts.js"></script>
<!-- Step 4 - Including FusionTime file -->
<script type="text/javascript" src=" path/to/local/fusioncharts.timeseries.js"></script>
<!-- Step 5 - Including the react-fusioncharts component -->
<script type="text/javascript" src="path/to/local/react-fusioncharts.js"></script>
</head>
Create your First Chart
Let's create a time-series chart using react-fusioncharts
component showing Online sales of a SuperStore.
The chart will look as shown below:
The data for the above chart is too big to be displayed here. The table below shows the sample data of the above chart:
Time | Sales |
---|---|
1/4/2011 | 16.448 |
1/5/2011 | 272.736 |
1/5/2011 | 11.784 |
1/5/2011 | 3.54 |
1/6/2011 | 19.536 |
1/7/2011 | 2573.82 |
1/7/2011 | 609.98 |
FusionTime accepts a DataTable as it's data source. DataTable
is the tabular representation of data. To create the DataTable
, you need to provide the following:
schema
- which defines the properties of the columns.data
- values of each row and column of the DataTable.
For an instance of FusionTime, you can create
n
number ofDataTables
, but only 1DataStore
.
Now, let's learn how to prepare the schema and the data of the DataTable
.
Create the schema
The schema outlines each column represented in the above table. The schema contains an array which has multiple objects created in it. Each object represents a column of the above table.
name
andtype
are mandatory keys for each object. If the object type istime
thenformat
is also a mandatory key.
To define the schema, let's create a schema.json
file and copy the following code:
It is not mandatory to create the schema in a different
.json
file. You can also define the schema within the.html
file.
[{
"name": "Time",
"type": "date",
"format": "%-m/%-d/%Y"
}, {
"name": "Sales",
"type": "number"
}]
In the above code:
schema
is the variable in which the array is stored.- Each object of a schema maps to a column of the tabular representation of the data.
- The JSON object has the following attributes:
- name - Specify the name of the column of the tabular representation of data
- type - Specify the type of the column.
- format - Specify the input format of the date as per your data. In this example, the format is
%-m/%-d/%Y
. To know more on date formats click here.
Now that we have the schema ready, let's work on the data.
Create data
Data can be provided either in JSON format or 2D array format. We will use the 2D array format.
Data for the above chart is too big to be shown here. A sample of it has been used in the data object defined below:
var data = [
[
"1/4/2011",
16.448
],
[
"1/5/2011",
272.736
],
[
"1/5/2011",
11.784
],
[
"1/5/2011",
3.54
],
[
"1/6/2011",
19.536
],
[
"1/7/2011",
2573.82
],
]
Create a new data.json
file, and copy the above code there. Next, copy the entire data and replace it with the content of the data object in your 'data.json' file.
It is not mandatory to create a
data.json
file. You can also do the same in your HTML file.
In the above code:
data
is the variable in which the data array is stored.- Each object in the data array corresponds to a row in the tabular representation of the data.
- Each element in an object is represented by each object of the
schema
. The values in each object of the array represent the following:- Time according to the format
- Total Sales amount
We are all set with our data to create the chart.
By default, FusionTime applies the average function to aggregate the data and display on the chart. You can change the aggregate function from
average
to any other numeric calculation. To know more click here.
Now, let's create the files to render the above chart.
Render the chart
To render the chart, create a .js
file and copy the following code:
// Step 1 - Including react
import React from 'react';
// Step 2 - Including the react-fusioncharts component
import FusionCharts from 'fusioncharts';
// Step 3 - Including the fusiontime file
import TimeSeries from 'fusioncharts/fusioncharts.timeseries';
import ReactFC from '../lib/ReactFC';
// Step 4 - Adding the chart as dependency to the core fusioncharts
ReactFC.fcRoot(FusionCharts, TimeSeries);
// Step 5 - Creating the JSON object to store the chart configurations
const jsonify = res => res.json();
const dataFetch = fetch(
'https://raw.githubusercontent.com/fusioncharts/dev_centre_docs/master/assets/datasources/fusiontime/online-sales-single-series-area-data-plot/data.json'
).then(jsonify);
const schemaFetch = fetch( 'https://raw.githubusercontent.com/fusioncharts/dev_centre_docs/master/assets/datasources/fusiontime/online-sales-single-series-area-data-plot/schema.json'
).then(jsonify);
class SimpleTimeSeries extends React.Component {
constructor(props) {
super(props);
// In this method we are fetching our data and schema from remote URLs and creating our DataTable
this.onFetchData = this.onFetchData.bind(this);
this.state = {
timeseriesDs: {
type: 'timeseries',
renderAt: 'container',
width: '600',
height: '400',
dataSource: {
caption: { text: 'Online Sales of a SuperStore in the US' },
data: null,
yAxis: [{
plot: [{
value: 'Sales ($)'
}]
}]
}
}
};
}
// We are creating our DataStore immediately after a component is mounted
componentDidMount() {
this.onFetchData();
}
onFetchData() {
Promise.all([dataFetch, schemaFetch]).then(res => {
const data = res[0];
const schema = res[1];
// Here we are creating our DataTable
const fusionTable = new FusionCharts.DataStore().createDataTable(data, schema);
const timeseriesDs = Object.assign({}, this.state.timeseriesDs);
timeseriesDs.dataSource.data = fusionTable;
this.setState({
timeseriesDs
});
});
}
render() {
return (
{this.state.timeseriesDs.dataSource.data ? (
) : (
'loading'
)}
);
}
}
<html>
<head>
<!-- Including react -->
<script type="text/javascript" src=" https://unpkg.com/react@16/umd/react.development.js"></script>
<!-- Including react-dom -->
<script type="text/javascript" src=" https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<!-- Including babel -->
<script type="text/javascript" src=" https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<!-- Including the fusioncharts core library -->
<script type="text/javascript" src=" http://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.js"></script>
<!-- Including the fusiontime library to render charts -->
<script type="text/javascript" src=" http://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.timeseries.js"></script>
<!-- Including react-fusioncharts component -->
<script type="text/javascript" src=" https://unpkg.com/[email protected]/dist/react-fusioncharts.min.js"></script>
<script type="text/jsx">
ReactFC.fcRoot(FusionCharts);
const jsonify = res => res.json();
const dataFetch = fetch( 'https://raw.githubusercontent.com/fusioncharts/dev_centre_docs/fusiontime-beta-release/charts-resources/fusiontime/online-sales-single-series/data.json').then(jsonify);
const schemaFetch = fetch('https://raw.githubusercontent.com/fusioncharts/dev_centre_docs/fusiontime-beta-release/charts-resources/fusiontime/online-sales-single-series/schema.json').then(jsonify);
</script>
<script type="text/babel">
class ChartViewer extends React.Component {
constructor(props) {
super(props);
this.onFetchData = this.onFetchData.bind(this);
this.state = {
timeseriesDs: {
type: 'timeseries',
renderAt: 'container',
width: '600',
height: '400',
dataSource: {
caption: { text: 'Online Sales of a SuperStore in the US' },
data: null,
yAxis: [{
plot: [{
value: 'Sales ($)'
}]
}]
}
}
};
}
componentDidMount() {
this.onFetchData();
}
onFetchData() {
Promise.all([dataFetch, schemaFetch]).then(res => {
const data = res[0];
const schema = res[1];
const fusionTable = new FusionCharts.DataStore().createDataTable(data, schema);
const timeseriesDs = Object.assign({}, this.state.timeseriesDs);
timeseriesDs.dataSource.data = fusionTable;
this.setState({
timeseriesDs
});
});
}
render() {
return (
<div>
{this.state.timeseriesDs.dataSource.data ? (
<ReactFC {...this.state.timeseriesDs} />
) : (
'loading'
)}
</div>
);
}
}
ReactDOM.render(
<ChartViewer />,
document.getElementById('chart-container')
);
</script>
</head>
<body>
<div id='chart-container'></div>
</body>
</html>
<html>
<head>
<!-- Including react -->
<script type="text/javascript" src="path/to/local/react.development.js"></script>
<!-- Including react-dom -->
<script type="text/javascript" src="path/to/local/react-dom.development.js"></script>
<!-- Including babel -->
<script type="text/javascript" src="path/to/local/babel.min.js"></script>
<!-- Including the fusioncharts core library -->
<script type="text/javascript" src="path/to/local/fusioncharts.js"></script>
<!-- Including the fusiontime library to render charts -->
<script type="text/javascript" src="path/to/local/fusioncharts.timeseries.js"></script>
<!-- Including react-fusioncharts component -->
<script type="text/javascript" src="path/to/local/react-fusioncharts.min.js"></script>
<script type="text/babel">
ReactFC.fcRoot(FusionCharts);
const jsonify = res => res.json();
const dataFetch = fetch( 'https://raw.githubusercontent.com/fusioncharts/dev_centre_docs/fusiontime-beta-release/charts-resources/fusiontime/online-sales-single-series/data.json').then(jsonify);
const schemaFetch = fetch('https://raw.githubusercontent.com/fusioncharts/dev_centre_docs/fusiontime-beta-release/charts-resources/fusiontime/online-sales-single-series/schema.json').then(jsonify);
</script>
<script type="text/jsx">
class ChartViewer extends React.Component {
constructor(props) {
super(props);
this.onFetchData = this.onFetchData.bind(this);
this.state = {
timeseriesDs: {
type: 'timeseries',
renderAt: 'container',
width: '600',
height: '400',
dataSource: {
caption: { text: 'Online Sales of a SuperStore in the US' },
data: null,
yAxis: [{
plot: [{
value: 'Sales ($)'
}]
}]
}
}
};
}
componentDidMount() {
this.onFetchData();
}
onFetchData() {
Promise.all([dataFetch, schemaFetch]).then(res => {
const data = res[0];
const schema = res[1];
const fusionTable = new FusionCharts.DataStore().createDataTable(data, schema);
const timeseriesDs = Object.assign({}, this.state.timeseriesDs);
timeseriesDs.dataSource.data = fusionTable;
this.setState({
timeseriesDs
});
});
}
render() {
return (
<div>
{this.state.timeseriesDs.dataSource.data ? (
<ReactFC {...this.state.timeseriesDs} />
) : (
'loading'
)}
</div>
);
}
}
ReactDOM.render(
<ChartViewer />,
document.getElementById('chart-container')
);
</script>
</head>
<body>
<div id='chart-container'></div>
</body>
</html>
In the above code:
- Create a React Component
ChartViewer
. - Add
timeseriesDs
in component state. - Include the
fusioncharts
library. - Include
data.json
andschema.json
files. - Define the chart configuration in the FusionCharts constructor:
- Set the type as
timeseries
. - Set the chart container as
container
using therenderAt
property. - Set the width and height (in pixels).
- Set the name of the
DataTable
as the value of thedata
property ofdataSource
. - Set the data to create the chart.
- Specify the caption of the chart using
text
attribute incaption
object.
- Set the type as
- Create an empty storage as
fusionDataStore
usingFusionCharts.DataStore
. - Create a
DataTable
within the empty storage usingfusionDataStore.createDataTable
and pass theschema
anddata
to theDataTable
.