我在将数据插入高图时遇到问题我尝试自定义http://www.highcharts.com/stock/demo中的示例 但我的图表没有显示任何信息,我查看了示例数据,它的格式与我的数据相同:
这是我在c#中的代码:
[HttpPost]
public JsonResult GetData()
{
...
var view= new JavaScriptSerializer().Serialize(dictionary.dicValues.Select(x => new object[] {x.Key, x.Value}));
view= Regex.Replace(view, @"\""\\/Date\((-?\d+)\)\\/\""", "$1");
view= view.Replace(@"[", "").Replace(@"]", "");
return new JsonResult
{
Data = new
{
view
},
ContentType = null,
ContentEncoding = null,
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
这是我用于创建highchart的js代码:
$(elem).highcharts('StockChart', {
rangeSelector: {
selected: 1
},
title: {
text: "title"
},
xAxis: {
type: 'datetime',
},
yAxis: {
type: 'double',
},
series: [{
name: 'AAPL',
data: data.view,
tooltip: {
valueDecimals: 2
}
}]
});
这是我传递给视图的数据:
“1421751600000,4.9928500000000007,1421755200000,13.314966666666665,1421758800000,8.316766666666668,1421845200000,14.738,1421848800000,7.9762000000000013”
或者如果我没有删除括号:
“[[1421751600000,4.9928500000000007],[1421755200000,13.314966666666665],[1421758800000,8.316766666666668],[1421845200000,14.738],[1421848800000,7.9762000000000013]]”
如果有人能帮助我,我将非常感激!
答案 0 :(得分:1)
你应该能够简单地这样做:
public JsonResult GetData()
{
return new JsonResult()
{
Data = dictionary.dicValues.Select(x => new object[] {x.Key, x.Value})
};
}
JsonResult
的默认设置应该为ContentType
提供正确的设置,它应该自动使用默认的序列化程序将对象序列化为正确的JSON字符串(除非您需要一些自定义序列化)。< / p>