KD2 Framework  Check-in [be46854133]

Overview
Comment:Improve SVG graphs
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | 7.3
Files: files | file ages | folders
SHA1: be4685413364204e9a205ebdf1ff63020869115c
User & Date: bohwaz on 2020-10-21 12:54:16
Other Links: branch diff | manifest | tags
Context
2020-10-24
19:01
HTMLDocument: make sure the selector is a string check-in: 666cb9fae0 user: bohwaz tags: 7.3
2020-10-21
12:54
Improve SVG graphs check-in: be46854133 user: bohwaz tags: 7.3
2020-10-14
20:22
ErrorManager: rewrite headers to bypass content-disposition and other stuff, manage iterators in stack trace check-in: c088059e8c user: bohwaz tags: 7.3
Changes

Modified src/lib/KD2/Graphics/SVG/Pie.php from [f503c92675] to [2b3bc9ce3a].

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

	public function __construct($width = 600, $height = 400)
	{
		$this->width = (int) $width;
		$this->height = (int) $height;
	}

	public function add(SVGPie_Data $data)
	{
		$this->data[] = $data;
		return true;
	}

	public function setTitle($title)
	{







|







31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

	public function __construct($width = 600, $height = 400)
	{
		$this->width = (int) $width;
		$this->height = (int) $height;
	}

	public function add(Pie_Data $data)
	{
		$this->data[] = $data;
		return true;
	}

	public function setTitle($title)
	{
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
		}

		$out .= '</svg>';
		return $out;
	}
}

class SVGPie_Data
{
	public $fill = 'blue';
	public $data = 0.0;
	public $label = null;

	public function __construct($data, $label = null, $fill = 'blue')
	{
		$this->data = $data;
		$this->fill = $fill;
		$this->label = $label;
	}
}

?>







|












<
<
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168


		}

		$out .= '</svg>';
		return $out;
	}
}

class Pie_Data
{
	public $fill = 'blue';
	public $data = 0.0;
	public $label = null;

	public function __construct($data, $label = null, $fill = 'blue')
	{
		$this->data = $data;
		$this->fill = $fill;
		$this->label = $label;
	}
}


Modified src/lib/KD2/Graphics/SVG/Plot.php from [b2b63c6a5f] to [a564ed5558].

25
26
27
28
29
30
31

