Need help in plotting a chart using highcharts in angularjs

I have some data which is coming from elasticsearch which has task's start & end time for a particular agent's task which needs to be plotted on chart horizontally indicating start and endtime of that particular task. I tried with columnrange chart type but in that I was not able to get to and from range selector, see here an example.

Then again I tried this and I was able to get it working, but need help in plotting the Elasticsearch data in highcharts.

One other problem is that the date which I get from server is in a human readable dateformat (check start_time and end_time), EST timezone and needs to be converted to a format which can be consumed by highcharts. Please help.

My highcharts directive

angular.module('myApp').directive('operationalhighstackstock', function() {
    return {
        restrict : 'E',
        scope : true,
        link : function postLink(scope, element, attrs) {
            scope.$watch('operationHighChartsData', function(values){
                new Highcharts.StockChart(values);
            });
        }
    };
});

My template

<div style="width: 100%;">
    <operationalhighstackstock></operationalhighstackstock>
    <div id="container" style="width: 100%; height: 500px;"></div>
</div>

My controller

angular.module('myApp').controller('OperationReportChartController', function($scope, $filter, $location, $transition, MyService, $timeout) {

    $scope.init = function() {
        $scope.isOperationalReport = true;            
        $scope.initOperationalReport();
    }

    $scope.initOperationalReport = function() {
        $scope.loading = true;
        $scope.isChartDataAvailable = true;
        $scope.operationReportDefaultQuery = operationReportDefaultQuery;
        $scope.operationHighChartsData = {
            chart : {
                renderTo : 'container',
                alignTicks : false
            }
        }; 

        $scope.operationalReportDefaultServiceRequests();
    }

    $scope.serviceRequests = function() {
        //Common service requests if any
    }

    $scope.operationalReportDefaultServiceRequests = function() {
        $scope.isOperationalReport = true;
        MyService.getOperationalReportChartData($scope.operationReportDefaultQuery).then(function(result) {
            renderOperationalHighCharts(result);
        });
    }

    function renderOperationalHighCharts(result) {
        //var xAxisCategories = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        //var series = getOperationReportChartSeriesData(result);
        var chart_options = {
                chart: {
                    renderTo: 'container'
                },
                xAxis: {
                    type: 'datetime'
                },
                yAxis: {
                    tickInterval: 1,
                    labels: {
                        formatter: function() {
                            if (tasks[this.value]) {
                                return tasks[this.value].name;
                            }
                        }
                    },
                    startOnTick: false,
                    endOnTick: false,
                    minPadding: 1,
                    maxPadding: 1
                },
                legend: {
                    enabled: false
                },
                plotOptions: {
                    line: {
                        lineWidth: 9,
                        marker: {
                            enabled: false
                        },
                        dataLabels: {
                            enabled: true,
                            align: 'left',
                            formatter: function() {
                                return this.point.options && this.point.options.label;
                            }
                        }
                    }
                },
                series: series
        };
        $scope.operationHighChartsData = chart_options;
    }
    $scope.init();
});

Partly complete data which needs to be consumed by Highcharts showing tasks start and end time for a particular agent.

