KD2 Framework  Check-in [fc76ab5c45]

Overview
Comment:Plot: Improve plot axis
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | 7.3
Files: files | file ages | folders
SHA1: fc76ab5c45778a69aa2a2b658a8708de46de5ab1
User & Date: bohwaz on 2020-11-13 02:24:24
Other Links: branch diff | manifest | tags
Context
2020-11-13
02:24
Image: Fix issues with compression and Imagick check-in: f895c67fd2 user: bohwaz tags: 7.3
02:24
Plot: Improve plot axis check-in: fc76ab5c45 user: bohwaz tags: 7.3
2020-11-11
21:57
Query builder: fix issues with IN operator check-in: 9be40e9fa2 user: bohwaz tags: 7.3
Changes

Modified src/lib/KD2/Graphics/SVG/Plot.php from [ce05b90b99] to [66214b7235].

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

		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) ?: 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);

			if ($k > 0 && $step > 100) {
				$v = round($v / 100) * 100;
				//$v = $k < count($lines) - 1;
			}

			$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);
	}
}








|

>
>
>

|

|
|


>
|



<
<
<


<
<
<
<
<

>




>
>
|
>
>
>
>
>

<






|
<
|
|
<
|
<
|
<
|
|
<
<
<
|
<
<

|
|
<
<
<
<
<
<
>













|








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
245
246
247
248

		if ($this->count < 1) {
			return $out;
		}

		$this->margin_left = $this->width * 0.1;
		$this->margin_top = $this->height * 0.1;


		$range = $this->max - $this->min;
		$step = $this->stepValue($range, 7);

		$lines = [];
		$min = round($this->min / $step)*$step;

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

		$y = 10 + $this->height - $this->margin_top;
		$axis_height = $y / count($lines);

		// Horizontal lines and Y axis legends
		foreach ($lines as $k => $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));
			$y -= $axis_height + 1;
		}

		// X-axis lines
		$y = 10 + $this->height - ($this->margin_top);
		$x = $this->margin_left;

		$axis_width = $this->width - $x;
		$column_width = 70 + $this->data[0]->width;
		$nb_items = ceil($axis_width / $column_width);
		$item_width = $axis_width / $this->count;
		$step = max(1, $this->count / $nb_items);

		$i = 0;


		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');


			if (!($i % $step) && isset($this->labels[$i+1]))

			{

				$label = $this->encodeText($this->labels[$i+1]);

				$anchor = $x >= ($this->width - ($column_width / 3)) ? 'end': 'middle';
				$out .= sprintf('<g><text x="%f" y="%f" font-size="%s" fill="gray" text-anchor="%s" style="font-family: Verdana, Arial, sans-serif;">%s</text></g>' . PHP_EOL, $x, $y+($this->height * 0.06), $this->height * 0.04, $anchor, $label);



			}



			$i++;







			$x += $item_width;
		}

		$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 + ($item_width * $i++);
				$out.= sprintf('%f,%f ', $x, $this->y($v));
			}

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

		return $out;
	}

	protected function stepValue(float $range, float $targetSteps)
	{
		$tempStep = $range / $targetSteps;
		$mag = floor(log10($tempStep));
		$magPow = pow(10, $mag);
		$magMsd = (int)($tempStep/$magPow + 0.5);

		if ($magMsd > 5)
			$magMsd = 10;
		else if ($magMsd > 2)
			$magMsd = 5;
		else if ($magMsd > 1)
			$magMsd = 2;

		return $magMsd*$magPow;
	}

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