anki/ts/graphs/graph-helpers.ts
Henrik Giesel 30bbbaf00b
Use eslint for sorting our imports (#1637)
* Make eslint sort our imports

* fix missing deps in eslint rule (dae)

Caught on Linux due to the stricter sandboxing

* Remove exports-last eslint rule (for now?)

* Adjust browserslist settings

- We use ResizeObserver which is not supported in browsers like KaiOS,
  Baidu or Android UC

* Raise minimum iOS version 13.4

- It's the first version that supports ResizeObserver

* Apply new eslint rules to sort imports
2022-02-04 18:36:34 +10:00

101 lines
2.2 KiB
TypeScript

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
/* eslint
@typescript-eslint/no-explicit-any: "off",
@typescript-eslint/ban-ts-comment: "off" */
import type { Selection } from "d3";
import type { Cards, Stats } from "../lib/proto";
// amount of data to fetch from backend
export enum RevlogRange {
Year = 1,
All = 2,
}
export function daysToRevlogRange(days: number): RevlogRange {
return days > 365 || days === 0 ? RevlogRange.All : RevlogRange.Year;
}
// period a graph should cover
export enum GraphRange {
Month = 0,
ThreeMonths = 1,
Year = 2,
AllTime = 3,
}
export interface GraphsContext {
cards: Cards.Card[];
revlog: Stats.RevlogEntry[];
revlogRange: RevlogRange;
nightMode: boolean;
}
export interface GraphBounds {
width: number;
height: number;
marginLeft: number;
marginRight: number;
marginTop: number;
marginBottom: number;
}
export function defaultGraphBounds(): GraphBounds {
return {
width: 600,
height: 250,
marginLeft: 70,
marginRight: 70,
marginTop: 20,
marginBottom: 25,
};
}
export function setDataAvailable(
svg: Selection<SVGElement, any, any, any>,
available: boolean,
): void {
svg.select(".no-data")
.attr("pointer-events", available ? "none" : "all")
.transition()
.duration(600)
.attr("opacity", available ? 0 : 1);
}
export function millisecondCutoffForRange(
range: GraphRange,
nextDayAtSecs: number,
): number {
let days;
switch (range) {
case GraphRange.Month:
days = 31;
break;
case GraphRange.ThreeMonths:
days = 90;
break;
case GraphRange.Year:
days = 365;
break;
case GraphRange.AllTime:
default:
return 0;
}
return (nextDayAtSecs - 86400 * days) * 1000;
}
export interface TableDatum {
label: string;
value: string;
}
export type SearchEventMap = { search: { query: string } };
export type SearchDispatch = <EventKey extends Extract<keyof SearchEventMap, string>>(
type: EventKey,
detail: SearchEventMap[EventKey],
) => void;