'use client'; import { LineChart, Line, YAxis, XAxis, CartesianGrid, Tooltip, Area, Label } from 'recharts'; import spacetime from 'spacetime'; import regression from 'regression'; function polynomialTrendline(data, degree) { // Map your data to the form required by regression-js: [x, y] // Here we convert datetime to a numeric value (e.g., milliseconds since epoch) const regressionData = data.map((dp, i) => [i, dp.value]); const result = regression.polynomial(regressionData, { order: degree }); // result.equation contains the polynomial coefficients, // and result.predict(x) returns the predicted y for a given x value. return result; } function formatXAxis(tickItem) { return spacetime(tickItem).format('{date}'); } export default function TimeseriesChart({ data }) { const unit = data[0] ? data[0].unit : ''; const polyResult = polynomialTrendline(data, 1)// 2 for quadratic const dataWithFit = (data.map((dp, i) => { return { datetime: dp.datetime, value: dp.value, smoothed: polyResult.predict(i)[1], }; })); return (

Trend: {7 * polyResult.equation[0]} {unit}/week

`${spacetime(d).format("{month-short} {date}")}`} labelStyle={{ color: 'black' }} /> 0 ? "red" : "green"} dot={false} />
); }