import { createMemo, For, Show, type Accessor, type JSX } from "solid-js"; import type { Entry } from "./data"; import { daysInMilliseconds, daysInMonth, endOfMonth, startOfDay, startOfMonth, } from "./date"; type Properties = { entries: Accessor; width: Accessor }; type TrendLine = Required< Pick, "x1" | "x2" | "y1" | "y2"> >; type Range = { from: number; to: number; }; type Ranges = { x: Range; y: Range; }; type Aggregation = { sums: { xSquared: number; xTimesY: number; x: number; y: number; }; ranges?: Ranges; }; type Point = { x: number; y: number }; const isPathAnimationSupported = CSS.supports('d: path("")'); function aggregate(points: Point[]): Aggregation { console.debug("Aggregating", points.length); if (points.length === 0) return { sums: { xSquared: 0, xTimesY: 0, x: 0, y: 0, }, }; const sums = { xSquared: 0, xTimesY: 0, x: 0, y: 0, }; const ranges = { x: { from: Infinity, to: 0, }, y: { from: Infinity, to: 0, }, }; for (const { x, y } of points) { sums.xSquared += x * x; sums.xTimesY += x * y; sums.x += x; sums.y += y; ranges.x.to = Math.max(ranges.x.to, x); ranges.y.to = Math.max(ranges.y.to, y); ranges.x.from = Math.min(ranges.x.from, x); ranges.y.from = Math.min(ranges.y.from, y); } return { sums, ranges }; } //TODO write tests for 1, 2, n inputs /** * @param xRange - The range of the x axis that is visible in the graph */ function useTrendLine( entries: Accessor, xRange: Range ): { trendLine: Accessor; aggregation: Accessor; } { const n = () => entries().length; const aggregation = createMemo(() => aggregate(entries())); const slope = () => { const { sums } = aggregation(); const currentN = n(); if (currentN === 0) return 0; const dividend = currentN * sums.xTimesY - sums.x * sums.y; if (dividend === 0) return 0; const divisor = currentN * sums.xSquared - sums.x * sums.x; if (divisor === 0) return Infinity; return dividend / divisor; }; const xAverage = () => aggregation().sums.x / n(); const yAverage = () => aggregation().sums.y / n(); const yIntercept = () => yAverage() - slope() * xAverage(); const y = (x: number) => slope() * x + yIntercept(); const trendLine = (): TrendLine | undefined => { const { ranges } = aggregation(); if (ranges === undefined) return; return { x1: 0, x2: xRange.to, y1: yIntercept(), y2: y(xRange.to), }; }; return { trendLine, aggregation }; } function XAxisMarks({ height, xMarks, projectX, }: { height: Accessor; xMarks: number[]; projectX: Projector; }) { return ( {(xMark) => { const x = projectX(xMark); return ( <> {xMark} ); }} ); } function YAxisMarks({ yMarks, projectY, }: { yMarks: number[]; projectY: Projector; }) { return ( {(yMark) => { const y = projectY(yMark); return ( <> {yMark} ); }} ); } type Projector = (value: number) => number; function Path({ points, projectX, projectY, height, }: { points: Accessor; height: Accessor; projectX: Projector; projectY: Projector; }) { const projected = createMemo(() => points() // .values() .map(({ x, y }) => ({ x: projectX(x), y: projectY(y), })) ); const from = () => `M${projected() .map(({ x }) => `${x},${height()}`) .join(" L")}`; const to = () => `M${projected() .map(({ x, y }) => `${x},${y}`) .join(" L")}`; const fromPath = () => `path("${from()}")`; const toPath = () => `path("${to()}")`; /** * A semicolon-separated list of entire path definitions (d="..."). This is where you define the key shapes: */ const values = () => `${from()};${to()}`; /** * A list of time markers (0 to 1) corresponding to each shape in {@link values}. This controls the timing of the bounce. * For example, `0; 0.8; 1` would spend 80% of the duration moving to the overshoot shape and 20% settling back. */ const keyTimes = "0; 1"; /** * "ease-out" easing function bezier curve points (x1, y1, x2, y2) in keySplines syntax. * [MDN easing-function](https://developer.mozilla.org/en-US/docs/Web/CSS/easing-function#cubic-bezier-easing-function) */ const EASE_OUT_KEY_SPLINES = "0.42 0 1 1"; const path = () => `M${projected() .map(({ x, y }) => `${x},${y}`) .join(" L")}`; return ( <> ); } export default function Graph({ entries, width }: Properties) { // Need to curb time stamp or we get overflow const now = new Date(); const timeStart = startOfMonth(now).getTime(); const timeEnd = endOfMonth(now).getTime(); function isInMonth({ timestamp }: Entry) { return timeStart <= timestamp.getTime() && timestamp.getTime() < timeEnd; } const days = daysInMonth(now); const points = () => entries() .values() .filter(isInMonth) .map(({ weight, timestamp }) => { const time = timestamp.getTime() - startOfDay(timestamp).getTime(); const dayCompletion = time / daysInMilliseconds(1); // It is easier to work with full days instead of milliseconds const day = timestamp.getDate() + dayCompletion; return { x: day, y: weight, }; }) .toArray(); const { trendLine, aggregation } = useTrendLine(points, { from: 0, to: days, }); const height = width; function projectX(x: number) { const percentage = x / days; const inSvg = percentage * width(); return inSvg; } const xMarks = function* () { const step = 5; // 1st yield 1; // 5th yield step; // 10th until end of month for (let x = step * 2; x < days - step; x += step) { yield x; } yield days; }; return ( {(ranges) => { const yRange = () => { const { y } = ranges; const padding = 3; const median = (y.from + y.to) / 2; let from = Math.min(median - 5, y.from - padding); let to = Math.max(median + 5, y.to + padding); // let from = y.from - padding; // let to = y.to + padding; const stepSize = to - from > 10 ? 5 : 2; // Round to nearest step marker to make it look cleaner from = Math.floor(from / stepSize) * stepSize; to = Math.ceil(to / stepSize) * stepSize; return { from, to, stepSize }; }; function projectY(y: number) { const { from, to } = yRange(); const length = to - from; const percentage = (y - from) / length; const inSvg = height() - percentage * height(); return inSvg; } const yMarks = function* () { const { from, to, stepSize } = yRange(); for (let y = from + stepSize; y < to; y += stepSize) { yield y; } yield to; }; function projectTrendline(trendLine: TrendLine) { const { x1, x2, y1, y2 } = trendLine; return { // Assume when it is a string is is the correct percentage string x1: typeof x1 === "number" ? projectX(x1) : x1, x2: typeof x2 === "number" ? projectX(x2) : x2, y1: typeof y1 === "number" ? projectY(y1) : y1, y2: typeof y2 === "number" ? projectY(y2) : y2, }; } return ( {(trendLine) => { const projectedTrendLine = projectTrendline(trendLine()); return ( ); }} {({ x, y }) => { return ( ); }} ); }} ); }