来自 linq 查询的 cal-heatmap 数据

cal-heatmap data from linq query

我刚开始使用 d3 和 cal-heatmap,但遇到了一个与数据相关的问题。

根据文档(http://kamisama.github.io/cal-heatmap/#data-format) data need to be supplied in a format like this 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();

产生这种格式的:

[{"timestamp":63556099200,"value":2},{"timestamp":63556185600,"value":4},{"timestamp":63556272000,"value":2},{"timestamp":63556358400,"value":1},{"timestamp":63556704000,"value":2},{"timestamp":63556790400,"value":1},{"timestamp":63556876800,"value":1},{"timestamp":63556963200,"value":1},{"timestamp":63557222400,"value":1},{"timestamp":63557308800,"value":1},{"timestamp":63557827200,"value":1},{"timestamp":63557913600,"value":1},{"timestamp":63558000000,"value":2},{"timestamp":63558086400,"value":1},{"timestamp":63558172800,"value":1},{"timestamp":63559296000,"value":3},{"timestamp":63559728000,"value":2},{"timestamp":63559814400,"value":1},{"timestamp":63559900800,"value":3},{"timestamp":63559987200,"value":1},{"timestamp":63560246400,"value":2},{"timestamp":63560332800,"value":1},{"timestamp":63560419200,"value":2},{"timestamp":63560505600,"value":2},{"timestamp":63560592000,"value":1},{"timestamp":63560937600,"value":1},{"timestamp":63561456000,"value":4},{"timestamp":63561801600,"value":2},{"timestamp":63562060800,"value":2},{"timestamp":63562147200,"value":1},{"timestamp":63562233600,"value":2},{"timestamp":63562320000,"value":1},{"timestamp":63562406400,"value":1},{"timestamp":63562665600,"value":1},{"timestamp":63562752000,"value":1},{"timestamp":63562838400,"value":2},{"timestamp":63562924800,"value":1},{"timestamp":63563270400,"value":2},{"timestamp":63563961600,"value":2},{"timestamp":63564048000,"value":2},{"timestamp":63564134400,"value":3},{"timestamp":63564220800,"value":3},{"timestamp":63564566400,"value":2},{"timestamp":63564739200,"value":2},{"timestamp":63564825600,"value":1},{"timestamp":63565084800,"value":2},{"timestamp":63565171200,"value":1},{"timestamp":63565257600,"value":2},{"timestamp":63565344000,"value":2},{"timestamp":63565430400,"value":3}]

linq 查询可以直接生成正确的格式吗?

你们很接近。你想要做的是创建一个 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();

最终代码:

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();