Highcharts - Loading Image With Angular Custom Directive And Web API

This article will demonstrate how to show loading message or any spinner image when your chart is being loaded or the drill-down chart is being loaded, using Highcharts Library in AngularJS with Web API.

We load messages or images or essential parts when we are working with Asynchronous programming because we don’t know when our data will be ready. So, so far we have to show some message for end users that data is preparing and once the data is ready, we will show the data on UI. So, we are accomplishing the same thing  here; if we are using charting in our website and when we load our page then it takes time to get the data from the server and plot it on the chart.

If data is not available then we should display some kind of message that confirms that data is on the way and once data will ready, it will be available on the chart.

This is part third of this series “Highcharts with Angular JS custom directive and Web API”. In the first two parts, we have covered how to create charts in Angular JS and Web API using Highcharts, how to prepare data, how we can get the drill-down functionality and so one.

But this demonstration will cover all the steps which are required to show loading image or message when our chart is loading or we can say data is preparing behind

The whole demonstration is divided into three different parts, as follows.

  • Part 1: Create charts using Highcharts Library and AngularJS custom directive with Web API
  • Part 2: Implement Drilldown functionality with charts [Nested charts]
  • Part 3: Show loading image/message when rendering the charts

So, let’s move to practical implementation of PART 3 and create a loading image for the charts. This demonstration will cover only loading image or message functionality in the chart and we use existing codes which we have created in PART 1 and PART 2. So, to understand it clear just follows up PART 1 and PART 2 also.

To add message or image in Highcharts, we have a showLoading() and hideLoading() methods available in Highcharts library. Using these functions we can achieve loading and unloading message or image easily.

Let’s see how we can use these methods in the code. So, before binding any data to the series of the chart, we have to call showLoading() method and pass the message or any image path with <img> as a parameter as below.

image
Ref- https://cdn.codemyui.com/wp-content/uploads/2016/08/Circular-Water-Fill-Loading-Animation.gif

After that you have to check data is available or not, if data is available from API call or some other source then we can bind our data to the series of the charts. Once data is bind to chart, then immediately hide the loading message or image from the screen using the hideLoading() method as below.

  1. scope.$watch('data'function (newValue) {  
  2.     if (newValue != undefined) {  
  3.   
  4.         _.forEach(scope.data, function (item) {  
  5.             scope.categories.push(item.Name);  
  6.             scope.seriesData.push({ name: item.Name, y: item.TotalRuns, drilldown: item.Name });  
  7.         });  
  8.   
  9.         $timeout(function () {  
  10.             scope.chartConfig.addSeries({  
  11.                 name: 'Top ODI Batsman',  
  12.                 data: scope.seriesData  
  13.             });  
  14.             scope.chartConfig.hideLoading();  
  15.         }, 2000);  
  16.     }  
  17. });  

Same we can achieve in Drilldown charts. As we enter in drilldown event, we have to show our image or message using showLoading() method and hide the image or message once data bind with drilldown chart using hideLoading() method. See the below highlighted code.

  1. drilldown: function (e) {  
  2. if (!e.seriesOptions) {  
  3.     var chart = this;  
  4.     scope.chartConfig.showLoading('<img src="https://cdn.codemyui.com/wp-content/uploads/2016/08/Circular-Water-Fill-Loading-Animation.gif" width="100%" height="100%">');  
  5.   
  6.     var selectedColumn = e.point.name;  
  7.     var scoredRunFilteredData = _.filter(scope.runScoredByYear, { 'Name': selectedColumn });  
  8.   
  9.     var scoredRunFilteredDataPoints = [];  
  10.   
  11.     for (var i = 0; i < scoredRunFilteredData.length; i++) {  
  12.         scoredRunFilteredDataPoints.push({ name: scoredRunFilteredData[i].Year, y: scoredRunFilteredData[i].Runs });  
  13.     }  
  14.   
  15.     chart.drilldowns = {  
  16.         series1: {  
  17.             type: 'line',  
  18.             name: selectedColumn,  
  19.             data: scoredRunFilteredDataPoints,  
  20.             color: 'red'  
  21.         }  
  22.     };  
  23.   
  24.     var series = [chart.drilldowns.series1];  
  25.   
  26.     setTimeout(function () {  
  27.         chart.addSingleSeriesAsDrilldown(e.point, series[0]);  
  28.         chart.setTitle({ text: e.point.name + ' <b>Scored Runs</b>' });  
  29.         chart.applyDrilldown();  
  30.   
  31.         chart.hideLoading();  
  32.     }, 1000);  
  33.   }  
  34. }  

If you are playing with data when moving back to main chart using drillup event and it takes time to render then you should also show the message here in drillup event.

  1. drillup: function(e) {  
  2.     var chart = this;  
  3.     scope.chartConfig.showLoading('<img src="https://cdn.codemyui.com/wp-content/uploads/2016/08/Circular-Water-Fill-Loading-Animation.gif" width="100%" height="100%">');  
  4.     chart.setTitle({  
  5.         text: 'Top ODI Batsman in India'  
  6.     });  
  7.     chart.hideLoading();  
  8. }  

Now, everything has been setup, it’s time to run your application.

First screen when we run the application and data is not here, data has been fetching from API. So, here need to show loading image.

Angular Custom Directive

Second screen when data is ready. So, here don’t need to show loading image.

Angular Custom Directive
Third screen is when we click on any point to render the drill-down chart. So, data is not available for drill-down chart, data has been fetching from API. For this demonstration, we have clicked on the first chart. So, here need to show loading image.

Angular Custom Directive

Fourth screen is when data is ready for drill-down chart. So, here we don’t need to show loading image.

Angular Custom Directive

Conclusion

Today, we have learned how to show loading image or message when the chart is being loaded using Highcharts and AngularJS custom directive and Web API.

I hope this post helps you. Please put your feedback which will help me improve the next post. If you have any doubts, please ask in the comments section.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now