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

option for evaluating calendar weeks #16

Open
das399igste opened this issue Oct 31, 2023 · 0 comments
Open

option for evaluating calendar weeks #16

das399igste opened this issue Oct 31, 2023 · 0 comments

Comments

@das399igste
Copy link

Hello,
I've been using the great node-red-contrib-dashboard-bar-chart-data node for some time now.
But somehow I was missing the analysis by calendar week.

So I tried to incorporate this feature and created a fork for it.

In the end it was pretty easy. I needed a function that would calculate the calendar week for me. I found a very good one at https://weeknumber.com/how-to/javascript#.

// Returns the ISO week of the date.
Date.prototype.getWeek = function() {
  var date = new Date(this.getTime());
  date.setHours(0, 0, 0, 0);
  // Thursday in current week decides the year.
  date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
  // January 4 is always in week 1.
  var week1 = new Date(date.getFullYear(), 0, 4);
  // Adjust to Thursday in week 1 and count number of weeks from date to week1.
  return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000
                        - 3 + (week1.getDay() + 6) % 7) / 7);
}

I added this to the beginning of the bar-chart-data.js file.
Then I only had to adjust three functions in the bar-chart-data.js file:

function buildDateKey(date):

else if (myNode.x_interval == "weeks") {
return fullKey.slice(0, -10) + date.getWeek();
}

function buildLabels(date):

else if (myNode.x_interval == "weeks") {
label = "" + date.getFullYear() + "-" + ("0" + date.getWeek()).slice(-2);
}

function dateMinus(date_in, minus=1):

else if (myNode.x_interval == "weeks") {
date.setDate(date.getDate()-(minus*7));
}

And then the line in the bar-chart-data.html file
<option value='weeks'>weeks</option>
insert.

If you find the evaluation after weeks useful, I would be happy if you could incorporate it into your release.

Best regards
Harald (das399igste)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant