/**
* Jquery plugin to render like contribution graph on Github.
*
* @see {@link https://github.com/bachvtuan/Github-Contribution-Graph}
* @author bachvtuan@gmail.com
* @license MIT License
* @since 0.1.0
*/
//Format string
if (!String.prototype.formatString) {
String.prototype.formatString = function () {
var args = arguments;
return this.replace(/{(\d+)}/g, function (match, number) {
return typeof args[number] != "undefined" ? args[number] : match;
});
};
}
(function ($) {
$.fn.github_graph = function (options) {
//If the number less than 10, add Zero before it
var prettyNumber = function (number) {
return number < 10
? "0" + number.toString()
: (number = number.toString());
};
/*
Count the number on each day and store the object
*/
var processListTimeStamp = function (list_timestamp) {
//The result will store into this varriable
obj_timestamp = {};
for (var i = 0; i < list_timestamp.length; i++) {
var _type = typeof list_timestamp[i];
var _d =
_type == "number"
? new Date(list_timestamp[i])
: new Date(list_timestamp[i].timestamp);
var display_date = getDisplayDate(_d);
var increase = _type == "number" ? 1 : list_timestamp[i].count;
if (!obj_timestamp[display_date]) {
obj_timestamp[display_date] = increase;
} else {
obj_timestamp[display_date] += increase;
}
}
};
var getDisplayDate = function (date_obj) {
var pretty_month = prettyNumber(date_obj.getMonth() + 1);
var pretty_date = prettyNumber(date_obj.getDate());
return "{0}-{1}-{2}".formatString(
date_obj.getFullYear(),
pretty_month,
pretty_date
);
};
var getCount = function (display_date) {
if (obj_timestamp[display_date]) {
return obj_timestamp[display_date];
}
return 0;
};
var getColor = function (count) {
if (typeof settings.colors[0] == "string") {
return count > settings.colors.length - 1
? settings.colors[settings.colors.length - 1]
: settings.colors[count];
}
const isLargeNumber = (element) => element.count > count;
i = settings.colors.findIndex(isLargeNumber);
return i == -1
? settings.colors[settings.colors.length - 1].color
: settings.colors[i - 1].color;
};
var start = function () {
processListTimeStamp(settings.data);
var wrap_chart = _this;
settings.colors_length = settings.colors.length;
var radius = settings.border.radius;
var hoverColor = settings.border.hover_color;
var clickCallback = settings.click;
var start_date;
if (settings.start_date == null) {
// if set null, will get from 365 days from now
start_date = new Date();
start_date.setMonth(start_date.getMonth() - 12);
start_date.setDate(start_date.getDate() + 1);
} else {
// formats:
// - YYYY-MM-DD
// - YYYY/MM/DD
start_date = new Date(settings.start_date);
}
end_date = new Date(start_date);
end_date.setMonth(end_date.getMonth() + 12);
end_date.setDate(end_date.getDate() - 1);
var loop_html = "";
var step = 13;
var month_position = [];
month_position.push({ month_index: start_date.getMonth(), x: 0 });
var using_month = start_date.getMonth();
var week = 0;
var g_x = week * step;
var item_html =
'