Explore how to create your own interactive multi-page dashboard with Angular and Material. You will learn step by step how to build a comprehensive dashboard which retrieves and visualizes data from your database without writing SQL code.
Overview
Angular 🅰️ is the web framework of choice for many professional developers. According to Stack Overflow Developer Survey 2020, only just about ~10 % of developers prefer React to Angular.
Material is the reference implementation of Material Design components for Angular. It provides a lot of ready-to-use components to build web applications, including dashboards, fast and easy.
In this guide, we'll learn how to build a full-stack dashboard with KPIs, charts, and a data table. We'll go from data in the database to the interactive, filterable, and searchable dashboard.
We're going to use Cube for our analytics API. It removes all the hustle of building the API layer, generating SQL, and querying the database. It also provides many production-grade features like multi-level caching for optimal performance, multi-tenancy, security, and more.
Below you can see an animated image of the application we're going to build. Also, check out the live demo and the full source code available on GitHub.
Analytical API with Cube
We're going to build the dashboard for an e-commerce company that wants to track its overall performance and orders' statuses. Let's assume that the company keeps its data in an SQL database. So, in order to display that data on a dashboard, we're going to create an analytical API.
For that, we'll use the Cube command-line utility (CLI).
Cube supports all popular databases, and the API will be pre-configured to work with a particular database type. We’ll use a PostgreSQL database. Please make sure you have PostgreSQL installed.
Once the database is ready, the API can be configured to connect to the database. To do so, we provide a few options via the .env file in the root of the Cube project folder (angular-dashboard):
CUBEJS_DB_NAME=ecom
CUBEJS_DB_TYPE=postgres
CUBEJS_API_SECRET=secret
CUBEJS_DEV_MODE=true
Now we can run the API!
In development mode, the API will also run the Cube Playground. It's a time-saving web application that helps to create a data schema, test out the charts, and generate a React dashboard boilerplate. Run the following command in the Cube project folder:
We'll use the Cube Playground to create a data schema. It's essentially a JavaScript code that declaratively describes the data, defines analytical entities like measures and dimensions, and maps them to SQL queries. Here is an example of the schema which can be used to describe users’ data.
cube('Users',{
sql:'SELECT * FROM users',
measures:{
count:{
sql:`id`,
type:`count`
},
},
dimensions:{
city:{
sql:`city`,
type:`string`
},
signedUp:{
sql:`created_at`,
type:`time`
},
companyName:{
sql:`company_name`,
type:`string`
},
},
});
Cube can generate a simple data schema based on the database’s tables. If you already have a non-trivial set of tables in your database, consider using the data schema generation because it can save time.
For our API, we select the line_items, orders, products, and users tables and click “Generate Schema.” As the result, we'll have 4 generated files in the schema folder—one schema file per table.
Once the schema is generated, we can build sample charts via web UI. To do so, navigate to the “Build” tab and select some measures and dimensions from the schema.
The "Build" tab is a place where you can build sample charts using different visualization libraries and inspect every aspect of how that chart was created, starting from the generated SQL all the way up to the JavaScript code to render the chart. You can also inspect the Cube query encoded with JSON which is sent to Cube API.
Frontend application
Creating a complex dashboard from scratch usually takes time and effort. Fortunately, Angular provides a tool that helps to create an application boilerplate code with just a few commands. Adding the Material library and Cube as an analytical API is also very easy.
Installing the libraries
So, let's use Angular CLI and create the frontend application inside the angular-dashboard folder:
npminstall -g @angular/cli # Install Angular CLI
ng new dashboard-app # Create an app
cd dashboard-app # Change the folder
ng serve # Run the app
Congratulations! Now we have the dashboard-app folder in our project. This folder contains the frontend code that we're going to modify and evolve to build our analytical dashboard.
Now it's time to add the Material library. To install the Material library to our application, run:
ng add @angular/material
Choose a custom theme and the following options:
Set up global Angular Material typography styles? - Yes
Set up browser animations for Angular Material? - Yes
Great! We'll also need a charting library to add charts to the dashboard. Chart.js is the most popular charting library, it's stable and feature-rich. So...
It's time to add the Chart.js library. To install it, run:
npminstall ng2-charts
npminstall chart.js
Also, to be able to make use of ng2-charts directives in our Angular application we need to import ChartsModule. For that, we add the following import statement in the app.module.ts file:
+ import { ChartsModule } from 'ng2-charts';
The second step is to add ChartsModule to the imports array of the @NgModule decorator as well:
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
+ ChartsModule
],
providers: [],
bootstrap: [AppComponent]
})
Finally, it's time to add Cube. This is the final step that will let our application access the data in our database via an analytical API is to install Cube client libraries for Angular. Run:
npminstall --save @cubejs-client/ngx
npminstall --save @cubejs-client/core
Now we can add CubejsClientModule to your app.module.ts file:
...
+ import { CubejsClientModule } from '@cubejs-client/ngx';
CubejsClientModule provides CubejsClient which you can inject into your components or services to make API calls and retrieve data:
import{CubejsClient}from'@cubejs-client/ngx';
exportclassAppComponent{
constructor(privatecubejs:CubejsClient){}
ngOnInit(){
this.cubejs.load({
measures:["some_measure"]
}).subscribe(
resultSet=>{
this.data= resultSet.chartPivot();
},
err=>console.log('HTTP Error', err)
);
}
}
So far so good! Let's make it live.
Creating the first chart
Let's create a generic bar-chart component using Angular CLI. Run:
$ ng g c bar-chart # Oh these single-letter commands!
This command will add four new files to our app because this is what Angular uses for its components:
src/app/bar-chart/bar-chart.component.html
src/app/bar-chart/bar-chart.component.ts
src/app/bar-chart/bar-chart.component.scss
src/app/bar-chart/bar-chart.component.spec.ts
Open bar-chart.component.html and replace the content of that file with the following code:
<div>
<divstyle="display: block">
<canvasbaseChart
height="320"
[datasets]="barChartData"
[labels]="barChartLabels"
[options]="barChartOptions"
[legend]="barChartLegend"
[chartType]="barChartType">
</canvas>
</div>
</div>
Here we’re using the baseChart directive which is added to a canvas element. Furthermore, the datasets, labels, options, legend, and chartType attributes are bound to class members which are added to the implementation of the BarChartComponent class in bar-chart-component.ts:
Nice work! 🎉 That's all we need to display our first chart with the data loaded from Postgres via Cube.
In the next part, we'll make this chart interactive by letting users change the date range from "This year" to other predefined values.
Interactive Dashboard with Multiple Charts
In the previous part, we've created an analytical API and a basic dashboard with the first chart. Now we're going to expand the dashboard so it provides the view of key performance indicators of our e-commerce company.
Custom Date Range
As the first step, we'll let users change the date range of the existing chart.
For that, we'll need to make a change to the dashboard-page.component.ts file:
// ...
export class DashboardPageComponent implements OnInit {
Well done! 🎉 Here's what our dashboard application looks like:
KPI Chart
The KPI chart can be used to display business indicators that provide information about the current performance of our e-commerce company. The chart will consist of a grid of tiles, where each tile will display a single numeric KPI value for a certain category.
First, let's add the countUp package to add the count-up animation to the values on the KPI chart. Run the following command in the dashboard-app folder:
npm i ngx-countup @angular/material/progress-bar
We weed to import these modules:
+ import { CountUpModule } from 'ngx-countup';
+ import { MatProgressBarModule } from '@angular/material/progress-bar'
@NgModule({
imports: [
// ...
+ CountUpModule,
+ MatProgressBarModule
],
...
})
Second, let's add an array of cards we're going to display to the dashboard-page.component.ts file:
export class DashboardPageComponent implements OnInit {
The only thing left is to adjust the Cube schema. While doing it, we'll learn an important aspect of Cube...
Let's learn how to create custom measures in the data schema and display their values. In the e-commerce business, it's crucial to know the share of completed orders. To enable our users to monitor this metric, we'll want to display it on the KPI chart. So, we will modify the data schema by adding a custom measure (percentOfCompletedOrders) which will calculate the share based on another measure (completedCount).
Let's customize the "Orders" schema. Open the schema/Orders.js file in the root folder of the Cube project and make the following changes:
add the completedCount measure
add the percentOfCompletedOrders measure
cube(`Orders`, {
sql: `SELECT * FROM public.orders`,
// ...
measures: {
count: {
type: `count`,
drillMembers: [id, createdAt]
},
number: {
sql: `number`,
type: `sum`
},
+ completedCount: {
+ sql: `id`,
+ type: `count`,
+ filters: [
+ { sql: `${CUBE}.status = 'completed'` }
+ ]
+ },
+ percentOfCompletedOrders: {
+ sql: `${completedCount} * 100.0 / ${count}`,
+ type: `number`,
+ format: `percent`
+ }
},
// ...
Great! 🎉 Now our dashboard has a row of nice and informative KPI metrics:
Doughnut Chart
Now, using the KPI chart, our users are able to monitor the share of completed orders. However, there are two more kinds of orders: "processed" orders (ones that were acknowledged but not yet shipped) and "shipped" orders (essentially, ones that were taken for delivery but not yet completed).
To enable our users to monitor all these kinds of orders, we'll want to add one final chart to our dashboard. It's best to use the Doughnut chart for that, because it's quite useful to visualize the distribution of a certain metric between several states (e.g., all kinds of orders).
First, let's create the DoughnutChart component. Run:
Awesome! 🎉 Now the first page of our dashboard is complete:
Multi-Page Dashboard with Data Table
Now we have a single-page dashboard that displays aggregated business metrics and provides at-a-glance view of several KPIs. However, there's no way to get information about a particular order or a range of orders.
We're going to fix it by adding a second page to our dashboard with the information about all orders. However, we'll need a way to navigate between two pages. So, let's add a navigation side bar.
Navigation Side Bar
Now we need a router, so let's add a module for this. Run:
ng generate module app-routing --flat --module=app
Wow! 🎉 Here's our navigation side bar which can be used to switch between different pages of the dashboard:
Data Table for Orders
To fetch data for the Data Table, we'll need to customize the data schema and define a number of new metrics: amount of items in an order (its size), an order's price, and a user's full name.
First, let's add the full name in the "Users" schema in the schema/Users.js file:
cube(`Users`, {
sql: `SELECT * FROM public.users`,
// ...
dimensions: {
// ...
firstName: {
sql: `first_name`,
type: `string`
},
lastName: {
sql: `last_name`,
type: `string`
},
+ fullName: {
+ sql: `CONCAT(${firstName}, ' ', ${lastName})`,
+ type: `string`
+ },
age: {
sql: `age`,
type: `number`
},
createdAt: {
sql: `created_at`,
type: `time`
}
}
});
Then, let's add other measures to the "Orders" schema in the schema/Orders.js file.
For these measures, we're going to use the subquery feature of Cube. You can use subquery dimensions to reference measures from other cubes inside a dimension. Here's how to defined such dimensions:
cube(`Orders`, {
sql: `SELECT * FROM public.orders`,
dimensions: {
id: {
sql: `id`,
type: `number`,
primaryKey: true,
+ shown: true
},
status: {
sql: `status`,
type: `string`
},
createdAt: {
sql: `created_at`,
type: `time`
},
completedAt: {
sql: `completed_at`,
type: `time`
},
+ size: {
+ sql: `${LineItems.count}`,
+ subQuery: true,
+ type: 'number'
+ },
+
+ price: {
+ sql: `${LineItems.price}`,
+ subQuery: true,
+ type: 'number'
+ }
}
});
Now we're ready to add a new page. Let's creating the table-page component. Run:
Voila! 🎉 Now we have a table which displays information about all orders:
However, its hard to explore this orders using only the controls provided. To fix this, we'll add a comprehensive toolbar with filters and make our table interactive.
For this, let's create the table-filters component. Run:
Now you should be able to create comprehensive analytical dashboards powered by Cube using Angular and Material to display aggregate metrics and detailed information.
Check out Cube Cloud for yourself. Learn more about how we support embedded analytics though our universal sematnic layer.