Started as a simple charting tool for monitoring a snow depth near the owner's country house in Norway, Highcharts quickly became one of the most popular visualization libraries. It provides a lot of great built-in interactive features and is easy to use.
In this tutorial we are going to build a simple e-commerce dashboard with Cube.js and Highcharts. We’ll use the main Highcharts library, as well as Maps, Stock, and Solid Gauge modules.
Please keep in mind that Highcharts libraries are available under different licenses, depending on whether it is intended for commercial/government use, or for personal or non-profit projects. Make sure to check its license page.
Below, you can see the demo of the dashboard that we’re going to build.
To implement this example, we’ll need:
- Database (we will use PostgreSQL) with sample data
- Cube.js backend to handle communications between our database and the frontend
- The frontend application (we will build one with React)
We’re going to use a PostgreSQL database and an example e-commerce dataset. Use the following commands to download and import the example dataset.
Next, let’s install the Cube.js CLI and create a new project.
Cube.js uses environment variables inside the
.env file for configuration. Update the contents of the
.env file with your own database credentials.
Now, let’s start with the Cube.js backend.
At this step, you can find the Cube.js playground at http://localhost:4000.
Here, you can see all the tables from our database and we can choose any of them to generate the schema.
The Cube.js Data Schema concept is based on multidimensional analysis and should look familiar to those with experience in OLAP cubes.
The two main entities are measures and dimensions: dimensions are ”as is” properties that we get from our database, but measures are results of aggregation operations like count, sum, average, and others.
In this example, we need an
users table. Please, check it and click “Generate Schema.” Cube.js will then generate
Users.js files inside the
Let’s update the
We’ll keep only
id dimensions, and
count measure because we’ll need to use them in our example.
That’s it for our backend. We’ve configured the database and created the Cube.js. backend. Now, we’re ready to start working on our frontend application.
Frontend Dashboard with Highcharts
Let’s generate our app with Cube.js templates. Navigate to the Dashboard App tab and select “Create custom application” with React and Ant Design.
It will take some time to create a dashboard app and install dependencies. Once it is finished, you should see the
dashboard-app folder inside your project’s folder.
Next, let’s install the dependencies we’ll need. Run the following commands in the
The command above installs the following dependencies:
Feel free to remove all the files inside the
src folder and the
page folder, as well as update the
dashboard/index.js file with the following content.
Our application will have the following structure:
Appas the main app component
Dashboardcomponent that stores data and manages the app state
Stock, and other chart components that manage chart render according to applications data and state.
Let’s create the
<Dashboard /> component in the
dashboard-app/src/components/Dashboard.js file with the following content. (We’ll create the
<Map /> component later):
In the above snippet, we did several things. We imported useCubeQuery React hook first.
Next, to render the amount of orders in each state, we need to change the data into Highcharts’ format, where the first element is the state key and the second element is the value.
We’re using resultSet.tablePivot() to access data returned from the backend and to prepare it for rendering.
Now, we’re ready to pass our data to the Map chart. Let’s create a new
dashboard-app/src/components/Map.js file with the following content.
Map.js file, we imported
useEffect hooks, and a bunch of Highcharts components. Then, we defined chart options based on Highcharts Map API specs.
staticOptions, we can set map styling, source, data, event handlers, and other options.
Lastly, we merged our
props.data and then passed it to the Highcharts component.
That’s all for our
Now, we just need to update the ‘dashboard-app/App.js’ to include the
<Dashboard /> component:
...and we’re ready to check out our first chart!
Navigate to http://localhost:3000 in your browser, and you should be able to see the map chart that we’ve just built.
A similar workflow can be used to create other chart types, as in the GIF below.
- Define the static chart options, according to Highcharts API documentation.
- Add data to options.series.
- Pass options to the Highcharts component.
I hope you’ve found this tutorial helpful. If you have any questions or any kind of feedback, please let us know in this Slack channel.