PluginProbe
SQL Chart Builder / 2.2.2
SQL Chart Builder v2.2.2
3.0.2 3.0.1 trunk 1.0.2 1.0.3 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.7.1 2.3.7.2 2.3.8 3.0.0
sql-chart-builder / functions.php

functions.php in SQL Chart Builder 2.2.2, at functions.php

714 lines 23.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) {
3 die;
4 }
5 //postinstall function
6 function guaven_sqlcharts_load_defaults()
7 {
8 if (get_option("guaven_sqlcharts_already_installed_2") === false) {
9 update_option("guaven_sqlcharts_already_installed_2", "1");
10 guaven_sqlcharts_install_first_data();
11
12 }
13 }
14
15 function guaven_sqlcharts_install_first_data()
16 {
17 require_once(dirname(__FILE__) . "/initial_data.php");
18 gvn_chart_sample_nonxml_data();
19 }
20
21
22
23 function guaven_sqlcharts_my_admin_notice()
24 {
25 global $post;
26 if (!empty($post) and $post->post_type == 'gvn_schart'):
27 if (!current_user_can('manage_options')) {
28 echo '<br><br>
29 <div class="updated gf-alert gf-alert-danger">Only administrators can manage this page</div>';
30 die();
31 }
32 echo '<div class="updated gf-alert gf-alert-info">';
33 if (empty($_GET["post"]) and strpos($_SERVER["REQUEST_URI"], "post-new") === false):
34 $gf_message = 'Use <b>Add new</b> button above to create new sql report. And click on any existing rule names below
35 to manage them. ';
36 else:
37 $gf_message = '
38 1. Give any name to your report.<br>
39 2. Choose chart type, type sql query, enter field names, labels and then press to Publish/Update<br>
40 3. After update you will see needed shortcode below. You can use that shortcode anywhere in your website: in pages, posts, widgets etc. <br>
41 ';
42 endif;
43 _e('<div style="float:left">' . $gf_message . '</div>', 'guaven_sqlcharts');
44 echo '<div style="float: right;
45 margin-top: 0px;
46 padding-top: 0px;"><a target="_blank" style="border:0px solid #6200ee;border-radius:0px;color:white;font-weight:bold;background: #6200ee;"
47 class="button button-secondary" href="https://guaven.com/contact/solution-request/">Get Premium Support </a></div> </div>';
48 endif;
49 }
50 add_action('admin_notices', 'guaven_sqlcharts_my_admin_notice');
51
52 function guaven_sqlcharts_onboarding_notice(){
53 if((function_exists('wp_doing_ajax') and wp_doing_ajax()) or get_option('guaven_sqlcharts_onboarding_notice_dismissed') == 1)
54 return;
55
56 printf('
57 <div class="guaven-sqlcharts-notice notice notice-success is-dismissible" data-notice="onboarding_notice">
58 <p>Welcome aboard on MySQL Charts!</p>
59 </div>'
60 );
61 }
62 add_action('admin_notices', 'guaven_sqlcharts_onboarding_notice');
63
64 function guaven_sqlcharts_onboarding_notice_dismissed(){
65 check_ajax_referer('notice_dismissed', 'nonce');
66 switch ($_POST['type']){
67 case 'onboarding_notice':
68 update_option('guaven_sqlcharts_onboarding_notice_dismissed', 1);
69 break;
70 }
71 }
72 add_action('wp_ajax_guaven_sqlcharts_onboarding_notice_dismissed', 'guaven_sqlcharts_onboarding_notice_dismissed');
73
74 function guaven_sqlcharts_enqueue_chart()
75 {
76 wp_enqueue_script('guaven_sqlcharts_chartjs', plugins_url('asset/bundle.min.js', __FILE__),array('jquery'),GVNSQLCHARTS_VERSION);
77 wp_localize_script('guaven_sqlcharts_chartjs', 'guaven_sqlcharts_notice_dismissed', array(
78 'action' => 'guaven_sqlcharts_onboarding_notice_dismissed',
79 'nonce' => wp_create_nonce('notice_dismissed')
80 ));
81
82 }
83 add_action('wp_enqueue_scripts', 'guaven_sqlcharts_enqueue_chart');
84 add_action('admin_enqueue_scripts', 'guaven_sqlcharts_enqueue_chart');
85
86 function guaven_sqlcharts_enqueue_main_style()
87 {
88 wp_enqueue_style('guaven_sqlcharts_main_style', plugins_url('asset/guaven_sqlcharts.css', __FILE__),array(),GVNSQLCHARTS_VERSION);
89 }
90 add_action('admin_enqueue_scripts', 'guaven_sqlcharts_enqueue_main_style');
91
92
93 function guaven_sqlcharts_isJson($string)
94 {
95 json_decode($string);
96 return (json_last_error() == JSON_ERROR_NONE);
97 }
98
99 add_action('init', 'guaven_sqlcharts_register_post');
100 function guaven_sqlcharts_register_post()
101 {
102 //register_taxonomy('guaven_update_push_tag', 'termin');
103 register_post_type('gvn_schart', array(
104 'labels' => array(
105 'name' => __('My SQL Charts'),
106 'singular_name' => __('My SQL chart')
107 ),
108
109 'public' => true,
110 //'taxonomies' => array('guaven_update_push_tag'),
111 'supports' => array(
112 'title',
113 'postmeta'
114 ),
115 'register_meta_box_cb' => 'guaven_sqlcharts_metabox_area'
116 ));
117
118 guaven_sqlcharts_load_defaults();
119 }
120
121 add_action('admin_footer', 'guaven_sqlcharts_admin_front');
122
123
124 function guaven_sqlcharts_admin_front()
125 {
126 global $post;
127 if (!empty($post) and $post->post_type == 'gvn_schart') {
128 ?>
129 <style type="text/css">#normal-sortables{display: none}</style>
130 <?php
131 }
132 }
133
134 // metabox for editor
135 function guaven_sqlcharts_metabox_area()
136 {
137 add_meta_box('guaven_sqlcharts_metabox', 'Configure your graph chart', 'guaven_sqlcharts_metabox', 'gvn_schart', 'advanced', 'default');
138 }
139
140 function guaven_sqlcharts_metabox()
141 {
142 require_once(dirname(__FILE__) . "/admin_metabox.php");
143 }
144
145 function guaven_gutenberg_wrapper($atts){
146 if(isset($atts['sqlcharts_inserted_script'])){
147 global $sqlcharts_inserted_script;
148 $sqlcharts_inserted_script = $atts['sqlcharts_inserted_script'];
149 }
150
151 // if( isset($_GET["post_id"],$_GET["context"]) and $_GET["context"]=='edit'
152 // and strpos($_SERVER["REQUEST_URI"],'block-renderer/guaven-sqlcharts/gvn-chart-gutenberg')!==false
153 // ){
154 // $atts['chart_id']=$_GET["post_id"];
155 // }
156 $post = get_post($atts['chart_id']);
157 if( ! isset($atts['chart_id']) or !isset($post) or $post->post_type != 'gvn_schart'){
158
159 return "Invalid id";
160 }
161
162 return guaven_sqlcharts_local_shortcode(array('id' => $atts['chart_id'])); // temporary explicit value
163 }
164 function guaven_register_gutenberg_blocks()
165 {
166 wp_register_script(
167 'gvn_gutenberg_charts',
168 plugins_url( 'asset/guaven_gutenberg_charts.js', __FILE__ ),
169 array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-server-side-render' ),
170 GVNSQLCHARTS_VERSION.'_'.filemtime( plugin_dir_path( __FILE__ ) . 'asset/guaven_gutenberg_charts.js' )
171 );
172 wp_localize_script('gvn_gutenberg_charts', 'guaven', array(
173 'description' => 'Add My SQL Chart to your post',
174 ));
175
176 register_block_type( 'guaven-sqlcharts/gvn-chart-gutenberg', array(
177 'editor_script' => 'gvn_gutenberg_charts',
178 'render_callback' => 'guaven_gutenberg_wrapper',
179 'attributes' => array(
180 'chart_id' => array(
181 'type' => 'string',
182 'default' => null
183 ),
184 'sqlcharts_inserted_script' => array(
185 'type' => 'number',
186 'default' => null
187 )
188 )
189 ));
190 }
191 add_action('init', 'guaven_register_gutenberg_blocks');
192
193
194
195 function guaven_sqlcharts_save_metabox_area($post_id, $post)
196 {
197 if (!isset($_POST['meta_box_nonce_field']) or !wp_verify_nonce($_POST['meta_box_nonce_field'], 'meta_box_nonce_action')) {
198 return $post->ID;
199 }
200 $fields = array(
201 "guaven_sqlcharts_chartheight",
202 "guaven_sqlcharts_chartwidth",
203 "guaven_sqlcharts_graphtype",
204 "guaven_sqlcharts_xarg_s",
205 "guaven_sqlcharts_xarg_l",
206 "guaven_sqlcharts_yarg_s",
207 "guaven_sqlcharts_yarg_l",
208 "guaven_sqlcharts_tablepart",
209 "guaven_sqlcharts_variables",
210 "guaven_sqlcharts_formpartrole",
211 "guaven_sqlcharts_formpartbutton",
212 "guaven_sqlcharts_dbhost",
213 "guaven_sqlcharts_dblogin",
214 "guaven_sqlcharts_dbpass",
215 "guaven_sqlcharts_dbname",
216 "guaven_sqlcharts_colors",
217 "guaven_sqlcharts_begin_with_0_x",
218 "guaven_sqlcharts_begin_with_0_y"
219 );
220 foreach ($fields as $key => $value) {
221 update_post_meta($post->ID, $value, esc_attr($_POST[$value]));
222 }
223 update_post_meta($post->ID, 'guaven_sqlcharts_code', esc_attr(str_replace("'",'"',stripslashes($_POST['guaven_sqlcharts_code']))) );
224 }
225 add_action('save_post', 'guaven_sqlcharts_save_metabox_area', 1, 2);
226 // save the custom fields
227
228
229
230
231 function guaven_sqlcharts_libloads($type, $step)
232 {
233 $stty = array(
234 'bar' => array(
235 'packages' => "'corechart', 'bar'",
236 'charts' => "BarChart"
237 ),
238 'column' => array(
239 'packages' => "'corechart', 'bar'",
240 'charts' => "ColumnChart"
241 ),
242 'area' => array(
243 'packages' => "'corechart'",
244 'charts' => "AreaChart"
245 ),
246 'pie' => array(
247 'packages' => "'corechart'",
248 'charts' => "PieChart"
249 ),
250 '3dpie' => array(
251 'packages' => "'corechart'",
252 'charts' => "PieChart"
253 )
254 );
255
256 return $stty[$type][$step];
257 }
258 function gvn_chart_check_sql_query($sql)
259 {
260 $blacklister = array(
261 "delete",
262 "update",
263 "insert",
264 "drop",
265 "truncate",
266 "alter"
267 ); //add all
268 $blacklister_f = 0;
269 foreach ($blacklister as $key => $value) {
270 if (strpos($sql, $value) !== false)
271 $blacklister_f = 1;
272 }
273 return $blacklister_f;
274 }
275
276 function guaven_get_labels_and_values($id, $fvs)
277 {
278 $values = array();
279 $labels = array();
280 $xarg_s = get_post_meta($id, 'guaven_sqlcharts_xarg_s', true);
281 $xarg_l = get_post_meta($id, 'guaven_sqlcharts_xarg_l', true);
282 $yarg_s = get_post_meta($id, 'guaven_sqlcharts_yarg_s', true);
283 $yarg_l = get_post_meta($id, 'guaven_sqlcharts_yarg_l', true);
284 $chartype = array(
285 'line_l' => 'Line',
286 'pie_l' => 'Pie',
287 'donut_l' => 'Pie',
288 'bar_l' => 'Bar',
289 'horizontalbar_l' => 'Horizontal Bar',
290 'area_l' => 'Line'
291 );
292 foreach ($fvs as $key => $value) {
293 $values[] = $value->$yarg_s;
294 $labels[] = '"' . $value->$xarg_s . '"';
295 }
296 return array(
297 $labels,
298 $values,
299 explode(";", $yarg_l),
300 explode(";", $xarg_l)
301 );
302 }
303
304 function guaven_sqlcharts_print_chart_js($tip_g, $title, $labels, $values, $ylabel, $pid = null)
305 {
306 if ($tip_g == 'line_l') {
307 guaven_sqlcharts_linedata($title, $labels, $values, $ylabel, 'false', $pid);
308 }
309 if ($tip_g == 'area_l') {
310 guaven_sqlcharts_linedata($title, $labels, $values, $ylabel, 'true', $pid);
311 } elseif ($tip_g == 'pie_l') {
312 guaven_sqlcharts_piedata($title, $labels, $values, $ylabel, $pid);
313 } elseif ($tip_g == 'donut_l') {
314 guaven_sqlcharts_piedata($title, $labels, $values, $ylabel, $pid, 'doughnut');
315 } elseif ($tip_g == 'bar_l') {
316 guaven_sqlcharts_bardata($title, $labels, $values, $ylabel, 'bar', $pid);
317 } elseif ($tip_g == 'horizontalbar_l') {
318 guaven_sqlcharts_bardata($title, $labels, $values, $ylabel, 'horizontalBar', $pid);
319 }
320 }
321
322 function gvn_chart_put_variables($sql,$pid){
323 $sql_initial=$sql;
324 $variables_raw=get_post_meta($pid,'guaven_sqlcharts_variables',true);
325 $variables_arr=explode("|",$variables_raw);
326 foreach($variables_arr as $varfield){
327 $varfield_arr=explode("~",$varfield);
328 if (count($varfield_arr)<3) continue;
329 $varfield_arr=array_map("trim",$varfield_arr);
330 if (!empty($_GET[$varfield_arr[0]])) $varreplacement=$_GET[$varfield_arr[0]]; else $varreplacement=$varfield_arr[1];
331 if (!is_numeric($varreplacement) and strpos($varreplacement,'()')===false) $varreplacement='"'.$varreplacement.'"';
332
333 $sql_initial=str_replace('{'.$varfield_arr[0].'}',$varreplacement,$sql_initial);
334 }
335 //echo $sql_initial;
336 return $sql_initial;
337 }
338
339 function gvn_chart_top_form($atts){
340 if (get_post_meta($atts["id"],'guaven_sqlcharts_formpartrole',true)!='' and !is_user_logged_in()) return;
341 $topform='';$dateexists=false;
342 $variables_raw=get_post_meta($atts['id'],'guaven_sqlcharts_variables',true);
343 $variables_raw=explode("|",$variables_raw);
344 foreach ($variables_raw as $vrow){
345 $vrow_arr=explode("~",$vrow);
346 $vrow_arr=array_map("trim",$vrow_arr);
347 if (empty($vrow_arr[3])) continue;
348 $gvalue=!empty($_GET[$vrow_arr[0]])?esc_attr(urldecode($_GET[$vrow_arr[0]])):'';
349 $dvalue=(strpos($vrow_arr[1],'()')===false)?esc_attr($vrow_arr[1]):'';
350 if ($vrow_arr[3]=='date') {
351 $dateexists=true;
352 $topform.= $vrow_arr[2].' <input autocomplete="off" style="max-width:210px" type="text"
353 value="'.$gvalue.'"
354 data-toggle="datepicker" name="'.$vrow_arr[0].'" placeholder="'.$dvalue.'">
355 ';}
356 else {
357 $topform.= $vrow_arr[2].' <input autocomplete="off" style="max-width:210px;'.($vrow_arr[3]=='number'?'width:100px;':'').'"
358 type="'.$vrow_arr[3].'"
359 value="'.$gvalue.'" name="'.$vrow_arr[0].'" placeholder="'.$dvalue.'">
360 ';
361 }
362 }
363 if (!empty($topform)) {
364 $topform='<form method="get" action="" class="guaven_sqlcharts_form">'.$topform.'
365 <input type="submit"
366 value="'.(get_post_meta($atts['id'], 'guaven_sqlcharts_formpartbutton', true)!=''?esc_attr(get_post_meta($atts['id'], 'guaven_sqlcharts_formpartbutton', true)):'OK').'"></form>';
367 if ($dateexists) $topform.='<script>setTimeout(function(){jQuery(\'[data-toggle="datepicker"]\').datepicker({format: \'yyyy-mm-dd\'});
368 },300);</script>';
369 return $topform ;
370 }
371 return;
372 }
373
374 function guaven_sqlcharts_local_shortcode($atts)
375 {
376 $remote_host=get_post_meta($atts['id'], 'guaven_sqlcharts_dbhost', true);
377 if ($remote_host!=''){
378 $remote_db=get_post_meta($atts['id'], 'guaven_sqlcharts_dbname', true);
379 $remote_login=get_post_meta($atts['id'], 'guaven_sqlcharts_dblogin', true);
380 $remote_pass=get_post_meta($atts['id'], 'guaven_sqlcharts_dbpass', true);
381 $wpdb=new wpdb($remote_login,$remote_pass,$remote_db,$remote_host);
382 }
383 else {
384 global $wpdb;
385 }
386
387 $GLOBALS["guaven_sqlcharts_atts"]=$atts;
388
389 $sql = html_entity_decode(get_post_meta($atts['id'], 'guaven_sqlcharts_code', true));
390 $sql=gvn_chart_put_variables($sql,$atts['id']);
391 $blacklister_f = gvn_chart_check_sql_query($sql);
392 if ($blacklister_f == 1)
393 return 'You given SQL code contains forbidden commands. Remember that you should only use SELECT queries';
394 $tip_g = get_post_meta($atts['id'], 'guaven_sqlcharts_graphtype', true);
395
396 for($i=1;$i<20;$i++){ $replacearg=!empty($atts["arg".$i])?$atts["arg".$i]:0;
397 $sql=str_replace("{arg".$i."}",esc_sql($replacearg),$sql);}
398 $sql_split = explode(';', $sql);
399 $labels_and_values = array();
400 $post_g = get_post($atts['id']);
401
402 global $sqlcharts_inserted_script;
403 for ($i = 0; $i < count($sql_split); $i++) {
404 if (!empty($sql_split[$i])) {
405
406 $fvs = $wpdb->get_results($sql_split[$i]);
407 if (strpos($_SERVER["REQUEST_URI"],'wp-admin')!==false){
408 $wpdb->show_errors();
409 ob_start();
410 $wpdb->print_error();
411 $printerror = ob_get_clean();
412 if ($printerror != '' and strpos($printerror, "[]") === false)
413 return $printerror;
414 elseif (empty($fvs))
415 return 'Your SQL returnes empty date, please recheck your SQL query above';
416 }
417
418 ob_start();
419
420 if (empty($sqlcharts_inserted_script))
421 $sqlcharts_inserted_script = 1;
422 $labels_and_values[$i] = guaven_get_labels_and_values($atts['id'], $fvs);
423 $labels[$i] = $labels_and_values[$i][0];
424 $values[$i] = $labels_and_values[$i][1];
425 $ylabel[$i] = !empty($labels_and_values[$i][2][$i]) ? $labels_and_values[$i][2][$i] : '';
426 $xlabel[$i] = !empty($labels_and_values[$i][3][$i]) ? $labels_and_values[$i][3][$i] : '';
427 }
428 }
429
430 echo gvn_chart_top_form($atts);
431 ?>
432 <canvas
433 id="ct-chart_<?php echo $sqlcharts_inserted_script; ?>"
434 class="guaven_chart_canvas"
435 style="width: <?php echo get_post_meta($atts['id'], 'guaven_sqlcharts_chartwidth', true); ?>px !important; height: <?php echo get_post_meta($atts['id'], 'guaven_sqlcharts_chartheight', true); ?>px !important;"
436 ></canvas>
437
438 <script type="text/javascript" class="gvn_charts_script" async>
439 var ctx = jQuery("#ct-chart_<?php
440 echo $sqlcharts_inserted_script;
441 ?>");
442
443 <?php
444 guaven_sqlcharts_print_chart_js($tip_g, $post_g->post_title, $labels, $values, $ylabel, $atts['id']);
445 ?>
446 </script>
447
448 <?php
449 if (!empty($atts["table"])) echo guaven_sqlcharts_tablepart($post_g->post_title, $labels, $values, $ylabel,$xlabel);
450 $sqlcharts_inserted_script++;
451 return ob_get_clean();
452 }
453
454 add_shortcode('gvn_schart_2', 'guaven_sqlcharts_local_shortcode');
455
456 function guaven_sqlcharts_colors($index, $pid = null){
457 if(!isset($pid)) {
458 global $post;
459 $pid = $post->ID;
460 }
461 $colors=get_post_meta($pid,'guaven_sqlcharts_colors',true);
462 $colors=explode(",",$colors);
463 if (!empty($colors[$index])) return $colors[$index];
464 return rand(0, 255) . ',' . rand(0, 255) . ',' . rand(0, 255);
465 }
466
467 function guaven_sqlcharts_bardata($title, $labels, $values, $ylabel, $type = 'bar', $pid = null)
468 {
469 ?>
470 var data = {
471 labels: [<?php
472 echo implode(",", $labels[0]);
473 ?>],
474 datasets: [
475 <?php
476 for ($i = 0; $i < count($values); $i++) {
477 ?>
478 {
479 <?php
480 if(!empty($GLOBALS["guaven_sqlcharts_atts"]["params"])){
481 //passing chartJS params via the shortcode
482 echo esc_js($GLOBALS["guaven_sqlcharts_atts"]["params"]);
483 }
484 ?>
485 label: "<?php
486 echo esc_attr($ylabel[$i]);
487 ?>",
488 backgroundColor: [
489 <?php
490 echo guaven_sqlcharts_colorgenerator(count($values[$i]), 0, 0, guaven_sqlcharts_colors($i, $pid));
491 ?>
492 ],
493 borderColor: [
494 <?php
495 echo guaven_sqlcharts_colorgenerator(count($values[$i]), 0, 0.2, guaven_sqlcharts_colors($i, $pid));
496 ?>
497 ],
498 borderWidth: 1,
499 data: [<?php
500 echo implode(",", $values[$i]);
501 ?>],
502 },
503 <?php
504 }
505 ?>
506 ]
507 };
508 var options={
509 responsive: true,
510 scales: {
511 xAxes: [{
512 ticks: {
513 beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_x', true) == 1) ? 'true':'false'; ?>
514 }
515 }],
516 yAxes: [{
517 ticks: {
518 beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_y', true) == 1) ? 'true':'false'; ?>
519 }
520 }]
521 }
522 };
523 var myBarChart = new Chart(ctx, {
524 type: '<?php
525 echo $type;
526 ?>',
527 data: data,
528 options: options
529 });
530 <?php
531 }
532
533
534
535 function guaven_sqlcharts_linedata($title, $labels, $values, $ylabel, $type = 'false', $pid = null)
536 {
537 ?>
538 var data = {
539 labels: [<?php
540 echo implode(",", $labels[0]);
541 ?>],
542 datasets: [
543 <?php
544 for ($i = 0; $i < count($values); $i++) {
545 ?>
546 {
547 <?php
548 if(!empty($GLOBALS["guaven_sqlcharts_atts"]["params"])){
549 //passing chartJS params via the shortcode
550 echo esc_js($GLOBALS["guaven_sqlcharts_atts"]["params"]);
551 }
552 ?>
553 label: "<?php
554 echo esc_attr($ylabel[$i]);
555 ?>",
556 fill: <?php echo $type=="false"?$type:($i==0?'"+1"':'"origin"');
557 ?>,
558 lineTension: 0.1,
559 backgroundColor: <?php
560 echo guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid));
561 ?>
562 borderColor: <?php
563 echo guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid));
564 ?>
565 pointBorderColor: <?php
566 echo guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid));
567 ?>
568 pointHoverBackgroundColor: <?php
569 echo guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid));
570 ?>
571 pointHoverBorderColor: <?php
572 echo guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid));
573 ?>
574 data: [<?php
575 echo implode(",", $values[$i]);
576 ?>],
577 spanGaps: false,
578 },
579 <?php
580 }
581 ?>
582 ]
583 };
584 var myLineChart = new Chart(ctx, {
585 type: 'line',
586 data: data,
587 options: {
588 responsive: true,
589 scales: {
590 xAxes: [{
591 display: true,
592 ticks: {
593 beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_x', true) == 1) ? 'true':'false'; ?>
594 }
595 }],
596 yAxes: [{
597 ticks: {
598 beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_y', true) == 1) ? 'true':'false'; ?>
599 }
600 }]
601 }
602 }
603 });
604 <?php
605 }
606
607
608 function guaven_sqlcharts_piedata($title, $labels, $values, $ylabel, $pid, $type = 'pie')
609 {
610 ?>
611 var options={
612 responsive: true,
613 };
614 var data = {
615 labels: [ <?php echo implode(",", $labels[0]);?>],
616 datasets: [
617 <?php
618 for ($i = 0; $i < count($values); $i++) {
619 ?>
620 {
621 <?php
622 if(!empty($GLOBALS["guaven_sqlcharts_atts"]["params"])){
623 //passing chartJS params via the shortcode
624 echo esc_js($GLOBALS["guaven_sqlcharts_atts"]["params"]);
625 }
626 ?>
627 data: [<?php
628 echo implode(",", $values[$i]);
629 ?>],
630 backgroundColor: [
631 <?php
632 foreach($values[$i] as $vci=>$valuecolor){
633 echo guaven_sqlcharts_colorgenerator(1, 0, 0, guaven_sqlcharts_colors($vci, $pid));
634 }
635 ?>
636 ],
637 hoverBackgroundColor: [
638 <?php
639 foreach($values[$i] as $vci=>$valuecolor){
640 echo guaven_sqlcharts_colorgenerator(1, 0, 0.2, guaven_sqlcharts_colors($vci, $pid));
641 }
642 ?>
643 ]
644 },
645 <?php
646 }
647 ?>
648 ]
649 };
650 var myPieChart = new Chart(ctx,{
651 type: '<?php
652 echo $type;
653 ?>',
654 data: data,
655 options: options
656 });
657 <?php
658 }
659
660
661
662
663 function guaven_sqlcharts_colorgenerator($count, $indic, $darkness = 0, $initcolor = '255,0,0')
664 {
665 if (strpos($initcolor,'#')===0) {
666 $split = str_split(substr($initcolor,1), 2);
667 $r = hexdec($split[0]);
668 $g = hexdec($split[1]);
669 $b = hexdec($split[2]);
670 $ret='';
671 for ($i = 0; $i < $count; $i++) {
672 $ret .= "'rgba(" . $r . ", " . $g . ", " . $b . ",".($darkness + 0.8 - $indic * $i * 0.8 / ($count)).")',
673 ";
674 }
675 return $ret;
676 }
677 $initial_colors = array(
678 'linebg' => 'red',
679 'linebr' => 'yellow',
680 'linebc' => 'green',
681 'linehbg' => 'white',
682 'linehbc' => 'black'
683 );
684 if (!empty($initial_colors[$count]))
685 return '"' . $initial_colors[$count] . '",
686 ';
687 $ret = '';
688 for ($i = 0; $i < $count; $i++) {
689 $ret .= "'rgba(" . $initcolor . "," . ($darkness + 0.8 - $indic * $i * 0.8 / ($count)) . ")',
690 ";
691 }
692 return $ret;
693 }
694
695 function guaven_sqlcharts_tablepart($title, $labels, $values, $ylabel,$xlabel){
696 $tabledata='';
697 $fcol=[];$scol=[];
698 $tablein='<br>';
699 foreach($values as $valuerow){
700 foreach ($valuerow as $key => $value) {
701 $fcol[$key]='<td>'.str_replace('"',"",$labels[0][$key]).'</td>';
702 $scol[$key][]='<td>'.$value.'</td>';
703
704 }
705 }
706 foreach($fcol as $key=>$value){
707 $tablein.='<tr>'.$value.implode(" ",$scol[$key]).'</tr>'.PHP_EOL;
708 }
709 $tabledata.='<table><tr><th>'.$xlabel[0].'</th><th>'.implode("</th><th>",$ylabel).'</th></tr>
710 '.$tablein.'</table><br>';
711 return $tabledata;
712
713 }
714