Skip to content

Commit 5f2922d

Browse files
committed
Time interval chart
1 parent 8344f0e commit 5f2922d

File tree

1 file changed

+38
-172
lines changed

1 file changed

+38
-172
lines changed

src/components/TimeIntervalChart.vue

Lines changed: 38 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -16,51 +16,58 @@ import { StorageDeserializeParam } from '../storage/storage-params';
1616
import { TimeInterval } from '../entity/time-interval';
1717
import { todayLocalDate } from '../utils/date';
1818
import { useI18n } from 'vue-i18n';
19-
import { convertHoursToTime, convertStringTimeIntervalToSeconds } from '../utils/converter';
2019
import * as d3 from 'd3';
20+
import { convertStringTimeIntervalToSeconds } from '../utils/converter';
2121
2222
const { t } = useI18n();
23+
const storage = injecStorage();
2324
2425
const chart = ref<any>();
2526
27+
type DataForChart = {
28+
domain: string;
29+
interval: string;
30+
};
31+
2632
onMounted(async () => {
2733
const timeIntervalList = (await storage.getDeserializeList(
2834
StorageDeserializeParam.TIMEINTERVAL_LIST,
2935
)) as TimeInterval[];
3036
31-
// data.intervals.forEach(function (interval) {
32-
// resultArr.push({ domain: data.domain, interval: interval });
33-
// });
34-
const data = [
35-
{
36-
domain: 'google.com',
37-
interval: '10:12:18-10:25:17',
38-
},
39-
{
40-
domain: 'habr.com',
41-
interval: '10:28:18-10:31:17',
42-
},
43-
{
44-
domain: 'medium.com',
45-
interval: '11:41:18-11:48:17',
46-
},
47-
{
48-
domain: 'xy.com',
49-
interval: '02:41:18-03:01:17',
50-
},
51-
];
52-
drawIntervalChart(data);
37+
const todayIntervals = timeIntervalList?.filter(x => x.day == todayLocalDate());
38+
const dataForChart: DataForChart[] = [];
39+
todayIntervals.forEach(interval => {
40+
interval.intervals.forEach(int => {
41+
const from = int.split('-')[0];
42+
const to = int.split('-')[1];
43+
if (convertStringTimeIntervalToSeconds(to) - convertStringTimeIntervalToSeconds(from) > 5) {
44+
dataForChart.push({ domain: interval.domain, interval: convertInterval(int) });
45+
}
46+
});
47+
});
48+
drawIntervalChart(dataForChart);
5349
});
5450
55-
function drawIntervalChart(data) {
56-
data.forEach(function (item) {
57-
var hFrom = getHourFrom(item.interval);
58-
var hTo = getHourTo(item.interval);
51+
function convertInterval(interval: string): string {
52+
function convert(item: string[]) {
53+
item = item.map(x => (x.length == 1 ? `0${x}` : x));
54+
return item.join(':');
55+
}
56+
57+
const sourceTimeFrom = interval.split('-')[0].split(':');
58+
const sourceTimeTo = interval.split('-')[1].split(':');
59+
return `${convert(sourceTimeFrom)}-${convert(sourceTimeTo)}`;
60+
}
61+
62+
function drawIntervalChart(data: DataForChart[]) {
63+
data.forEach(item => {
64+
const hFrom = getHourFrom(item.interval);
65+
const hTo = getHourTo(item.interval);
5966
if (hFrom != hTo) {
60-
var sourceTimeFrom = item.interval.split('-')[0].split(':');
61-
var sourceTimeTo = item.interval.split('-')[1].split(':');
62-
var timeTo = `${sourceTimeFrom[0]}:59:59`;
63-
var timeFrom = `${sourceTimeTo[0]}:00:00`;
67+
const sourceTimeFrom = item.interval.split('-')[0].split(':');
68+
const sourceTimeTo = item.interval.split('-')[1].split(':');
69+
const timeTo = `${sourceTimeFrom[0]}:59:59`;
70+
const timeFrom = `${sourceTimeTo[0]}:00:00`;
6471
data.push({ domain: item.domain, interval: item.interval.split('-')[0] + '-' + timeTo });
6572
data.push({ domain: item.domain, interval: timeFrom + '-' + item.interval.split('-')[1] });
6673
}
@@ -179,147 +186,6 @@ function drawIntervalChart(data) {
179186
return Number(time.split(':')[2]);
180187
}
181188
}
182-
183-
type DataForChart = {
184-
summary: number;
185-
hour: number;
186-
domains: DomainsInterval[];
187-
};
188-
189-
type DomainsInterval = {
190-
hour: number;
191-
summary: number;
192-
domain: string;
193-
};
194-
195-
const storage = injecStorage();
196-
197-
const options = ref<any>();
198-
const data = ref<any>();
199-
const isLoaded = ref<boolean>();
200-
201-
const objects: DataForChart[] = [];
202-
const hours: number[] = [];
203-
204-
isLoaded.value = false;
205-
206-
function convertTimIntervalToObject(
207-
timeInterval: string,
208-
domain: string,
209-
): DomainsInterval[] | null {
210-
const array = timeInterval.split('-');
211-
const time1 = array[0].split(':');
212-
const time2 = array[1].split(':');
213-
if (time1[0] == time2[0]) {
214-
return [
215-
{
216-
hour: Number(time1[0]),
217-
summary:
218-
convertStringTimeIntervalToSeconds(array[1]) -
219-
convertStringTimeIntervalToSeconds(array[0]),
220-
domain: domain,
221-
},
222-
];
223-
} else {
224-
const arr = [];
225-
const firstPart1 = array[0];
226-
const firstPart2 = `${time1[0]}:59:59`;
227-
const hourForFirstPart = firstPart1.split(':');
228-
arr.push({
229-
hour: Number(hourForFirstPart[0]),
230-
summary:
231-
convertStringTimeIntervalToSeconds(firstPart2) -
232-
convertStringTimeIntervalToSeconds(firstPart1),
233-
domain: domain,
234-
});
235-
const secondPart1 = `${time2[0]}:00:00`;
236-
const secondPart2 = `${time2[0]}:${time2[1]}:${time2[2]}`;
237-
const hourForsecondPart = secondPart1.split(':');
238-
arr.push({
239-
hour: Number(hourForsecondPart[0]),
240-
summary:
241-
convertStringTimeIntervalToSeconds(secondPart2) -
242-
convertStringTimeIntervalToSeconds(secondPart1),
243-
domain: domain,
244-
});
245-
return arr;
246-
}
247-
}
248-
249-
function fillData(timeIntervalList: TimeInterval[]) {
250-
const items = timeIntervalList?.filter(x => x.day == todayLocalDate());
251-
const domains = items.map(x => x.domain);
252-
const result: any[] = [];
253-
const intervalsObj: DomainsInterval[] = [];
254-
domains.forEach(domain => {
255-
const intervals = items.filter(x => x.domain == domain);
256-
intervals.forEach(array => {
257-
const i = array.intervals;
258-
i.forEach(time => {
259-
const objs = convertTimIntervalToObject(time, domain);
260-
if (objs != null && objs.length > 0) {
261-
objs.forEach(obj => {
262-
const existDomain = intervalsObj.find(x => x.domain == domain && x.hour == obj.hour);
263-
if (existDomain != undefined) {
264-
existDomain.summary += obj.summary;
265-
} else intervalsObj.push(obj);
266-
});
267-
}
268-
});
269-
});
270-
});
271-
272-
const tempArray: number[] = [];
273-
for (let index = 0; index < 24; index++) {
274-
const obj = objects.find(x => x.hour == index);
275-
const intervalObj = intervalsObj.filter(x => x.hour == index);
276-
const summary =
277-
intervalObj.length == 0 ? 0 : intervalObj.map(x => x.summary).reduce((a, b) => a + b);
278-
if (obj == undefined) {
279-
const newObj = {
280-
summary: summary,
281-
hour: index,
282-
domains: intervalObj,
283-
};
284-
objects.push(newObj);
285-
} else {
286-
obj.summary += summary;
287-
intervalObj.forEach(element => {
288-
obj.domains.push(element);
289-
});
290-
}
291-
292-
tempArray.push(0);
293-
}
294-
295-
objects.forEach(obj => {
296-
const emptyArray: number[] = Object.assign([], tempArray);
297-
emptyArray[obj.hour] = Number(obj.summary / 60);
298-
result.push({
299-
backgroundColor: ['#5668e2'],
300-
data: emptyArray,
301-
});
302-
});
303-
304-
return result;
305-
}
306-
307-
async function buildChart() {
308-
const timeIntervalList = (await storage.getDeserializeList(
309-
StorageDeserializeParam.TIMEINTERVAL_LIST,
310-
)) as TimeInterval[];
311-
for (let index = 0; index <= 23; index++) {
312-
hours.push(index);
313-
}
314-
let minutes: number[] = [];
315-
for (let index = 1; index < 60; index++) {
316-
minutes.push(index);
317-
}
318-
319-
isLoaded.value = true;
320-
}
321-
322-
onMounted(async () => await buildChart());
323189
</script>
324190

325191
<style scoped>

0 commit comments

Comments
 (0)