anki/ts/graphs/ease.ts

134 lines
3.7 KiB
TypeScript
Raw Normal View History

2020-06-23 12:43:19 +02:00
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
/* eslint
@typescript-eslint/no-non-null-assertion: "off",
@typescript-eslint/no-explicit-any: "off",
*/
import type { Stats, Cards } from "lib/proto";
import {
extent,
histogram,
sum,
scaleLinear,
scaleSequential,
interpolateRdYlGn,
} from "d3";
2021-01-30 21:50:05 +01:00
import type { Bin, ScaleLinear } from "d3";
import { CardType } from "lib/cards";
import type { HistogramData } from "./histogram-graph";
import type { TableDatum, SearchDispatch } from "./graph-helpers";
import * as tr from "lib/i18n";
2020-06-23 12:43:19 +02:00
export interface GraphData {
eases: number[];
}
export function gatherData(data: Stats.GraphsResponse): GraphData {
2021-07-10 11:52:31 +02:00
const eases = (data.cards as Cards.Card[])
.filter((c) => [CardType.Review, CardType.Relearn].includes(c.ctype))
.map((c) => c.easeFactor / 10);
2020-06-23 12:43:19 +02:00
return { eases };
}
function makeQuery(start: number, end: number): string {
if (start === end) {
return `"prop:ease=${start / 100}"`;
}
const fromQuery = `"prop:ease>=${start / 100}"`;
const tillQuery = `"prop:ease<${(end + 1) / 100}"`;
return `${fromQuery} AND ${tillQuery}`;
}
2021-01-30 21:50:05 +01:00
function getAdjustedScaleAndTicks(
min: number,
max: number,
desiredBars: number
): [ScaleLinear<number, number, never>, number[]] {
const prescale = scaleLinear().domain([min, max]).nice();
const ticks = prescale.ticks(desiredBars);
const predomain = prescale.domain() as [number, number];
const minOffset = min - predomain[0];
const tickSize = ticks[1] - ticks[0];
if (minOffset === 0 || (minOffset % tickSize !== 0 && tickSize % minOffset !== 0)) {
return [prescale, ticks];
}
const add = (n: number): number => n + minOffset;
return [
scaleLinear().domain(predomain.map(add) as [number, number]),
ticks.map(add),
];
}
2020-08-21 04:58:02 +02:00
export function prepareData(
data: GraphData,
dispatch: SearchDispatch,
2021-01-25 17:36:04 +01:00
browserLinksSupported: boolean
2020-08-21 04:58:02 +02:00
): [HistogramData | null, TableDatum[]] {
2020-06-23 12:43:19 +02:00
// get min/max
const allEases = data.eases;
if (!allEases.length) {
2020-08-21 04:58:02 +02:00
return [null, []];
2020-06-23 12:43:19 +02:00
}
const total = allEases.length;
const [, origXMax] = extent(allEases);
2020-06-23 12:43:19 +02:00
const xMin = 130;
2021-01-30 21:50:05 +01:00
const xMax = origXMax! + 1;
2020-06-23 12:43:19 +02:00
const desiredBars = 20;
2021-01-30 21:50:05 +01:00
const [scale, ticks] = getAdjustedScaleAndTicks(xMin, xMax, desiredBars);
2020-06-23 12:43:19 +02:00
const bins = histogram()
2021-01-30 21:50:05 +01:00
.domain(scale.domain() as [number, number])
.thresholds(ticks)(allEases);
2020-06-23 12:43:19 +02:00
const colourScale = scaleSequential(interpolateRdYlGn).domain([xMin, 300]);
2021-01-30 02:08:01 +01:00
function hoverText(bin: Bin<number, number>, _percent: number): string {
2020-06-28 12:52:38 +02:00
const minPct = Math.floor(bin.x0!);
const maxPct = Math.floor(bin.x1!);
const percent = maxPct - minPct <= 10 ? `${bin.x0}%` : `${bin.x0}%-${bin.x1}%`;
return tr.statisticsCardEaseTooltip({
2020-06-28 12:52:38 +02:00
cards: bin.length,
percent,
});
}
function onClick(bin: Bin<number, number>): void {
const start = bin.x0!;
const end = bin.x1! - 1;
const query = makeQuery(start, end);
dispatch("search", { query });
}
2020-08-21 04:58:02 +02:00
const xTickFormat = (num: number): string => `${num.toFixed(0)}%`;
const tableData = [
{
label: tr.statisticsAverageEase(),
2020-08-21 04:58:02 +02:00
value: xTickFormat(sum(allEases) / total),
},
];
return [
{
scale,
bins,
total,
hoverText,
2021-01-25 17:36:04 +01:00
onClick: browserLinksSupported ? onClick : null,
colourScale,
showArea: false,
xTickFormat,
},
2020-08-21 04:58:02 +02:00
tableData,
];
2020-06-23 12:43:19 +02:00
}