PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.32
E2Pdf – Export Pdf Tool for WordPress v1.32.32
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.32, at classes/helper/e2pdf-graph.php

458 lines 22.5 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 'show_labels' => $this->is_exists('g_show_labels', $field) ? $this->get_boolean('g_show_labels', $field) : true,
136 'show_label_key' => $this->is_exists('g_show_label_key', $field) ? $this->get_boolean('g_show_label_key', $field) : true,
137 'show_label_percent' => $this->get_boolean('g_show_label_percent', $field),
138 'show_label_amount' => $this->get_boolean('g_show_label_amount', $field),
139 'inner_radius' => $this->get_number('g_inner_radius', '0.5', $field),
140 'start_angle' => $this->get_value('g_start_angle', '0', $field),
141 ];
142
143 if ($this->is_exists('g_legend_entry_height', $field)) {
144 $settings['legend_entry_height'] = $this->get_value('g_legend_entry_height', '0', $field);
145 } else {
146 $settings['legend_entry_height'] = $this->get_value('g_legend_entry_height', '20', $field);
147 }
148
149 if ($this->is_exists('g_bar_space', $field)) {
150 $settings['bar_space'] = $this->get_value('g_bar_space', '0', $field);
151 } else {
152 $settings['bar_space'] = $this->get_value('g_bar_space', '10', $field);
153 }
154
155 if (!$this->is_empty('g_axis_text_align_v', $field)) {
156 $settings['axis_text_align_v'] = $this->get_value('g_axis_text_align_v', '', $field);
157 }
158
159 if (!$this->is_empty('g_axis_text_align_h', $field)) {
160 $settings['axis_text_align_h'] = $this->get_value('g_axis_text_align_h', '', $field);
161 }
162
163 if (!$this->is_empty('g_line_curve', $field)) {
164 $settings['line_curve'] = $this->get_value('g_line_curve', '', $field);
165 }
166
167 if (!$this->is_empty('g_line_dataset', $field)) {
168 $settings['line_dataset'] = $this->get_array('g_line_dataset', '', $field);
169 }
170
171 if ($this->get_boolean('g_axis_min_max_h', $field)) {
172 $settings['axis_min_h'] = $this->get_value('g_axis_min_h', '0', $field);
173 $settings['axis_max_h'] = $this->get_value('g_axis_max_h', '0', $field);
174 }
175
176 if ($this->get_boolean('g_axis_min_max_v', $field)) {
177 $settings['axis_min_v'] = $this->get_value('g_axis_min_v', '0', $field);
178 $settings['axis_max_v'] = $this->get_value('g_axis_max_v', '0', $field);
179 }
180
181 /**
182 * Padding
183 */
184 $settings['pad_top'] = $this->get_value('margin_top', '0', $field);
185 $settings['pad_left'] = $this->get_value('margin_left', '0', $field);
186 $settings['pad_right'] = $this->get_value('margin_right', '0', $field);
187 $settings['pad_bottom'] = $this->get_value('margin_bottom', '0', $field);
188
189 $separators = array(
190 'key' => ' => ',
191 'array' => ', ',
192 'sub_array' => '|',
193 );
194 if (!$this->is_empty('g_key_sep', $field)) {
195 $separators['key'] = $field['properties']['g_key_sep'];
196 }
197 if (!$this->is_empty('g_array_sep', $field)) {
198 $separators['array'] = $field['properties']['g_array_sep'];
199 }
200 if (!$this->is_empty('g_sub_array_sep', $field)) {
201 $separators['sub_array'] = $field['properties']['g_sub_array_sep'];
202 }
203
204 $data = $this->get_graph_data(apply_filters('e2pdf_helper_graph_pre_data', $value, $field), $separators, $this->get_value('g_multiline', '0', $field));
205 $data = apply_filters('e2pdf_helper_graph_pre_data', $data, $field);
206 if ($this->get_boolean('g_reverse_data', $field) && is_array($data)) {
207 $data = array_reverse($data);
208 }
209
210 $single_array_conversion = array(
211 'StackedBarGraph',
212 'StackedBar3DGraph',
213 'StackedCylinderGraph',
214 'HorizontalStackedBarGraph',
215 'HorizontalStackedBar3DGraph',
216 'StackedBarAndLineGraph',
217 'StackedLineGraph',
218 'GroupedBarGraph',
219 'GroupedBar3DGraph',
220 'GroupedCylinderGraph',
221 'HorizontalGroupedBarGraph',
222 'HorizontalGroupedBar3DGraph',
223 'StackedGroupedBarGraph',
224 'StackedGroupedBar3DGraph',
225 'StackedGroupedCylinderGraph',
226 'BarAndLineGraph',
227 'Histogram',
228 'LineGraph',
229 'RadarGraph',
230 'ScatterGraph',
231 'SteppedLineGraph',
232 'MultiLineGraph',
233 'MultiSteppedLineGraph',
234 'MultiScatterGraph',
235 'MultiRadarGraph',
236 'StackedBarAndLineGraph',
237 'ParetoChart',
238 'PopulationPyramid'
239 );
240 if (in_array($this->get_value('g_type', 'BarGraph', $field), $single_array_conversion) && $this->get_value('g_multiline', '0', $field) == '2') {
241 $new_data = array();
242 foreach ($data as $key => $tmp) {
243 if (is_array($tmp)) {
244 foreach ($tmp as $sub_key => $sub_tmp) {
245 $new_data[$sub_key][$key] = $sub_tmp;
246 }
247 } else {
248 $new_data[0][$key] = $tmp;
249 }
250 }
251 $data = $new_data;
252 }
253
254 /*
255 * Structured Data
256 */
257 if ($this->get_value('g_structured_data', '0', $field)) {
258 $settings['structured_data'] = true;
259 $structure = array();
260 if (!$this->is_empty('g_structure_key', $field)) {
261 $structure['key'] = $this->get_value('g_structure_key', '0', $field);
262 }
263 if (!$this->is_empty('g_structure_value', $field)) {
264 $structure['value'] = $this->get_array('g_structure_value', '0', $field);
265 }
266 if (!$this->is_empty('g_structure_label', $field)) {
267 $structure['label'] = $this->get_array('g_structure_label', '0', $field);
268 $settings['show_data_labels'] = true;
269 }
270 if (!$this->is_empty('g_structure_legend_text', $field)) {
271 $structure['legend_text'] = $this->get_array('g_structure_legend_text', '0', $field);
272 }
273 if (!$this->is_empty('g_structure_area', $field)) {
274 $structure['area'] = $this->get_array('g_structure_area', '0', $field);
275 }
276 if (!$this->is_empty('g_structure_open', $field)) {
277 $structure['open'] = $this->get_array('g_structure_open', '0', $field);
278 }
279 if (!$this->is_empty('g_structure_end', $field)) {
280 $structure['end'] = $this->get_array('g_structure_end', '0', $field);
281 }
282 if (!$this->is_empty('g_structure_outliers', $field)) {
283 $structure['outliers'] = $this->get_array('g_structure_outliers', '0', $field);
284 }
285 if (!$this->is_empty('g_structure_top', $field)) {
286 $structure['top'] = $this->get_array('g_structure_top', '0', $field);
287 }
288 if (!$this->is_empty('g_structure_bottom', $field)) {
289 $structure['bottom'] = $this->get_array('g_structure_bottom', '0', $field);
290 }
291 if (!$this->is_empty('g_structure_wtop', $field)) {
292 $structure['wtop'] = $this->get_array('g_structure_wtop', '0', $field);
293 }
294 if (!$this->is_empty('g_structure_wbottom', $field)) {
295 $structure['wbottom'] = $this->get_array('g_structure_wbottom', '0', $field);
296 }
297 if (!$this->is_empty('g_structure_high', $field)) {
298 $structure['high'] = $this->get_array('g_structure_high', '0', $field);
299 }
300 if (!$this->is_empty('g_structure_low', $field)) {
301 $structure['low'] = $this->get_array('g_structure_low', '0', $field);
302 }
303 $settings['structure'] = $structure;
304 }
305
306 if (!$this->get_boolean('g_structured_data', $field) && $this->get_boolean('g_show_legend', $field)) {
307 $legends = array();
308 if ($this->get_value('g_multiline', '0', $field) == '1') {
309 if (isset($data[0]) && is_array($data[0])) {
310 $legends = $data[0];
311 }
312 } elseif (is_array($data)) {
313 $legends = $data;
314 }
315 $settings['legend_entries'] = array_keys($legends);
316 }
317
318 if (!$this->is_empty('g_legends', $field)) {
319 $tmp_legends = explode("\r\n", $this->get_value('g_legends', '', $field));
320 $legends = array();
321 foreach ($tmp_legends as $legend) {
322 $legends[] = implode("\n", explode('\n', $legend));
323 }
324 $settings['legend_entries'] = $legends;
325 }
326
327 $count = max(count($data), 1);
328 foreach ($data as $sub_data) {
329 if (is_array($sub_data)) {
330 $count = max($count, count($sub_data));
331 }
332 }
333 $colors = array();
334 if (!$this->is_empty('g_colors', $field)) {
335 $tmp_colors = explode("\r\n", $this->get_value('g_colors', '', $field));
336 foreach ($tmp_colors as $color) {
337 $colors[] = $this->get_array_value($color, '');
338 }
339 } else {
340 $hue = 0;
341 $color_change = 360 / $count;
342 for ($i = 0; $i < $count; $i++) {
343 $colors[] = array($this->get_value('g_palette', '#1e73be', $field) . '/hue(' . $hue . ')');
344 $hue += $color_change;
345 }
346 }
347
348 $colors = apply_filters('e2pdf_helper_graph_colors', $colors, $field);
349 if ($this->get_boolean('g_stroke_dynamic_colour', $field)) {
350 $settings['stroke_colour'] = apply_filters('e2pdf_helper_graph_stroke_colors', $colors, $field);
351 }
352 if ($this->get_boolean('g_marker_dynamic_colour', $field)) {
353 $settings['marker_colour'] = apply_filters('e2pdf_helper_graph_marker_colors', $colors, $field);
354 }
355
356 $graph = new Goat1000\SVGGraph\SVGGraph($width, $height, apply_filters('e2pdf_helper_graph_settings', $settings, $field));
357 $graph->colours($colors);
358 $graph->values(apply_filters('e2pdf_helper_graph_data', $data, $field));
359 try {
360 $svg = $graph->fetch($this->get_value('g_type', 'BarGraph', $field));
361 if (false !== strpos($svg, 'Goat1000')) {
362 $svg = preg_replace('/Goat1000\\\(.[^\s]*)\s/', 'Graph ', $svg);
363 }
364 return base64_encode($svg); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
365 } catch (Exception $e) {
366 return '';
367 }
368 }
369
370 public function get_graph_data($value, $separators = array(), $multiline = false) {
371 $data = array();
372 if ($multiline != '0') {
373 $lines = preg_split('/\r\n|\n/', $value);
374 foreach ($lines as $key => $line) {
375 $line_data = $this->get_line_data(trim($line), $separators);
376 if ($multiline == '1') {
377 $data[] = $line_data;
378 } elseif ($multiline == '2') {
379 $data = array_merge($data, $line_data);
380 }
381 }
382 } else {
383 $data = $this->get_line_data($value, $separators);
384 }
385 return $data;
386 }
387
388 public function get_line_data($line, $separators = array()) {
389 $value = array();
390 $data = explode($separators['array'], $line);
391 foreach ($data as $line_data) {
392 $key = null;
393 if (strpos($line_data, $separators['key']) !== false) {
394 list($key, $line_data) = explode($separators['key'], $line_data, 2);
395 }
396 if (strpos($line_data, $separators['sub_array']) !== false) {
397 if ($key !== null) {
398 $value[$key] = explode($separators['sub_array'], $line_data);
399 } else {
400 $value[] = explode($separators['sub_array'], $line_data);
401 }
402 } else {
403 if ($key !== null) {
404 if ($line_data === '') {
405 $value[$key] = null;
406 } else {
407 $value[$key] = $line_data;
408 }
409 } else {
410 if ($line_data === '') {
411 $value[] = null;
412 } else {
413 $value[] = $line_data;
414 }
415 }
416 }
417 }
418 return $value;
419 }
420
421 public function is_empty($key = '', $field = array()) {
422 return isset($field['properties'][$key]) && $field['properties'][$key] !== '' ? false : true;
423 }
424
425 public function is_exists($key = '', $field = array()) {
426 return isset($field['properties'][$key]) ? true : false;
427 }
428
429 public function get_value($key = '', $default = '', $field = array()) {
430 return isset($field['properties'][$key]) && $field['properties'][$key] ? $field['properties'][$key] : $default;
431 }
432
433 public function get_number($key = '', $default = '', $field = array()) {
434 return isset($field['properties'][$key]) ? (float) $field['properties'][$key] : (float) $default;
435 }
436
437 public function get_array($key = '', $default = '', $field = array()) {
438 $value = isset($field['properties'][$key]) && $field['properties'][$key] ? $field['properties'][$key] : '';
439 if ($value && (strpos($value, ',') !== false)) {
440 return $value = array_map('trim', explode(',', $value));
441 } else {
442 return $value ? $value : $default;
443 }
444 }
445
446 public function get_array_value($value = '', $default = '') {
447 if ($value && (strpos($value, ',') !== false)) {
448 return $value = array_map('trim', explode(',', $value));
449 } else {
450 return $value ? $value : $default;
451 }
452 }
453
454 public function get_boolean($key = '', $field = array()) {
455 return isset($field['properties'][$key]) && $field['properties'][$key] ? true : false;
456 }
457 }
458