为什么我的cal-heatmap不会显示数据

时间:2015-04-25 02:39:53

标签: cal-heatmap

我下面有cal-heatmap(+参见JSBin)我试图使用数据进行着色。

  

var data = {"63556099200":2,"63556185600":4,"63556272000":2,"63556358400":1,"63556704000":2,"63556790400":1,"63556876800":1,"63556963200":1,"63557222400":1,"63557308800":1,"63557827200":1,"63557913600":1,"63558000000":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};

var cal = new CalHeatMap();
cal.init({
  itemSelector: "#cal",
  data: data,
  itemName: ["visit", "visits"],
  considerMissingDataAsZero: true,
  legend: [1, 2, 3, 4],
  cellSize: 17,
  cellPadding: 2,
  domain: "month",
  domainGutter: 10,
  domainDynamicDimension: false,
  domainLabelFormat: function (date) {
    return moment(date).format("MMM, YYYY").toUpperCase();
  },
  subDomain: "x_day",
  subDomainTextFormat: "%d",
  range: 12,
  start: new Date(2015, 0, 1)
});

http://jsbin.com/yixuja/12/edit?html,output

cal-heatmap似乎没有将数据识别为格式正确。 我错过了什么?

1 个答案:

答案 0 :(得分:0)

事实证明,我的数据中的时间戳并不代表自1970年1月1日以来的秒数。

正确的数据:

data = {"1420498800":2,"1420585200":4,"1420671600":2,"1420758000":1,"1421103600":2,"1421190000":1,"1421276400":1,"1421362800":1,"1421622000":1,"1421708400":1,"1422226800":1,"1422313200":1,"1422399600":2,"1422486000":1,"1422572400":1,"1423695600":3,"1424127600":2,"1424214000":1,"1424300400":3,"1424386800":1,"1424646000":2,"1424732400":1,"1424818800":2,"1424905200":2,"1424991600":1,"1425337200":1,"1425855600":4,"1426201200":2,"1426460400":2,"1426546800":1,"1426633200":2,"1426719600":1,"1426806000":1,"1427065200":1,"1427151600":1,"1427238000":2,"1427324400":1,"1427670000":2,"1428361200":2,"1428447600":2,"1428534000":3,"1428620400":3,"1428966000":2,"1429138800":2,"1429225200":1,"1429484400":2,"1429570800":1,"1429657200":2,"1429743600":2,"1429830000":3};

http://jsbin.com/yixuja/15/edit?html,output

linq(使用linqpad)我用来获取cal-heatmap所需的dataformat: (参见How to export data from LinqPAD as JSON?了解如何让linqpad生成json)

int userId = 4;
DateTime start = new DateTime(2015, 1 , 2);
DateTime end = new DateTime(2015, 6, 30);

// group datetime (yyyy-MM-dd HH:mm:ss) values into date (yyyy-MM-dd) group and get count aggregate
// get timestamp representation of date
// serialize to cal-heatmap json data via .ToDictionary
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 
{
    Timestamp = grp.Key.date,
    Reports = grp.Count()
})
.ToList()
.Select (r => new 
{
    Timestamp = (r.Timestamp - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds,
    r.Reports
})
.ToDictionary (r => r.Timestamp.ToString(), r => r.Reports);

new JavaScriptSerializer().Serialize(result).Dump();