Angular Drilldown Chart is a popular data visualization tool that allows users to present hierarchical data in a series of nested charts. In this tutorial, we will explore how to create a drilldown chart using Angular and Highcharts library.
Step 1: Setting up the Angular project
To get started, make sure you have Angular CLI installed on your system. If not, you can install it using the following command:
npm install -g @angular/cli
Once you have Angular CLI installed, you can create a new Angular project by running the following command:
ng new my-project
Navigate to the project directory and install Highcharts library using the following command:
npm install highcharts
Step 2: Implementing Drilldown chart in Angular
First, import Highcharts modules in your Angular component where you want to create the drilldown chart. Add the following import statement at the top of the file:
import * as Highcharts from 'highcharts';
import Drilldown from 'highcharts/modules/drilldown';
Drilldown(Highcharts);
Next, create a function to generate the drilldown chart in your component:
generateChart() {
Highcharts.chart('container', {
chart: {
type: 'column'
},
title: {
text: 'Sales Data'
},
xAxis: {
type: 'category'
},
yAxis: {
title: {
text: 'Total Sales'
}
},
series: [{
name: 'Product Sales',
data: [{
name: 'Category A',
y: 100,
drilldown: 'category-A'
}, {
name: 'Category B',
y: 200,
drilldown: 'category-B'
}]
}],
drilldown: {
series: [{
id: 'category-A',
name: 'Category A Details',
data: [
['Product A', 50],
['Product B', 50]
]
}, {
id: 'category-B',
name: 'Category B Details',
data: [
['Product C', 100],
['Product D', 100]
]
}]
}
});
}
In this function, we are creating a column chart with two main categories (‘Category A’ and ‘Category B’) and their respective drilldown data. Each main category has drilldown data containing product names and sales values.
Step 3: Rendering the Drilldown chart
Call the generateChart()
function inside the ngOnInit()
lifecycle hook of your component to render the drilldown chart when the component is initialized:
ngOnInit() {
this.generateChart();
}
Step 4: Displaying the chart in the template
In your component’s HTML template, add a container element with the id ‘container’ where the chart will be rendered:
<div id="container"></div>
Step 5: Running the Angular app
Finally, run your Angular app using the following command:
ng serve --open
Navigate to the URL displayed in the terminal to see the drilldown chart in action. You should see the main categories displayed as columns, and when you click on a category, the drilldown data for that category will be revealed in a nested chart.
Congratulations! You have successfully created a drilldown chart using Angular and Highcharts. Feel free to customize the chart further by exploring Highcharts documentation and experimenting with different chart types and options.