Passed
Push — master ( ead853...557cfb )
by Alexey
03:41
created

Demo::randomWalk()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 15
rs 9.6111
cc 5
nc 7
nop 2
1
<?php
2
3
namespace backend\components;
4
5
/**
6
 * Class Demo
7
 *
8
 * @package backend\components
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
    /**
119
     * Visitors Demo Data
120
     *
121
     * @return int[]
122
     */
123
    public static function getVisitorsData()
124
    {
125
        return [
126
            'US' => 398, // USA
127
            'SA' => 400, // Saudi Arabia
128
            'CA' => 1000, // Canada
129
            'DE' => 500, // Germany
130
            'FR' => 760, // France
131
            'CN' => 300, // China
132
            'AU' => 700, // Australia
133
            'BR' => 600, // Brazil
134
            'IN' => 800, // India
135
            'GB' => 320, // Great Britain
136
            'RU' => 3000 // Russia
137
        ];
138
    }
139
}
140