我开始使用d3和cal-heatmap,并且遇到与数据相关的问题。
根据文档(http://kamisama.github.io/cal-heatmap/#data-format),需要以这样的格式提供数据http://kamisama.github.io/cal-heatmap/datas-years.json
我正在使用LinqPad4:
int userId = 4;
DateTime start = new DateTime(2015, 1 , 2);
DateTime end = new DateTime(2015, 6, 30);
var result = (from r in Response
where (r.RespondentUserId == userId)
&& (r.ResponseBegin >= start)
&& (r.ResponseBegin <= end)
group r by new { date = r.ResponseBegin.Date } into grp
select new
{
Ticks = grp.Key.date.Ticks,
Reports = grp.Count()
})
.ToList()
.Select (r => new
{
timestamp = new TimeSpan(r.Ticks).TotalSeconds,
value = r.Reports
});
new JavaScriptSerializer().Serialize(result).Dump();
哪种产生这种格式:
[{ “时间戳”:63556099200, “值”:2},{ “时间戳”:63556185600, “值”:4},{ “时间戳”:63556272000, “值”:2},{ “时间戳” :63556358400, “值”:1},{ “时间戳”:63556704000, “值”:2},{ “时间戳”:63556790400, “值”:1},{ “时间戳”:63556876800, “值”:1 },{ “时间戳”:63556963200, “值”:1},{ “时间戳”:63557222400, “值”:1},{ “时间戳”:63557308800, “值”:1},{ “时间戳”:63557827200 “值”:1},{ “时间戳”:63557913600, “值”:1},{ “时间戳”:635.58亿, “值”:2},{ “时间戳”:63558086400, “值”:1}, { “时间戳”:63558172800, “值”:1},{ “时间戳”:63559296000, “值”:3},{ “时间戳”:63559728000, “值”:2},{ “时间戳”:63559814400,”值 “:1},{” 时间戳 “:63559900800,” 值 “:3},{” 时间戳 “:63559987200,” 值 “:1},{” 时间戳 “:63560246400,” 值 “:2},{”时间戳 “:63560332800,” 值 “:1},{” 时间戳 “:63560419200,” 值 “:2},{” 时间戳 “:63560505600,” 值 “:2},{” 时间戳 “:63560592000,” 值” :1},{ “时间戳”:63560937600, “值”:1},{ “时间戳”:63561456000, “值”:4},{ “时间戳”:63561801600 “值”:2},{ “时间戳”:63562060800, “值”:2},{ “时间戳”:63562147200, “值”:1},{ “时间戳”:63562233600, “值”:2}, { “时间戳”:63562320000, “值”:1},{ “时间戳”:63562406400, “值”:1},{ “时间戳”:63562665600, “值”:1},{ “时间戳”:63562752000,”值 “:1},{” 时间戳 “:63562838400,” 值 “:2},{” 时间戳 “:63562924800,” 值 “:1},{” 时间戳 “:63563270400,” 值 “:2},{”时间戳 “:63563961600,” 值 “:2},{” 时间戳 “:63564048000,” 值 “:2},{” 时间戳 “:63564134400,” 值 “:3},{” 时间戳 “:63564220800,” 值” :3},{ “时间戳”:63564566400, “值”:2},{ “时间戳”:63564739200, “值”:2},{ “时间戳”:63564825600, “值”:1},{ “时间戳” :63565084800, “值”:2},{ “时间戳”:63565171200, “值”:1},{ “时间戳”:63565257600, “值”:2},{ “时间戳”:63565344000, “值”:2 },{ “时间戳”:63565430400, “值”:3}]
linq查询可以直接生成正确的格式吗?
答案 0 :(得分:2)
你很亲密。你想要做的是创建一个Dictionary
,序列化将看起来像你想要的格式:
var result = (from r in Response
where (r.RespondentUserId == userId)
&& (r.ResponseBegin >= start)
&& (r.ResponseBegin <= end)
group r by new { date = r.ResponseBegin.Date } into grp
select new
{
Ticks = grp.Key.date.Ticks,
Reports = grp.Count()
})
.ToDictionary(r => new TimeSpan(r.Ticks).TotalSeconds, r => r.Reports);
new JavaScriptSerializer().Serialize(result).Dump();
答案 1 :(得分:0)
最终代码:
var result = (from r in Response
where (r.RespondentUserId == userId)
&& (r.ResponseBegin >= start)
&& (r.ResponseBegin <= end)
group r by new { date = r.ResponseBegin.Date } into grp
select new
{
Ticks = grp.Key.date.Ticks,
Reports = grp.Count()
})
.ToDictionary(r => new TimeSpan(r.Ticks).TotalSeconds.ToString(), r => r.Reports);
new JavaScriptSerializer().Serialize(result).Dump();