-1

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

AabinGunz
  • 12,109
  • 54
  • 146
  • 218

2 Answers2

2

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.

Paweł Fus
  • 44,795
  • 3
  • 61
  • 77
  • Sorry for the confusion. To make it simple given a **json response** (mentioned in my question) 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** and y axis is **tasks**. Similar to the screenshot provided in my question. – AabinGunz Jan 19 '15 at 17:14
  • So you need to write parser from your data to format required by Highcharts? Why you can't do it? – Paweł Fus Jan 20 '15 at 10:48
  • I prepared the data format following [this jsfiddle](http://jsfiddle.net/abhisheksimion/1ycuwuuw/) (please check `getOperationReportChartSeriesData()` function ) but I am not getting chart to show the data. – AabinGunz Jan 20 '15 at 11:45
  • I get `Error: Invalid negative value for attribute width="-694737840"` and `Highcharts error #15: www.highcharts.com/errors/15` errors. Not sure what needs to be sorted and from where is the negative value coming – AabinGunz Jan 20 '15 at 11:47
  • Updated my question with proper chart [here](http://jsfiddle.net/abhisheksimion/JnGVh/60/). The only thing that is missing now is navigation, range selections as shown in the screenshot from my question and somehow `tasks` is showing only 2, but actually it should show 4 – AabinGunz Jan 20 '15 at 13:04
  • 1
    I see 6 tasks in your last example. The problem with missing navigation is that you are using Highcharts, not Highcharts (replace urls and in #71 line add StockChart constructor), see fixed demo: http://jsfiddle.net/JnGVh/62/ Now as you can see, inverted option isn't supported anyway in Highstock, so you can't use columnrange series. Instead use line series, just like one of your demos: http://jsfiddle.net/abhisheksimion/bx2000cb/2/ – Paweł Fus Jan 20 '15 at 13:35
  • oh! but when using line series in [my example](http://jsfiddle.net/abhisheksimion/bx2000cb/2/) I am unable to get task run `duration`, please suggest me a way to prepare data so that tooltip shows `from` & `to` of each `task`. – AabinGunz Jan 20 '15 at 15:31
  • Something like this: http://jsfiddle.net/bx2000cb/4/ ? Just use `tooltip.formatter` so you can create string which will be later used in tooltip. – Paweł Fus Jan 20 '15 at 19:47
  • I used your [jsfiddle link](http://jsfiddle.net/bx2000cb/4/) and only modified the data. In this data, I have 4 **tasks** but the graph shows me only 3 and bar is displayed only for 2 of them, also hovering on left part of a task displays tooltip on right side.. Please help [http://jsfiddle.net/bx2000cb/6/](http://jsfiddle.net/bx2000cb/6/) – AabinGunz Jan 21 '15 at 06:38
  • Why didn't you just replaced tasks variable with your own and let Highcharts converted prepare proper format? This is what I mean: http://jsfiddle.net/bx2000cb/7/ – Paweł Fus Jan 21 '15 at 11:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69298/discussion-between-abi1964-and-pawel-fus). – AabinGunz Jan 21 '15 at 13:00
1

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.

Anna Smother
  • 322
  • 2
  • 4
  • 14
  • Thanks for your answer. I am already using momentjs to convert my date `var st = new Date(moment(start_temp).valueOf()).getTime();` under `getOperationReportChartSeriesData` function. I am even getting some bars but looks like the difference is very low.. in seconds so the bar is almost invisible.. Trying to fix the same now. – AabinGunz Jan 19 '15 at 10:23
  • In my local system I am able to get some data but it is not visible since there are many tasks and difference between 1 of their run time is very low (in seconds) so the bar is almost invisible. I tried giving rangeSelectors default value to 1 hour but still it is not getting displayed properly. Also my plunker might have some library issues so it is not the exact result as my local systems. – AabinGunz Jan 19 '15 at 10:54
  • I'm sorry, I don't understand what you want to achieve. Btw you are using getTime from the dateObject, what are you interested in? The hours, min.? Because with moment.js, you can get the hours or min. like this: momentObj.get("hour"), this will make sure you will get the hours. Look at the docs: http://momentjs.com/docs/ – Anna Smother Jan 19 '15 at 12:56
  • Sorry for the confusion I updated my question with an image showing exactly what i want. By default range should be an hour. – AabinGunz Jan 19 '15 at 13:39