anki/ts/graphs/card-counts.ts

209 lines
5.9 KiB
TypeScript
Raw Normal View History

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
2020-07-04 05:38:46 +02:00
/* eslint
@typescript-eslint/no-non-null-assertion: "off",
@typescript-eslint/no-explicit-any: "off",
*/
import { CardQueue, CardType } from "anki/cards";
import type pb from "anki/backend_proto";
2021-01-04 15:36:15 +01:00
import { schemeGreens, schemeBlues, schemeOranges } from "d3-scale-chromatic";
2020-07-04 05:38:46 +02:00
import "d3-transition";
import { select } from "d3-selection";
2020-07-04 05:38:46 +02:00
import { scaleLinear } from "d3-scale";
2020-08-12 10:58:21 +02:00
import { pie, arc } from "d3-shape";
import { interpolate } from "d3-interpolate";
import type { GraphBounds } from "./graph-helpers";
2020-07-04 05:38:46 +02:00
import { cumsum } from "d3-array";
import type { I18n } from "anki/i18n";
type Count = [string, number, boolean];
2020-07-04 05:38:46 +02:00
export interface GraphData {
2020-06-27 13:10:17 +02:00
title: string;
counts: Count[];
2020-07-04 05:38:46 +02:00
totalCards: number;
}
const barColours = [
schemeBlues[5][2] /* new */,
schemeOranges[5][2] /* learn */,
schemeOranges[5][3] /* relearn */,
schemeGreens[5][2] /* young */,
schemeGreens[5][3] /* mature */,
"#FFDC41" /* suspended */,
"grey" /* buried */,
];
2021-01-05 17:15:47 +01:00
function countCards(
cards: pb.BackendProto.ICard[],
separateInactive: boolean,
i18n: I18n
): Count[] {
let newCards = 0;
let learn = 0;
let relearn = 0;
let young = 0;
let mature = 0;
let suspended = 0;
let buried = 0;
for (const card of cards as pb.BackendProto.Card[]) {
if (separateInactive) {
switch (card.queue) {
case CardQueue.Suspended:
suspended += 1;
continue;
case CardQueue.SchedBuried:
case CardQueue.UserBuried:
buried += 1;
continue;
}
}
switch (card.ctype) {
case CardType.New:
newCards += 1;
break;
case CardType.Learn:
learn += 1;
break;
case CardType.Review:
if (card.interval < 21) {
young += 1;
2021-01-04 15:36:15 +01:00
} else {
mature += 1;
}
2021-01-04 15:36:15 +01:00
break;
case CardType.Relearn:
relearn += 1;
2021-01-04 15:36:15 +01:00
break;
}
}
const counts: Count[] = [
[i18n.tr(i18n.TR.STATISTICS_COUNTS_NEW_CARDS), newCards, true],
[i18n.tr(i18n.TR.STATISTICS_COUNTS_LEARNING_CARDS), learn, true],
[i18n.tr(i18n.TR.STATISTICS_COUNTS_RELEARNING_CARDS), relearn, true],
[i18n.tr(i18n.TR.STATISTICS_COUNTS_YOUNG_CARDS), young, true],
[i18n.tr(i18n.TR.STATISTICS_COUNTS_MATURE_CARDS), mature, true],
2021-01-05 17:15:47 +01:00
[
i18n.tr(i18n.TR.STATISTICS_COUNTS_SUSPENDED_CARDS),
suspended,
separateInactive,
],
[i18n.tr(i18n.TR.STATISTICS_COUNTS_BURIED_CARDS), buried, separateInactive],
2021-01-05 17:15:47 +01:00
];
return counts;
}
2021-01-04 15:36:15 +01:00
export function gatherData(
data: pb.BackendProto.GraphsOut,
separateInactive: boolean,
2021-01-04 15:36:15 +01:00
i18n: I18n
): GraphData {
const totalCards = data.cards.length;
2021-01-05 17:15:47 +01:00
const counts = countCards(data.cards, separateInactive, i18n);
return {
2020-06-27 13:10:17 +02:00
title: i18n.tr(i18n.TR.STATISTICS_COUNTS_TITLE),
counts,
2020-07-04 05:38:46 +02:00
totalCards,
};
}
interface Reviews {
mature: number;
young: number;
learn: number;
relearn: number;
early: number;
}
export interface SummedDatum {
2020-07-16 04:28:31 +02:00
label: string;
// count of this particular item
2020-07-16 04:28:31 +02:00
count: number;
// show up in the table
2021-01-05 17:15:47 +01:00
show: boolean;
// running total
2020-07-16 04:28:31 +02:00
total: number;
}
export interface TableDatum {
label: string;
count: number;
percent: string;
colour: string;
}
2020-07-04 05:38:46 +02:00
export function renderCards(
svgElem: SVGElement,
bounds: GraphBounds,
2020-08-12 10:58:21 +02:00
sourceData: GraphData
): TableDatum[] {
2021-01-04 15:14:50 +01:00
const summed = cumsum(sourceData.counts, (d: Count) => d[1]);
2020-07-04 05:38:46 +02:00
const data = Array.from(summed).map((n, idx) => {
2020-07-16 04:28:31 +02:00
const count = sourceData.counts[idx];
2020-07-04 05:38:46 +02:00
return {
2020-07-16 04:28:31 +02:00
label: count[0],
count: count[1],
show: count[2],
2020-07-04 05:38:46 +02:00
idx,
total: n,
2020-07-16 04:28:31 +02:00
} as SummedDatum;
2020-07-04 05:38:46 +02:00
});
2020-08-12 10:58:21 +02:00
// ensuring a non-zero range makes the percentages not break
// in an empty collection
2020-07-06 06:01:49 +02:00
const xMax = Math.max(1, summed.slice(-1)[0]);
2020-07-04 05:38:46 +02:00
const x = scaleLinear().domain([0, xMax]);
const svg = select(svgElem);
2020-08-12 10:58:21 +02:00
const paths = svg.select(".counts");
2021-01-04 15:14:50 +01:00
const pieData = pie()(sourceData.counts.map((d: Count) => d[1]));
2020-08-12 10:58:21 +02:00
const radius = bounds.height / 2 - bounds.marginTop - bounds.marginBottom;
const arcGen = arc().innerRadius(0).outerRadius(radius);
2020-07-04 05:38:46 +02:00
const trans = svg.transition().duration(600) as any;
2020-08-12 10:58:21 +02:00
paths
.attr("transform", `translate(${radius},${radius + bounds.marginTop})`)
.selectAll("path")
.data(pieData)
.join(
2021-01-05 17:15:47 +01:00
(enter) =>
enter
2020-08-12 10:58:21 +02:00
.append("path")
.attr("fill", (_d, idx) => {
return barColours[idx];
2020-08-12 10:58:21 +02:00
})
.attr("d", arcGen as any),
function (update) {
return update.call((d) =>
2021-01-05 17:15:47 +01:00
d.transition(trans).attrTween("d", (d) => {
const interpolator = interpolate(
{ startAngle: 0, endAngle: 0 },
d
);
return (t): string => arcGen(interpolator(t) as any) as string;
})
2020-08-12 10:58:21 +02:00
);
}
);
x.range([bounds.marginLeft, bounds.width - bounds.marginRight]);
2020-07-04 05:38:46 +02:00
2021-01-05 17:15:47 +01:00
const tableData = (data as any).flatMap((d: SummedDatum, idx: number) => {
const percent = ((d.count / xMax) * 100).toFixed(1);
return d.show
2021-01-05 17:15:47 +01:00
? ({
label: d.label,
count: d.count,
percent: `${percent}%`,
colour: barColours[idx],
} as TableDatum)
: [];
});
2020-07-04 05:38:46 +02:00
return tableData;
}