Here’s the thing about dashboards: No matter what you do, you can’t escape them. Every department inside every company is chasing a goal—if not several—and there’s no better way to monitor a goal’s status than to have a compact dashboard that monitors all related metrics.
Just think about it. Marketing is running after leads, sales is running after closing those leads and developers are running to solve problems. There’s a dashboard for all of these cases—and, if you’re a developer, at some point you’ll have to make a dashboard for your company. So, I why not learn how to create one now?
In this tutorial, we’ll use JavaScript charts library, offered by FusionCharts, for making charts. But instead of doing it in vanilla JavaScript, we’re going to use React; the hottest JavaScript framework at the moment (and rightly so, if you ask me). In our dashboard we’ll explore the performance of Acme, Inc. in 2015; we’ll see what its top revenue generating countries, monthly revenue trend and highest performing product categories. This is what our final dashboard will look like:
FusionCharts makes our job a bit easier by providing a dedicated plugin for making charts in React. Before we jump right in, let’s first explore a bit more about React.
React is a JavaScript library for creating reusable View components for the Web. React is fast in that if you want to update your View, you simply re-render the whole View and React will figure out the optimal way to update the Document Object Model (DOM). To achieve this, React uses Virtual DOM and DOM Diff algorithm. You can read more about it on this StackOverflow thread.
As mentioned, to create our dashboard we’ll use React and FusionCharts. To abstract out the details of FusionCharts in a React-friendly manner, we’ll use the React Plugin provided by FusionCharts. So, we need to include the JavaScript files of React, FusionCharts and React-FusionCharts plugin. To get started, embed the above three scripts in the HTML page using <script>
tags.
To write applications in React, it’s better if we use JSX syntax. Although it’s not advised, and applications can be created in vanilla javascript, using JSX will give a clear picture of the code and the code will be concise. For that reason we’ll build this dashboard in JSX. You can read more about JSX here.
1<script src='https://fb.me/react-with-addons-0.14.6.js'></script>
2<script src='https://fb.me/JSXTransformer-0.13.3.js'></script> // React Scripts
3
4<script src='js/fusioncharts.js'></script>
5<script src='js/fusioncharts.charts.js'></script>
6<script src='js/fusioncharts.powercharts.js'></script> // FusionCharts files
7
8<script src='js/react-fusioncharts.min.js'></script> // React FusionCharts plugin
9
10<script src='js/fusioncharts.theme.carbon.js'></script> // theme file
Note: Including the JSX Transformer is only suggested for development applications. For production-ready code, precompile the JSX and you won’t need to include this file.
With the basic setup ready, we’ll add some markup and React JSX code to create the dashboard.
In the dashboard we’re building, we’re creating three visualizations of various revenue metrics of Acme Inc. We’ll create a column chart to show revenues from top six revenue generating countries, a spline chart (smoother version of line chart) to show the revenue distribution over various months of the year and a donut chart to figure out which product category generated the highest percentage share of the revenue. In short, we’ll render column, spline and donut charts. vWe will create the column chart spanning the entire width of the page, and spline and donut charts in a two column layout to span 50 percent width. The following HTML and CSS would create HTML container to create this layout. The HTML containers are where the charts will be rendered.
HTML:
1<div class="chart-row">
2 <div id="country-revenue"></div>
3</div>
4
5<div class="chart-row">
6 <div id="monthly-revenue" class="inline-chart">
7 </div>
8 <div id="product-revenue" class="inline-chart">
9 </div>
10</div>
CSS:
1.chart-row {
2 margin-bottom: 15px;
3}
4.inline-chart {
5 display: inline-block;
6 width: 48%;
7 margin-left: 1%;
8}
Now we’ll prepare the data for these three charts and render them in the containers we have just created.
With the HTML structure ready, we will create a root React component based on our HTML structure. We make the HTML structure into a valid JSX and return it in the render function of the component. To achieve that, we make a few changes like changing class
to className
.
This is the react component that we create:
1var DashboardApp = React.createClass({
2 render: function() {
3 return (
4 <div>
5 <h1 className="main-title">Acme Inc. Revenue Analysis for 2015</h1>
6 <div id="interactive-dashbaord"></div>
7 <div className="chart-row">
8 <div id="country-revenue">
9 // country revenue chart here
10 </div>
11 </div>
12 <div className="chart-row">
13 <div id="monthly-revenue" className="inline-chart">
14 // chart 2 here - spline
15 </div>
16 <div id="product-revenue" className="inline-chart">
17 // chart 3 here - donut
18 </div>
19 </div>
20 </div>
21 );
22 }
23});
Next we render this component into an HTML element:
1React.render(
2 <DashboardApp />,
3 document.getElementById("dashboard")
4);
This is an empty react component that we have created and rendered. We need to create the charts in it and render them in the skeleton markup that we’ve designed (we’ll cover this in the following sections, so sit tight!).
Now, let’s go through the process of creating one chart (column) in detail. Creating the other two charts is similar and you’ll notice the differences in the description below.
Every charting library has a slightly different input data format. Since we’re using FusionCharts for our project, we need to format our data in a format that FusionCharts understands. It accepts JSON data as an array of objects containing label and value. This is what the syntax looks like:
1[{
2 "label": "Orange",
3 "value": "90"
4},{
5 "label": "Yellow",
6 "value": "60"
7}...]
Chart configuration contains various attributes of the chart-like chart type, caption, sub-caption and theme, to name a few. It also contains the data to be plotted. Remember those differences in attributes I just mentioned? These include data, chart type, height and width. There are other theme-related attributes as well, which will be mostly the same for all three charts.
A sample chart configuration looks something like this:
1{
2 type: "doughnut2d",
3 renderAt: "product-revenue",
4 width: '100%',
5 height: 400,
6 dataFormat: "json",
7 dataSource: {
8 chart: {
9 caption: "Top 5 stores in last month by revenue",
10 theme: "carbon"
11 },
12 data: [{
13 "label": "Jan",
14 "value": "657000"
15 }, {
16 "label": "Feb",
17 "value": "138000"
18 },
19 ... {
20 "label": "Dec",
21 "value": "730000"
22 }
23 ]
24 }
25}
With the chart configuration ready, we can create the React chart. We’ll use the React plugin API to create it. The only input to the plugin is the configuration that we created earlier. We can simply create it using the following code.
1<react_fc.FusionCharts {...countryChartConfigs} />
The chart we created above is a column chart, the other two charts are spline and donut. We’ll create chart configurations for these two and mention the right types and create the React chart component.
1<react_fc.FusionCharts {...monthlyChartConfigs} />
2<react_fc.FusionCharts {...productChartConfigs} />
Now we need to place the three chart components appropriately in the JSX of the DashboardApp
we created earlier. This is the new JSX after placing the chart components.
1<div>
2 <h1 className="main-title">Acme Inc. Revenue Analysis for 2015</h1>
3 <div id="interactive-dashbaord"></div>
4 <div className="chart-row">
5 <div id="country-revenue">
6 <react_fc.FusionCharts {...countryChartConfigs} />
7 </div>
8 </div>
9 <div className="chart-row">
10 <div id="monthly-revenue" className="inline-chart">
11 <react_fc.FusionCharts {...monthlyChartConfigs} />
12 </div>
13 <div id="product-revenue" className="inline-chart">
14 <react_fc.FusionCharts {...productChartConfigs} />
15 </div>
16 </div>
17</div>
After adding this you should see the dashboard rendered on your page. If you had any issues or want to see the source code of the project, head over to project’s GitHub repo.
Using the process I described above, you can create relatively sophisticated charts and dashboards in React. Of course, we all come across issues when we try to build things on our own. To help you out in times like these, I’ve listed some resources below.