32
33
34
35
36
37
38
{
	protected $width = null;
	protected $height = null;
	protected $data = array();
	protected $title = null;
	protected $labels = array();
	protected $legend = true;


	public function __construct($width = 600, $height = 400)
	{
		$this->width = (int) $width;
		$this->height = (int) $height;
	}








>







25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{
	protected $width = null;
	protected $height = null;
	protected $data = array();
	protected $title = null;
	protected $labels = array();
	protected $legend = true;
	protected $count, $min, $max, $margin_top, $margin_left;

	public function __construct($width = 600, $height = 400)
	{
		$this->width = (int) $width;
		$this->height = (int) $height;
	}

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

	public function setLabels($labels)
	{
		$this->labels = $labels;
		return true;
	}

	public function add(SVGPlot_Data $data)
	{
		$this->data[] = $data;
		return true;
	}

	public function display()
	{







|







50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

	public function setLabels($labels)
	{
		$this->labels = $labels;
		return true;
	}

	public function add(Plot_Data $data)
	{
		$this->data[] = $data;
		return true;
	}

	public function display()
	{
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160

161
162



163
164
165
166
167







168
169
170
171
172
173

174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

232




233
234
235
236
237
238
239
240
		$out = '';

		if (empty($this->data))
		{
			return $out;
		}

		// Figure out the maximum Y-axis value
		$max_value = 0;
		$nb_elements = 0;

		foreach ($this->data as $row)
		{
			if (count($row->get()) < 1)
				continue;

			if ($max_value == 0)
			{

				$nb_elements = count($row->get());
			}

			$max = max($row->get());

			if ($max > $max_value)
			{
				$max_value = $max;
			}
		}

		if ($nb_elements < 1)
		{
			return $out;
		}

		$divide = round($max_value / ($this->height * 0.8), 2) ?: 1;
		$y_axis_val = ceil(abs($max_value) / ($this->height * 0.8)) * 50;
		$space = round(($this->width - ($this->width * 0.1)) / $nb_elements, 2);

		for ($i = 0; $i < 10; $i++)

		{
			if (($y_axis_val * $i) <= $max_value)



			{
				$line_y = ($this->height * 0.93) - (($y_axis_val / $divide) * $i);
				$out .= '<line x1="'.($this->width * 0.1).'" y1="'.($line_y).'" x2="'.$this->width.'" y2="'.($line_y).'" stroke-width="1" stroke="#ccc" />' . PHP_EOL;
				$out .= '<g><text x="'.($this->width * 0.08).'" y="'.($line_y).'" font-size="'.($this->height * 0.04).'" fill="gray" text-anchor="end" style="font-family: Verdana, Arial, sans-serif;">'.($y_axis_val * $i).'</text></g>' . PHP_EOL;
			}







		}

		// X-axis lines
		$y = $this->height - ($this->height * 0.07);
		$x = $this->width * 0.1;
		$i = 0;


		foreach ($this->data[0]->get() as $k=>$v)
		{
			if ($x >= $this->width)
				break;

			$out .= '<line x1="'.$x.'" y1="'.($y).'" x2="'.$x.'" y2="'.($this->height * 0.1).'" stroke-width="1" stroke="#ccc" />' . PHP_EOL;
			$x += $space + $this->data[0]->width;
		}

		if (!empty($this->labels))
		{
			// labels for x axis
			$y = $this->height - ($this->height * 0.07);
			$i = 0;
			$step = max(1, round($nb_elements / 5));

			for ($i = 0; $i <= $nb_elements; $i += $step)
			{
				$x = ($i * ($space + $this->data[0]->width)) + ($this->width * 0.1);

				if ($x >= $this->width)
					break;

				if (isset($this->labels[$i]))
				{
					$out .= '<g><text x="'.$x.'" y="'.($y+($this->height * 0.06)).'" '
						.	'font-size="'.($this->height * 0.04).'" fill="gray" text-anchor="middle" '
						.	'style="font-family: Verdana, Arial, sans-serif;">'
						.	$this->encodeText($this->labels[$i]).'</text></g>' . PHP_EOL;
				}
			}
		}

		$y = ($this->height * 0.1);
		$w = $this->width - ($this->width * 0.1);
		$h = $this->height - ($this->height * 0.17);

		foreach ($this->data as $row)
		{
			$out .= '<polyline fill="none" stroke="'.$row->color.'" stroke-width="'.$row->width.'" '
				.'stroke-linecap="round" points="';

			$x = ($this->width * 0.1);

			foreach ($row->get() as $k=>$v)
			{
				$_y = $y + ($h - round($v / $divide, 2)) + round($row->width / 2);
				$out.= $x.','.$_y.' ';
				$x += $space + $row->width;
			}

			$out .= '" />' . PHP_EOL;
		}

		return $out;
	}
}






class SVGPlot_Data
{
	public $color = 'blue';
	public $width = '10';
	public $title = null;
	protected $data = array();

	public function __construct($data)







|
<
<
<


|

|
<
|
>
|


|
|
<
<
<
|
|
|
<
<



|
|
|

|
>
|
<
>
>
>
|
|
<
<
|
>
>
>
>
>
>
>



|


>






|
|





|

|

|

|














<
<







|

|

|
|
<







|
>
|
>
>
>
>
|







123
124
125
126
127
128
129
130



131
132
133
134
135

136
137
138
139
140
141
142



143
144
145


146
147
148
149
150
151
152
153
154
155

156
157
158
159
160


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209


210
211
212
213
214
215
216
217
218
219
220
221
222

223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
		$out = '';

		if (empty($this->data))
		{
			return $out;
		}

		// Figure out the minimum/maximum Y-axis value



		foreach ($this->data as $row)
		{
			if (count($row->get()) < 1) {
				continue;
			}


			if (null === $this->count) {
				$this->count = count($row->get());
			}

			$this->max = max((int)$this->max, max($row->get()));
			$this->min = min((int)$this->min, min($row->get()));



		}

		if ($this->count < 1) {


			return $out;
		}

		$this->margin_left = $this->width * 0.1;
		$this->margin_top = $this->height * 0.1;
		$column_space = ($this->width - $this->margin_left) / ($this->count - 1);

		$lines = [];
		$step = ($this->max - $this->min) / 7;


		for ($i = $this->min; $i < $this->max; $i += $step) {
			$lines[] = round($i);
		}

		$lines[] = $this->max;



		// Horizontal lines and Y axis legends
		foreach ($lines as $k => $v) {
			$v = ($k*($this->max-$this->min))/count($lines);
			$v += $this->min;
			$y = $this->y($v);
			$out .= sprintf('<line x1="%f" y1="%f" x2="%f" y2="%f" stroke-width="1" stroke="#ccc" />' . PHP_EOL, $this->margin_left, $y, $this->width, $y);
			$out .= sprintf('<g><text x="%f" y="%f" font-size="%f" fill="gray" text-anchor="end" style="font-family: Verdana, Arial, sans-serif;">%s</text></g>' . PHP_EOL, $this->width * 0.08, $y, $this->height * 0.04, round($v));
		}

		// X-axis lines
		$y = 10 + $this->height - ($this->margin_top);
		$x = $this->width * 0.1;
		$i = 0;
		$step = max(1, round($this->count / ($this->width / 50)));

		foreach ($this->data[0]->get() as $k=>$v)
		{
			if ($x >= $this->width)
				break;

			$out .= sprintf('<line x1="%d" y1="%d" x2="%d" y2="%d" stroke-width="1" stroke="%s" />', $x, $y, $x, 0, !($i++ % $step) ? '#ccc' : '#eee');
			$x += $column_space + $this->data[0]->width;
		}

		if (!empty($this->labels))
		{
			// labels for x axis
			$y = $this->height - $this->margin_top + 10;
			$i = 0;
			$step = max(1, round($this->count / ($this->width / 50)));

			for ($i = 0; $i <= $this->count; $i += $step)
			{
				$x = ($i * ($column_space + $this->data[0]->width)) + ($this->width * 0.1);

				if ($x >= $this->width)
					break;

				if (isset($this->labels[$i]))
				{
					$out .= '<g><text x="'.$x.'" y="'.($y+($this->height * 0.06)).'" '
						.	'font-size="'.($this->height * 0.04).'" fill="gray" text-anchor="middle" '
						.	'style="font-family: Verdana, Arial, sans-serif;">'
						.	$this->encodeText($this->labels[$i]).'</text></g>' . PHP_EOL;
				}
			}
		}



		$h = $this->height - ($this->height * 0.17);

		foreach ($this->data as $row)
		{
			$out .= '<polyline fill="none" stroke="'.$row->color.'" stroke-width="'.$row->width.'" '
				.'stroke-linecap="round" points="';

			$i = 0;

			foreach ($row->get() as $v)
			{
				$x = $this->margin_left + ($column_space * $i++);
				$out.= sprintf('%f,%f ', $x, $this->y($v));

			}

			$out .= '" />' . PHP_EOL;
		}

		return $out;
	}

	protected function y($value)
	{
		return 10 + $this->height - $this->margin_top - (($value - $this->min)*($this->height - $this->margin_top))/(($this->max - $this->min)?:1);
	}
}

class Plot_Data
{
	public $color = 'blue';
	public $width = '10';
	public $title = null;
	protected $data = array();

	public function __construct($data)