1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** @noinspection PhpDocMissingThrowsInspection */ |
4
|
|
|
|
5
|
|
|
require_once 'vendor/autoload.php'; |
6
|
|
|
|
7
|
|
|
use Vctls\IntervalGraph\IntervalGraph; |
8
|
|
|
use Vctls\IntervalGraph\Util\Date as D; |
9
|
|
|
|
10
|
|
|
$today = new DateTime('today'); |
11
|
|
|
|
12
|
|
|
$base = D::intv(0,5); |
13
|
|
|
$intv1 = D::intv(0,4, 7/10); |
14
|
|
|
$intv2 = D::intv(1,5,3/10); |
15
|
|
|
$intv3 = D::intv(2,3,3/10); |
16
|
|
|
$overlapped1 = D::intvg([$base, $intv1]); |
17
|
|
|
$overlapped2 = D::intvg([$base, $intv2]); |
18
|
|
|
$overlapped3 = D::intvg([$base, $intv3]); |
19
|
|
|
$overlapped = D::intvg([$base, $intv1, $intv2, $intv3]); |
20
|
|
|
|
21
|
|
|
$withNull1 = D::intvg([$base, D::intv(0,3,4/10)]); |
22
|
|
|
$withNull2 = D::intvg([$base, D::intv(1,2)]); |
23
|
|
|
$withNull3 = D::intvg([$base, D::intv(2,3,4/10)]); |
24
|
|
|
$withNull4 = D::intvg([$base, D::intv(4,5,5/10)]); |
25
|
|
|
$withNullIntervals = D::intvg([ |
26
|
|
|
[$today, new DateTime('today + 3 days'), 4 / 10], |
27
|
|
|
[new DateTime('today + 1 day'), new DateTime('today + 2 days')], |
28
|
|
|
D::intv(2, 3, 4 / 10), |
29
|
|
|
D::intv(4, 5, 5 / 10), |
30
|
|
|
]); |
31
|
|
|
|
32
|
|
|
$longIntervals = [ |
33
|
|
|
[$today, new DateTime('today + 3 days'), 2 / 10], |
34
|
|
|
D::intv(1, 4, 2 / 10), |
35
|
|
|
D::intv(2, 5, 3 / 10), |
36
|
|
|
D::intv(3, 6, 5 / 10), |
37
|
|
|
D::intv(4, 7, 4 / 10), |
38
|
|
|
D::intv(5, 8, 2 / 10), |
39
|
|
|
D::intv(6, 9, 2 / 10), |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
$longDateFormat = function (DateTime $bound) { |
43
|
|
|
return $bound->format('Y-m-d H:i:s'); |
44
|
|
|
}; |
45
|
|
|
|
46
|
|
|
$long = (D::intvg($longIntervals))->setBoundToString($longDateFormat); |
47
|
|
|
|
48
|
|
|
/* |
49
|
|
|
* CUSTOM VALUE TYPES |
50
|
|
|
*/ |
51
|
|
|
|
52
|
|
|
// An aggregate function for arrays representing fractions with the same denominator. |
53
|
|
|
$agg = function ($a, $b) { |
54
|
|
|
if ($a === null && $b === null) return null; |
55
|
|
|
return [$a[0] + $b[0], $b[1]]; |
56
|
|
|
}; |
57
|
|
|
|
58
|
|
|
// A toNumeric function… |
59
|
|
|
$toNumeric = function ($a) { |
60
|
|
|
return $a === null ? null : (int)($a[0] / $a[1] * 100); |
61
|
|
|
}; |
62
|
|
|
|
63
|
|
|
// A toString function… |
64
|
|
|
$toString = function ($a) { |
65
|
|
|
return $a === null ? null : ($a[0] . '/' . $a[1]); |
66
|
|
|
}; |
67
|
|
|
|
68
|
|
|
$fractions = [ |
69
|
|
|
[$today, new DateTime('today + 3 days'), [2, 10]], |
70
|
|
|
D::intv(1, 4, [2, 10]), |
71
|
|
|
D::intv(2, 5, [3, 10]), |
72
|
|
|
D::intv(3, 6, [5, 10]), |
73
|
|
|
D::intv(4, 7, [4, 10]), |
74
|
|
|
D::intv(5, 8, [2, 10]), |
75
|
|
|
D::intv(6, 9, [2, 10]), |
76
|
|
|
]; |
77
|
|
|
$fractim = (D::intvg($fractions))->setAggregate($agg) |
78
|
|
|
->setValueToNumeric($toNumeric) |
79
|
|
|
->setValueToString($toString); |
80
|
|
|
$fract = $fractim->draw(); |
81
|
|
|
|
82
|
|
|
/* /CUSTOM VALUE TYPES */ |
83
|
|
|
|
84
|
|
|
/* |
85
|
|
|
* TRUNCATED INTERVALS |
86
|
|
|
*/ |
87
|
|
|
try { |
88
|
|
|
$intv1 = (clone $today)->add(new DateInterval('PT60H')); |
89
|
|
|
$intv2 = (clone $today)->add(new DateInterval('PT108H')); |
90
|
|
|
$intv3 = (clone $intv2)->add(new DateInterval('PT60H')); |
91
|
|
|
} catch (Exception $e) { |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$truncated = ($fractim->setIntervals(IntervalGraph::truncate($fractions, $intv1, $intv2))) |
95
|
|
|
->setBoundToString($longDateFormat); |
96
|
|
|
/* /TRUNCATED INTERVALS */ |
97
|
|
|
|
98
|
|
|
$withDates = (D::intvg([ |
99
|
|
|
[$intv1, $intv1], |
100
|
|
|
D::intv(0,4,7/10), |
101
|
|
|
[$intv2, $intv2], |
102
|
|
|
D::intv(1, 5, 3 / 10), |
103
|
|
|
D::intv(2, 3, 3 / 10), |
104
|
|
|
[$intv3, $intv3], |
105
|
|
|
])) |
106
|
|
|
->setBoundToString($longDateFormat);; |
107
|
|
|
|
108
|
|
|
$intvGraphs = []; |
109
|
|
|
foreach (range(0, 20) as $t) { |
110
|
|
|
$intervals = []; |
111
|
|
|
$j = (int)rand(3, 6); |
112
|
|
|
for ($i = 0; $i < $j; $i++) { |
113
|
|
|
$intervals[] = [D::rdm(), D::rdm(), rand(1, 9) / 10]; |
114
|
|
|
} |
115
|
|
|
$intvGraphs[] = (D::intvg($intervals))->checkIntervals(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
?> |
119
|
|
|
<!doctype html> |
120
|
|
|
<html lang="en"> |
121
|
|
|
<head> |
122
|
|
|
<title>Php IntervalGraph demo</title> |
123
|
|
|
<link rel="stylesheet" href="styles.css"> |
124
|
|
|
<script type="application/javascript" src="app.js"></script> |
125
|
|
|
</head> |
126
|
|
|
<body style="font-family: sans-serif;"> |
127
|
|
|
<header> |
128
|
|
|
<h1>PHP Interval Graph demo</h1> |
129
|
|
|
</header> |
130
|
|
|
<p> |
131
|
|
|
PHP Interval Graph is a small utility to manipulate and |
132
|
|
|
display arrays of intervals. |
133
|
|
|
</p> |
134
|
|
|
<a href="https://github.com/vctls/php-interval-graph"> |
135
|
|
|
https://github.com/vctls/php-interval-graph |
136
|
|
|
</a> |
137
|
|
|
<h2>How it works</h2> |
138
|
|
|
<p> |
139
|
|
|
Here are three overlapping date intervals. |
140
|
|
|
Each one has a linked rate, displayed as a percentage when hovering it. |
141
|
|
|
<br>They are all displayed over the same period of time, which has no rate. |
142
|
|
|
</p> |
143
|
|
|
<div style="margin-bottom: 2px"><?= $overlapped1 ?></div> |
144
|
|
|
<div style="margin-bottom: 2px"><?= $overlapped2 ?></div> |
145
|
|
|
<div style="margin-bottom: 2px"><?= $overlapped3 ?></div> |
146
|
|
|
|
147
|
|
|
<p> |
148
|
|
|
Gathered on the same graph, they are displayed as follows. |
149
|
|
|
</p> |
150
|
|
|
<?= $overlapped ?> |
151
|
|
|
|
152
|
|
|
<h2>More examples</h2> |
153
|
|
|
<p> |
154
|
|
|
Overlapping intervals with a couple null intervals.<br> |
155
|
|
|
The first null interval overlaps a non null one. |
156
|
|
|
This cuts the non null interval, while the weight remains the same.<br> |
157
|
|
|
The second null interval is implicit. |
158
|
|
|
It is simply the gap between the two last intervals. |
159
|
|
|
</p> |
160
|
|
|
<div style="margin-bottom: 2px"><?= $withNull1 ?></div> |
161
|
|
|
<div style="margin-bottom: 2px"><?= $withNull2 ?></div> |
162
|
|
|
<div style="margin-bottom: 2px"><?= $withNull3 ?></div> |
163
|
|
|
<div style="margin-bottom: 2px"><?= $withNull4 ?></div> |
164
|
|
|
<br> |
165
|
|
|
<?= $withNullIntervals ?> |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
<h2>Custom value types</h2> |
169
|
|
|
<p> |
170
|
|
|
The following graph takes arrays of two values |
171
|
|
|
and displays them as fractions.<br> |
172
|
|
|
In order to use custom value types, you need to set the custom functions |
173
|
|
|
that will aggregate the values, convert them to numeric values and strings. |
174
|
|
|
</p> |
175
|
|
|
<?= $fract ?> |
176
|
|
|
|
177
|
|
|
<p> |
178
|
|
|
The same graph, truncated between <?= $intv1->format('Y-m-d H:i:s') ?> |
179
|
|
|
and <?= $intv2->format('Y-m-d H:i:s') ?>. |
180
|
|
|
<?= $truncated ?> |
181
|
|
|
</p> |
182
|
|
|
|
183
|
|
|
<p> |
184
|
|
|
A graph with three isolated dates, shown as black bars. |
185
|
|
|
<br>One of the dates goes beyond all intervals. |
186
|
|
|
<?= $withDates ?> |
187
|
|
|
</p> |
188
|
|
|
<p> |
189
|
|
|
A bunch of random graphs, this time generated through JavaScript: |
190
|
|
|
<br> |
191
|
|
|
</p> |
192
|
|
|
<div id="random"></div> |
193
|
|
|
<script> |
194
|
|
|
'use strict'; |
195
|
|
|
const graphs = JSON.parse('<?= json_encode($intvGraphs) ?>'); |
196
|
|
|
const el = document.getElementByIU::d('random'); |
197
|
|
|
|
198
|
|
|
try { |
199
|
|
|
graphs.forEach(function (graph) { |
200
|
|
|
let html = document.createRange().createContextualFragment(intvg(graph)); |
201
|
|
|
el.appendChild(html); |
202
|
|
|
}); |
203
|
|
|
} catch (e) { |
204
|
|
|
el.innerHTML = 'The JavaScript function uses ES6 string literals. Sorry not sorry, IE.'; |
205
|
|
|
} |
206
|
|
|
</script> |
207
|
|
|
</body> |
208
|
|
|
</html> |