在 angularjs 中使用 highcharts 绘制图表时需要帮助 [英] Need help in plotting a chart using highcharts in angularjs

查看:38
本文介绍了在 angularjs 中使用 highcharts 绘制图表时需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些来自elasticsearch的数据,它具有任务开始 &特定代理任务结束时间,需要在图表上水平绘制,指示该特定任务的开始和结束时间.我尝试使用 columnrange 图表类型,但无法获得 tofrom 范围选择器,

更新了 jsfiddle 链接这里但在这方面我还需要 StockChart 导航 &范围选择器.

@Pawel Fus:非常感谢您对我的包容.这是任何面临类似问题的人的最终结果.

工作 plunker 链接

解决方案

我对你的要求有点困惑(仍然).

Highcharts,需要时间戳(以毫秒为单位)才能呈现.因此,您需要对日期进行预处理,例如: new Date("2014-11-20T11:03:55.000-08:00").getTime() 以获取 UTC 时间戳.现在,当您有 UTC 时间时,您可以按照上面的建议使用 moment.js 在特定时区呈现数据.

要在 Highcharts 中使用 moment.js,您可以扩展 getTimezoneOffset 选项(实验性),这样:

Highcharts.setOptions({全球的: {/*** 使用 moment-timezone.js 返回个人的时区偏移量* 时间戳,用于 X 轴标签和工具提示标题.*/getTimezoneOffset:函数(时间戳){var zone = '欧洲/奥斯陆',timezoneOffset = moment.tz.zone(zone).parse(timestamp);返回时区偏移量;}}});

和实例:http://jsfiddle.net/k96t1dy7/3/

注意:你的第二个plunker不起作用..

注2:以上测试用例使用的是github版本的Highcharts.它是 Highcharts 4.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.

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

解决方案

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.

这篇关于在 angularjs 中使用 highcharts 绘制图表时需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