2011-10-25 140 views
1

我有下面的C#代碼:顯示日期

public JsonResult Graph() 
{ 
    var result = new Dictionary<DateTime, decimal> { { DateTime.Today.ToUniversalTime(), 1000 }, { DateTime.Today.AddDays(-1).ToUniversalTime(), 2000 }, { DateTime.Today.AddDays(-2).ToUniversalTime(), 5000 } }; 

    return Json(result.ToArray(), JsonRequestBehavior.AllowGet); 
} 

當我看着螢火蟲的JSON數據是這樣的:

[{"Key":"\/Date(1319515200000)\/","Value":1000},{"Key":"\/Date(1319428800000)\/","Value":2000},{"Key":"\/Date(1319342400000)\/","Value":5000}] 

我Highcharts配置看起來像:

var options = { 
    chart: { 
     renderTo: 'chart', 
    }, 
    xAxis: { 
     type: 'datetime' 
    }, 
    series: [] 
} 

jQuery.getJSON("/graph", null, function (items) { 
    var series = { 
     type: 'column', 
     data: [] 
    }; 

    jQuery.each(items, function (itemNo, item) { 
     series.data.push({ 
      name: item.Key, 
      y: item.Value 
     }) 
    }); 

    options.series.push(series); 

    chart = new Highcharts.Chart(options); 
    chart.render(); 
}); 

x軸不會顯示我的日期。任何幫助深表感謝。

回答

1

我最終使用:

Dictionary<string, decimal>() 

我基本上通過的日期爲一個字符串的觀點和最終使用

Date.parse(item.Key) 

客戶端。它工作得很好。

0

假設,item.Key包含有類似 「日期(1319342400000)」,你可以嘗試以下方法:

jQuery.each(items, function (itemNo, item) { 
    var dateStr = item.Key; 
    var dateVal = dateStr.substring(dateStr.indexOf('(')+1, dateStr.indexOf(')')); 
    series.data.push([dateVal, item.Value]); 
}); 

而且,你不需要以下行:

chart.render(); 

而且如果以上不起作用,您可以檢查item.Key的值如下:

jQuery.each(items, function (itemNo, item) { 
    alert(item.Key); 
    series.data.push(//... 

更新:

那麼你不需要使用substringindexOf。只要改變你jQuery.each如下:

jQuery.each(items, function (itemNo, item) {   
    series.data.push([item.Key, item.Value]); 
}); 
+0

謝謝,但沒有奏效。 – Thomas

+0

@Thomas:你有沒有試過'alert(item.Key);'看到'item.Key'的內容? –

+0

是的,它包含1319342400000,但它不在x軸上顯示。 – Thomas