forked from Stigmatoz/web-activity-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHourlyChart.vue
More file actions
213 lines (193 loc) · 5.32 KB
/
HourlyChart.vue
File metadata and controls
213 lines (193 loc) · 5.32 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
<template>
<p class="description">{{ t('timeChartDescription.message') }}</p>
<Bar :data="data" :options="options" v-if="isLoaded" />
</template>
<script lang="ts">
export default {
name: 'HourlyChart',
};
</script>
<script lang="ts" setup>
import { Bar } from 'vue-chartjs';
import {
Chart as ChartJS,
Title,
Tooltip,
Legend,
BarElement,
LinearScale,
CategoryScale,
} from 'chart.js';
import { onMounted, ref } from 'vue';
import { injecStorage } from '../storage/inject-storage';
import { StorageDeserializeParam } from '../storage/storage-params';
import { TimeInterval } from '../entity/time-interval';
import { todayLocalDate } from '../utils/date';
import { convertHoursToTime, convertStringTimeIntervalToSeconds } from '../utils/converter';
import { useI18n } from 'vue-i18n';
const { t } = useI18n();
type DataForChart = {
summary: number;
hour: number;
domains: DomainsInterval[];
};
type DomainsInterval = {
hour: number;
summary: number;
domain: string;
};
const storage = injecStorage();
ChartJS.register(CategoryScale, LinearScale, BarElement, Title, Tooltip, Legend);
const options = ref<any>();
const data = ref<any>();
const isLoaded = ref<boolean>();
const objects: DataForChart[] = [];
const hours: number[] = [];
isLoaded.value = false;
function convertTimIntervalToObject(
timeInterval: string,
domain: string,
): DomainsInterval[] | null {
const array = timeInterval.split('-');
const time1 = array[0].split(':');
const time2 = array[1].split(':');
if (time1[0] == time2[0]) {
return [
{
hour: Number(time1[0]),
summary:
convertStringTimeIntervalToSeconds(array[1]) -
convertStringTimeIntervalToSeconds(array[0]),
domain: domain,
},
];
} else {
const arr = [];
const firstPart1 = array[0];
const firstPart2 = `${time1[0]}:59:59`;
const hourForFirstPart = firstPart1.split(':');
arr.push({
hour: Number(hourForFirstPart[0]),
summary:
convertStringTimeIntervalToSeconds(firstPart2) -
convertStringTimeIntervalToSeconds(firstPart1),
domain: domain,
});
const secondPart1 = `${time2[0]}:00:00`;
const secondPart2 = `${time2[0]}:${time2[1]}:${time2[2]}`;
const hourForsecondPart = secondPart1.split(':');
arr.push({
hour: Number(hourForsecondPart[0]),
summary:
convertStringTimeIntervalToSeconds(secondPart2) -
convertStringTimeIntervalToSeconds(secondPart1),
domain: domain,
});
return arr;
}
}
function fillData(timeIntervalList: TimeInterval[]) {
const items = timeIntervalList?.filter(x => x.day == todayLocalDate());
const domains = items.map(x => x.domain);
const result: any[] = [];
const intervalsObj: DomainsInterval[] = [];
domains.forEach(domain => {
const intervals = items.filter(x => x.domain == domain);
intervals.forEach(array => {
const i = array.intervals;
i.forEach(time => {
const objs = convertTimIntervalToObject(time, domain);
if (objs != null && objs.length > 0) {
objs.forEach(obj => {
const existDomain = intervalsObj.find(x => x.domain == domain && x.hour == obj.hour);
if (existDomain != undefined) {
existDomain.summary += obj.summary;
} else intervalsObj.push(obj);
});
}
});
});
});
const tempArray: number[] = [];
for (let index = 0; index < 24; index++) {
const obj = objects.find(x => x.hour == index);
const intervalObj = intervalsObj.filter(x => x.hour == index);
const summary =
intervalObj.length == 0 ? 0 : intervalObj.map(x => x.summary).reduce((a, b) => a + b);
if (obj == undefined) {
const newObj = {
summary: summary,
hour: index,
domains: intervalObj,
};
objects.push(newObj);
} else {
obj.summary += summary;
intervalObj.forEach(element => {
obj.domains.push(element);
});
}
tempArray.push(0);
}
objects.forEach(obj => {
const emptyArray: number[] = Object.assign([], tempArray);
emptyArray[obj.hour] = Number(obj.summary / 60);
result.push({
backgroundColor: ['#5668e2'],
data: emptyArray,
});
});
return result;
}
async function buildChart() {
const timeIntervalList = (await storage.getDeserializeList(
StorageDeserializeParam.TIMEINTERVAL_LIST,
)) as TimeInterval[];
for (let index = 0; index <= 23; index++) {
hours.push(index);
}
let minutes: number[] = [];
for (let index = 1; index < 60; index++) {
minutes.push(index);
}
const dataForChart = fillData(timeIntervalList);
data.value = {
labels: hours,
datasets: dataForChart,
};
options.value = {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: 'none',
},
tooltip: {
callbacks: {
label: function (context: any) {
return `${context.label}:00-${Number(context.label) + 1}:00 ${convertHoursToTime(
context.raw,
)}`;
},
},
},
},
scales: {
y: {
min: 0,
max: 60,
ticks: {
stepSize: 5,
},
},
x: {
stacked: true,
min: 0,
max: 23,
},
},
};
isLoaded.value = true;
}
onMounted(async () => await buildChart());
</script>