Merge pull request #933 from hgiesel/calendardays
Add labels to calendar days
This commit is contained in:
commit
e2af5fb718
@ -74,6 +74,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<svg bind:this={svg} viewBox={`0 0 ${bounds.width} ${bounds.height}`}>
|
<svg bind:this={svg} viewBox={`0 0 ${bounds.width} ${bounds.height}`}>
|
||||||
|
<g class="weekdays" />
|
||||||
<g class="days" />
|
<g class="days" />
|
||||||
<AxisTicks {bounds} />
|
<AxisTicks {bounds} />
|
||||||
<NoDataOverlay {bounds} {i18n} />
|
<NoDataOverlay {bounds} {i18n} />
|
||||||
|
@ -28,6 +28,7 @@ export interface GraphData {
|
|||||||
// indexed by day, where day is relative to today
|
// indexed by day, where day is relative to today
|
||||||
reviewCount: Map<number, number>;
|
reviewCount: Map<number, number>;
|
||||||
timeFunction: CountableTimeInterval;
|
timeFunction: CountableTimeInterval;
|
||||||
|
weekdayLabels: number[];
|
||||||
}
|
}
|
||||||
|
|
||||||
interface DayDatum {
|
interface DayDatum {
|
||||||
@ -63,7 +64,12 @@ export function gatherData(data: pb.BackendProto.GraphsOut): GraphData {
|
|||||||
? timeSaturday
|
? timeSaturday
|
||||||
: timeSunday;
|
: timeSunday;
|
||||||
|
|
||||||
return { reviewCount, timeFunction };
|
const weekdayLabels: number[] = [];
|
||||||
|
for (let i = 0; i < 7; i++) {
|
||||||
|
weekdayLabels.push((data.firstWeekday + i) % 7);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { reviewCount, timeFunction, weekdayLabels };
|
||||||
}
|
}
|
||||||
|
|
||||||
export function renderCalendar(
|
export function renderCalendar(
|
||||||
@ -82,7 +88,8 @@ export function renderCalendar(
|
|||||||
|
|
||||||
const x = scaleLinear()
|
const x = scaleLinear()
|
||||||
.range([bounds.marginLeft, bounds.width - bounds.marginRight])
|
.range([bounds.marginLeft, bounds.width - bounds.marginRight])
|
||||||
.domain([0, 53]);
|
.domain([-1, 53]);
|
||||||
|
|
||||||
// map of 0-365 -> day
|
// map of 0-365 -> day
|
||||||
const dayMap: Map<number, DayDatum> = new Map();
|
const dayMap: Map<number, DayDatum> = new Map();
|
||||||
let maxCount = 0;
|
let maxCount = 0;
|
||||||
@ -153,20 +160,31 @@ export function renderCalendar(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const height = bounds.height / 10;
|
const height = bounds.height / 10;
|
||||||
let emptyColour = "#ddd";
|
const emptyColour = nightMode ? "#333" : "#ddd";
|
||||||
if (nightMode) {
|
|
||||||
emptyColour = "#333";
|
svg.select("g.weekdays")
|
||||||
}
|
.selectAll("text")
|
||||||
svg.select(`g.days`)
|
.data(sourceData.weekdayLabels)
|
||||||
|
.join("text")
|
||||||
|
.text((d: number) => i18n.weekdayLabel(d))
|
||||||
|
.attr("width", x(-1)! - 2)
|
||||||
|
.attr("height", height - 2)
|
||||||
|
.attr("x", x(1)! - 3)
|
||||||
|
.attr("y", (_d, index) => bounds.marginTop + index * height)
|
||||||
|
.attr("dominant-baseline", "hanging")
|
||||||
|
.attr("text-anchor", "end")
|
||||||
|
.attr("font-size", "small")
|
||||||
|
.attr("font-family", "monospace")
|
||||||
|
.style("user-select", "none");
|
||||||
|
|
||||||
|
svg.select("g.days")
|
||||||
.selectAll("rect")
|
.selectAll("rect")
|
||||||
.data(data)
|
.data(data)
|
||||||
.join("rect")
|
.join("rect")
|
||||||
.attr("fill", emptyColour)
|
.attr("fill", emptyColour)
|
||||||
.attr("width", (d) => {
|
.attr("width", (d) => x(d.weekNumber + 1)! - x(d.weekNumber)! - 2)
|
||||||
return x(d.weekNumber + 1)! - x(d.weekNumber)! - 2;
|
|
||||||
})
|
|
||||||
.attr("height", height - 2)
|
.attr("height", height - 2)
|
||||||
.attr("x", (d) => x(d.weekNumber)!)
|
.attr("x", (d) => x(d.weekNumber + 1)!)
|
||||||
.attr("y", (d) => bounds.marginTop + d.weekDay * height)
|
.attr("y", (d) => bounds.marginTop + d.weekDay * height)
|
||||||
.on("mousemove", function (this: any, d: any) {
|
.on("mousemove", function (this: any, d: any) {
|
||||||
const [x, y] = mouse(document.body);
|
const [x, y] = mouse(document.body);
|
||||||
|
@ -59,6 +59,13 @@ export class I18n {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
weekdayLabel(n: number): string {
|
||||||
|
const firstLang = this.bundles[0].locales[0];
|
||||||
|
return new Date(86_400_000 * (3 + n)).toLocaleDateString(firstLang, {
|
||||||
|
weekday: "narrow",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private keyName(msg: pb.FluentProto.FluentString): string {
|
private keyName(msg: pb.FluentProto.FluentString): string {
|
||||||
return this.TR[msg].toLowerCase().replace(/_/g, "-");
|
return this.TR[msg].toLowerCase().replace(/_/g, "-");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user