{
   "took": 43,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 29,
      "max_score": 1
   },
   "aggregations": {
      "agent_names": {
         "buckets": [
            {
               "key": "LUME - US AGG",
               "doc_count": 4,
               "top-sites": {
                  "buckets": [
                     {
                        "key": "000AAA0I00000000007W",
                        "doc_count": 2,
                        "top_tags_hits": {
                           "hits": {
                              "total": 2,
                              "max_score": 1,
                              "hits": [
                                 {
                                    "_index": "asta_sync",
                                    "_type": "tasks_run_history",
                                    "_id": "216934321",
                                    "_score": 1,
                                    "_source": {
                                       "end_time": "2014-11-20T11:12:49.000-08:00",
                                       "start_time": "2014-11-20T11:12:41.000-08:00"
                                    }
                                 },
                                 {
                                    "_index": "asta_sync",
                                    "_type": "tasks_run_history",
                                    "_id": "234218016",
                                    "_score": 1,
                                    "_source": {
                                       "end_time": "2014-11-20T11:12:49.000-08:00",
                                       "start_time": "2014-11-20T11:12:41.000-08:00"
                                    }
                                 }
                              ]
                           }
                        }
                     },
                     {
                        "key": "000AAA0I00000000007X",
                        "doc_count": 2,
                        "top_tags_hits": {
                           "hits": {
                              "total": 2,
                              "max_score": 1,
                              "hits": [
                                 {
                                    "_index": "asta_sync",
                                    "_type": "tasks_run_history",
                                    "_id": "216556106",
                                    "_score": 1,
                                    "_source": {
                                       "end_time": "2014-11-20T11:04:33.000-08:00",
                                       "start_time": "2014-11-20T11:04:28.000-08:00"
                                    }
                                 },
                                 {
                                    "_index": "asta_sync",
                                    "_type": "tasks_run_history",
                                    "_id": "234218026",
                                    "_score": 1,
                                    "_source": {
                                       "end_time": "2014-11-20T11:04:33.000-08:00",
                                       "start_time": "2014-11-20T11:04:28.000-08:00"
                                    }
                                 }
                              ]
                           }
                        }
                     },
                     {
                        "key": "000AAA0I00000000007Y",
                        "doc_count": 2,
                        "top_tags_hits": {
                           "hits": {
                              "total": 2,
                              "max_score": 1,
                              "hits": [
                                 {
                                    "_index": "asta_sync",
                                    "_type": "tasks_run_history",
                                    "_id": "235761199",
                                    "_score": 1,
                                    "_source": {
                                       "end_time": "2014-11-20T10:59:44.000-08:00",
                                       "start_time": "2014-11-20T10:59:43.000-08:00"
                                    }
                                 },
                                 {
                                    "_index": "asta_sync",
                                    "_type": "tasks_run_history",
                                    "_id": "217265041",
                                    "_score": 1,
                                    "_source": {
                                       "end_time": "2014-11-20T10:59:44.000-08:00",
                                       "start_time": "2014-11-20T10:59:43.000-08:00"
                                    }
                                 }
                              ]
                           }
                        }
                     },
                     {
                        "key": "000AAA0G00000000006K",
                        "doc_count": 1,
                        "top_tags_hits": {
                           "hits": {
                              "total": 1,
                              "max_score": 1,
                              "hits": [
                                 {
                                    "_index": "asta_sync",
                                    "_type": "tasks_run_history",
                                    "_id": "234732452",
                                    "_score": 1,
                                    "_source": {
                                       "end_time": "2014-11-20T10:52:59.000-08:00",
                                       "start_time": "2014-11-20T10:52:59.000-08:00"
                                    }
                                 }
                              ]
                           }
                        }
                     }
                  ]
               }
            },
            {
               "key": "USWF - 01D",
               "doc_count": 8,
               "top-sites": {
                  "buckets": [
                     {
                        "key": "000AAA0I00000000007T",
                        "doc_count": 3,
                        "top_tags_hits": {
                           "hits": {
                              "total": 3,
                              "max_score": 1,
                              "hits": [
                                 {
                                    "_index": "asta_sync",
                                    "_type": "tasks_run_history",
                                    "_id": "216603056",
                                    "_score": 1,
                                    "_source": {
                                       "end_time": "2014-11-20T11:03:01.000-08:00",
                                       "start_time": "2014-11-20T11:02:41.000-08:00"
                                    }
                                 },
                                 {
                                    "_index": "asta_sync",
                                    "_type": "tasks_run_history",
                                    "_id": "216957850",
                                    "_score": 1,
                                    "_source": {
                                       "end_time": "2014-11-20T07:52:12.000-08:00",
                                       "start_time": "2014-11-20T07:52:01.000-08:00"
                                    }
                                 },
                                 {
                                    "_index": "asta_sync",
                                    "_type": "tasks_run_history",
                                    "_id": "216839441",
                                    "_score": 1,
                                    "_source": {
                                       "end_time": "2014-11-20T08:08:10.000-08:00",
                                       "start_time": "2014-11-20T08:08:02.000-08:00"
                                    }
                                 }
                              ]
                           }
                        }
                     },
                     {
                        "key": "000AAA0G00000000006M",
                        "doc_count": 2,
                        "top_tags_hits": {
                           "hits": {
                              "total": 2,
                              "max_score": 1,
                              "hits": [
                                 {
                                    "_index": "asta_sync",
                                    "_type": "tasks_run_history",
                                    "_id": "216839440",
                                    "_score": 1,
                                    "_source": {
                                       "end_time": "2014-11-20T11:01:12.000-08:00",
                                       "start_time": "2014-11-20T11:01:08.000-08:00"
                                    }
                                 },
                                 {
                                    "_index": "asta_sync",
                                    "_type": "tasks_run_history",
                                    "_id": "216603055",
                                    "_score": 1,
                                    "_source": {
                                       "end_time": "2014-11-20T10:55:23.000-08:00",
                                       "start_time": "2014-11-20T10:55:22.000-08:00"
                                    }
                                 }
                              ]
                           }
                        }
                     },
                     {
                        "key": "000AAA0G00000000006K",
                        "doc_count": 1,
                        "top_tags_hits": {
                           "hits": {
                              "total": 1,
                              "max_score": 1,
                              "hits": [
                                 {
                                    "_index": "asta_sync",
                                    "_type": "tasks_run_history",
                                    "_id": "216981636",
                                    "_score": 1,
                                    "_source": {
                                       "end_time": "2014-11-20T10:52:59.000-08:00",
                                       "start_time": "2014-11-20T10:52:59.000-08:00"
                                    }
                                 }
                              ]
                           }
                        }
                     },
                     {
                        "key": "000AAA0G00000000006L",
                        "doc_count": 1,
                        "top_tags_hits": {
                           "hits": {
                              "total": 1,
                              "max_score": 1,
                              "hits": [
                                 {
                                    "_index": "asta_sync",
                                    "_type": "tasks_run_history",
                                    "_id": "216650284",
                                    "_score": 1,
                                    "_source": {
                                       "end_time": "2014-11-20T10:49:06.000-08:00",
                                       "start_time": "2014-11-20T10:49:06.000-08:00"
                                    }
                                 }
                              ]
                           }
                        }
                     },
                     {
                        "key": "000AAA0G00000000006N",
                        "doc_count": 1,
                        "top_tags_hits": {
                           "hits": {
                              "total": 1,
                              "max_score": 1,
                              "hits": [
                                 {
                                    "_index": "asta_sync",
                                    "_type": "tasks_run_history",
                                    "_id": "216768388",
                                    "_score": 1,
                                    "_source": {
                                       "end_time": "2014-11-20T11:05:03.000-08:00",
                                       "start_time": "2014-11-20T11:05:02.000-08:00"
                                    }
                                 }
                              ]
                           }
                        }
                     },
                     {
                        "key": "000AAA0I00000000007N",
                        "doc_count": 1,
                        "top_tags_hits": {
                           "hits": {
                              "total": 1,
                              "max_score": 1,
                              "hits": [
                                 {
                                    "_index": "asta_sync",
                                    "_type": "tasks_run_history",
                                    "_id": "216981635",
                                    "_score": 1,
                                    "_source": {
                                       "end_time": "2014-11-20T11:03:55.000-08:00",
                                       "start_time": "2014-11-20T11:03:43.000-08:00"
                                    }
                                 }
                              ]
                           }
                        }
                     }
                  ]
               }
            }
         ]
      }
   }
}

