refactoring so imperial temp works in graph

feature/frontend-charts
Kenneth Barbour 2020-07-29 20:38:47 -04:00
parent 365bf032ba
commit 551d69be8e
6 changed files with 95 additions and 93 deletions

View File

@ -119,7 +119,7 @@ Graph.defaults = {
title: false,
width: 100,
height: 40,
margins: [5, 3, 2, 3], // top right bottom left
margins: [5, 3, 2, 5], // top right bottom left
datasets: [],
}
Graph.datasetDefaults = {

View File

@ -2,12 +2,15 @@ const m = require("mithril");
const Weather = {
current: null,
history: [],
loadCurrent: function() {
return m.request({
method: "GET",
url: "/weather/current"
}).then(function(result){
Weather.current = result;
Weather.history.push(result);
Weather.history = Weather.history.slice(Math.max(Weather.history.length - 120, 0));
});
}
};

View File

@ -0,0 +1,56 @@
import UserPrefs from './models/UserPrefs';
export function localeIsImperial() {
return !!(UserPrefs.userImperial);
}
export function localeTemperature(celsius, precision=1)
{
if (UserPrefs.prefs.useImperial) {
const f = cToF(celsius);
return `${f.toFixed(precision)} °F`;
}
return `${celsius.toFixed(precision)} °C`;
}
export function localePressure(hpa)
{
if (UserPrefs.prefs.useImperial) {
const inHg = hpa * 0.00029529980164712;
return `${inHg.toFixed(2)} inHg`;
}
const millibar = hpa * 0.01;
return `${millibar.toFixed(0)} mb`;
}
export function cToF(celsius)
{
return celsius * 1.8 + 32;
}
export function fToC(f)
{
return (f - 32) / 1.8;
}
export function feelsLike(celsius, rh)
{
const f = cToF(celsius);
const hindex = -42.379 + (2.04901523 * f) + (10.14333127 * rh)
- (0.22475541 * f * rh) - (6.83783e-3 * f * f)
- (5.481717e-2 * rh * rh) + (1.22874e-3 * f * f * rh)
+ (8.5282e-4 * f * rh * rh) - (1.99e-6 * f * f * rh * rh);
return fToC(hindex);
}
export function dewPoint(celsius, rh)
{
const a = 6.1121;
const b = 18.678;
const c = 257.14;
const d = 234.5;
const e = 2.7182818284;
const trh = Math.log(rh / 100) + (b*celsius/(c + celsius));
return (c * trh / (b-trh));
}

View File

@ -3,59 +3,7 @@ import Layout from '../components/Layout';
import Weather from '../models/weather';
import MeasurementList from '../components/MeasurementList';
import Measurement from '../components/Measurement';
import UserPrefs from '../models/UserPrefs';
function localeTemperature(celsius)
{
if (UserPrefs.prefs.useImperial) {
const f = cToF(celsius);
return `${f.toFixed(1)} °F`;
}
return `${celsius.toFixed(1)} °C`;
}
function localePressure(hpa)
{
if (UserPrefs.prefs.useImperial) {
const inHg = hpa * 0.00029529980164712;
return `${inHg.toFixed(2)} inHg`;
}
const millibar = hpa * 0.01;
return `${millibar.toFixed(0)} mb`;
}
function cToF(celsius)
{
return celsius * 1.8 + 32;
}
function fToC(f)
{
return (f - 32) / 1.8;
}
function feelsLike(celsius, rh)
{
const f = cToF(celsius);
const hindex = -42.379 + (2.04901523 * f) + (10.14333127 * rh)
- (0.22475541 * f * rh) - (6.83783e-3 * f * f)
- (5.481717e-2 * rh * rh) + (1.22874e-3 * f * f * rh)
+ (8.5282e-4 * f * rh * rh) - (1.99e-6 * f * f * rh * rh);
return fToC(hindex);
}
function dewPoint(celsius, rh)
{
const a = 6.1121;
const b = 18.678;
const c = 257.14;
const d = 234.5;
const e = 2.7182818284;
const trh = Math.log(rh / 100) + (b*celsius/(c + celsius));
return (c * trh / (b-trh));
}
import { localeTemperature, localePressure, feelsLike, dewPoint } from '../util';
class CurrentConditions {
oninit() {

View File

@ -2,60 +2,55 @@ import m from 'mithril';
import Layout from '../components/Layout';
import Graph from '../components/Graph';
import Weather from '../models/weather';
import { localePressure, localeTemperature } from '../util';
function generateDataset({title, field, minY, maxY}) {
if (Weather.history.length == 0) return null;
function generateDataset({title, field, minY, maxY, yLabels}) {
if (Weather.history.length == 0) return { title};
return {
title: title,
data: Weather.history.map((e) => ([e.time, e[field]])),
minX: Weather.history[0].time,
maxX: Weather.current.time,
minY: minY,
maxY: maxY
maxY: maxY,
yLabels: yLabels,
}
}
class HistoryView {
view() {
const title = 'Recent Conditions';
let now = (new Date()).getTime()/1000;
const datasets = [
//generateDataset({title: "Temperature", field: 'temperature'}),
{
generateDataset({
title: "Temperature",
data: [
[0, 25],
[1, 26],
[2, 26.5],
[3, 25.9],
[4, 24.2],
[5, 24.3],
[6, 24.1],
],
yLabels: [
{ position: 25, label: '25', line: true},
{ position: 20, label: '20'},
{ position: 0, label: '0', line: true},
],
xLabels: [
{ position: 0, label: '0'},
{ position: 3, label: '3', line: true},
{ position: 6, label: '6', line: true},
]
},
{
field: "temperature",
minY: 0,
maxY: 45,
yLabels: [...Array(10).keys()].map((i) => ({
position: i*5,
label: localeTemperature(i*5, 0),
line: true,
})),
}),
generateDataset({
title: "Pressure",
data: [
[0, 1013],
[1, 1013],
[2, 1014],
[3, 1013],
[4, 1012],
[5, 1012.5],
[6, 1012.2],
],
minY: 1000,
maxY: 1020,
}
field: "pressure",
minY: 100000,
maxY: 103000,
yLabels: [],
}),
generateDataset({
title: "Humidity",
field: "humidity",
minY: 0,
maxY: 100,
yLabels: [...Array(11).keys()].map((v) => ({
position: v*10,
label: `${v*10} %`,
line: true,
})),
})
];
console.log({datasets});
return (