fixed up the mode graph

This commit is contained in:
csawatzky 2025-08-15 11:36:54 -06:00
parent e57f75bf56
commit 2bd04acb68

View file

@ -1,18 +1,13 @@
import { import {
Box,
Card, Card,
CircularProgress, CircularProgress,
FormControlLabel,
Grid,
Switch,
Typography Typography
} from "@mui/material"; } from "@mui/material";
import { blue, grey, red, yellow, orange } from "@mui/material/colors"; import { blue, grey, red, yellow, orange } from "@mui/material/colors";
import BarGraph, { BarData, RefArea } from "charts/BarGraph"; import BarGraph, { BarData } from "charts/BarGraph";
import MultiLineGraph, { LineData } from "charts/measurementCharts/MultiLineGraph";
import { Bin } from "models"; import { Bin } from "models";
import moment, { Moment } from "moment"; import moment, { Moment } from "moment";
import { pond, quack } from "protobuf-ts/pond"; import { pond } from "protobuf-ts/pond";
import { useBinAPI, useGlobalState } from "providers"; import { useBinAPI, useGlobalState } from "providers";
import { useCallback, useEffect, useState } from "react"; import { useCallback, useEffect, useState } from "react";
import { Legend } from "recharts"; import { Legend } from "recharts";
@ -39,7 +34,6 @@ export default function BinLevelOverTime(props: Props) {
const binAPI = useBinAPI(); const binAPI = useBinAPI();
const [{as}] = useGlobalState(); const [{as}] = useGlobalState();
const [inventoryData, setInventoryData] = useState<InventoryAt[]>([]); const [inventoryData, setInventoryData] = useState<InventoryAt[]>([]);
const [modeAreas, setModeAreas] = useState<RefArea[]>([]);
const [dataLoading, setDataLoading] = useState(false); const [dataLoading, setDataLoading] = useState(false);
const [capacity, setCapacity] = useState<number | undefined>(); const [capacity, setCapacity] = useState<number | undefined>();
@ -83,32 +77,14 @@ export default function BinLevelOverTime(props: Props) {
.listHistoryBetween(bin.key(), 500, startDate.toISOString(), endDate.toISOString()) .listHistoryBetween(bin.key(), 500, startDate.toISOString(), endDate.toISOString())
.then(resp => { .then(resp => {
let data: BarData[] = []; let data: BarData[] = [];
let modeAreas: RefArea[] = [];
let lastBushels = -1; let lastBushels = -1;
let currentMode: pond.BinMode | undefined = undefined
//values for ref areas let modeData: BarData[] = []
let x1 = 0;
let x2 = 0;
let y1 = -50;
let y2 = -200;
let currentMode: pond.BinMode;
resp.data.history.forEach(hist => { resp.data.history.forEach(hist => {
//build the data for the inventory bar graph
if (hist.settings?.inventory) { if (hist.settings?.inventory) {
let settings = pond.BinSettings.fromObject(hist.settings);
let bushels = hist.settings.inventory.grainBushels ?? 0; let bushels = hist.settings.inventory.grainBushels ?? 0;
if (bushels !== lastBushels || currentMode !== settings.mode) { if (bushels !== lastBushels) {
x1 = moment(hist.timestamp).valueOf();
x2 = moment(hist.timestamp).valueOf();
modeAreas.push({
x1: x1,
x2: x2,
y1: cap ? cap * -0.1 : y1,
y2: cap ? cap * -0.2 : y2,
fill: getFill(settings.mode)
});
currentMode = settings.mode;
let newData: InventoryAt = { let newData: InventoryAt = {
timestamp: moment(hist.timestamp).valueOf(), timestamp: moment(hist.timestamp).valueOf(),
value: fertilizerBin value: fertilizerBin
@ -121,6 +97,17 @@ export default function BinLevelOverTime(props: Props) {
lastBushels = bushels; lastBushels = bushels;
} }
} }
//build the data for the mode change bar graph
let histBin = Bin.create()
histBin.settings = hist.settings ?? pond.BinSettings.create()
if(hist.settings && currentMode !== hist.settings.mode){
currentMode = hist.settings.mode
modeData.push({
timestamp: moment(hist.timestamp).valueOf(),
value: 1,
fill: getFill(currentMode)
})
}
}); });
if (data.length === 0) { if (data.length === 0) {
let bushels = bin.settings.inventory?.grainBushels ?? 0; let bushels = bin.settings.inventory?.grainBushels ?? 0;
@ -133,17 +120,9 @@ export default function BinLevelOverTime(props: Props) {
: bushels, : bushels,
timestamp: currentTime timestamp: currentTime
}); });
modeAreas.push({
x1: currentTime,
x2: currentTime,
y1: cap ? cap * -0.1 : y1,
y2: cap ? cap * -0.2 : y2,
fill: getFill(bin.settings.mode)
});
} }
setInventoryData(data); setInventoryData(data);
setModeAreas(modeAreas); setModeData(modeData)
}) })
.finally(() => { .finally(() => {
setDataLoading(false); setDataLoading(false);
@ -290,7 +269,6 @@ export default function BinLevelOverTime(props: Props) {
<BarGraph <BarGraph
customHeight={customHeight} customHeight={customHeight}
data={inventoryData} data={inventoryData}
refAreas={modeAreas}
barColour={colour} barColour={colour}
yMax={capacity} yMax={capacity}
yMin={0} yMin={0}
@ -303,6 +281,7 @@ export default function BinLevelOverTime(props: Props) {
}; };
const binModeChart = () => { const binModeChart = () => {
console.log(modeData)
return ( return (
<BarGraph <BarGraph
data={modeData} data={modeData}