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

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