forked from pyrochlore/obsidian-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonth.ts
More file actions
1166 lines (1080 loc) · 36.7 KB
/
month.ts
File metadata and controls
1166 lines (1080 loc) · 36.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { Moment } from "moment";
import {
Datasets,
DataPoint,
RenderInfo,
MonthInfo,
Dataset,
Size,
Transform,
ChartElements,
GraphType,
ValueType,
} from "./data";
import * as helper from "./helper";
import * as d3 from "d3";
let logToConsole = false;
let ratioCellToText = 2.8;
let ratioDotToText = 1.8;
interface DayInfo {
date: string;
value: number;
scaledValue: number;
dayInMonth: number;
isInThisMonth: boolean;
isOutOfDataRange: boolean;
row: number;
col: number;
showCircle: boolean;
streakIn: boolean;
streakOut: boolean;
annotation: string;
}
function setChartScale(
_canvas: HTMLElement,
chartElements: ChartElements,
renderInfo: RenderInfo
) {
let canvas = d3.select(_canvas);
let svg = chartElements.svg;
let svgWidth = parseFloat(svg.attr("width"));
let svgHeight = parseFloat(svg.attr("height"));
svg.attr("width", null)
.attr("height", null)
.attr("viewBox", `0 0 ${svgWidth} ${svgHeight}`)
.attr("preserveAspectRatio", "xMidYMid meet");
if (renderInfo.fitPanelWidth) {
canvas.style("width", "100%");
} else {
canvas.style(
"width",
(svgWidth * renderInfo.fixedScale).toString() + "px"
);
canvas.style(
"height",
(svgHeight * renderInfo.fixedScale).toString() + "px"
);
}
}
function toNextDataset(renderInfo: RenderInfo, monthInfo: MonthInfo): boolean {
let datasetIds = monthInfo.dataset;
if (datasetIds.length === 0) return false; // false if selected dataset not changed
let dataset = null;
if (monthInfo.selectedDataset === null) {
for (let datasetId of datasetIds) {
dataset = renderInfo.datasets.getDatasetById(datasetId);
if (dataset && !dataset.getQuery().usedAsXDataset) break;
}
if (dataset) {
monthInfo.selectedDataset = dataset.getId();
return true; // true if selectec dataset changed
}
} else {
let curDatasetId = monthInfo.selectedDataset;
let curIndex = datasetIds.findIndex((id) => {
return id === curDatasetId;
});
if (curIndex >= 0) {
if (curIndex === monthInfo.dataset.length - 1) {
// search from start
for (let datasetId of datasetIds) {
dataset = renderInfo.datasets.getDatasetById(datasetId);
if (dataset && !dataset.getQuery().usedAsXDataset) break;
}
if (dataset) {
monthInfo.selectedDataset = dataset.getId();
return true; // true if selectec dataset changed
} else {
return false;
}
} else {
curIndex++;
let datasetId = datasetIds[curIndex];
dataset = renderInfo.datasets.getDatasetById(datasetId);
monthInfo.selectedDataset = datasetId;
if (dataset && !dataset.getQuery().usedAsXDataset) {
return true;
} else {
toNextDataset(renderInfo, monthInfo);
}
}
}
}
return false;
}
function createAreas(
chartElements: ChartElements,
canvas: HTMLElement,
renderInfo: RenderInfo,
monthInfo: MonthInfo
): ChartElements {
// clean areas
d3.select(canvas).select("#svg").remove();
var props = Object.getOwnPropertyNames(chartElements);
for (var i = 0; i < props.length; i++) {
// d3.select(chartElements[props[i]]).remove();
delete chartElements[props[i]];
}
// console.log(chartElements);
// whole area for plotting, includes margins
let svg = d3
.select(canvas)
.append("svg")
.attr("id", "svg")
.attr(
"width",
renderInfo.dataAreaSize.width +
renderInfo.margin.left +
renderInfo.margin.right
)
.attr(
"height",
renderInfo.dataAreaSize.height +
renderInfo.margin.top +
renderInfo.margin.bottom
);
chartElements["svg"] = svg;
// graphArea, includes chartArea, title, legend
let graphArea = svg
.append("g")
.attr("id", "graphArea")
.attr(
"transform",
"translate(" +
renderInfo.margin.left +
"," +
renderInfo.margin.top +
")"
)
.attr("width", renderInfo.dataAreaSize.width + renderInfo.margin.right)
.attr(
"height",
renderInfo.dataAreaSize.height + renderInfo.margin.bottom
);
chartElements["graphArea"] = graphArea;
// dataArea, under graphArea, includes points, lines, xAxis, yAxis
let dataArea = graphArea
.append("g")
.attr("id", "dataArea")
.attr("width", renderInfo.dataAreaSize.width)
.attr("height", renderInfo.dataAreaSize.height);
chartElements["dataArea"] = dataArea;
return chartElements;
}
function clearSelection(chartElements: ChartElements, monthInfo: MonthInfo) {
let circles = chartElements.svg.selectAll("circle");
// console.log(circles);
for (let circle of circles) {
// console.log(circle);
let id = d3.select(circle).attr("id");
if (id && id.startsWith("tracker-selected-circle-")) {
d3.select(circle).style("stroke", "none");
}
}
monthInfo.selectedDate = "";
chartElements.monitor.text("");
}
function renderMonthHeader(
canvas: HTMLElement,
chartElements: ChartElements,
renderInfo: RenderInfo,
monthInfo: MonthInfo,
curMonthDate: Moment
) {
// console.log("renderMonthHeader")
if (!renderInfo || !monthInfo) return;
let curDatasetId = monthInfo.selectedDataset;
if (curDatasetId === null) return;
let dataset = renderInfo.datasets.getDatasetById(curDatasetId);
if (!dataset) return;
let datasetName = dataset.getName();
let curMonth = curMonthDate.month(); // 0~11
let curDaysInMonth = curMonthDate.daysInMonth(); // 28~31
let curYear = curMonthDate.year();
let maxDayTextSize = helper.measureTextSize("30", "tracker-month-label");
let cellSize =
Math.max(maxDayTextSize.width, maxDayTextSize.height) * ratioCellToText;
let dotRadius = ((cellSize / ratioCellToText) * ratioDotToText) / 2.0;
let headerYearText = curMonthDate.format("YYYY");
let headerMonthText = curMonthDate.format("MMM");
let headerYearSize = helper.measureTextSize(
headerYearText,
"tracker-month-header-year"
);
let headerMonthSize = helper.measureTextSize(
headerMonthText,
"tracker-month-header-month"
);
let headerHeight = 0;
let ySpacing = 8;
// Append header group
let headerGroup = chartElements.graphArea.append("g");
// haeder month
let headerMonthColor = null;
if (monthInfo.headerMonthColor) {
headerMonthColor = monthInfo.headerMonthColor;
} else {
if (monthInfo.color) {
headerMonthColor = monthInfo.color;
}
}
let headerMonth = headerGroup
.append("text")
.text(headerMonthText) // pivot at center
.attr("id", "titleMonth")
.attr(
"transform",
"translate(" + cellSize / 4.0 + "," + headerMonthSize.height + ")"
)
.attr("class", "tracker-month-header-month")
.style("cursor", "default")
.on("click", function (event: any) {
clearSelection(chartElements, monthInfo);
});
if (headerMonthColor) {
headerMonth.style("fill", headerMonthColor);
}
headerHeight += headerMonthSize.height;
// header year
let headerYearColor = null;
if (monthInfo.headerYearColor) {
headerYearColor = monthInfo.headerYearColor;
} else {
if (monthInfo.color) {
headerYearColor = monthInfo.color;
}
}
let headerYear = headerGroup
.append("text")
.text(headerYearText) // pivot at center
.attr("id", "titleYear")
.attr(
"transform",
"translate(" +
cellSize / 4.0 +
"," +
(headerHeight + headerYearSize.height) +
")"
)
.attr("class", "tracker-month-header-year")
.style("cursor", "default")
.attr("font-weight", "bold")
.on("click", function (event: any) {
clearSelection(chartElements, monthInfo);
});
if (headerYearColor) {
headerYear.style("fill", headerYearColor);
}
headerHeight += headerYearSize.height;
// dataset rotator
let datasetNameSize = helper.measureTextSize(
datasetName,
"tracker-month-title-rotator"
);
if (
monthInfo.mode === "circle" ||
(monthInfo.mode === "annotation" &&
!monthInfo.showAnnotationOfAllTargets)
) {
let datasetRotator = headerGroup
.append("text")
.text(datasetName)
.attr(
"transform",
"translate(" +
3.5 * cellSize +
"," +
datasetNameSize.height +
")"
)
.attr("class", "tracker-month-title-rotator")
.style("cursor", "pointer")
.on("click", function (event: any) {
// show next target
if (toNextDataset(renderInfo, monthInfo)) {
// clear circles
clearSelection(chartElements, monthInfo);
refresh(
canvas,
chartElements,
renderInfo,
monthInfo,
curMonthDate
);
}
});
chartElements["rotator"] = datasetRotator;
}
// value monitor
let monitorTextSize = helper.measureTextSize(
"0.0000",
"tracker-month-title-monitor"
);
let monitor = headerGroup
.append("text")
.text("")
.attr("id", "monitor")
.attr("class", "tracker-month-title-monitor")
.attr(
"transform",
"translate(" +
3.5 * cellSize +
"," +
(datasetNameSize.height + monitorTextSize.height) +
")"
)
.style("cursor", "pointer")
.style("fill", monthInfo.selectedRingColor);
chartElements["monitor"] = monitor;
// arrow left
let arrowSize = helper.measureTextSize("<", "tracker-month-title-arrow");
let arrowLeft = headerGroup
.append("text")
.text("<") // pivot at center
.attr("id", "arrowLeft")
.attr(
"transform",
"translate(" +
5.5 * cellSize +
"," +
(headerHeight / 2 + arrowSize.height / 2) +
")"
)
.attr("class", "tracker-month-title-arrow")
.on("click", function (event: any) {
// console.log("left arrow clicked");
clearSelection(chartElements, monthInfo);
monthInfo.selectedDate = "";
let prevMonthDate = curMonthDate.clone().add(-1, "month");
refresh(
canvas,
chartElements,
renderInfo,
monthInfo,
prevMonthDate
);
})
.style("cursor", "pointer");
// arrow right
let arrowRight = headerGroup
.append("text")
.text(">") // pivot at center
.attr("id", "arrowLeft")
.attr(
"transform",
"translate(" +
6.5 * cellSize +
"," +
(headerHeight / 2 + arrowSize.height / 2) +
")"
)
.attr("class", "tracker-month-title-arrow")
.on("click", function (event: any) {
// console.log("right arrow clicked");
clearSelection(chartElements, monthInfo);
let nextMonthDate = curMonthDate.clone().add(1, "month");
refresh(
canvas,
chartElements,
renderInfo,
monthInfo,
nextMonthDate
);
})
.style("cursor", "pointer");
// arrow today
let arrowToday = headerGroup
.append("text")
.text("◦") // pivot at center
.attr("id", "arrowToday")
.attr(
"transform",
"translate(" +
6 * cellSize +
"," +
(headerHeight / 2 + arrowSize.height / 2) +
")"
)
.attr("class", "tracker-month-title-arrow")
.on("click", function (event: any) {
// console.log("today arrow clicked");
clearSelection(chartElements, monthInfo);
let todayDate = helper.getDateToday(renderInfo.dateFormat);
refresh(canvas, chartElements, renderInfo, monthInfo, todayDate);
})
.style("cursor", "pointer");
headerHeight += ySpacing;
// week day names
let weekdayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
if (monthInfo.startWeekOn.toLowerCase() === "mon") {
weekdayNames.push(weekdayNames.shift());
}
let weekdayNameSize = helper.measureTextSize(
weekdayNames[0],
"tracker-month-weekday"
);
let weekDays = chartElements.graphArea
.selectAll("weekDays")
.data(weekdayNames)
.enter()
.append("text")
.text(function (n: string) {
return n;
})
.attr("transform", function (n: string, i: number) {
let strTranslate =
"translate(" +
(i + 0.5) * cellSize +
"," +
(headerHeight + weekdayNameSize.height) +
")";
return strTranslate;
})
.attr("class", "tracker-month-weekday")
.attr("text-anchor", "middle")
.style("cursor", "default")
.on("click", function (event: any) {
clearSelection(chartElements, monthInfo);
});
headerHeight += weekdayNameSize.height + ySpacing;
// dividing line
let dividingLineHeight = 1;
let dividingLineColor = null;
if (monthInfo.dividingLineColor) {
dividingLineColor = monthInfo.dividingLineColor;
} else {
if (monthInfo.color) {
dividingLineColor = monthInfo.color;
}
}
let dividingLine = chartElements.graphArea
.append("rect")
.attr("x", 0)
.attr("y", headerHeight)
.attr("width", 6.5 * cellSize + weekdayNameSize.width)
.attr("height", dividingLineHeight)
.attr("class", "tracker-month-dividing-line");
if (dividingLineColor) {
dividingLine.style("fill", dividingLineColor);
}
headerHeight += dividingLineHeight;
headerGroup.attr("height", headerHeight);
chartElements["header"] = headerGroup;
// Move sibling areas
helper.moveArea(chartElements.dataArea, 0, headerHeight);
}
function renderMonthDays(
canvas: HTMLElement,
chartElements: ChartElements,
renderInfo: RenderInfo,
monthInfo: MonthInfo,
curMonthDate: Moment
) {
// console.log("renderMonthDays");
// console.log(renderInfo);
// console.log(monthInfo);
if (!renderInfo || !monthInfo) return;
let mode = monthInfo.mode;
if (mode !== "circle" && mode !== "annotation") {
return "Unknown month view mode";
}
let curDatasetId = monthInfo.selectedDataset;
if (curDatasetId === null) return;
let dataset = renderInfo.datasets.getDatasetById(curDatasetId);
if (!dataset) return;
// console.log(dataset);
let curDatasetIndex = monthInfo.dataset.findIndex((id) => {
return id === curDatasetId;
});
if (curDatasetId < 0) curDatasetIndex = 0;
let threshold = monthInfo.threshold[curDatasetIndex];
let curMonth = curMonthDate.month(); // 0~11
let curDaysInMonth = curMonthDate.daysInMonth(); // 28~31
let maxDayTextSize = helper.measureTextSize("30", "tracker-month-label");
let cellSize =
Math.max(maxDayTextSize.width, maxDayTextSize.height) * ratioCellToText;
let dotRadius = ((cellSize / ratioCellToText) * ratioDotToText) / 2.0;
let streakWidth = (cellSize - dotRadius * 2.0) / 2.0;
let streakHeight = 3;
// Get min and max
let yMin = d3.min(dataset.getValues());
if (monthInfo.yMin[curDatasetIndex] !== null) {
yMin = monthInfo.yMin[curDatasetIndex];
}
let yMax = d3.max(dataset.getValues());
if (monthInfo.yMax[curDatasetIndex] !== null) {
yMax = monthInfo.yMax[curDatasetIndex];
}
// console.log(`yMin:${yMin}, yMax:${yMax}`);
let allowScaledValue = true;
if (yMax === null || yMin === null || yMax <= yMin) {
// scaledValue can not be calculated, do not use gradient color
allowScaledValue = false;
// console.log("scaledValue not allowed");
}
// Start and end
const monthStartDate = curMonthDate.clone().startOf("month");
let startDate = monthStartDate
.clone()
.subtract(monthStartDate.day(), "days");
if (monthInfo.startWeekOn.toLowerCase() === "mon") {
startDate = startDate.add(1, "days");
}
const monthEndDate = curMonthDate.clone().endOf("month");
let endDate = monthEndDate.clone().add(7 - monthEndDate.day() - 1, "days");
if (monthInfo.startWeekOn.toLowerCase() === "mon") {
endDate = endDate.add(1, "days");
}
const dataStartDate = dataset.getStartDate();
const dataEndDate = dataset.getEndDate();
// console.log(monthStartDate.format("YYYY-MM-DD"));
// console.log(startDate.format("YYYY-MM-DD"));
// annotations
let showAnnotation = monthInfo.showAnnotation;
let annotations = monthInfo.annotation;
let curAnnotation = annotations[curDatasetIndex];
let showAnnotationOfAllTargets = monthInfo.showAnnotationOfAllTargets;
// Prepare data for graph
let daysInMonthView: Array<DayInfo> = [];
let indCol = 0;
let indRow = 0;
let ind = 0;
for (
let curDate = startDate.clone();
curDate <= endDate;
curDate.add(1, "days")
) {
// not sure why we need to do this to stablize the date
// sometimes, curValue is wrong without doing this
curDate = helper.strToDate(
helper.dateToStr(curDate, renderInfo.dateFormat),
renderInfo.dateFormat
);
if (curDate.format("YYYY-MM-DD") === "2021-09-13") {
logToConsole = false; // Change this to do dubugging
}
if (monthInfo.startWeekOn.toLowerCase() === "mon") {
indCol = curDate.day() - 1;
if (indCol < 0) {
indCol = 6;
}
indRow = Math.floor(ind / 7);
} else {
indCol = curDate.day(); // 0~6
indRow = Math.floor(ind / 7);
}
// is this day in this month
let isInThisMonth = true;
if (
curDate.diff(monthStartDate) < 0 ||
curDate.diff(monthEndDate) > 0
) {
isInThisMonth = false;
}
// is this day out of data range
let isOutOfDataRange = true;
if (
dataStartDate &&
dataEndDate &&
curDate.diff(dataStartDate) >= 0 &&
curDate.diff(dataEndDate) <= 0
) {
isOutOfDataRange = false;
}
const curValue = dataset.getValue(curDate);
if (logToConsole) {
console.log(dataset);
console.log(helper.dateToStr(curDate, renderInfo.dateFormat));
console.log(curValue);
}
// showCircle
let showCircle = false;
if (!monthInfo.circleColorByValue) {
// shown or not shown
if (curValue !== null) {
if (curValue > threshold) {
showCircle = true;
}
}
} else {
if (!allowScaledValue) {
if (curValue !== null) {
if (curValue > threshold) {
showCircle = true;
}
}
} else {
showCircle = true;
}
}
// scaledValue
let scaledValue = null;
if (monthInfo.circleColorByValue) {
if (allowScaledValue && curValue !== null) {
scaledValue = (curValue - yMin) / (yMax - yMin);
}
}
if (logToConsole) {
console.log(yMin);
console.log(yMax);
console.log(scaledValue);
}
// streakIn and streakOut
let nextValue = dataset.getValue(curDate, 1);
let prevValue = dataset.getValue(curDate, -1);
let streakIn = false;
if (curValue !== null && curValue > threshold) {
if (prevValue !== null && prevValue > threshold) {
streakIn = true;
}
}
let streakOut = false;
if (curValue !== null && curValue > threshold) {
if (nextValue !== null && nextValue > threshold) {
streakOut = true;
}
}
if (logToConsole) {
console.log(
`preValue: ${prevValue}, curValue: ${curValue}, nextValue: ${nextValue}`
);
console.log(monthInfo.threshold);
console.log(`streakIn: ${streakIn}, streakOut: ${streakOut}`);
}
let textAnnotation = "";
if (showAnnotation) {
if (!showAnnotationOfAllTargets) {
if (curValue > threshold) {
textAnnotation = curAnnotation;
}
} else {
for (let datasetId of monthInfo.dataset) {
let datasetIndex = monthInfo.dataset.findIndex((id) => {
return id === datasetId;
});
if (datasetIndex >= 0) {
let v = renderInfo.datasets
.getDatasetById(datasetId)
.getValue(curDate);
let t = monthInfo.threshold[datasetIndex];
if (v !== null && v > t) {
textAnnotation += annotations[datasetIndex];
}
}
}
}
}
daysInMonthView.push({
date: helper.dateToStr(curDate, renderInfo.dateFormat),
value: curValue,
scaledValue: scaledValue,
dayInMonth: curDate.date(),
isInThisMonth: isInThisMonth,
isOutOfDataRange: isOutOfDataRange,
row: indRow,
col: indCol,
showCircle: showCircle,
streakIn: streakIn,
streakOut: streakOut,
annotation: textAnnotation,
});
ind++;
// Disable logging starts at the beginning of each loop
if (logToConsole) {
logToConsole = false;
}
}
// console.log(daysInMonthView);
// console.log(daysInMonthView.filter(function (d: DayInfo) {
// return d.streakIn;
// }));
// console.log(daysInMonthView.filter(function (d: DayInfo) {
// return d.streakOut;
// }));
// scale
let totalDayBlockWidth = (indCol + 1) * cellSize;
let totalBlockHeight = (indRow + 1) * cellSize;
let scale = d3
.scaleLinear()
.domain([-0.5, 6.5])
.range([0, totalDayBlockWidth]);
// streak lines
if (mode === "circle" && monthInfo.showCircle && monthInfo.showStreak) {
let streakColor = "#69b3a2";
if (monthInfo.circleColor) {
streakColor = monthInfo.circleColor;
} else if (monthInfo.color) {
streakColor = monthInfo.color;
}
// console.log(streakColor);
chartElements.dataArea
.selectAll("streakIn")
.data(
daysInMonthView.filter(function (d: DayInfo) {
return d.streakIn;
})
)
.enter()
.append("rect")
// .attr("id", function(d: DayInfo) {
// return "in" + d.date.format("YYYY-MM-DD");
// })
.attr("x", function (d: DayInfo) {
let x = scale(d.col) - dotRadius - streakWidth;
return x;
})
.attr("y", function (d: DayInfo) {
return scale(d.row) - streakHeight / 2.0;
})
.attr("width", streakWidth)
.attr("height", streakHeight)
.style("fill", function (d: DayInfo) {
if (d.showCircle) {
if (!monthInfo.circleColorByValue) {
return streakColor;
}
if (d.scaledValue !== null) {
return d3.interpolateLab(
"white",
streakColor
)(d.scaledValue * 0.8 + 0.2);
} else {
return "none";
}
}
return "none";
})
.style("opacity", function (d: DayInfo) {
if (
d.isOutOfDataRange ||
(monthInfo.dimNotInMonth && !d.isInThisMonth)
) {
return 0.2;
}
return 1.0;
});
chartElements.dataArea
.selectAll("streakOut")
.data(
daysInMonthView.filter(function (d: DayInfo) {
return d.streakOut;
})
)
.enter()
.append("rect")
// .attr("id", function(d: DayInfo) {
// return "out" + d.date.format("YYYY-MM-DD");
// })
.attr("x", function (d: DayInfo) {
let x = scale(d.col) + dotRadius;
return x;
})
.attr("y", function (d: DayInfo) {
return scale(d.row) - streakHeight / 2.0;
})
.attr("width", streakWidth)
.attr("height", streakHeight)
.style("fill", function (d: DayInfo) {
if (d.showCircle) {
if (!monthInfo.circleColorByValue) {
return streakColor;
}
if (d.scaledValue !== null) {
return d3.interpolateLab(
"white",
streakColor
)(d.scaledValue * 0.8 + 0.2);
} else {
return "none";
}
}
return "none";
})
.style("opacity", function (d: DayInfo) {
if (
d.isOutOfDataRange ||
(monthInfo.dimNotInMonth && !d.isInThisMonth)
) {
return 0.2;
}
return 1.0;
});
}
// circles
let circleColor = "#69b3a2";
if (monthInfo.circleColor) {
circleColor = monthInfo.circleColor;
} else if (monthInfo.color) {
circleColor = monthInfo.color;
}
if (mode === "circle" && monthInfo.showCircle) {
let dots = chartElements.dataArea
.selectAll("dot")
.data(daysInMonthView)
.enter()
.append("circle")
.attr("r", dotRadius)
.attr("cx", function (d: DayInfo) {
return scale(d.col);
})
.attr("cy", function (d: DayInfo) {
return scale(d.row);
})
.style("fill", function (d: DayInfo) {
if (d.showCircle) {
if (!monthInfo.circleColorByValue) {
return circleColor;
}
if (d.scaledValue !== null) {
let scaledColor = d3.interpolateLab(
"white",
circleColor
)(d.scaledValue * 0.8 + 0.2);
// console.log(d.scaledValue);
// console.log(scaledColor);
return scaledColor;
} else {
return "none";
}
}
return "none";
})
.style("opacity", function (d: DayInfo) {
if (
d.isOutOfDataRange ||
(monthInfo.dimNotInMonth && !d.isInThisMonth)
) {
return 0.2;
}
return 1.0;
})
.style("cursor", "default");
}
// today rings
let today = helper.dateToStr(window.moment(), renderInfo.dateFormat);
if (mode === "circle" && monthInfo.showTodayRing) {
let todayRings = chartElements.dataArea
.selectAll("todayRing")
.data(
daysInMonthView.filter(function (d: DayInfo) {
return d.date === today;
})
)
.enter()
.append("circle")
.attr("r", dotRadius * 0.9)
.attr("cx", function (d: DayInfo) {
return scale(d.col);
})
.attr("cy", function (d: DayInfo) {
return scale(d.row);
})
.attr("class", "tracker-month-today-circle") // stroke not works??
.style("cursor", "default");
if (monthInfo.todayRingColor !== "") {
todayRings.style("stroke", monthInfo.todayRingColor);
} else {
todayRings.style("stroke", "white");
}
}
// selected rings
if (mode === "circle" && monthInfo.showSelectedRing) {
let selectedRings = chartElements.dataArea
.selectAll("selectedRing")
.data(daysInMonthView)
.enter()
.append("circle")
.attr("r", dotRadius)
.attr("cx", function (d: DayInfo) {
return scale(d.col);
})
.attr("cy", function (d: DayInfo) {
return scale(d.row);
})
.attr("id", function (d: DayInfo) {
return "tracker-selected-circle-" + d.date;
})
.attr("class", "tracker-month-selected-circle") // stroke not works??
.style("cursor", "default")
.style("stroke", "none");
}
// labels
let dayLabals = chartElements.dataArea
.selectAll("dayLabel")
.data(daysInMonthView)
.enter()
.append("text")
.text(function (d: DayInfo) {
return d.dayInMonth.toString();
})
.attr("transform", function (d: DayInfo) {
let transX = scale(d.col);
let transY = scale(d.row) + maxDayTextSize.height / 4;
let strTranslate = "translate(" + transX + "," + transY + ")";
return strTranslate;
})
.style("fill-opacity", function (d: DayInfo) {
if (
d.isOutOfDataRange ||
(monthInfo.dimNotInMonth && !d.isInThisMonth)
) {
return 0.2;
}
return 1.0;
})
.attr("date", function (d: DayInfo) {
return d.date;
})
.attr("value", function (d: DayInfo) {