2020-06-27 04:35:13 +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",
|
|
|
|
*/
|
|
|
|
|
update to latest rules_nodejs & switch to ts_project
ts_library() is deprecated and will presumably be dropped from a
future rules_nodejs, and it wasn't working with the jest tests
after updating, so we switch over to ts_project().
There are some downsides:
- It's a bit slower, as the worker mode doesn't appear to function
at the moment.
- Getting it working with a mix of source files and generated files
was quite tricky, especially as things behave differently on Windows,
and differently when editing with VS Code. Solved with a small patch
to the rules, and a wrapper script that copies everything into the
bin folder first. To keep VS Code working correctly as well, the built
files are symlinked into the source folder.
- TS libraries are not implicitly linked to node_modules, so they
can't be imported with an absolute name like "lib/proto" - we need
to use relative paths like "../lib/proto" instead. Adjusting "paths"
in tsconfig.json makes it work for TS compilation, but then it fails
at the esbuild stage. We could resolve it by wrapping the TS
libraries in a subsequent js_library() call, but that has the downside
of losing the transient dependencies, meaning they need to be listed
again. Alternatively we might be able to solve it in the future by
adjusting esbuild, but for now the paths have been made relative to
keep things simple.
Upsides:
- Along with updates to the Svelte tooling, Svelte typing has improved.
All exports made in a Svelte file are now visible to other files that
import them, and we no longer rebuild the Svelte files when TS files
are updated, as the Svelte files do no type checking themselves, and
are just a simple transpilation. Svelte-check now works on Windows again,
and there should be no errors when editing in VS Code after you've
built the project. The only downside seems to be that cmd+clicking
on a Svelte imports jumps to the .d.ts file instead of the original now;
presumably they'll fix that in a future plugin update.
- Each subfolder now has its own tsconfig.json, and tsc can be called
directly for testing purposes (but beware it will place build products
in the source tree): ts/node_modules/.bin/tsc -b ts
- We can drop the custom esbuild_toolchain, as it's included in the
latest rules_nodejs.
Other changes:
- "image_module_support" is moved into lib/, and imported with
<reference types=...>
- Images are now imported directly from their npm package; the
extra copy step has been removed.
Windows users may need to use "bazel clean" before building this,
due to old files lying around in the build folder.
2021-09-30 14:16:29 +02:00
|
|
|
import { Stats } from "../lib/proto";
|
2021-03-26 11:23:43 +01:00
|
|
|
|
update to latest rules_nodejs & switch to ts_project
ts_library() is deprecated and will presumably be dropped from a
future rules_nodejs, and it wasn't working with the jest tests
after updating, so we switch over to ts_project().
There are some downsides:
- It's a bit slower, as the worker mode doesn't appear to function
at the moment.
- Getting it working with a mix of source files and generated files
was quite tricky, especially as things behave differently on Windows,
and differently when editing with VS Code. Solved with a small patch
to the rules, and a wrapper script that copies everything into the
bin folder first. To keep VS Code working correctly as well, the built
files are symlinked into the source folder.
- TS libraries are not implicitly linked to node_modules, so they
can't be imported with an absolute name like "lib/proto" - we need
to use relative paths like "../lib/proto" instead. Adjusting "paths"
in tsconfig.json makes it work for TS compilation, but then it fails
at the esbuild stage. We could resolve it by wrapping the TS
libraries in a subsequent js_library() call, but that has the downside
of losing the transient dependencies, meaning they need to be listed
again. Alternatively we might be able to solve it in the future by
adjusting esbuild, but for now the paths have been made relative to
keep things simple.
Upsides:
- Along with updates to the Svelte tooling, Svelte typing has improved.
All exports made in a Svelte file are now visible to other files that
import them, and we no longer rebuild the Svelte files when TS files
are updated, as the Svelte files do no type checking themselves, and
are just a simple transpilation. Svelte-check now works on Windows again,
and there should be no errors when editing in VS Code after you've
built the project. The only downside seems to be that cmd+clicking
on a Svelte imports jumps to the .d.ts file instead of the original now;
presumably they'll fix that in a future plugin update.
- Each subfolder now has its own tsconfig.json, and tsc can be called
directly for testing purposes (but beware it will place build products
in the source tree): ts/node_modules/.bin/tsc -b ts
- We can drop the custom esbuild_toolchain, as it's included in the
latest rules_nodejs.
Other changes:
- "image_module_support" is moved into lib/, and imported with
<reference types=...>
- Images are now imported directly from their npm package; the
extra copy step has been removed.
Windows users may need to use "bazel clean" before building this,
due to old files lying around in the build folder.
2021-09-30 14:16:29 +02:00
|
|
|
import { timeSpan, dayLabel } from "../lib/time";
|
2020-06-27 04:35:13 +02:00
|
|
|
import {
|
|
|
|
interpolateGreens,
|
|
|
|
interpolateReds,
|
|
|
|
interpolateOranges,
|
2021-01-19 12:22:13 +01:00
|
|
|
interpolatePurples,
|
2021-01-30 01:13:47 +01:00
|
|
|
select,
|
|
|
|
pointer,
|
|
|
|
scaleLinear,
|
|
|
|
scaleSequential,
|
|
|
|
axisBottom,
|
|
|
|
axisLeft,
|
|
|
|
axisRight,
|
|
|
|
area,
|
|
|
|
curveBasis,
|
|
|
|
min,
|
|
|
|
histogram,
|
|
|
|
sum,
|
|
|
|
max,
|
|
|
|
cumsum,
|
2021-08-19 13:43:00 +02:00
|
|
|
ScaleSequential,
|
2021-01-30 01:13:47 +01:00
|
|
|
} from "d3";
|
|
|
|
import type { Bin } from "d3";
|
|
|
|
|
2020-11-01 05:26:58 +01:00
|
|
|
import type { TableDatum } from "./graph-helpers";
|
2021-03-21 13:47:52 +01:00
|
|
|
import { GraphBounds, setDataAvailable, GraphRange } from "./graph-helpers";
|
|
|
|
import { showTooltip, hideTooltip } from "./tooltip";
|
update to latest rules_nodejs & switch to ts_project
ts_library() is deprecated and will presumably be dropped from a
future rules_nodejs, and it wasn't working with the jest tests
after updating, so we switch over to ts_project().
There are some downsides:
- It's a bit slower, as the worker mode doesn't appear to function
at the moment.
- Getting it working with a mix of source files and generated files
was quite tricky, especially as things behave differently on Windows,
and differently when editing with VS Code. Solved with a small patch
to the rules, and a wrapper script that copies everything into the
bin folder first. To keep VS Code working correctly as well, the built
files are symlinked into the source folder.
- TS libraries are not implicitly linked to node_modules, so they
can't be imported with an absolute name like "lib/proto" - we need
to use relative paths like "../lib/proto" instead. Adjusting "paths"
in tsconfig.json makes it work for TS compilation, but then it fails
at the esbuild stage. We could resolve it by wrapping the TS
libraries in a subsequent js_library() call, but that has the downside
of losing the transient dependencies, meaning they need to be listed
again. Alternatively we might be able to solve it in the future by
adjusting esbuild, but for now the paths have been made relative to
keep things simple.
Upsides:
- Along with updates to the Svelte tooling, Svelte typing has improved.
All exports made in a Svelte file are now visible to other files that
import them, and we no longer rebuild the Svelte files when TS files
are updated, as the Svelte files do no type checking themselves, and
are just a simple transpilation. Svelte-check now works on Windows again,
and there should be no errors when editing in VS Code after you've
built the project. The only downside seems to be that cmd+clicking
on a Svelte imports jumps to the .d.ts file instead of the original now;
presumably they'll fix that in a future plugin update.
- Each subfolder now has its own tsconfig.json, and tsc can be called
directly for testing purposes (but beware it will place build products
in the source tree): ts/node_modules/.bin/tsc -b ts
- We can drop the custom esbuild_toolchain, as it's included in the
latest rules_nodejs.
Other changes:
- "image_module_support" is moved into lib/, and imported with
<reference types=...>
- Images are now imported directly from their npm package; the
extra copy step has been removed.
Windows users may need to use "bazel clean" before building this,
due to old files lying around in the build folder.
2021-09-30 14:16:29 +02:00
|
|
|
import * as tr from "../lib/i18n";
|
2020-06-27 04:35:13 +02:00
|
|
|
|
|
|
|
interface Reviews {
|
|
|
|
learn: number;
|
|
|
|
relearn: number;
|
2021-01-19 12:22:13 +01:00
|
|
|
young: number;
|
|
|
|
mature: number;
|
2021-08-19 12:13:22 +02:00
|
|
|
filtered: number;
|
2020-06-27 04:35:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface GraphData {
|
|
|
|
// indexed by day, where day is relative to today
|
|
|
|
reviewCount: Map<number, Reviews>;
|
|
|
|
reviewTime: Map<number, Reviews>;
|
|
|
|
}
|
|
|
|
|
2021-07-10 13:58:34 +02:00
|
|
|
const ReviewKind = Stats.RevlogEntry.ReviewKind;
|
2020-06-27 04:35:13 +02:00
|
|
|
type BinType = Bin<Map<number, Reviews[]>, number>;
|
|
|
|
|
2021-07-10 13:58:34 +02:00
|
|
|
export function gatherData(data: Stats.GraphsResponse): GraphData {
|
2020-06-27 04:35:13 +02:00
|
|
|
const reviewCount = new Map<number, Reviews>();
|
|
|
|
const reviewTime = new Map<number, Reviews>();
|
2021-08-19 12:13:22 +02:00
|
|
|
const empty = { mature: 0, young: 0, learn: 0, relearn: 0, filtered: 0 };
|
2020-06-27 04:35:13 +02:00
|
|
|
|
2021-07-10 13:58:34 +02:00
|
|
|
for (const review of data.revlog as Stats.RevlogEntry[]) {
|
2020-09-02 09:18:29 +02:00
|
|
|
if (review.reviewKind == ReviewKind.MANUAL) {
|
|
|
|
// don't count days with only manual scheduling
|
|
|
|
continue;
|
|
|
|
}
|
2020-06-27 04:35:13 +02:00
|
|
|
const day = Math.ceil(
|
|
|
|
((review.id as number) / 1000 - data.nextDayAtSecs) / 86400
|
|
|
|
);
|
|
|
|
const countEntry =
|
|
|
|
reviewCount.get(day) ?? reviewCount.set(day, { ...empty }).get(day)!;
|
|
|
|
const timeEntry =
|
|
|
|
reviewTime.get(day) ?? reviewTime.set(day, { ...empty }).get(day)!;
|
|
|
|
|
|
|
|
switch (review.reviewKind) {
|
2021-01-19 12:22:13 +01:00
|
|
|
case ReviewKind.LEARNING:
|
|
|
|
countEntry.learn += 1;
|
|
|
|
timeEntry.learn += review.takenMillis;
|
|
|
|
break;
|
|
|
|
case ReviewKind.RELEARNING:
|
|
|
|
countEntry.relearn += 1;
|
|
|
|
timeEntry.relearn += review.takenMillis;
|
|
|
|
break;
|
2020-06-27 04:35:13 +02:00
|
|
|
case ReviewKind.REVIEW:
|
2020-08-01 02:04:05 +02:00
|
|
|
if (review.lastInterval < 21) {
|
2020-06-27 04:35:13 +02:00
|
|
|
countEntry.young += 1;
|
|
|
|
timeEntry.young += review.takenMillis;
|
|
|
|
} else {
|
|
|
|
countEntry.mature += 1;
|
|
|
|
timeEntry.mature += review.takenMillis;
|
|
|
|
}
|
|
|
|
break;
|
2021-08-19 12:13:22 +02:00
|
|
|
case ReviewKind.FILTERED:
|
|
|
|
countEntry.filtered += 1;
|
|
|
|
timeEntry.filtered += review.takenMillis;
|
2020-06-27 04:35:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { reviewCount, reviewTime };
|
|
|
|
}
|
|
|
|
|
2021-08-19 13:43:00 +02:00
|
|
|
enum BinIndex {
|
|
|
|
Mature = 0,
|
|
|
|
Young = 1,
|
|
|
|
Relearn = 2,
|
|
|
|
Learn = 3,
|
|
|
|
Filtered = 4,
|
|
|
|
}
|
|
|
|
|
2020-06-27 04:35:13 +02:00
|
|
|
function totalsForBin(bin: BinType): number[] {
|
|
|
|
const total = [0, 0, 0, 0, 0];
|
|
|
|
for (const entry of bin) {
|
2021-08-19 13:43:00 +02:00
|
|
|
total[BinIndex.Mature] += entry[1].mature;
|
|
|
|
total[BinIndex.Young] += entry[1].young;
|
|
|
|
total[BinIndex.Relearn] += entry[1].relearn;
|
|
|
|
total[BinIndex.Learn] += entry[1].learn;
|
|
|
|
total[BinIndex.Filtered] += entry[1].filtered;
|
2020-06-27 04:35:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// eg idx=0 is mature count, idx=1 is mature+young count, etc
|
|
|
|
function cumulativeBinValue(bin: BinType, idx: number): number {
|
|
|
|
return sum(totalsForBin(bin).slice(0, idx + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function renderReviews(
|
|
|
|
svgElem: SVGElement,
|
|
|
|
bounds: GraphBounds,
|
|
|
|
sourceData: GraphData,
|
2020-07-17 05:46:06 +02:00
|
|
|
range: GraphRange,
|
2021-03-26 11:23:43 +01:00
|
|
|
showTime: boolean
|
2020-08-04 06:37:21 +02:00
|
|
|
): TableDatum[] {
|
2020-07-06 06:01:49 +02:00
|
|
|
const svg = select(svgElem);
|
|
|
|
const trans = svg.transition().duration(600) as any;
|
|
|
|
|
2020-06-28 11:34:19 +02:00
|
|
|
const xMax = 1;
|
2020-06-27 04:35:13 +02:00
|
|
|
let xMin = 0;
|
|
|
|
// cap max to selected range
|
|
|
|
switch (range) {
|
2020-07-17 05:46:06 +02:00
|
|
|
case GraphRange.Month:
|
2020-08-10 05:56:16 +02:00
|
|
|
xMin = -30;
|
2020-06-27 04:35:13 +02:00
|
|
|
break;
|
2020-07-17 05:46:06 +02:00
|
|
|
case GraphRange.ThreeMonths:
|
2020-08-10 05:56:16 +02:00
|
|
|
xMin = -89;
|
2020-06-27 04:35:13 +02:00
|
|
|
break;
|
2020-07-17 05:46:06 +02:00
|
|
|
case GraphRange.Year:
|
2020-08-10 05:56:16 +02:00
|
|
|
xMin = -364;
|
2020-06-27 04:35:13 +02:00
|
|
|
break;
|
2020-07-17 05:46:06 +02:00
|
|
|
case GraphRange.AllTime:
|
2020-06-27 04:35:13 +02:00
|
|
|
xMin = min(sourceData.reviewCount.keys())!;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const desiredBars = Math.min(70, Math.abs(xMin!));
|
|
|
|
|
2020-07-06 11:00:14 +02:00
|
|
|
const x = scaleLinear().domain([xMin!, xMax]).nice(desiredBars);
|
2020-07-08 05:31:09 +02:00
|
|
|
x.domain([x.domain()[0], xMax]);
|
|
|
|
|
2020-06-27 04:35:13 +02:00
|
|
|
const sourceMap = showTime ? sourceData.reviewTime : sourceData.reviewCount;
|
|
|
|
const bins = histogram()
|
|
|
|
.value((m) => {
|
|
|
|
return m[0];
|
|
|
|
})
|
|
|
|
.domain(x.domain() as any)
|
|
|
|
.thresholds(x.ticks(desiredBars))(sourceMap.entries() as any);
|
|
|
|
|
2020-07-06 06:01:49 +02:00
|
|
|
// empty graph?
|
2020-08-04 06:28:46 +02:00
|
|
|
const totalDays = sum(bins, (bin) => bin.length);
|
|
|
|
if (!totalDays) {
|
2020-07-06 06:01:49 +02:00
|
|
|
setDataAvailable(svg, false);
|
2020-08-04 06:28:46 +02:00
|
|
|
return [];
|
2020-07-06 06:01:49 +02:00
|
|
|
} else {
|
|
|
|
setDataAvailable(svg, true);
|
|
|
|
}
|
2020-06-27 04:35:13 +02:00
|
|
|
|
|
|
|
x.range([bounds.marginLeft, bounds.width - bounds.marginRight]);
|
2021-04-02 05:25:38 +02:00
|
|
|
svg.select<SVGGElement>(".x-ticks")
|
|
|
|
.call((selection) =>
|
|
|
|
selection.transition(trans).call(axisBottom(x).ticks(7).tickSizeOuter(0))
|
|
|
|
)
|
|
|
|
.attr("direction", "ltr");
|
2020-06-27 04:35:13 +02:00
|
|
|
|
|
|
|
// y scale
|
|
|
|
|
2020-08-05 07:01:51 +02:00
|
|
|
const yTickFormat = (n: number): string => {
|
|
|
|
if (showTime) {
|
2021-03-26 11:23:43 +01:00
|
|
|
return timeSpan(n / 1000, true);
|
2020-08-05 07:01:51 +02:00
|
|
|
} else {
|
|
|
|
if (Math.round(n) != n) {
|
|
|
|
return "";
|
|
|
|
} else {
|
2020-08-05 11:05:24 +02:00
|
|
|
return n.toLocaleString();
|
2020-08-05 07:01:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-27 04:35:13 +02:00
|
|
|
const yMax = max(bins, (b: Bin<any, any>) => cumulativeBinValue(b, 4))!;
|
|
|
|
const y = scaleLinear()
|
|
|
|
.range([bounds.height - bounds.marginBottom, bounds.marginTop])
|
2020-08-05 07:01:51 +02:00
|
|
|
.domain([0, yMax])
|
|
|
|
.nice();
|
2021-04-02 05:25:38 +02:00
|
|
|
svg.select<SVGGElement>(".y-ticks")
|
|
|
|
.call((selection) =>
|
|
|
|
selection.transition(trans).call(
|
|
|
|
axisLeft(y)
|
|
|
|
.ticks(bounds.height / 50)
|
|
|
|
.tickSizeOuter(0)
|
|
|
|
.tickFormat(yTickFormat as any)
|
|
|
|
)
|
2021-03-21 13:47:52 +01:00
|
|
|
)
|
2021-04-02 05:25:38 +02:00
|
|
|
.attr("direction", "ltr");
|
2020-06-27 04:35:13 +02:00
|
|
|
|
|
|
|
// x bars
|
|
|
|
|
2021-01-30 02:08:01 +01:00
|
|
|
function barWidth(d: Bin<number, number>): number {
|
|
|
|
const width = Math.max(0, x(d.x1!) - x(d.x0!) - 1);
|
|
|
|
return width ?? 0;
|
2020-06-27 04:35:13 +02:00
|
|
|
}
|
|
|
|
|
2020-08-05 08:06:34 +02:00
|
|
|
const cappedRange = scaleLinear().range([0.3, 0.5]);
|
2020-06-27 04:35:13 +02:00
|
|
|
const shiftedRange = scaleLinear().range([0.4, 0.7]);
|
|
|
|
const darkerGreens = scaleSequential((n) =>
|
2020-09-29 14:13:25 +02:00
|
|
|
interpolateGreens(shiftedRange(n)!)
|
2020-06-27 04:35:13 +02:00
|
|
|
).domain(x.domain() as any);
|
|
|
|
const lighterGreens = scaleSequential((n) =>
|
2020-09-29 14:13:25 +02:00
|
|
|
interpolateGreens(cappedRange(n)!)
|
2020-06-27 04:35:13 +02:00
|
|
|
).domain(x.domain() as any);
|
2020-09-29 14:13:25 +02:00
|
|
|
const reds = scaleSequential((n) => interpolateReds(cappedRange(n)!)).domain(
|
2020-06-27 04:35:13 +02:00
|
|
|
x.domain() as any
|
|
|
|
);
|
2020-09-29 14:13:25 +02:00
|
|
|
const oranges = scaleSequential((n) => interpolateOranges(cappedRange(n)!)).domain(
|
2020-06-27 04:35:13 +02:00
|
|
|
x.domain() as any
|
|
|
|
);
|
2021-01-19 12:22:13 +01:00
|
|
|
const purples = scaleSequential((n) => interpolatePurples(cappedRange(n)!)).domain(
|
|
|
|
x.domain() as any
|
|
|
|
);
|
2020-06-27 04:35:13 +02:00
|
|
|
|
2021-08-19 13:43:00 +02:00
|
|
|
function binColor(idx: BinIndex): ScaleSequential<string> {
|
|
|
|
switch (idx) {
|
|
|
|
case BinIndex.Mature:
|
|
|
|
return darkerGreens;
|
|
|
|
case BinIndex.Young:
|
|
|
|
return lighterGreens;
|
|
|
|
case BinIndex.Learn:
|
|
|
|
return oranges;
|
|
|
|
case BinIndex.Relearn:
|
|
|
|
return reds;
|
|
|
|
case BinIndex.Filtered:
|
|
|
|
return purples;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 11:34:19 +02:00
|
|
|
function valueLabel(n: number): string {
|
|
|
|
if (showTime) {
|
2021-03-26 11:23:43 +01:00
|
|
|
return timeSpan(n / 1000);
|
2020-06-28 11:34:19 +02:00
|
|
|
} else {
|
2021-03-26 11:23:43 +01:00
|
|
|
return tr.statisticsReviews({ reviews: n });
|
2020-06-28 11:34:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-27 07:35:34 +02:00
|
|
|
function tooltipText(d: BinType, cumulative: number): string {
|
2021-03-26 11:23:43 +01:00
|
|
|
const day = dayLabel(d.x0!, d.x1!);
|
2020-06-27 07:35:34 +02:00
|
|
|
const totals = totalsForBin(d);
|
2020-07-08 06:07:10 +02:00
|
|
|
const dayTotal = valueLabel(sum(totals));
|
2020-07-16 05:25:13 +02:00
|
|
|
let buf = `<table><tr><td>${day}</td><td align=right>${dayTotal}</td></tr>`;
|
2021-08-19 13:43:00 +02:00
|
|
|
const lines: [BinIndex | null, string][] = [
|
|
|
|
[BinIndex.Filtered, tr.statisticsCountsFilteredCards()],
|
|
|
|
[BinIndex.Learn, tr.statisticsCountsLearningCards()],
|
|
|
|
[BinIndex.Relearn, tr.statisticsCountsRelearningCards()],
|
|
|
|
[BinIndex.Young, tr.statisticsCountsYoungCards()],
|
|
|
|
[BinIndex.Mature, tr.statisticsCountsMatureCards()],
|
|
|
|
[null, tr.statisticsRunningTotal()],
|
2020-06-27 07:35:34 +02:00
|
|
|
];
|
2021-08-19 13:43:00 +02:00
|
|
|
for (const [idx, label] of lines) {
|
|
|
|
let color;
|
|
|
|
let detail;
|
|
|
|
if (idx == null) {
|
|
|
|
color = "transparent";
|
|
|
|
detail = valueLabel(cumulative);
|
|
|
|
} else {
|
|
|
|
color = binColor(idx)(1);
|
|
|
|
detail = valueLabel(totals[idx]);
|
|
|
|
}
|
2020-07-16 05:25:13 +02:00
|
|
|
buf += `<tr>
|
2021-08-19 13:43:00 +02:00
|
|
|
<td><span style="color: ${color};">■</span> ${label}</td>
|
2020-07-16 05:25:13 +02:00
|
|
|
<td align=right>${detail}</td>
|
|
|
|
</tr>`;
|
2020-06-27 07:35:34 +02:00
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2020-06-27 04:35:13 +02:00
|
|
|
const updateBar = (sel: any, idx: number): any => {
|
|
|
|
return sel
|
|
|
|
.attr("width", barWidth)
|
|
|
|
.transition(trans)
|
|
|
|
.attr("x", (d: any) => x(d.x0))
|
|
|
|
.attr("y", (d: any) => y(cumulativeBinValue(d, idx))!)
|
2020-09-29 14:13:25 +02:00
|
|
|
.attr("height", (d: any) => y(0)! - y(cumulativeBinValue(d, idx))!)
|
2021-08-19 13:43:00 +02:00
|
|
|
.attr("fill", (d: any) => binColor(idx)(d.x0));
|
2020-06-27 04:35:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
for (const barNum of [0, 1, 2, 3, 4]) {
|
|
|
|
svg.select(`g.bars${barNum}`)
|
|
|
|
.selectAll("rect")
|
|
|
|
.data(bins)
|
|
|
|
.join(
|
|
|
|
(enter) =>
|
|
|
|
enter
|
|
|
|
.append("rect")
|
|
|
|
.attr("rx", 1)
|
2020-09-29 14:13:25 +02:00
|
|
|
.attr("x", (d: any) => x(d.x0)!)
|
|
|
|
.attr("y", y(0)!)
|
2020-06-27 04:35:13 +02:00
|
|
|
.attr("height", 0)
|
|
|
|
.call((d) => updateBar(d, barNum)),
|
|
|
|
(update) => update.call((d) => updateBar(d, barNum)),
|
|
|
|
(remove) =>
|
|
|
|
remove.call((remove) =>
|
2020-09-29 14:13:25 +02:00
|
|
|
remove.transition(trans).attr("height", 0).attr("y", y(0)!)
|
2020-06-27 04:35:13 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// cumulative area
|
|
|
|
|
|
|
|
const areaCounts = bins.map((d: any) => cumulativeBinValue(d, 4));
|
|
|
|
areaCounts.unshift(0);
|
|
|
|
const areaData = cumsum(areaCounts);
|
2020-08-04 06:28:46 +02:00
|
|
|
const yCumMax = areaData.slice(-1)[0];
|
2020-08-05 07:01:51 +02:00
|
|
|
const yAreaScale = y.copy().domain([0, yCumMax]).nice();
|
2020-06-27 04:35:13 +02:00
|
|
|
|
2020-08-04 06:28:46 +02:00
|
|
|
if (yCumMax) {
|
2021-04-02 05:25:38 +02:00
|
|
|
svg.select<SVGGElement>(".y2-ticks")
|
|
|
|
.call((selection) =>
|
|
|
|
selection.transition(trans).call(
|
|
|
|
axisRight(yAreaScale)
|
|
|
|
.ticks(bounds.height / 50)
|
|
|
|
.tickFormat(yTickFormat as any)
|
|
|
|
.tickSizeOuter(0)
|
|
|
|
)
|
2021-03-21 13:47:52 +01:00
|
|
|
)
|
2021-04-02 05:25:38 +02:00
|
|
|
.attr("direction", "ltr");
|
2020-08-05 07:01:51 +02:00
|
|
|
|
2021-03-21 10:50:35 +01:00
|
|
|
svg.select("path.cumulative-overlay")
|
2020-06-27 04:35:13 +02:00
|
|
|
.datum(areaData as any)
|
|
|
|
.attr(
|
|
|
|
"d",
|
|
|
|
area()
|
|
|
|
.curve(curveBasis)
|
2021-01-30 02:08:01 +01:00
|
|
|
.x((_d: [number, number], idx: number) => {
|
2020-06-27 04:35:13 +02:00
|
|
|
if (idx === 0) {
|
2020-09-29 14:13:25 +02:00
|
|
|
return x(bins[0].x0!)!;
|
2020-06-27 04:35:13 +02:00
|
|
|
} else {
|
2020-09-29 14:13:25 +02:00
|
|
|
return x(bins[idx - 1].x1!)!;
|
2020-06-27 04:35:13 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.y0(bounds.height - bounds.marginBottom)
|
2020-09-29 14:13:25 +02:00
|
|
|
.y1((d: any) => yAreaScale(d)!) as any
|
2020-06-27 04:35:13 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:21:33 +02:00
|
|
|
const hoverData: [Bin<number, number>, number][] = bins.map(
|
|
|
|
(bin: Bin<number, number>, index: number) => [bin, areaData[index + 1]]
|
|
|
|
);
|
2021-01-30 02:08:01 +01:00
|
|
|
|
|
|
|
// hover/tooltip
|
2021-03-21 10:58:39 +01:00
|
|
|
svg.select("g.hover-columns")
|
2020-06-27 04:35:13 +02:00
|
|
|
.selectAll("rect")
|
2021-01-30 02:08:01 +01:00
|
|
|
.data(hoverData)
|
2020-06-27 04:35:13 +02:00
|
|
|
.join("rect")
|
2021-01-30 02:08:01 +01:00
|
|
|
.attr("x", ([bin]) => x(bin.x0!))
|
|
|
|
.attr("y", () => y(yMax))
|
|
|
|
.attr("width", ([bin]) => barWidth(bin))
|
|
|
|
.attr("height", () => y(0) - y(yMax))
|
|
|
|
.on("mousemove", (event: MouseEvent, [bin, area]): void => {
|
2021-01-30 02:35:33 +01:00
|
|
|
const [x, y] = pointer(event, document.body);
|
2021-01-30 02:08:01 +01:00
|
|
|
showTooltip(tooltipText(bin as any, area), x, y);
|
2020-06-27 04:35:13 +02:00
|
|
|
})
|
|
|
|
.on("mouseout", hideTooltip);
|
2020-08-04 06:28:46 +02:00
|
|
|
|
2020-08-10 05:56:16 +02:00
|
|
|
const periodDays = -xMin + 1;
|
2020-08-04 06:28:46 +02:00
|
|
|
const studiedDays = sum(bins, (bin) => bin.length);
|
|
|
|
const total = yCumMax;
|
|
|
|
const periodAvg = total / periodDays;
|
|
|
|
const studiedAvg = total / studiedDays;
|
|
|
|
|
|
|
|
let totalString: string,
|
|
|
|
averageForDaysStudied: string,
|
|
|
|
averageForPeriod: string,
|
|
|
|
averageAnswerTime: string,
|
|
|
|
averageAnswerTimeLabel: string;
|
|
|
|
if (showTime) {
|
2021-03-26 11:23:43 +01:00
|
|
|
totalString = timeSpan(total / 1000, false);
|
|
|
|
averageForDaysStudied = tr.statisticsMinutesPerDay({
|
2020-08-04 06:28:46 +02:00
|
|
|
count: Math.round(studiedAvg / 1000 / 60),
|
|
|
|
});
|
2021-03-26 11:23:43 +01:00
|
|
|
averageForPeriod = tr.statisticsMinutesPerDay({
|
2020-08-04 06:28:46 +02:00
|
|
|
count: Math.round(periodAvg / 1000 / 60),
|
|
|
|
});
|
2021-03-26 11:23:43 +01:00
|
|
|
averageAnswerTimeLabel = tr.statisticsAverageAnswerTimeLabel();
|
2020-08-04 06:28:46 +02:00
|
|
|
|
|
|
|
// need to get total review count to calculate average time
|
|
|
|
const countBins = histogram()
|
|
|
|
.value((m) => {
|
|
|
|
return m[0];
|
|
|
|
})
|
|
|
|
.domain(x.domain() as any)(sourceData.reviewCount.entries() as any);
|
|
|
|
const totalReviews = sum(countBins, (bin) => cumulativeBinValue(bin as any, 4));
|
|
|
|
const totalSecs = total / 1000;
|
|
|
|
const avgSecs = totalSecs / totalReviews;
|
|
|
|
const cardsPerMin = (totalReviews * 60) / totalSecs;
|
2021-03-26 11:23:43 +01:00
|
|
|
averageAnswerTime = tr.statisticsAverageAnswerTime({
|
2021-03-26 10:09:50 +01:00
|
|
|
averageSeconds: avgSecs,
|
|
|
|
cardsPerMinute: cardsPerMin,
|
2020-08-04 06:28:46 +02:00
|
|
|
});
|
|
|
|
} else {
|
2021-03-26 11:23:43 +01:00
|
|
|
totalString = tr.statisticsReviews({ reviews: total });
|
|
|
|
averageForDaysStudied = tr.statisticsReviewsPerDay({
|
2020-08-04 06:28:46 +02:00
|
|
|
count: Math.round(studiedAvg),
|
|
|
|
});
|
2021-03-26 11:23:43 +01:00
|
|
|
averageForPeriod = tr.statisticsReviewsPerDay({
|
2020-08-04 06:28:46 +02:00
|
|
|
count: Math.round(periodAvg),
|
|
|
|
});
|
|
|
|
averageAnswerTime = averageAnswerTimeLabel = "";
|
|
|
|
}
|
|
|
|
|
2020-08-04 06:37:21 +02:00
|
|
|
const tableData: TableDatum[] = [
|
|
|
|
{
|
2021-03-26 11:23:43 +01:00
|
|
|
label: tr.statisticsDaysStudied(),
|
|
|
|
value: tr.statisticsAmountOfTotalWithPercentage({
|
2020-08-04 06:28:46 +02:00
|
|
|
amount: studiedDays,
|
|
|
|
total: periodDays,
|
|
|
|
percent: Math.round((studiedDays / periodDays) * 100),
|
|
|
|
}),
|
2020-08-04 06:37:21 +02:00
|
|
|
},
|
2020-08-04 06:28:46 +02:00
|
|
|
|
2021-03-26 11:23:43 +01:00
|
|
|
{ label: tr.statisticsTotal(), value: totalString },
|
2020-08-04 06:28:46 +02:00
|
|
|
|
2020-08-04 06:37:21 +02:00
|
|
|
{
|
2021-03-26 11:23:43 +01:00
|
|
|
label: tr.statisticsAverageForDaysStudied(),
|
2020-08-04 06:37:21 +02:00
|
|
|
value: averageForDaysStudied,
|
|
|
|
},
|
2020-08-04 06:28:46 +02:00
|
|
|
|
2020-08-04 06:37:21 +02:00
|
|
|
{
|
2021-03-26 11:23:43 +01:00
|
|
|
label: tr.statisticsAverageOverPeriod(),
|
2020-08-04 06:37:21 +02:00
|
|
|
value: averageForPeriod,
|
|
|
|
},
|
2020-08-04 06:28:46 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
if (averageAnswerTime) {
|
2020-08-04 06:37:21 +02:00
|
|
|
tableData.push({ label: averageAnswerTimeLabel, value: averageAnswerTime });
|
2020-08-04 06:28:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return tableData;
|
2020-06-27 04:35:13 +02:00
|
|
|
}
|