Series::point()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
nc 6
nop 3
dl 0
loc 17
rs 9.9666
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Series.php - Contains data to be printed in a graph.
5
 *
6
 * @package jaxon-flot
7
 * @author Thierry Feuzeu <[email protected]>
8
 * @copyright 2017 Thierry Feuzeu <[email protected]>
9
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
10
 * @link https://github.com/jaxon-php/jaxon-flot
11
 */
12
13
namespace Jaxon\Flot\Data;
14
15
use JsonSerializable;
16
use stdClass;
17
18
use function count;
19
20
class Series implements JsonSerializable
21
{
22
    /**
23
     * The points
24
     *
25
     * @var array
26
     */
27
    protected $aPoints;
28
29
    /**
30
     * The points values
31
     *
32
     * @var array
33
     */
34
    protected $aValues;
35
36
    /**
37
     * The points labels
38
     *
39
     * @var array
40
     */
41
    protected $aLabels;
42
43
    /**
44
     * The constructor.
45
     */
46
    public function __construct()
47
    {
48
        $this->aPoints = [];
49
        $this->aValues = ['data' => null, 'func' => null];
50
        $this->aLabels = ['data' => null, 'func' => null];
51
    }
52
53
    /**
54
     * Add a point to the series.
55
     *
56
     * @param integer       $iXaxis                 The point on the X axis
57
     * @param string        $sLabel                 The value on the graph
58
     *
59
     * @return static
60
     */
61
    public function point($iXaxis, $xValue, $sLabel = ''): static
62
    {
63
        $this->aPoints[] = $iXaxis;
64
        if(!$this->aValues['data'])
65
        {
66
            $this->aValues['data'] = [];
67
        }
68
        $this->aValues['data'][$iXaxis] = $xValue;
69
        if(($sLabel))
70
        {
71
            if(!$this->aLabels['data'])
72
            {
73
                $this->aLabels['data'] = [];
74
            }
75
            $this->aLabels['data'][$iXaxis] = $sLabel;
76
        }
77
        return $this;
78
    }
79
80
    /**
81
     * Add an array of points to the series.
82
     *
83
     * @param array         $aPoints                The points to be added
84
     *
85
     * @return int
86
     */
87
    public function points($aPoints): int
88
    {
89
        foreach($aPoints as $aPoint)
90
        {
91
            if(count($aPoint) === 2)
92
            {
93
                $this->point($aPoint[0], $aPoint[1]);
94
            }
95
            else if(count($aPoint) === 3)
96
            {
97
                $this->point($aPoint[0], $aPoint[1], $aPoint[2]);
98
            }
99
        }
100
        return count($this->aPoints);
101
    }
102
103
    /**
104
     * Add points to the graph series using an expression.
105
     *
106
     * @param numeric       $iStart                 The first point
107
     * @param numeric       $iEnd                   The last point
108
     * @param numeric       $iStep                  The step between next points
0 ignored issues
show
Bug introduced by
The type Jaxon\Flot\Data\numeric was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
109
     * @param string        $sJsValue               The javascript function to compute points values
110
     * @param string        $sJsLabel               The javascript function to make points labels
111
     *
112
     * The first three parameters are used in a for loop.
113
     * The x variable is used in the $sJsValue javascript function to represent each point.
114
     * The series, x and y variables are used in the $sJsLabel javascript function to
115
     * represent resp. the series label, the xaxis and graph values of the point.
116
     *
117
     * @return int
118
     */
119
    public function expr($iStart, $iEnd, $iStep, $sJsValue, $sJsLabel = ''): int
120
    {
121
        for($x = $iStart; $x < $iEnd; $x += $iStep)
122
        {
123
            $this->aPoints[] = $x;
124
        }
125
        $this->aValues['func'] = $sJsValue;
126
        if(($sJsLabel))
127
        {
128
            $this->aLabels['func'] = $sJsLabel;
129
        }
130
        return count($this->aPoints);
131
    }
132
133
    /**
134
     * Convert this object to another object more suitable for json format.
135
     *
136
     * This is a method of the JsonSerializable interface.
137
     *
138
     * @return stdClass
139
     */
140
    public function jsonSerialize(): stdClass
141
    {
142
        // Surround the js var with a special marker that will later be removed
143
        // Note: does not work when returning an array
144
        $json = new stdClass;
145
        $json->points = $this->aPoints;
146
        $json->values = $this->aValues;
147
        $json->labels = $this->aLabels;
148
        return $json;
149
    }
150
}
151