Implementation csv with cal-heatmap jquery

I have been attempting to use cal-heatmap with a csv to display data. However nothing seems to make it work. There are no errors in the console.

cal-heatmap: http://kamisama.github.io/cal-heatmap/
Does anyone have any clue on how to make it work with csv files? Or even have an example with a csv? Does anyone have any alternatives that are easily to implement with Google Analytics?

THE CODE:

    
    
    
    
    
    
var cal = new CalHeatMap(); cal.init({ data: "test.csv", dataType: "csv", start: new Date(2013, 0), domain : "day", subDomain : "hour", itemName: ["visit", "visits"], range : 12, cellsize: 15, cellpadding: 3, cellradius: 5, domainGutter: 15, weekStartOnMonday: 0, scale: [40, 60, 80, 100] });

FORMAT OF CSV:

Hour,Visits
000000,53
000001,40
000002,58
000003,45
000004,35
000005,37
000006,41

Yes, Cal-heatmap does only understand data formatted like a JSON object.

BUT, you can set the dataType
to csv
, and use the csv parser, as in your example. The reason it doesn’t works out of the box is because after parsing, the resulting JSON object returned by d3.csv()
do have keys, and looks like that :

[
    {
        "Hour": 000000,
        "Visits": 53
    },
    ...
]

You’ll need to convert that back to something like that:

{
    "000000": 53,
    ...
}

Use the following function as your afterLoad()
callback:

function(data) {
    "use strict";

    var d = {};
    var keys = Object.keys(data[0]);
    var i, total;
    for (i = 0, total = data.length; i < total; i++) {
        d[data[i][keys[0]]] = parseInt(data[i][keys[1]], 10);
    }
    return d;
}

That will take care of the conversion. This function will work only if the first column is the timestamp, and the second column, the value. Adjust keys[n]
to your need if your CSV has a different structure.

AFAIK, Google Analytics does not use timestamp, so you have to incorporate the timestamp conversion in the function above.

Future version of Cal-Heatmap will do this conversion automatically.

Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » Implementation csv with cal-heatmap jquery

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录