migrated ubiquitous libraries to composer autoloader (#421)
[openemr.git] / library / openflashchart / graph_track_anything.php
blobb13b452a7fcd39c9f7b4b0e9e19f58e5549276c6
1 <?php
2 // Copyright (C) 2010 Brady Miller <brady@sparmy.com>
3 // Modified 2011 Rod Roark <rod@sunsetsystems.com>
4 // Modified 2014 Joe Slam <joe@produnis.de>
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // Flexible script for graphing entities in OpenEMR
14 //SANITIZE ALL ESCAPES
15 $sanitize_all_escapes=true;
18 //STOP FAKE REGISTER GLOBALS
19 $fake_register_globals=false;
22 require_once(dirname(__FILE__) . "/../../interface/globals.php");
23 require_once($GLOBALS['srcdir'] . "/openflashchart/php-ofc-library/open-flash-chart.php");
25 // get $_POSTed data
26 //+++++++++++++++++++++++
27 $titleGraph = json_decode($_POST['track'],TRUE);
28 $the_date_array = json_decode($_POST['dates'],TRUE);
29 $the_value_array = json_decode($_POST['values'],TRUE);
30 $the_item_names = json_decode($_POST['items'],TRUE);
31 $the_checked_cols = json_decode($_POST['thecheckboxes'],TRUE);
32 // ++++++/end get POSTed data
33 $laenge = count($the_date_array);
35 // set up colors for lines to plot
36 $line_colors[] = "#a40000";
37 $line_colors[] = "#5c3566";
38 $line_colors[] = "#204a87";
39 $line_colors[] = "#4e9a06";
40 $line_colors[] = "#babdb6";
41 $line_colors[] = "#0000FF";
42 $line_colors[] = "#DB1750";
47 // check if something was sent
48 // and quit if not
49 //-------------------------------
50 if ($the_checked_cols == NULL) {
51 // nothing to plot
52 echo "No item checked,\n";
53 echo "nothing to plot."; // DEBUG ONLY! COMMENT ME OUT!
54 exit;
56 // end check if NULL data
61 // get ideal y-axis-steps
62 if(!function_exists('getIdealYSteps')) {
63 function getIdealYSteps($a) {
64 if ($a>1000) {
65 return 400;
66 } else if ($a>500) {
67 return 200;
68 } else if ($a>100) {
69 return 40;
70 } else if ($a>50) {
71 return 20;
72 } else {
73 return 5;
75 } // end function getIdeal...
76 } // end if function_exist
81 // Prepare look and feel of data points
82 $def = new hollow_dot();
83 $def->size(4)->halo_size(3)->tooltip('#val#<br>#date:Y-m-d H:i#');
86 // Build and show the chart
87 $chart = new open_flash_chart();
88 $chart->set_title( new Title( $titleGraph ));
91 // do this for each checked data-column
92 //-----------------------------------
93 //#############################################################
94 foreach($the_checked_cols as $col) {
96 // reset loop-arrays
97 $the_values = array();
98 $the_dates = array();
99 $the_data = array();
102 // skip NULL or not-numeric entries
103 // check if values are numeric
104 // and change date into UNIX-format
105 for ($i = 0; $i < $laenge; $i++){
106 if( is_numeric($the_value_array[$col][$i]) ) {
107 // is numeric
108 $the_values[] = $the_value_array[$col][$i];
109 $the_dates[] = strtotime($the_date_array[$i]); // convert to UNIX-format
110 $the_data[] = new scatter_value(strtotime($the_date_array[$i]), $the_value_array[$col][$i]); // save into array for plotting
111 } else {
112 // is NOT numeric, do nothing
115 // -----------------------------------------------------------
116 // all graph-data are now in array $the_data
117 // UNIX times (for x-axis-range) are in array $the_dates
118 // -----------------------------------------------------------
121 $s_{$col} = new scatter_line( $line_colors[$col] , 2 );
122 $s_{$col}->set_default_dot_style( $def );
124 // Prepare and insert data
125 $s_{$col}->set_values( $the_data );
126 $s_{$col}-> set_key( $the_item_names[$col] , 10 );
127 $chart->add_element( $s_{$col} );
128 } // end foreach data-column-------------------------------------
129 //###############################################################
133 // get ranges
134 //--------------------------------------
135 // dates (for x-axis)
136 $the_sort = $the_dates;# // UNIX.time
137 sort($the_sort);
138 $lowest = $the_sort[0];
139 rsort($the_sort);
140 $highest = $the_sort[0];
142 // get maximum value (for y-axis)
143 $the_sort = $the_value_array;
144 foreach($the_checked_cols as $col) {
145 rsort($the_sort[$col]);
146 $maxima[] = $the_sort[$col][0];
148 rsort($maxima);
149 $maximum = $maxima[0];
150 //-----/end get ranges -----------------
153 // Prepare the x-axis
154 $x = new x_axis();
155 $x->set_range( $lowest, $highest );
157 // Calculate the steps and visible steps
158 $step= ($highest - $lowest)/60;
159 $step_vis=2;
161 // do not allow steps to be less than 30 minutes
162 if ($step < 26400) { # 86400
163 $step = 26400;
164 $step_vis=1;
167 $x->set_steps($step);
168 $labels = new x_axis_labels();
169 $labels->text('#date:Y-m-d#');
170 $labels->set_steps($step);
171 $labels->visible_steps($step_vis);
172 $labels->rotate(90);
173 $x->set_labels($labels);
175 // Prepare the y-axis
176 $y = new y_axis(); // $maximum is already set above
178 // set the range and y-step
179 $y->set_range( 0 , $maximum + getIdealYSteps( $maximum ) );
180 $y->set_steps( getIdealYSteps( $maximum ) );
182 # $chart->add_element( $s );
183 $chart->set_x_axis( $x );
184 $chart->add_y_axis( $y );
187 // echo a pretty ofc-string anyway
188 echo $chart->toPrettyString();