Skip to content

Instantly share code, notes, and snippets.

@mtvbrianking
Created July 23, 2023 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtvbrianking/bf48c84902afeb043eb57cf6f8b123e5 to your computer and use it in GitHub Desktop.
Save mtvbrianking/bf48c84902afeb043eb57cf6f8b123e5 to your computer and use it in GitHub Desktop.
Chart.js Zoom Plugin Utils
// https://github.com/chartjs/chartjs-plugin-zoom/blob/master/docs/scripts/utils.js
import { addDays, addHours, startOfWeek, endOfWeek, isWeekend, nextMonday, getHours, setHours } from "https://cdn.jsdelivr.net/
function valueOrDefault(value, defaultValue) {
return typeof value === 'undefined' ? defaultValue : value;
}
// Adapted from http://indiegamr.com/generate-repeatable-random-numbers-in-js/
let _seed = Date.now();
function srand(seed) {
_seed = seed;
}
function rand(min, max) {
min = valueOrDefault(min, 0);
max = valueOrDefault(max, 0);
_seed = (_seed * 9301 + 49297) % 233280;
return min + (_seed / 233280) * (max - min);
}
function numbers(config) {
const cfg = config || {};
const min = valueOrDefault(cfg.min, 0);
const max = valueOrDefault(cfg.max, 100);
const from = valueOrDefault(cfg.from, []);
const count = valueOrDefault(cfg.count, 8);
const decimals = valueOrDefault(cfg.decimals, 8);
const continuity = valueOrDefault(cfg.continuity, 1);
const dfactor = Math.pow(10, decimals) || 0;
const data = [];
let i, value;
for (i = 0; i < count; ++i) {
value = (from[i] || 0) + rand(min, max);
if (rand() <= continuity) {
data.push(Math.round(dfactor * value) / dfactor);
} else {
data.push(null);
}
}
return data;
}
function points(config) {
const xs = numbers(config);
const ys = numbers(config);
return xs.map((x, i) => ({x, y: ys[i]}));
}
const rand255 = () => Math.round(Math.random() * 255);
function randomColor(alpha) {
return 'rgba(' + rand255() + ',' + rand255() + ',' + rand255() + ',' + (alpha || '.3') + ')';
}
const MONTHS = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
function months(config) {
const cfg = config || {};
const count = cfg.count || 12;
const section = cfg.section;
const values = [];
let i, value;
for (i = 0; i < count; ++i) {
value = MONTHS[Math.ceil(i) % 12];
values.push(value.substring(0, section));
}
return values;
}
function hourlyPoints(config) {
const ys = numbers(config);
const start = new Date().valueOf();
return ys.map((y, i) => ({x: addHours(start, i), y}));
}
function nextOfficeHour(time) {
if (getHours(time) > 17) {
time = setHours(addDays(time, 1), 8);
}
if (getHours(time) < 9) {
time = setHours(time, 9);
} else {
time = addHours(time, 1);
}
if (isWeekend(time)) {
time = setHours(nextMonday(time), 9);
}
return time;
}
function officeHourPoints(config) {
const ys = numbers(config);
let time = new Date().valueOf();
return ys.map(y => {
time = nextOfficeHour(time);
return {x: +time, y};
});
}
function nextWeek() {
const now = new Date().valueOf();
const min = startOfWeek(addHours(endOfWeek(now), 24));
return {
min: +min,
max: +endOfWeek(min)
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment