Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom end date and start of week to heatmap #66

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,3 @@ This repository has been released under the [MIT License](LICENSE)
------------------
Project maintained by [Frappe](https://frappe.io).
Used in [ERPNext](https://erpnext.com). Read the [blog post](https://medium.com/@pratu16x7/so-we-decided-to-create-our-own-charts-a95cb5032c97).

53 changes: 33 additions & 20 deletions dist/frappe-charts.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2600,6 +2600,8 @@ function addDays(date, numberOfDays) {
class Heatmap extends BaseChart {
constructor({
start = '',
end = '',
start_monday = 0,
domain = '',
subdomain = '',
data = {},
Expand All @@ -2617,11 +2619,13 @@ class Heatmap extends BaseChart {
this.discrete_domains = discrete_domains;
this.count_label = count_label;

let today = new Date();
this.start = start || addDays(today, 365);

this.start = start;
this.end = end;

this.start_monday = start_monday;

legend_colors = legend_colors.slice(0, 5);
this.legend_colors = this.validate_colors(legend_colors)
this.legend_colors = this.validate_colors(legend_colors)
? legend_colors
: ['#ebedf0', '#c6e48b', '#7bc96f', '#239a3b', '#196127'];

Expand All @@ -2633,6 +2637,21 @@ class Heatmap extends BaseChart {
this.setup();
}

setup_start_end() {
if (!this.start) {
let anchor_date = this.end ? this.end : new Date();
let year_before = new Date(anchor_date);
year_before.setFullYear(year_before.getFullYear() - 1);
this.start = year_before;
}

if (!this.end) {
let year_after = new Date(this.start);
year_after.setFullYear(year_after.getFullYear() + 1);
this.end = year_after;
}
}

validate_colors(colors) {
if(colors.length < 5) return 0;

Expand All @@ -2648,19 +2667,15 @@ class Heatmap extends BaseChart {
}

setup_base_values() {
this.setup_start_end();
this.today = new Date();

if(!this.start) {
this.start = new Date();
this.start.setFullYear( this.start.getFullYear() - 1 );
}
this.first_week_start = new Date(this.start.toDateString());
this.last_week_start = new Date(this.today.toDateString());
if(this.first_week_start.getDay() !== 7) {
addDays(this.first_week_start, (-1) * this.first_week_start.getDay());
addDays(this.first_week_start, (-1) * this.first_week_start.getDay() + this.start_monday);
}
this.last_week_start = new Date(this.end.toDateString());
if(this.last_week_start.getDay() !== 7) {
addDays(this.last_week_start, (-1) * this.last_week_start.getDay());
addDays(this.last_week_start, (-1) * this.last_week_start.getDay() + this.start_monday);
}
this.no_of_cols = getWeeksBetween(this.first_week_start + '', this.last_week_start + '') + 1;
}
Expand Down Expand Up @@ -2698,9 +2713,9 @@ class Heatmap extends BaseChart {
}

render_all_weeks_and_store_x_values(no_of_weeks) {
let current_week_sunday = new Date(this.first_week_start);
let first_week_start_day = new Date(this.first_week_start);
this.week_col = 0;
this.current_month = current_week_sunday.getMonth();
this.current_month = first_week_start_day.getMonth();

this.months = [this.current_month + ''];
this.month_weeks = {}, this.month_start_points = [];
Expand All @@ -2709,7 +2724,7 @@ class Heatmap extends BaseChart {

for(var i = 0; i < no_of_weeks; i++) {
let data_group, month_change = 0;
let day = new Date(current_week_sunday);
let day = new Date(first_week_start_day);

[data_group, month_change] = this.get_week_squares_group(day, this.week_col);
this.data_groups.appendChild(data_group);
Expand All @@ -2720,7 +2735,7 @@ class Heatmap extends BaseChart {
this.months.push(this.current_month + '');
this.month_weeks[this.current_month] = 1;
}
addDays(current_week_sunday, 7);
addDays(first_week_start_day, 7);
}
this.render_month_labels();
}
Expand All @@ -2730,7 +2745,7 @@ class Heatmap extends BaseChart {
const square_side = 10;
const cell_padding = 2;
const step = 1;
const today_time = this.today.getTime();
const end_time = this.end.getTime();

let month_change = 0;
let week_col_change = 0;
Expand Down Expand Up @@ -2770,9 +2785,7 @@ class Heatmap extends BaseChart {

let next_date = new Date(current_date);
addDays(next_date, 1);
if(next_date.getTime() > today_time) break;


if(next_date.getTime() > end_time) break;
if(next_date.getMonth() - current_date.getMonth()) {
month_change = 1;
if(this.discrete_domains) {
Expand Down
2 changes: 1 addition & 1 deletion dist/frappe-charts.min.cjs.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/frappe-charts.min.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/frappe-charts.min.iife.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/assets/js/frappe-charts.min.js

Large diffs are not rendered by default.

24 changes: 18 additions & 6 deletions docs/assets/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,17 +361,24 @@ document.querySelector('[data-aggregation="average"]').addEventListener("click",
// ================================================================================

let heatmap_data = {};
let current_date = new Date();
let timestamp = current_date.getTime()/1000;
timestamp = Math.floor(timestamp - (timestamp % 86400)).toFixed(1); // convert to midnight
for (var i = 0; i< 375; i++) {
heatmap_data[parseInt(timestamp)] = Math.floor(Math.random() * 5);
timestamp = Math.floor(timestamp - 86400).toFixed(1);
let start_date = new Date();
let tmp_date = new Date(start_date);
let end_date = new Date(start_date);
end_date.setFullYear(end_date.getFullYear() + 1);

while (tmp_date < end_date) {
tmp_date.setDate(tmp_date.getDate() + 1);
let timestamp = tmp_date.getTime()/1000;
timestamp = Math.floor(timestamp - (timestamp % 86400)).toFixed(1); // convert to midnight
heatmap_data[parseInt(timestamp)] = Math.floor(Math.random() * 6);
}

new Chart({
parent: "#chart-heatmap",
start: start_date,
end: end_date,
data: heatmap_data,
start_monday: 1,
type: 'heatmap',
legend_scale: [0, 1, 2, 4, 5],
height: 115,
Expand Down Expand Up @@ -400,6 +407,8 @@ Array.prototype.slice.call(

new Chart({
parent: "#chart-heatmap",
start: start_date,
end: end_date,
data: heatmap_data,
type: 'heatmap',
legend_scale: [0, 1, 2, 4, 5],
Expand Down Expand Up @@ -440,6 +449,9 @@ Array.prototype.slice.call(
new Chart({
parent: "#chart-heatmap",
data: heatmap_data,
start: start_date,
start_monday: 1,
end: end_date,
type: 'heatmap',
legend_scale: [0, 1, 2, 4, 5],
height: 115,
Expand Down
4 changes: 4 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ <h6 class="margin-vertical-rem">
</div>
<pre><code class="hljs javascript margin-vertical-px"> let heatmap = new Chart({
parent: "#heatmap",
data: heatmap_data, // object with date/timestamp-value pairs
start: start_date, // Defaults to Date() - 12 months
end: end_date, // Defaults to Date()
start_monday: 1, // default 0 (= Sunday)
type: 'heatmap',
height: 115,
data: heatmap_data, // object with date/timestamp-value pairs
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 33 additions & 20 deletions src/js/charts/Heatmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { isValidColor } from '../utils/colors';
export default class Heatmap extends BaseChart {
constructor({
start = '',
end = '',
start_monday = 0,
domain = '',
subdomain = '',
data = {},
Expand All @@ -24,11 +26,13 @@ export default class Heatmap extends BaseChart {
this.discrete_domains = discrete_domains;
this.count_label = count_label;

let today = new Date();
this.start = start || addDays(today, 365);

this.start = start;
this.end = end;

this.start_monday = start_monday;

legend_colors = legend_colors.slice(0, 5);
this.legend_colors = this.validate_colors(legend_colors)
this.legend_colors = this.validate_colors(legend_colors)
? legend_colors
: ['#ebedf0', '#c6e48b', '#7bc96f', '#239a3b', '#196127'];

Expand All @@ -40,6 +44,21 @@ export default class Heatmap extends BaseChart {
this.setup();
}

setup_start_end() {
if (!this.start) {
let anchor_date = this.end ? this.end : new Date();
let year_before = new Date(anchor_date);
year_before.setFullYear(year_before.getFullYear() - 1);
this.start = year_before;
}

if (!this.end) {
let year_after = new Date(this.start);
year_after.setFullYear(year_after.getFullYear() + 1);
this.end = year_after;
}
}

validate_colors(colors) {
if(colors.length < 5) return 0;

Expand All @@ -55,19 +74,15 @@ export default class Heatmap extends BaseChart {
}

setup_base_values() {
this.setup_start_end();
this.today = new Date();

if(!this.start) {
this.start = new Date();
this.start.setFullYear( this.start.getFullYear() - 1 );
}
this.first_week_start = new Date(this.start.toDateString());
this.last_week_start = new Date(this.today.toDateString());
if(this.first_week_start.getDay() !== 7) {
addDays(this.first_week_start, (-1) * this.first_week_start.getDay());
addDays(this.first_week_start, (-1) * this.first_week_start.getDay() + this.start_monday);
}
this.last_week_start = new Date(this.end.toDateString());
if(this.last_week_start.getDay() !== 7) {
addDays(this.last_week_start, (-1) * this.last_week_start.getDay());
addDays(this.last_week_start, (-1) * this.last_week_start.getDay() + this.start_monday);
}
this.no_of_cols = getWeeksBetween(this.first_week_start + '', this.last_week_start + '') + 1;
}
Expand Down Expand Up @@ -105,9 +120,9 @@ export default class Heatmap extends BaseChart {
}

render_all_weeks_and_store_x_values(no_of_weeks) {
let current_week_sunday = new Date(this.first_week_start);
let first_week_start_day = new Date(this.first_week_start);
this.week_col = 0;
this.current_month = current_week_sunday.getMonth();
this.current_month = first_week_start_day.getMonth();

this.months = [this.current_month + ''];
this.month_weeks = {}, this.month_start_points = [];
Expand All @@ -116,7 +131,7 @@ export default class Heatmap extends BaseChart {

for(var i = 0; i < no_of_weeks; i++) {
let data_group, month_change = 0;
let day = new Date(current_week_sunday);
let day = new Date(first_week_start_day);

[data_group, month_change] = this.get_week_squares_group(day, this.week_col);
this.data_groups.appendChild(data_group);
Expand All @@ -127,7 +142,7 @@ export default class Heatmap extends BaseChart {
this.months.push(this.current_month + '');
this.month_weeks[this.current_month] = 1;
}
addDays(current_week_sunday, 7);
addDays(first_week_start_day, 7);
}
this.render_month_labels();
}
Expand All @@ -137,7 +152,7 @@ export default class Heatmap extends BaseChart {
const square_side = 10;
const cell_padding = 2;
const step = 1;
const today_time = this.today.getTime();
const end_time = this.end.getTime();

let month_change = 0;
let week_col_change = 0;
Expand Down Expand Up @@ -177,9 +192,7 @@ export default class Heatmap extends BaseChart {

let next_date = new Date(current_date);
addDays(next_date, 1);
if(next_date.getTime() > today_time) break;


if(next_date.getTime() > end_time) break;
if(next_date.getMonth() - current_date.getMonth()) {
month_change = 1;
if(this.discrete_domains) {
Expand Down