anki/ts/graphs/CalendarGraph.svelte

94 lines
2.7 KiB
Svelte
Raw Normal View History

2020-06-30 07:09:20 +02:00
<script lang="typescript">
import type pb from "anki/backend_proto";
import type { I18n } from "anki/i18n";
import { createEventDispatcher } from "svelte";
import Graph from "./Graph.svelte";
import InputBox from "./InputBox.svelte";
2020-07-06 06:01:49 +02:00
import NoDataOverlay from "./NoDataOverlay.svelte";
2020-06-30 07:09:20 +02:00
import AxisTicks from "./AxisTicks.svelte";
import { defaultGraphBounds, RevlogRange } from "./graph-helpers";
import type { SearchEventMap } from "./graph-helpers";
import { gatherData, renderCalendar } from "./calendar";
import type { PreferenceStore } from "./preferences";
import type { GraphData } from "./calendar";
2020-06-30 07:09:20 +02:00
export let sourceData: pb.BackendProto.GraphsOut | null = null;
export let preferences: PreferenceStore | null = null;
2020-07-06 10:29:35 +02:00
export let revlogRange: RevlogRange;
2020-06-30 07:09:20 +02:00
export let i18n: I18n;
2020-06-30 08:23:46 +02:00
export let nightMode: boolean;
2020-06-30 07:09:20 +02:00
let { calendarFirstDayOfWeek } = preferences;
const dispatch = createEventDispatcher<SearchEventMap>();
2020-06-30 07:09:20 +02:00
let graphData: GraphData | null = null;
let bounds = defaultGraphBounds();
bounds.height = 120;
bounds.marginLeft = 20;
bounds.marginRight = 20;
let svg = null as HTMLElement | SVGElement | null;
let maxYear = new Date().getFullYear();
2020-07-22 06:11:22 +02:00
let minYear = 0;
2020-06-30 07:09:20 +02:00
let targetYear = maxYear;
$: if (sourceData) {
graphData = gatherData(sourceData, $calendarFirstDayOfWeek);
2020-06-30 07:09:20 +02:00
}
$: {
if (revlogRange < RevlogRange.Year) {
minYear = maxYear;
} else if ((revlogRange as RevlogRange) === RevlogRange.Year) {
minYear = maxYear - 1;
} else {
minYear = 2000;
}
if (targetYear < minYear) {
targetYear = minYear;
}
}
$: if (graphData) {
2020-06-30 08:23:46 +02:00
renderCalendar(
svg as SVGElement,
bounds,
graphData,
dispatch,
2020-06-30 08:23:46 +02:00
targetYear,
i18n,
nightMode,
revlogRange,
calendarFirstDayOfWeek.set
);
2020-06-30 07:09:20 +02:00
}
const title = i18n.statisticsCalendarTitle();
2020-06-30 07:09:20 +02:00
</script>
<Graph {title}>
<InputBox>
2020-06-30 07:09:20 +02:00
<span>
<button on:click={() => targetYear--} disabled={minYear >= targetYear}>
</button>
</span>
<span>{targetYear}</span>
<span>
<button on:click={() => targetYear++} disabled={targetYear >= maxYear}>
</button>
</span>
</InputBox>
2020-06-30 07:09:20 +02:00
<svg bind:this={svg} viewBox={`0 0 ${bounds.width} ${bounds.height}`}>
2021-01-20 21:17:36 +01:00
<g class="weekdays" />
2020-06-30 07:09:20 +02:00
<g class="days" />
<AxisTicks {bounds} />
2020-07-06 06:01:49 +02:00
<NoDataOverlay {bounds} {i18n} />
2020-06-30 07:09:20 +02:00
</svg>
</Graph>