PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.23
E2Pdf – Export Pdf Tool for WordPress v1.32.23
1.32.49 1.32.48 1.32.43 1.32.40 1.32.34 1.32.32 1.32.31 1.32.26 1.32.22 1.32.23 1.32.18 1.32.17 1.32.15 trunk 1.00.00 1.00.13 1.01.01 1.02.02 1.03.07 1.04.07 1.05.03 1.06.02 1.07.11 1.08.00 1.08.06 All 72 releases
e2pdf / classes / helper / e2pdf-graph.php

e2pdf-graph.php in E2Pdf – Export Pdf Tool for WordPress 1.32.23, at classes/helper/e2pdf-graph.php

448 lines 21.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * E2pdf Graph Helper
5 * @copyright Copyright 2017 https://e2pdf.com
6 * @license GPLv3
7 * @version 1
8 * @link https://e2pdf.com
9 * @since 1.22.00
10 */
11 if (!defined('ABSPATH')) {
12 die('Access denied.');
13 }
14
15 class Helper_E2pdf_Graph {
16
17 private $helper;
18
19 public function __construct() {
20 $this->helper = Helper_E2pdf_Helper::instance();
21 }
22
23 public function graph($value = '', $field = array()) {
24
25 if (!$value) {
26 return '';
27 }
28
29 if (!class_exists('Goat1000\SVGGraph\SVGGraph')) {
30 require_once $this->helper->get('plugin_dir') . 'vendors/svggraph/autoloader.php';
31 }
32
33 $width = $field['width'];
34 $height = $field['height'];
35
36 $settings = [
37 'auto_fit' => false,
38 'back_stroke_width' => 0,
39 'back_stroke_colour' => '',
40 'link_base' => '/',
41 'link_target' => '_top',
42 'axis_font' => $this->get_value('text_font', 'Noto Sans Regular', $field),
43 'label_font' => $this->get_value('text_font', 'Noto Sans Regular', $field),
44 'graph_title_font' => $this->get_value('text_font', 'Noto Sans Regular', $field),
45 'legend_title_font' => $this->get_value('text_font', 'Noto Sans Regular', $field),
46 'back_colour' => $this->get_value('background', 'none', $field),
47 /* Graph */
48 'stroke_colour' => $this->get_value('g_stroke_colour', '', $field),
49 'stroke_width' => $this->get_value('g_stroke_width', '0', $field),
50 'fill_under' => $this->get_boolean('g_fill_under', $field),
51 'sort' => $this->get_boolean('g_sort', $field),
52 'percentage' => $this->get_boolean('g_percentage', $field),
53 'reverse' => $this->get_boolean('g_reverse', $field),
54 /* Title */
55 'graph_title_colour' => $this->get_value('g_graph_title_colour', '', $field),
56 'graph_title' => $this->get_value('g_graph_title', '', $field),
57 'graph_title_font_size' => $this->get_value('g_graph_title_font_size', '12', $field),
58 'graph_title_position' => $this->get_value('g_graph_title_position', 'top', $field),
59 'graph_title_space' => $this->get_value('g_graph_title_space', '0', $field),
60 /* Labels */
61 'label_colour' => $this->get_value('g_label_colour', '', $field),
62 'label_font_size' => $this->get_value('g_label_font_size', '10', $field),
63 'label_space' => $this->get_value('g_label_space', '0', $field),
64 'label_h' => $this->get_value('g_label_h', '', $field),
65 'label_v' => $this->get_value('g_label_v', '', $field),
66 'data_label_type' => $this->get_value('g_data_label_type', 'plain', $field),
67 /* Markers */
68 'marker_type' => $this->get_value('g_marker_type', 'circle', $field),
69 'marker_size' => $this->get_value('g_marker_size', '0', $field),
70 'marker_colour' => $this->get_value('g_marker_colour', '', $field),
71 /* Legend */
72 'show_legend' => $this->get_boolean('g_show_legend', $field),
73 'legend_back_colour' => $this->get_value('g_legend_back_colour', '', $field),
74 'legend_stroke_colour' => $this->get_value('g_legend_stroke_colour', '', $field),
75 'legend_stroke_width' => $this->get_value('g_legend_stroke_width', '0', $field),
76 'legend_entry_width' => $this->get_value('g_legend_entry_width', '0', $field),
77 'legend_padding_x' => $this->get_value('g_legend_padding_x', '0', $field),
78 'legend_padding_y' => $this->get_value('g_legend_padding_y', '0', $field),
79 'legend_colour' => $this->get_value('g_legend_colour', '', $field),
80 'legend_title' => $this->get_value('g_legend_title', '', $field),
81 'legend_title_font_size' => $this->get_value('g_legend_title_font_size', '', $field) === '' ? null : $this->get_value('g_legend_title_font_size', '0', $field),
82 'legend_font_size' => $this->get_value('g_legend_font_size', '0', $field),
83 'legend_position' => $this->get_value('g_legend_position_horizontal', 'right', $field) . ' ' . $this->get_value('g_legend_position_vertical', 'top', $field) . ' ' . $this->get_value('g_legend_position_horizontal_margin', '0', $field) . ' ' . $this->get_value('g_legend_position_vertical_margin', '0', $field) . ' ' . $this->get_value('g_legend_position_join', 'inner', $field),
84 'legend_text_side' => $this->get_value('g_legend_text_side', 'left', $field),
85 'legend_columns' => $this->get_value('g_legend_columns', '1', $field),
86 'legend_shadow_opacity' => '0',
87 /* Grid */
88 'grid_colour' => $this->get_value('g_grid_colour', '', $field),
89 'grid_subdivision_colour' => $this->get_value('g_grid_subdivision_colour', '', $field),
90 'minimum_grid_spacing' => $this->get_value('g_minimum_grid_spacing', '0', $field),
91 'minimum_grid_spacing_v' => $this->get_value('g_minimum_grid_spacing_v', '0', $field) > '0' ? $this->get_value('g_minimum_grid_spacing_v', '0', $field) : null,
92 'minimum_grid_spacing_h' => $this->get_value('g_minimum_grid_spacing_h', '0', $field) > '0' ? $this->get_value('g_minimum_grid_spacing_h', '0', $field) : null,
93 'grid_division_v' => $this->get_value('g_grid_division_v', '0', $field) > '0' ? $this->get_value('g_grid_division_v', '0', $field) : null,
94 'grid_division_h' => $this->get_value('g_grid_division_h', '0', $field) > '0' ? $this->get_value('g_grid_division_h', '0', $field) : null,
95 'show_grid' => $this->get_boolean('g_show_grid', $field),
96 'show_grid_subdivisions' => $this->get_boolean('g_show_grid_subdivisions', $field),
97 /* Axis */
98 'axis_colour' => $this->get_value('g_axis_colour', '', $field),
99 'axis_font_size' => $this->get_value('g_axis_font_size', '12', $field),
100 'axis_overlap' => $this->get_value('g_axis_overlap', '0', $field),
101 'show_axis_v' => $this->get_boolean('g_show_axis_v', $field),
102 'show_axis_text_v' => $this->get_boolean('g_show_axis_text_v', $field),
103 'show_axis_h' => $this->get_boolean('g_show_axis_h', $field),
104 'show_axis_text_h' => $this->get_boolean('g_show_axis_text_h', $field),
105 'show_subdivisions' => $this->get_boolean('g_show_subdivisions', $field),
106 'axis_stroke_width_v' => $this->get_value('g_axis_stroke_width_v', '0', $field),
107 'axis_stroke_width_h' => $this->get_value('g_axis_stroke_width_h', '0', $field),
108 'axis_text_position_v' => $this->get_value('g_axis_text_position_v', 'outside', $field),
109 'axis_text_position_h' => $this->get_value('g_axis_text_position_h', 'outside', $field),
110 'axis_text_space_v' => $this->get_value('g_axis_text_space_v', '2', $field),
111 'axis_text_space_h' => $this->get_value('g_axis_text_space_h', '2', $field),
112 'axis_text_offset_x_v' => $this->get_value('g_axis_text_offset_x_v', '0', $field),
113 'axis_text_offset_y_v' => $this->get_value('g_axis_text_offset_y_v', '0', $field),
114 'axis_text_offset_x_h' => $this->get_value('g_axis_text_offset_x_h', '0', $field),
115 'axis_text_offset_y_h' => $this->get_value('g_axis_text_offset_y_h', '0', $field),
116 /* Bar Labels */
117 'show_bar_labels' => $this->get_boolean('g_show_bar_labels', $field),
118 'bar_label_colour' => $this->get_value('g_bar_label_colour', '', $field),
119 'bar_label_font_size' => $this->get_value('g_bar_label_font_size', '0', $field),
120 'bar_label_position' => $this->get_value('g_bar_label_position_join', 'outer', $field) . ' ' . $this->get_value('g_bar_label_position_horizontal', 'centre', $field) . ' ' . $this->get_value('g_bar_label_position_vertical', 'top', $field),
121 'bar_label_space' => $this->get_value('g_bar_label_space', '0', $field),
122 'units_x' => $this->get_value('g_units_x', '', $field),
123 'units_y' => $this->get_value('g_units_y', '', $field),
124 'units_label' => $this->get_value('g_units_label', '', $field),
125 /* Additional */
126 'bubble_scale' => $this->get_value('g_bubble_scale', '1', $field),
127 'increment' => $this->get_value('g_increment', '0', $field),
128 'stack_group' => $this->get_value('g_stack_group', '0', $field),
129 'project_angle' => $this->get_value('g_project_angle', '0', $field),
130 'depth' => $this->get_value('g_depth', '40', $field),
131 'depth_shade_gradient' => array("black:0.2", "black:0.2", "black:0.2", "h"),
132 'aspect_ratio' => $this->get_value('g_aspect_ratio', '1', $field),
133 'bar_width' => $this->get_value('g_bar_width', '0', $field),
134 'bar_width_min' => $this->get_value('g_bar_width_min', '1', $field),
135 ];
136
137 if ($this->is_exists('g_legend_entry_height', $field)) {
138 $settings['legend_entry_height'] = $this->get_value('g_legend_entry_height', '0', $field);
139 } else {
140 $settings['legend_entry_height'] = $this->get_value('g_legend_entry_height', '20', $field);
141 }
142
143 if ($this->is_exists('g_bar_space', $field)) {
144 $settings['bar_space'] = $this->get_value('g_bar_space', '0', $field);
145 } else {
146 $settings['bar_space'] = $this->get_value('g_bar_space', '10', $field);
147 }
148
149 if (!$this->is_empty('g_axis_text_align_v', $field)) {
150 $settings['axis_text_align_v'] = $this->get_value('g_axis_text_align_v', '', $field);
151 }
152
153 if (!$this->is_empty('g_axis_text_align_h', $field)) {
154 $settings['axis_text_align_h'] = $this->get_value('g_axis_text_align_h', '', $field);
155 }
156
157 if (!$this->is_empty('g_line_curve', $field)) {
158 $settings['line_curve'] = $this->get_value('g_line_curve', '', $field);
159 }
160
161 if (!$this->is_empty('g_line_dataset', $field)) {
162 $settings['line_dataset'] = $this->get_array('g_line_dataset', '', $field);
163 }
164
165 if ($this->get_boolean('g_axis_min_max_h', $field)) {
166 $settings['axis_min_h'] = $this->get_value('g_axis_min_h', '0', $field);
167 $settings['axis_max_h'] = $this->get_value('g_axis_max_h', '0', $field);
168 }
169
170 if ($this->get_boolean('g_axis_min_max_v', $field)) {
171 $settings['axis_min_v'] = $this->get_value('g_axis_min_v', '0', $field);
172 $settings['axis_max_v'] = $this->get_value('g_axis_max_v', '0', $field);
173 }
174
175 /**
176 * Padding
177 */
178 $settings['pad_top'] = $this->get_value('margin_top', '0', $field);
179 $settings['pad_left'] = $this->get_value('margin_left', '0', $field);
180 $settings['pad_right'] = $this->get_value('margin_right', '0', $field);
181 $settings['pad_bottom'] = $this->get_value('margin_bottom', '0', $field);
182
183 $separators = array(
184 'key' => ' => ',
185 'array' => ', ',
186 'sub_array' => '|',
187 );
188 if (!$this->is_empty('g_key_sep', $field)) {
189 $separators['key'] = $field['properties']['g_key_sep'];
190 }
191 if (!$this->is_empty('g_array_sep', $field)) {
192 $separators['array'] = $field['properties']['g_array_sep'];
193 }
194 if (!$this->is_empty('g_sub_array_sep', $field)) {
195 $separators['sub_array'] = $field['properties']['g_sub_array_sep'];
196 }
197
198 $data = $this->get_graph_data(apply_filters('e2pdf_helper_graph_pre_data', $value, $field), $separators, $this->get_value('g_multiline', '0', $field));
199 $data = apply_filters('e2pdf_helper_graph_pre_data', $data, $field);
200 if ($this->get_boolean('g_reverse_data', $field) && is_array($data)) {
201 $data = array_reverse($data);
202 }
203
204 $single_array_conversion = array(
205 'StackedBarGraph',
206 'StackedBar3DGraph',
207 'StackedCylinderGraph',
208 'HorizontalStackedBarGraph',
209 'HorizontalStackedBar3DGraph',
210 'StackedBarAndLineGraph',
211 'StackedLineGraph',
212 'GroupedBarGraph',
213 'GroupedBar3DGraph',
214 'GroupedCylinderGraph',
215 'HorizontalGroupedBarGraph',
216 'HorizontalGroupedBar3DGraph',
217 'StackedGroupedBarGraph',
218 'StackedGroupedBar3DGraph',
219 'StackedGroupedCylinderGraph',
220 'BarAndLineGraph',
221 'Histogram',
222 'LineGraph',
223 'RadarGraph',
224 'ScatterGraph',
225 'SteppedLineGraph',
226 'MultiLineGraph',
227 'MultiSteppedLineGraph',
228 'MultiScatterGraph',
229 'MultiRadarGraph',
230 'StackedBarAndLineGraph',
231 'ParetoChart',
232 'PopulationPyramid'
233 );
234 if (in_array($this->get_value('g_type', 'BarGraph', $field), $single_array_conversion) && $this->get_value('g_multiline', '0', $field) == '2') {
235 $new_data = array();
236 foreach ($data as $key => $tmp) {
237 if (is_array($tmp)) {
238 foreach ($tmp as $sub_key => $sub_tmp) {
239 $new_data[$sub_key][$key] = $sub_tmp;
240 }
241 } else {
242 $new_data[0][$key] = $tmp;
243 }
244 }
245 $data = $new_data;
246 }
247
248 /*
249 * Structured Data
250 */
251 if ($this->get_value('g_structured_data', '0', $field)) {
252 $settings['structured_data'] = true;
253 $structure = array();
254 if (!$this->is_empty('g_structure_key', $field)) {
255 $structure['key'] = $this->get_value('g_structure_key', '0', $field);
256 }
257 if (!$this->is_empty('g_structure_value', $field)) {
258 $structure['value'] = $this->get_array('g_structure_value', '0', $field);
259 }
260 if (!$this->is_empty('g_structure_label', $field)) {
261 $structure['label'] = $this->get_array('g_structure_label', '0', $field);
262 $settings['show_data_labels'] = true;
263 }
264 if (!$this->is_empty('g_structure_legend_text', $field)) {
265 $structure['legend_text'] = $this->get_array('g_structure_legend_text', '0', $field);
266 }
267 if (!$this->is_empty('g_structure_area', $field)) {
268 $structure['area'] = $this->get_array('g_structure_area', '0', $field);
269 }
270 if (!$this->is_empty('g_structure_open', $field)) {
271 $structure['open'] = $this->get_array('g_structure_open', '0', $field);
272 }
273 if (!$this->is_empty('g_structure_end', $field)) {
274 $structure['end'] = $this->get_array('g_structure_end', '0', $field);
275 }
276 if (!$this->is_empty('g_structure_outliers', $field)) {
277 $structure['outliers'] = $this->get_array('g_structure_outliers', '0', $field);
278 }
279 if (!$this->is_empty('g_structure_top', $field)) {
280 $structure['top'] = $this->get_array('g_structure_top', '0', $field);
281 }
282 if (!$this->is_empty('g_structure_bottom', $field)) {
283 $structure['bottom'] = $this->get_array('g_structure_bottom', '0', $field);
284 }
285 if (!$this->is_empty('g_structure_wtop', $field)) {
286 $structure['wtop'] = $this->get_array('g_structure_wtop', '0', $field);
287 }
288 if (!$this->is_empty('g_structure_wbottom', $field)) {
289 $structure['wbottom'] = $this->get_array('g_structure_wbottom', '0', $field);
290 }
291 if (!$this->is_empty('g_structure_high', $field)) {
292 $structure['high'] = $this->get_array('g_structure_high', '0', $field);
293 }
294 if (!$this->is_empty('g_structure_low', $field)) {
295 $structure['low'] = $this->get_array('g_structure_low', '0', $field);
296 }
297 $settings['structure'] = $structure;
298 }
299
300 if (!$this->get_boolean('g_structured_data', $field) && $this->get_boolean('g_show_legend', $field)) {
301 $legends = array();
302 if ($this->get_value('g_multiline', '0', $field) == '1') {
303 if (isset($data[0]) && is_array($data[0])) {
304 $legends = $data[0];
305 }
306 } elseif (is_array($data)) {
307 $legends = $data;
308 }
309 $settings['legend_entries'] = array_keys($legends);
310 }
311
312 if (!$this->is_empty('g_legends', $field)) {
313 $tmp_legends = explode("\r\n", $this->get_value('g_legends', '', $field));
314 $legends = array();
315 foreach ($tmp_legends as $legend) {
316 $legends[] = implode("\n", explode('\n', $legend));
317 }
318 $settings['legend_entries'] = $legends;
319 }
320
321 $count = max(count($data), 1);
322 foreach ($data as $sub_data) {
323 if (is_array($sub_data)) {
324 $count = max($count, count($sub_data));
325 }
326 }
327 $colors = array();
328 if (!$this->is_empty('g_colors', $field)) {
329 $tmp_colors = explode("\r\n", $this->get_value('g_colors', '', $field));
330 foreach ($tmp_colors as $color) {
331 $colors[] = $this->get_array_value($color, '');
332 }
333 } else {
334 $hue = 0;
335 $color_change = 360 / $count;
336 for ($i = 0; $i < $count; $i++) {
337 $colors[] = array($this->get_value('g_palette', '#1e73be', $field) . '/hue(' . $hue . ')');
338 $hue += $color_change;
339 }
340 }
341
342 $colors = apply_filters('e2pdf_helper_graph_colors', $colors, $field);
343 if ($this->get_boolean('g_stroke_dynamic_colour', $field)) {
344 $settings['stroke_colour'] = apply_filters('e2pdf_helper_graph_stroke_colors', $colors, $field);
345 }
346 if ($this->get_boolean('g_marker_dynamic_colour', $field)) {
347 $settings['marker_colour'] = apply_filters('e2pdf_helper_graph_marker_colors', $colors, $field);
348 }
349
350 $graph = new Goat1000\SVGGraph\SVGGraph($width, $height, apply_filters('e2pdf_helper_graph_settings', $settings, $field));
351 $graph->colours($colors);
352 $graph->values(apply_filters('e2pdf_helper_graph_data', $data, $field));
353 try {
354 $svg = $graph->fetch($this->get_value('g_type', 'BarGraph', $field));
355 if (false !== strpos($svg, 'Goat1000')) {
356 $svg = preg_replace('/Goat1000\\\(.[^\s]*)\s/', 'Graph ', $svg);
357 }
358 return base64_encode($svg); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
359 } catch (Exception $e) {
360 return '';
361 }
362 }
363
364 public function get_graph_data($value, $separators = array(), $multiline = false) {
365 $data = array();
366 if ($multiline != '0') {
367 $lines = preg_split('/\r\n|\n/', $value);
368 foreach ($lines as $key => $line) {
369 $line_data = $this->get_line_data(trim($line), $separators);
370 if ($multiline == '1') {
371 $data[] = $line_data;
372 } elseif ($multiline == '2') {
373 $data = array_merge($data, $line_data);
374 }
375 }
376 } else {
377 $data = $this->get_line_data($value, $separators);
378 }
379 return $data;
380 }
381
382 public function get_line_data($line, $separators = array()) {
383 $value = array();
384 $data = explode($separators['array'], $line);
385 foreach ($data as $line_data) {
386 $key = null;
387 if (strpos($line_data, $separators['key']) !== false) {
388 list($key, $line_data) = explode($separators['key'], $line_data, 2);
389 }
390 if (strpos($line_data, $separators['sub_array']) !== false) {
391 if ($key !== null) {
392 $value[$key] = explode($separators['sub_array'], $line_data);
393 } else {
394 $value[] = explode($separators['sub_array'], $line_data);
395 }
396 } else {
397 if ($key !== null) {
398 if ($line_data === '') {
399 $value[$key] = null;
400 } else {
401 $value[$key] = $line_data;
402 }
403 } else {
404 if ($line_data === '') {
405 $value[] = null;
406 } else {
407 $value[] = $line_data;
408 }
409 }
410 }
411 }
412 return $value;
413 }
414
415 public function is_empty($key = '', $field = array()) {
416 return isset($field['properties'][$key]) && $field['properties'][$key] !== '' ? false : true;
417 }
418
419 public function is_exists($key = '', $field = array()) {
420 return isset($field['properties'][$key]) ? true : false;
421 }
422
423 public function get_value($key = '', $default = '', $field = array()) {
424 return isset($field['properties'][$key]) && $field['properties'][$key] ? $field['properties'][$key] : $default;
425 }
426
427 public function get_array($key = '', $default = '', $field = array()) {
428 $value = isset($field['properties'][$key]) && $field['properties'][$key] ? $field['properties'][$key] : '';
429 if ($value && (strpos($value, ',') !== false)) {
430 return $value = array_map('trim', explode(',', $value));
431 } else {
432 return $value ? $value : $default;
433 }
434 }
435
436 public function get_array_value($value = '', $default = '') {
437 if ($value && (strpos($value, ',') !== false)) {
438 return $value = array_map('trim', explode(',', $value));
439 } else {
440 return $value ? $value : $default;
441 }
442 }
443
444 public function get_boolean($key = '', $field = array()) {
445 return isset($field['properties'][$key]) && $field['properties'][$key] ? true : false;
446 }
447 }
448