<div id="cal-heatmap"></div>
<script type="text/javascript">
var cal = new CalHeatMap();
cal.init({
data: "datas.json"
});
</script>
This will insert a new calendar into #cal-heatmap in your page, and fill it with data from datas.json
.
By default, the calendar will display 12 hours, divided into 60 minutes.
There's a lot of other settings available to customize your calendar.
Name | Type | Default | Description |
---|---|---|---|
data |
String|Object | "" |
Example
The calendar data source The data source is an object with informations used to fill the calendar. Those datas can be retrieved by two ways :
In both cases, the datas must follow a formatting standard for cal-heatmap to process them. Refer to Date source section for accepted format. If you're getting your data form an API, with an URL like Available templates are :
Example: All timestamp are in seconds
|
dataType |
String | json |
Parser engine for the data source. Sometime, we don't have a word to say about how an API return its datas. Whereas it's usually a json object (default), it also can be a simple plain text, or csv. You can tell cal-heatmap which parser to use to interpret the data source response :
|
id |
String | #cal-heatmap | Example ID of the element to insert the calendar to |
domain |
String | hour |
Example
Type of time unit for the domain
Can take one of the following values :
|
subDomain |
String | min |
Example
Time division inside the domain
Can take one of the following values :
Obviously, the subDomain must be "smaller" than the domain. A day domain can only accept an hour or min subDomain, not day, week, month or year.
|
range |
integer | 12 | Example Number of domain to display |
start |
Date | new Date() |
Example
Start Date of the calendar
Default is the current date. |
loadOnInit |
Boolean | true | Whether to fill the calendar with the data, on calendar creation
Passing |
Name | Type | Default | Description |
---|---|---|---|
afterLoad |
callback | null | Called after drawing the empty calendar, with no datas in it yet |
onClick |
callback | null |
Example
Called when clicking on a subdomain cell
2 arguments is passed to the callback :
|
afterLoadPreviousDomain |
callback | null |
Example
Called after loading the previous domain
2 arguments is passed to the callback :
|
afterLoadNextDomain |
callback | null |
Example
Called after loading the next domain
2 arguments is passed to the callback :
|
onComplete |
callback | null |
Called after finish drawing and filling the graph, or just after drawing the graph if |
afterLoadData |
callback | null |
Called after fetching the data source, but before interpreting its contents. This callback takes the result of the data source as argument, after interpretation by the dataType engine. This argument can takes various format, depending on the data source. This callback must return a json object, formatted like described in the data source section. This callback is used primary for converting data from various source and format to the format used by cal-heatmap. If the data source is a :
|
Name | Type | Default | Description |
---|---|---|---|
cellsize |
integer | 10 |
Example Size of each subDomain cell, in pixel |
cellpadding |
integer | 2 |
Example Gutter between each subDomain cell, in pixel |
cellradius |
integer | 0 |
Example
For rounding the corner of the subdomain cell, in pixels. You have to use another value than crispedges for the shape-rendering property in your css. |
domainGutter |
integer | 2 |
Gutter between each domain. |
scale |
Array | [10, 20, 30, 40] |
Example
Threshold for the scale
Each couple of values represent a color on the calendar :
It also accepts negative and float values. You can set more than 4 threshold. Each threshold correspond to 1 class in the css file. By default, the css support only 4 thresholds. Add the corresponding class to the css. |
displayScale |
Boolean | true |
Example Whether to display the scale |
scaleLabel |
Object |
{ lower: "less than {min} {name}", inner: "between {down} and {up} {name}", upper: "more than {max} {name}" } |
Example
Template for the title when hovering on a scale/legend See Example for usage
Strings can take various tokens, that are replaced on runtime.
|
itemName |
Array | ["item", "items"] |
Example
Name of the data type you're representing on the calendar
First index is the singular form, second index is the plural form. |
cellLabel |
Object |
{ empty: "{date}", filled: "{count} {name} {connector} {date}" } |
Example
Template for the title when hovering on date cell See Example for usage
Strings can take various tokens, that are replaced on runtime.
|
format |
Object | {} |
Example
Custom formatting of dates and labels
This object has two properties :
Refer to d3.js time formatting documentation for accepted format. You can also pass a function to You have to always set the 2 properties, even you wish to only change one. They're by default null, and depends on the domain and subdomain used. |
browsing |
boolean | false |
Example Whether to enable domain browsing. Will display 2 links to shift the calendar one domain back or forward. |
browsingOptions |
Object |
{ nextLabel: "Next", previousLabel: "Previous" } |
Text to display inside the links for browsing domains. Can also accept html.
By default, will display links like in the second example. You can customize the text and the css to have more beautiful buttons, like in the main example. |
duration |
integer | 500 |
Duration of the animation in milliseconds. Two things are animated in the calendar :
|
startWeekOnMonday |
integer | 1 |
ExampleWhether to start a week on Sunday or Monday |
By default, cal-heatmap will always display 12 hours, split into 60 minutes each.
Unless you set the date
property in init()
, the calendar will start at the beginning of the current hour.
var calendar = new CalHeatMap();
calendar.init({});
Display all the hours for 10 days, starting from January 15th, 2000.
JsFiddlevar calendar = new CalHeatMap();
calendar
data: "datas-years.json", // Fill the graph with datas from that json file
start: new Date(2000, 0, 15), // Start the calendar on 15th January, 2000
id : "graph_b",
domain : "day", // Group data by days
subDomain : "hour", // Split each day by hours
range : 10, // Number of days to display
browsing: true, // Enable browsing
afterLoadNextDomain: function (start, end) {
alert("You just loaded a new domain starting on " + start + " and ending on " + end);
}
});
Display all days, for 3 months, from February 2000.
Let's try changing the scale threshold, since the number of data represented in a subdomain is larger, since it's now a day instead of hour. Hover on the scale to view the new range each color is representing.
JsFiddlevar calendar = new CalHeatMap();
calendar
data: "datas-years.json",
start: new Date(2000, 1),
id : "graph_c",
domain : "month", // Group data by month
subDomain : "day", // Split each month by days
range : 3, // Just display 3 months
scale: [40, 60, 80, 100] // Custom threshold for the scale
});
Same as above, but with the days layered like a regular month calendar, with each column corresponding to a day, and each row to a week. Also start the week on Sunday.
var calendar = new CalHeatMap();
calendar
data: "datas-years.json",
start: new Date(2000, 5),
id : "graph_k",
domain : "month",
subDomain : "x_day",
range : 3,
cellsize: 15,
cellpadding: 3,
cellradius: 5,
domainGutter: 15,
weekStartOnMonday: 0,
scale: [40, 60, 80, 100]
});
Display all the hours of January 2000.
Also resize the cell to fit in the page.
JsFiddlevar calendar = new CalHeatMap();
calendar
data: "datas-years.json",
start: new Date(2000, 0),
id : "graph_f",
domain : "month",
subDomain : "hour",
range : 1,
cellsize: 6,
cellpadding: 1
});
Display weeks for February, March and April 2000.
JsFiddlevar calendar = new CalHeatMap();
calendar
data: "datas-years.json",
start: new Date(2000, 1),
id : "graph_c",
domain : "month", // Group data by month
subDomain : "week", // Split each month by week
range : 3, // Just display 3 months
scale: [120, 400, 450, 500] // Custom threshold for the scale
});
Display all the hours of the first 2 weeks of Januray 2000
JsFiddlevar calendar = new CalHeatMap();
calendar
data: "datas-years.json",
start: new Date(2000, 0),
id : "graph_h",
domain : "week",
subDomain : "hour",
range : 2
});
Display all days of year 2000.
Additionally, use a custom name for the data : we're representing the number of lost kittens by days, in french.
JsFiddlevar calendar = new CalHeatMap();
calendar.init({
moment.lang("fr");
var calendar = new CalHeatMap();
calendar.init({
data: "datas-years.json",
start: new Date(2000, 0),
id : "graph_d",
domain : "year",
subDomain : "day",
range : 1,
scale: [40, 60, 80, 100],
itemName: ["chat perdu", "chats perdus"],
cellLabel: {
empty: "Aucune données pour le {date}",
filled: "il y a eu {count} {name} le {date}"
},
scaleLabel: {
lower: "Belle journée, il y a eu moins de {min} {name}",
inner: "Pas mal, entre {down} et {up} {name}",
upper: "Peut faire mieux, plus de {max} {name}"
},
format: {
date: function(date) {
return moment(date).format("LL");
},
legend: null,
}
});
});
Display 1 year, split by months, with a custom legend and title formatting, as well as a custom callback when clicking on a month.
JsFiddlevar calendar = new CalHeatMap();
calendar.init({
data: "datas-years.json",
start: new Date(2000, 0),
id : "graph_e",
range : 1,
domain : "year",
subDomain : "month",
scale: [1950, 2050, 2150, 2250],
displayScale: false, // Don't display the scale
format: {
date: "==%B==", // Custom date format, hover on a month to view
legend: "Thy year is %Y, yeah !" // Custom domain legend
},
onClick: function(date, count) {
alert("Oh gosh, " + count + " things occured on " + date.toISOString());
}
});
All colors can be customized via the css
This graph represents the number of visitors per hour for one month. Datas are pulled from Google Analytics, in csv format. This example also highlight the datas format conversion, to transform your datas to something understandable by cal-heatmap.
var calendar = new CalHeatMap();
calendar.init({
id: "graph_l",
data: "ga.csv",
dataType: "csv",
domain: "day",
subDomain: "hour",
range: 31,
cellpadding: 1,
itemName : ["visitor", "visitors"],
scale: [2, 4, 6, 8, 10],
start: new Date(2013, 03, 14),
afterLoadData: function(d) {
var start = +new Date(2013, 03, 14) / 1000;
var stats = {};
for (var i = 0, total = d.length; i < total; i++ ) {
o = d[i];
stats[(parseInt(o.Hour, 10) * 3600) + start] = o.Visits;
}
return stats;
}
});
To display datas in the calendar, cal-heatmap takes a set of dates, associated with a count value. Those datas are grouped inside a json object, following the convention :
{
"946702811" : 12,
"946702812" : 53
....
}
The object must contains one property by date, keyname is the unix timestamp of the date (in seconds), and the value, is the count number associated to this date.
See the example datas-hours.json file.
It's the same formatting for all domain type, data will automatically be grouped into min/days/week/hours/month, depending on the domain type. Having a count for each seconds lets you jump to the smaller domain without losing datas.
Of course, we can't expect all the API to return the results following the format above.
That's why there's a special callback (afterLoadData(d)
) that you can use to convert your results to the required format. This function takes the result of your API as argument, after interpretation by the dataType engine, and must return a json object formatted as above.
// datas is an array of object
var datas = [
{date: 946702811, value: 15},
{date: 946702812, value: 25},
{date: 946702813, value: 10}
]
var parser = function(data) {
var stats = {};
for (var d in data) {
stats[data[d].date] = data[d].value;
}
return stats;
};
// Parser will output the object
//{
// "946702811": 15,
// "946702812": 25,
// "946702813": 10
}
var calendar = new CalHeatMap();
calendar.init({
data: datas,
afterLoadData: parser
});
Support and bug reports are on tracked with the project github issues
Faq : https://github.com/kamisama/cal-heatmap/wiki/FAQ
Wan Qi Chen
CalHeatMap is released under the MIT License
Copyright (c) 2013 Wan Qi Chen Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.