1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace backend\widgets\chart\flot\data; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Demo |
7
|
|
|
* |
8
|
|
|
* @package modules\main\models\backend |
9
|
|
|
*/ |
10
|
|
|
class Demo |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Demo Random Data |
14
|
|
|
* |
15
|
|
|
* @param array|null $data |
16
|
|
|
* @param int $totalPoints |
17
|
|
|
* @return array |
18
|
|
|
*/ |
19
|
|
|
public static function getRandomData($data = [], $totalPoints = 100) |
20
|
|
|
{ |
21
|
|
|
if (is_null($data)) { |
22
|
|
|
$data = []; |
23
|
|
|
} |
24
|
|
|
if (count($data) > 0) { |
25
|
|
|
$data = array_slice($data, 1); |
26
|
|
|
} |
27
|
|
|
$randomWalk = self::randomWalk($data, $totalPoints); |
28
|
|
|
// Zip the generated y values with the x values |
29
|
|
|
$res = []; |
30
|
|
|
foreach ($randomWalk as $key => $items) { |
31
|
|
|
$items[0] = $key; |
32
|
|
|
$res[] = $items; |
33
|
|
|
} |
34
|
|
|
return $res; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Do a random walk |
39
|
|
|
* |
40
|
|
|
* @param array $data |
41
|
|
|
* @param int $totalPoints |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
public static function randomWalk($data = [], $totalPoints = 100) |
45
|
|
|
{ |
46
|
|
|
$count = count($data); |
47
|
|
|
while ($count < $totalPoints) { |
48
|
|
|
$prev = $count > 0 ? (int)$data[$count - 1] : $totalPoints / 2; |
49
|
|
|
$val = $prev + self::randomFloat(0, $totalPoints); |
50
|
|
|
if ($val < 0) { |
51
|
|
|
$val = 0; |
52
|
|
|
} elseif ($val > $totalPoints) { |
53
|
|
|
$val = $totalPoints; |
54
|
|
|
} |
55
|
|
|
$data[] = [$count, $val]; |
56
|
|
|
$count++; |
57
|
|
|
} |
58
|
|
|
return $data; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Calculating a random floating point number |
63
|
|
|
* |
64
|
|
|
* @param int $min |
65
|
|
|
* @param int $max |
66
|
|
|
* @return float|int|mixed |
67
|
|
|
*/ |
68
|
|
|
public static function randomFloat($min = 0, $max = 1) |
69
|
|
|
{ |
70
|
|
|
return $min + mt_rand() / mt_getrandmax() * ($max - $min); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get Sin Demo Data |
75
|
|
|
* |
76
|
|
|
* @param float $limit |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public static function getSin($limit = 0.25) |
80
|
|
|
{ |
81
|
|
|
$data = []; |
82
|
|
|
for ($i = 0; $i < M_PI * 2; $i += $limit) { |
83
|
|
|
$data[] = [$i, sin($i)]; |
84
|
|
|
} |
85
|
|
|
return $data; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get Cos Demo Data |
90
|
|
|
* |
91
|
|
|
* @param float $limit |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
public static function getCos($limit = 0.25) |
95
|
|
|
{ |
96
|
|
|
$data = []; |
97
|
|
|
for ($i = 0; $i < M_PI * 2; $i += $limit) { |
98
|
|
|
$data[] = [$i, cos($i)]; |
99
|
|
|
} |
100
|
|
|
return $data; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get Tan Demo Data |
105
|
|
|
* |
106
|
|
|
* @param float $limit |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
public static function getTan($limit = 0.1) |
110
|
|
|
{ |
111
|
|
|
$data = []; |
112
|
|
|
for ($i = 0; $i < M_PI * 2; $i += $limit) { |
113
|
|
|
$data[] = [$i, tan($i)]; |
114
|
|
|
} |
115
|
|
|
return $data; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|