[英]Adding data to highstocks by pushing array
Been struggling to get my data right from the array to the chart. 一直在努力使我的数据正确地从数组传递到图表。 This is my code,
这是我的代码
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "<%=ResolveUrl("Services/HighstockService.asmx/TempData") %>",
data: "{}", /* Sending specific indata */
dataType: "json",
success: function (Result) {
//debugger;
Result = Result.d;
var tempData = [];
for (var i in Result) {
var serie = new Array("[" + parseInt(Result[i].Date.substr(6)) + "," + Result[i].Value + "],");
tempData.push(serie);
}
DrawTempChart(tempData);
},
error: function (Result) {
alert("Error");
}
});
});
function DrawTempChart(tempData) {
debugger;
$('#tempChart').highcharts('StockChart', {
title: {
text: 'AAPL Stock Price'
},
type: 'datetime',
dateTimeLabelFormats: {
second: '%Y-%m-%d %H:%M:%S',
minute: '%Y-%m-%d %H:%M',
hour: '%Y-%m-%d %H:%M',
day: '%Y %m-%d',
week: '%Y %m-%d',
month: '%Y-%m',
year: '%Y'
},
series: [{
name: 'AAPL',
data: [tempData],
}]
});
}
</script>
I am adding the brackets and comma to every index in the array to follow the structure of how highstocks accepts data. 我将括号和逗号添加到数组中的每个索引,以遵循高级股票如何接受数据的结构。 When debugging, I then receive data like this: "[1418017375000,33],".
调试时,我将收到如下数据:“ [1418017375000,33]”。
I need to parseInt the Date due to javascript adding parenthesis and forward slashes when moving it from Web Services. 我需要parseInt Date,因为javascript从Web Services中移出括号时会加上括号并使用正斜杠。 I am unsure if it gets rightly formatted in the javascript to work in the chart.
我不确定它是否在javascript中正确格式化以在图表中工作。
Since I really can't debug the DrawTempChart function, I don't actually know how the array is sent into that function. 由于我确实无法调试DrawTempChart函数,因此我实际上不知道如何将数组发送到该函数。 Anyone that can see what I am doing wrong?
有人可以看到我在做什么吗?
[edit] Should probably be added that the closest I have come to an output is to show only the last index in the array on the chart. [edit]应该补充一点,我最接近输出的是仅显示图表上数组中的最后一个索引。
First of all, the data
in series
should be like this: 首先,
series
data
应如下所示:
[[UTCTime1, value1], [UTCTime2, value2], ...]
So change the line data: [tempData]
to this: 因此,将行
data: [tempData]
更改为此:
data: tempData
Second, when you are creating a new Array
, the format is not this: 其次,当您创建新的
Array
,格式不是这样:
var serie = new Array("[" + parseInt(Result[i].Date.substr(6)) + "," + Result[i].Value + "],");
You should write it this way: 您应该这样写:
var serie = new Array(parseInt(Result[i].Date.substr(6)), Result[i].Value);
and it will automatically add the []
and ,
: [item1, item2]
它将自动添加
[]
和,
:: [item1, item2]
When you push serie
into tempData
it will also put ,
between items. 当你把
serie
到tempData
也将投入,
项目之间。
At last, be sure that Result[i].Date.substr(6)
creates correct and sorted 13 digits UTC numbers because highcharts doesn't sort the data. 最后,请确保
Result[i].Date.substr(6)
创建正确且已排序的13位UTC数字,因为highcharts不会对数据进行排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.