fix for attendance form
[openemr.git] / library / openflashchart / graph_track_anything.php
blob1d131bcd2b8fed7ab69f87ffe445d4c7dc7fa30e
1 <?php
2 // Copyright (C) 2010 Brady Miller <brady.g.miller@gmail.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 //STOP FAKE REGISTER GLOBALS
15 $fake_register_globals=false;
18 require_once(dirname(__FILE__) . "/../../interface/globals.php");
19 require_once($GLOBALS['srcdir'] . "/openflashchart/php-ofc-library/open-flash-chart.php");
21 // get $_POSTed data
22 //+++++++++++++++++++++++
23 $titleGraph = json_decode($_POST['track'],TRUE);
24 $the_date_array = json_decode($_POST['dates'],TRUE);
25 $the_value_array = json_decode($_POST['values'],TRUE);
26 $the_item_names = json_decode($_POST['items'],TRUE);
27 $the_checked_cols = json_decode($_POST['thecheckboxes'],TRUE);
28 // ++++++/end get POSTed data
29 $laenge = count($the_date_array);
31 // set up colors for lines to plot
32 $line_colors[] = "#a40000";
33 $line_colors[] = "#5c3566";
34 $line_colors[] = "#204a87";
35 $line_colors[] = "#4e9a06";
36 $line_colors[] = "#babdb6";
37 $line_colors[] = "#0000FF";
38 $line_colors[] = "#DB1750";
43 // check if something was sent
44 // and quit if not
45 //-------------------------------
46 if ($the_checked_cols == NULL) {
47 // nothing to plot
48 echo "No item checked,\n";
49 echo "nothing to plot."; // DEBUG ONLY! COMMENT ME OUT!
50 exit;
52 // end check if NULL data
57 // get ideal y-axis-steps
58 if(!function_exists('getIdealYSteps')) {
59 function getIdealYSteps($a) {
60 if ($a>1000) {
61 return 400;
62 } else if ($a>500) {
63 return 200;
64 } else if ($a>100) {
65 return 40;
66 } else if ($a>50) {
67 return 20;
68 } else {
69 return 5;
71 } // end function getIdeal...
72 } // end if function_exist
77 // Prepare look and feel of data points
78 $def = new hollow_dot();
79 $def->size(4)->halo_size(3)->tooltip('#val#<br>#date:Y-m-d H:i#');
82 // Build and show the chart
83 $chart = new open_flash_chart();
84 $chart->set_title( new Title( $titleGraph ));
87 // do this for each checked data-column
88 //-----------------------------------
89 //#############################################################
90 foreach($the_checked_cols as $col) {
92 // reset loop-arrays
93 $the_values = array();
94 $the_dates = array();
95 $the_data = array();
98 // skip NULL or not-numeric entries
99 // check if values are numeric
100 // and change date into UNIX-format
101 for ($i = 0; $i < $laenge; $i++){
102 if( is_numeric($the_value_array[$col][$i]) ) {
103 // is numeric
104 $the_values[] = $the_value_array[$col][$i];
105 $the_dates[] = strtotime($the_date_array[$i]); // convert to UNIX-format
106 $the_data[] = new scatter_value(strtotime($the_date_array[$i]), $the_value_array[$col][$i]); // save into array for plotting
107 } else {
108 // is NOT numeric, do nothing
111 // -----------------------------------------------------------
112 // all graph-data are now in array $the_data
113 // UNIX times (for x-axis-range) are in array $the_dates
114 // -----------------------------------------------------------
117 $s_{$col} = new scatter_line( $line_colors[$col] , 2 );
118 $s_{$col}->set_default_dot_style( $def );
120 // Prepare and insert data
121 $s_{$col}->set_values( $the_data );
122 $s_{$col}-> set_key( $the_item_names[$col] , 10 );
123 $chart->add_element( $s_{$col} );
124 } // end foreach data-column-------------------------------------
125 //###############################################################
129 // get ranges
130 //--------------------------------------
131 // dates (for x-axis)
132 $the_sort = $the_dates;# // UNIX.time
133 sort($the_sort);
134 $lowest = $the_sort[0];
135 rsort($the_sort);
136 $highest = $the_sort[0];
138 // get maximum value (for y-axis)
139 $the_sort = $the_value_array;
140 foreach($the_checked_cols as $col) {
141 rsort($the_sort[$col]);
142 $maxima[] = $the_sort[$col][0];
144 rsort($maxima);
145 $maximum = $maxima[0];
146 //-----/end get ranges -----------------
149 // Prepare the x-axis
150 $x = new x_axis();
151 $x->set_range( $lowest, $highest );
153 // Calculate the steps and visible steps
154 $step= ($highest - $lowest)/60;
155 $step_vis=2;
157 // do not allow steps to be less than 30 minutes
158 if ($step < 26400) { # 86400
159 $step = 26400;
160 $step_vis=1;
163 $x->set_steps($step);
164 $labels = new x_axis_labels();
165 $labels->text('#date:Y-m-d#');
166 $labels->set_steps($step);
167 $labels->visible_steps($step_vis);
168 $labels->rotate(90);
169 $x->set_labels($labels);
171 // Prepare the y-axis
172 $y = new y_axis(); // $maximum is already set above
174 // set the range and y-step
175 $y->set_range( 0 , $maximum + getIdealYSteps( $maximum ) );
176 $y->set_steps( getIdealYSteps( $maximum ) );
178 # $chart->add_element( $s );
179 $chart->set_x_axis( $x );
180 $chart->add_y_axis( $y );
183 // echo a pretty ofc-string anyway
184 echo $chart->toPrettyString();