Anki/ts/routes/graphs/hours.ts
Damien Elmes 9f55cf26fc
Switch to SvelteKit (#3077)
* Update to latest Node LTS

* Add sveltekit

* Split tslib into separate @generated and @tslib components

SvelteKit's path aliases don't support multiple locations, so our old
approach of using @tslib to refer to both ts/lib and out/ts/lib will no
longer work. Instead, all generated sources and their includes are
placed in a separate out/ts/generated folder, and imported via @generated
instead. This also allows us to generate .ts files, instead of needing
to output separate .d.ts and .js files.

* Switch package.json to module type

* Avoid usage of baseUrl

Incompatible with SvelteKit

* Move sass into ts; use relative links

SvelteKit's default sass support doesn't allow overriding loadPaths

* jest->vitest, graphs example working with yarn dev

* most pages working in dev mode

* Some fixes after rebasing

* Fix/silence some svelte-check errors

* Get image-occlusion working with Fabric types

* Post-rebase lock changes

* Editor is now checked

* SvelteKit build integrated into ninja

* Use the new SvelteKit entrypoint for pages like congrats/deck options/etc

* Run eslint once for ts/**; fix some tests

* Fix a bunch of issues introduced when rebasing over latest main

* Run eslint fix

* Fix remaining eslint+pylint issues; tests now all pass

* Fix some issues with a clean build

* Latest bufbuild no longer requires @__PURE__ hack

* Add a few missed dependencies

* Add yarn.bat to fix Windows build

* Fix pages failing to show when ANKI_API_PORT not defined

* Fix svelte-check and vitest on Windows

* Set node path in ./yarn

* Move svelte-kit output to ts/.svelte-kit

Sadly, I couldn't figure out a way to store it in out/ if out/ is
a symlink, as it breaks module resolution when SvelteKit is run.

* Allow HMR inside Anki

* Skip SvelteKit build when HMR is defined

* Fix some post-rebase issues

I should have done a normal merge instead.
2024-03-31 09:16:31 +01:00

196 lines
6.1 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",
*/
import type { GraphsResponse } from "@generated/anki/stats_pb";
import type { GraphsResponse_Hours_Hour } from "@generated/anki/stats_pb";
import * as tr from "@generated/ftl";
import { localizedNumber } from "@tslib/i18n";
import {
area,
axisBottom,
axisLeft,
axisRight,
curveBasis,
interpolateBlues,
pointer,
scaleBand,
scaleLinear,
scaleSequential,
select,
} from "d3";
import type { GraphBounds } from "./graph-helpers";
import { GraphRange, setDataAvailable } from "./graph-helpers";
import { oddTickClass } from "./graph-styles";
import { hideTooltip, showTooltip } from "./tooltip";
interface Hour {
hour: number;
totalCount: number;
correctCount: number;
}
function gatherData(data: GraphsResponse, range: GraphRange): Hour[] {
function convert(hours: GraphsResponse_Hours_Hour[]): Hour[] {
return hours.map((e, idx) => {
return { hour: idx, totalCount: e.total!, correctCount: e.correct! };
});
}
switch (range) {
case GraphRange.Month:
return convert(data.hours!.oneMonth);
case GraphRange.ThreeMonths:
return convert(data.hours!.threeMonths);
case GraphRange.Year:
return convert(data.hours!.oneYear);
case GraphRange.AllTime:
return convert(data.hours!.allTime);
}
}
export function renderHours(
svgElem: SVGElement,
bounds: GraphBounds,
origData: GraphsResponse,
range: GraphRange,
): void {
const data = gatherData(origData, range);
const yMax = Math.max(...data.map((d) => d.totalCount));
const svg = select(svgElem);
const trans = svg.transition().duration(600) as any;
if (!yMax) {
setDataAvailable(svg, false);
return;
} else {
setDataAvailable(svg, true);
}
const x = scaleBand()
.domain(data.map((d) => d.hour.toString()))
.range([bounds.marginLeft, bounds.width - bounds.marginRight])
.paddingInner(0.1);
svg.select<SVGGElement>(".x-ticks")
.call((selection) => selection.transition(trans).call(axisBottom(x).tickSizeOuter(0)))
.selectAll(".tick")
.selectAll("text")
.classed(oddTickClass, (d: any): boolean => d % 2 != 0)
.attr("direction", "ltr");
const cappedRange = scaleLinear().range([0.1, 0.8]);
const colour = scaleSequential((n) => interpolateBlues(cappedRange(n)!)).domain([
0,
yMax,
]);
// y scale
const yTickFormat = (n: number): string => localizedNumber(n);
const y = scaleLinear()
.range([bounds.height - bounds.marginBottom, bounds.marginTop])
.domain([0, yMax])
.nice();
svg.select<SVGGElement>(".y-ticks")
.call((selection) =>
selection.transition(trans).call(
axisLeft(y)
.ticks(bounds.height / 50)
.tickSizeOuter(0)
.tickFormat(yTickFormat as any),
)
)
.attr("direction", "ltr");
const yArea = y.copy().domain([0, 1]);
// x bars
const updateBar = (sel: any): any => {
return sel
.attr("width", x.bandwidth())
.transition(trans)
.attr("x", (d: Hour) => x(d.hour.toString())!)
.attr("y", (d: Hour) => y(d.totalCount)!)
.attr("height", (d: Hour) => y(0)! - y(d.totalCount)!)
.attr("fill", (d: Hour) => colour(d.totalCount!));
};
svg.select("g.bars")
.selectAll("rect")
.data(data)
.join(
(enter) =>
enter
.append("rect")
.attr("rx", 1)
.attr("x", (d: Hour) => x(d.hour.toString())!)
.attr("y", y(0)!)
.attr("height", 0)
// .attr("opacity", 0.7)
.call(updateBar),
(update) => update.call(updateBar),
(remove) => remove.call((remove) => remove.transition(trans).attr("height", 0).attr("y", y(0)!)),
);
svg.select<SVGGElement>(".y2-ticks")
.call((selection) =>
selection.transition(trans).call(
axisRight(yArea)
.ticks(bounds.height / 50)
.tickFormat((n: any) => `${Math.round(n * 100)}%`)
.tickSizeOuter(0),
)
)
.attr("direction", "ltr");
svg.select("path.cumulative-overlay")
.datum(data)
.attr(
"d",
area<Hour>()
.curve(curveBasis)
.defined((d) => d.totalCount > 0)
.x((d: Hour) => {
return x(d.hour.toString())! + x.bandwidth() / 2;
})
.y0(bounds.height - bounds.marginBottom)
.y1((d: Hour) => {
const correctRatio = d.correctCount! / d.totalCount!;
return yArea(isNaN(correctRatio) ? 0 : correctRatio)!;
}),
);
function tooltipText(d: Hour): string {
const hour = tr.statisticsHoursRange({
hourStart: d.hour,
hourEnd: d.hour + 1,
});
const reviews = tr.statisticsHoursReviews({ reviews: d.totalCount });
const correct = tr.statisticsHoursCorrectReviews({
percent: d.totalCount ? (d.correctCount / d.totalCount) * 100 : 0,
reviews: d.correctCount,
});
return `${hour}<br>${reviews}<br>${correct}`;
}
// hover/tooltip
svg.select("g.hover-columns")
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", (d: Hour) => x(d.hour.toString())!)
.attr("y", () => y(yMax)!)
.attr("width", x.bandwidth())
.attr("height", () => y(0)! - y(yMax!)!)
.on("mousemove", (event: MouseEvent, d: Hour) => {
const [x, y] = pointer(event, document.body);
showTooltip(tooltipText(d), x, y);
})
.on("mouseout", hideTooltip);
}