Here is my plunker link with angularjs

Updating in progress plunker link with data mapping with Highcharts

Updating mapping code

var getOperationReportChartSeriesData = function(result) {
    var series = {};
    if (result != null && result != undefined) {
        var bucket = result.aggregations[AGENT_NAMES].buckets;
        bucket.forEach(function(item) {

            var agentSeries = [], agentData = {}, opTaskIdBucket = item["top-sites"].buckets;
            opTaskIdBucket.forEach(function(taskidEntry) {
                var intervalBucket = taskidEntry["top_tags_hits"]["hits"]["hits"],
                    intervalArr = [], opTaskidObj = {};
                opTaskidObj["name"] = taskidEntry["key"];
                intervalBucket.forEach(function(intervalEntry) {
                    var intervalObj = {}, start_temp = intervalEntry["_source"].start_time, end_temp = intervalEntry["_source"].end_time;
                    var st = new Date(moment(start_temp).valueOf()).getTime();
                    var et = new Date(moment(end_temp).valueOf()).getTime();

                    intervalObj["to"] = et;
                    intervalObj["from"] = st;
                    intervalObj["x"] = st;
                    intervalObj["y"] = 1;
                    intervalArr.push(intervalObj);
                })
                opTaskidObj["data"] = intervalArr;
                agentSeries.push(opTaskidObj);
            })

            series[item["key"]] = agentSeries;
        })
    } else 
        console.log("Result is "+result);
    console.log(JSON.stringify(series));
    console.log(series);
    //console.log(JSON.stringify(series[0]["LUME - US AGG"]));
    return series;
}

Update

Sorry for all the confusion. To make it simple given a json response (given above) a function (getOperationReportChartSeriesData()) will prepare data in such a way that Highcharts will show individual tasks and it's run history in a horizontal bar, where x axis is the date time (including hours and minutes) and y axis is tasks. Similar to the screenshot provided below.

Below is a screenshot which is expected result. enter image description here

click here for the jsfiddle link

Updated with a jsfiddle link here but in this I also need StockChart navigation & range selectors.

@Pawel Fus: Thanks a lot for bearing with me. Here is the end result for anyone who faces similar problems.

Working plunker link

Answers:

Answer

I'm a little confused with your requirements (still).

Highcharts, requires timestamps (in ms) to render. So you need to preprocess your dates, for example: new Date("2014-11-20T11:03:55.000-08:00").getTime() to get UTC timestamp. Now, when you have UTC times, you can use moment.js as suggested above to render data in specific timezone.

To use moment.js with Highcharts, you can extend getTimezoneOffset option (experimental), this way:

Highcharts.setOptions({
    global: {
        /**
         * Use moment-timezone.js to return the timezone offset for individual 
         * timestamps, used in the X axis labels and the tooltip header.
         */
        getTimezoneOffset: function (timestamp) {
            var zone = 'Europe/Oslo',
                timezoneOffset = moment.tz.zone(zone).parse(timestamp);

            return timezoneOffset;
        }
    }
});

And live example: http://jsfiddle.net/k96t1dy7/3/

Note: your second plunker doesn't work..

Note2: Above test case is using github version of Highcharts. It's candidate version for Highcharts 4.1.

Answer

If I understand correctly, you want to parse your date object, that Highchart can consume? You can use the library moment.js, works like a charm.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.