[英]Highcharts Time Series data from C# in a format required by HighCharts
Highcharts requires that the series data be in this format Highcharts要求系列数据采用这种格式
series : {
data : [[x1,y1],[x2,y2],[x3,y3],[x4,y4],[x5,y5]]
}
My data is coming from ASP.net web application in the form of JSON ie 我的数据来自JSON形式的ASP.net Web应用程序,即
[{"time": x1, "value" : y1},{"time": x2, "value" : y2}]
In order for the data to work with Highcharts, I have to manually push the objects as an array in an empty array using JavaScript. 为了使数据能够与Highcharts一起使用,我必须使用JavaScript手动将对象作为数组推入空数组中。
var tempData = [];
$.each(data, function(i, item) {
tempData.push([item.time,item.value]);
});
//then add the series using tempData
Ideally I would not like this to be done on the browser as it degrades the performance of the application on the client side and I am using large set of data. 理想情况下,我不希望在浏览器上执行此操作,因为它会降低客户端上应用程序的性能,并且我正在使用大量数据。
On the C# side, this is what I have, 在C#方面,这就是我所拥有的,
public class TimeValuePair
{
double time {get;set;}
double value{get;set;}
}
List<TimeValuePair> data= dataFromServer;
And I am returning the data as; 我将数据返回为:
return Json(data, JsonRequestBehavior.AllowGet);
What is the proper way of dealing with this ? 处理此问题的正确方法是什么?
You can use LINQ to achieve what you want in a compact way: 您可以使用LINQ以紧凑的方式实现所需的功能:
return new JavaScriptSerializer().Serialize(data.Select(t => new object[] {t.Time, t.Value}))
(Not sure what Json serializer you are using, so my example uses the one from System.Web.Extensions) (不知道您使用的是哪种Json序列化程序,因此我的示例使用System.Web.Extensions中的序列化程序)
So you basically want to do this: 因此,您基本上想这样做:
StringBuilder sb = new StringBuilder("series : {data : [");
StringBuilder sb = new StringBuilder(“ series:{data:[”);
foreach (var time_value_pair in data) sb.Append("[" + time_value_pair.time + "," + time_value_pair.value + "]");
foreach(数据中的time_value_pair变量)sb.Append(“ [” + time_value_pair.time +“,” + time_value_pair.value +“]”);
sb.Append("]}");
sb.Append( “]}”);
and send sb.ToString()
who whomever needs the string. 并向需要该字符串的人发送
sb.ToString()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.