2015-04-25 27 views
-1

我有下面的cal-heatmap(+參見JSBin)我試圖用數據着色。爲什麼我的cal-heatmap無法顯示數據

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-熱圖似乎不承認正確格式化數據。 我錯過了什麼?

回答

0

事實證明,我的數據中的時間戳不是自1970年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-熱圖需要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(); 
相關問題