Almost any website has some embedded analytics. You can find usage charts for every public Github repository or any social network today. Cube.js is designed to help developers build such analytical applications. It solves a plethora of different problems every production-ready analytic application needs to solve: analytic SQL generation, query results caching and execution orchestration, data pre-aggregation, security, and API for query results fetch.
We recently covered how to build an analytic dashboard with Cube.js and React, but what about Angular? Starting with version 0.8.4, the Cube.js Client ships with an Angular module for easy integration. Today I will show you how to build an analytical dashboard using Angular, Cube.js, and ng2-charts.
You can find a final dashboard here and a CodeSandbox with the source code below.
Setting up a Cube.js Backend
We covered this topic in other tutorials, so if you already have your Cube.js backend set up and running, you can skip this section.
You can install Cube.js CLI, which is used for various Cube.js workflows, via NPM or Yarn.
Let's prepare a Cube.js Backend to serve data for the dashboard we're building. Cube.js supports many databases and deployment options. You can learn more about it in the documentation. For this tutorial, we’ll use a Postgres database and deploy Cube.js to Heroku. Let's create a new Cube.js application using the CLI we just installed.
In case you don't have a database for the dashboard yet, you can download our demo e-commerce dataset for Postgres.
The next step is to define a data model. In a production application, you most likely will have multiple schema files, but for our demo app we are going to have only one cube. If you're not familiar with Cube.js data schema, there's an in-depth tutorial here.
Cube.js uses data schema to generate and execute SQL in the connected database. We can test it out by sending a sample request to the Cube.js REST API endpoint.
You can learn more about the Cube.js Query format here.
Finally, let’s deploy our backend to Heroku:
You can find full deployment guide in the documentation.
Now, when we have a functional backend running, we can move to the next part—building a dashboard. Cube.js has an Angular binding, which doesn’t provide any visualization itself, but is designed to work with any charting library. This way it provides great flexibility for developers to build unique and custom user experiences.
First, install ng-cli if you don't have it already:
Let's create a new Angular app using SCSS templates:
We'll be using an ng2-charts library, which is an Angular wrapper for Chart.js, to draw charts. The Cube.js Angular Client will be used to load the data from the backend, and finally Bootstrap will provide us with some nice styling. Let’s add these dependencies:
Next, add the required modules to the app.module.ts file:
Now we're finished with our app setup. Let's create a chart component:
Add some style and an element for ng2-charts:
Let's get the data for our chart. We need to define the inputs, which we'll pass to the ngx-chart component to allow customization:
To gather the data, we'll add an input for the query and use the Cube.js Angular watch API:
This will allow us to get and display new data every time the query changes. Now let's create a simple dashboard in our app.component: