如何从C#中以DateTime格式格式化Google图表x轴 [英] How to format google chart x axis in DateTime format from C#
问题描述
我正在MVC 5中开发一个网站,我正在使用谷歌图表为我的数据显示一些图表。我使用折线图来表示具有值和日期的数据。如下所示:
class ChartData
{
public double Value {get;组; }
public DateTime Date {get;组; }
};
在我的控制器中,我有一个请求处理程序来为图表生成数据:
public JsonResult GenerateChartData(int id)
{
List< ChartData> list = new List< ChartData>();
//一些代码来填充列表
return Json(list,JsonRequestBehavior.AllowGet);
$ b 一切正常,除了应该显示日期时间顺序的X轴是格式错误。看起来像绝对时间不是可读的日期格式。
感谢任何回答
解决方案好吧,我明白了。阅读这篇文章使一切都变得清晰
我修改了我内部的java脚本代码页面以下列方式:
var dataArray = [
['Date','Value']
];
$ .each(jsondata,function(i,item){
var d = new Date(parseInt(item.Instant.substr(6)));
dataArray.push([d ,item.Value]);
});
现在它完美地工作了
I'm developing a web site in MVC 5 and I'm using google chart to display some chart for my data. I'm using the line chart for a data which have a value and a date. Something like the follow:
class ChartData
{
public double Value { get; set; }
public DateTime Date { get; set; }
};
In my controller I have a request handler to generate the data for the chart:
public JsonResult GenerateChartData(int id)
{
List<ChartData> list = new List<ChartData>();
// some code to populate the list
return Json(list, JsonRequestBehavior.AllowGet);
}
Everything works fine except that the X axis which should show the date time sequence is formatted in the wrong way. The looks like absolute time not in readable date format.
thanks for any answer
解决方案 Ok I got it. Reading this article made everything clear
How to parse JSON to receive a Date object in JavaScript?
I modified the java script code inside my page in the following way:
var dataArray = [
['Date', 'Value']
];
$.each(jsondata, function (i, item) {
var d = new Date(parseInt(item.Instant.substr(6)));
dataArray.push([d, item.Value]);
});
Now it works perfectly
这篇关于如何从C#中以DateTime格式格式化Google图表x轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!