javascript – 需要帮助在angularjs中使用highcharts绘制图表
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 需要帮助在angularjs中使用highcharts绘制图表,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含9479字,纯文字阅读大概需要14分钟。
内容图文
我有一些来自elasticsearch的数据,它有任务的开始和时间.特定代理程序任务的结束时间,需要在图表上水平绘制,指示该特定任务的开始和结束时间.我尝试使用columnrange图表类型但是因为我无法进入范围选择器see here an example.
然后我再次尝试了this,我能够使它工作,但需要帮助在高级图中绘制Elasticsearch数据.
另一个问题是我从服务器获得的日期是人类可读的dateformat(检查start_time和end_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轴是任务.与下面提供的屏幕截图类似.
以下是预期结果的屏幕截图.
click here for the jsfiddle link
更新了jsfiddle链接here
但在此我还需要StockChart navigation & range selectors.
@Pawel Fus:非常感谢与我合作.对于遇到类似问题的人来说,这是最终结果.
解决方法:
我对你的要求有点困惑(仍然).
Highcharts,需要时间戳(以毫秒为单位)进行渲染.因此,您需要预处理日期,例如:new Date(“2014-11-20T11:03:55.000-08:00”).getTime()以获取UTC时间戳.现在,当您有UTC时间时,您可以使用上面建议的moment.js来呈现特定时区的数据.
要在Highcharts中使用moment.js,可以通过以下方式扩展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:以上测试用例使用的是Highfrts的github版本.它是Highcharts 4.1的候选版本.
内容总结
以上是互联网集市为您收集整理的javascript – 需要帮助在angularjs中使用highcharts绘制图表全部内容,希望文章能够帮你解决javascript – 需要帮助在angularjs中使用highcharts绘制图表所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。