在angularjs中使用highcharts绘制图表需要帮助

时间:2015-01-16 11:44:17

标签: javascript angularjs highcharts momentjs

我有一些来自elasticsearch的数据,其中任务开始&特定代理 task结束时间,需要在图表上水平绘制,指明该特定任务的开始和结束时间。我尝试使用columnrange图表类型,但由于我无法获得tofrom范围选择器,see here an example

然后我再次尝试this并且我能够使其工作,但需要帮助在高级图中绘制Elasticsearch数据。

另一个问题是我从服务器获取的日期是人类可读的日期格式(检查start_timeend_time),EST时区,需要转换为可以使用的格式由highcharts。请帮忙。

我的高等教育指令

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);
            });
        }
    };
});

我的模板

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

我的控制器

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();
});

Highcharts需要使用的部分完整数据,显示特定代理的任务开始和结束时间。

{
   "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"
                                    }
                                 }
                              ]
                           }
                        }
                     }
                  ]
               }
            }
         ]
      }
   }
}

这是我的plunker link with angularjs

Updating in progress plunker link with data mapping with Highcharts

更新地图代码

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;
}

更新

抱歉所有的困惑。为了简化 json响应(如上所述),函数(getOperationReportChartSeriesData())将以Highcharts将显示单个任务及其运行历史的方式准备数据在水平栏中,x轴是日期时间(包括小时和分钟),y轴是任务。与下面提供的屏幕截图类似。

以下是预期结果的屏幕截图。 enter image description here

click here for the jsfiddle link

更新了jsfiddle 链接here 但在这方面我还需要StockChart navigation & range selectors

@Pawel Fus:非常感谢与我合作。对于遇到类似问题的人来说,这是最终结果。

Working plunker link

2 个答案:

答案 0 :(得分:2)

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

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

要将moment.js与Highcharts一起使用,您可以通过以下方式扩展getTimezoneOffset选项(实验性):

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;
        }
    }
});

直播示例:http://jsfiddle.net/k96t1dy7/3/

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

注2:上面的测试用例是使用github版本的Highcharts。它是Highcharts 4.1的候选版本。

答案 1 :(得分:1)

如果我理解正确,你想解析你的日期对象,Highchart可以消费吗?您可以使用库moment.js,就像魅力一样。