import { DateTime } from '../../vendor/auto/luxon.mjs'; import { AVAILABLE_TRENDS, DAY_BG_COLORS } from '../config.mjs'; import { dark, convertTimestamp } from '../utils.mjs'; import { getSavedLocation } from '../placemanager.mjs'; import SunCalc from '../../vendor/auto/suncalc.mjs'; // dark vertical line at day boundaries const dayBoundaries = (startDays, axes, lineWidth) => { const markings = []; // reset to midnight on first day let index = startDays * 24 * 60 * 60 * 1000; do { // vertical black line markings.push({ xaxis: { from: index - lineWidth / 2, to: index + lineWidth / 2 }, color: '#000000' }); index += 2 * 12 * 60 * 60 * 1000; } while (index < axes.xaxis.max); return markings; }; const bgMarkingsGenerator = (metaData) => { // day and night background colors based on sunrise/sunset const dayNight = (startDays, axes, isDark) => { const markings = []; // calculate midnight on start day let index = startDays * 24 * 60 * 60 * 1000 + metaData.minTimestamp.o * 1000; // grab latitude and longitude for sun calculations const { lat, lon } = getSavedLocation(); // pre-populate sunrise and sunset times let yesterdayTimes = SunCalc.getTimes(DateTime.fromMillis(index).minus({ days: 1 }).toJSDate(), lat, lon); const freezingColor = (isDark) ? DAY_BG_COLORS.FREEZING.DARK : DAY_BG_COLORS.FREEZING.LIGHT; const dayColor = (isDark) ? DAY_BG_COLORS.DAY.DARK : DAY_BG_COLORS.DAY.LIGHT; do { // calculate today's const todayTimes = SunCalc.getTimes(DateTime.fromMillis(index).toJSDate(), lat, lon); let from; let to; if (isDark) { // dark mode slightly lightens the background from dawn to dusk from = convertTimestamp(DateTime.fromJSDate(todayTimes.sunrise, { zone: metaData.minTimestamp.zone.name })); to = convertTimestamp(DateTime.fromJSDate(todayTimes.sunset, { zone: metaData.minTimestamp.zone.name })); } else { from = convertTimestamp(DateTime.fromJSDate(yesterdayTimes.sunset, { zone: metaData.minTimestamp.zone.name })); to = convertTimestamp(DateTime.fromJSDate(todayTimes.sunrise, { zone: metaData.minTimestamp.zone.name })); } // gray on top markings.push({ xaxis: { from, to, }, yaxis: { from: AVAILABLE_TRENDS.apparentTemperature.scale.set(0, 0), }, color: dayColor, }); // blue (freezing) on bottom markings.push({ xaxis: { from, to, }, yaxis: { to: AVAILABLE_TRENDS.apparentTemperature.scale.set(0, 0) }, color: freezingColor, }); index += 24 * 60 * 60 * 1000; // shift sunrise/set data yesterdayTimes = todayTimes; } while (index < axes.xaxis.max + 26 * 60 * 60 * 1000); return markings; }; const generateMarks = (axes) => { const isDark = dark(); // days since epoch const startDays = Math.floor(axes.xaxis.min / (1000 * 60 * 60 * 24)); // calculate size of background boxes based on visible area // light = daylight // array of highlights const markings = []; markings.push(...dayNight(startDays, axes, isDark)); // calculate 5 px width for black line (as ms) // since the plot is not yet drawn, we have to make a best guess about the width of the plot area based on the width of the chart placeholder const lineWidth = axes.xaxis.c2p(2) - axes.xaxis.c2p(0); markings.push(...dayBoundaries(startDays, axes, lineWidth)); // blue line at current time const currentMillis = convertTimestamp(DateTime.local().setZone(metaData.minTimestamp.zone.name)); markings.push({ xaxis: { from: currentMillis - lineWidth / 3, to: currentMillis + lineWidth / 3 }, color: '#0000FF' }); return markings; }; return generateMarks; }; export default bgMarkingsGenerator;