Cosmetic tweaks for column widths in the layout editor.
[openemr.git] / library / openflashchart / graph_track_anything.php
blobe21971934f6d6003223f53e0b6ad43e7e824bc96
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");
24 require_once($GLOBALS['srcdir'] . "/formdata.inc.php");
26 // get $_POSTed data
27 //+++++++++++++++++++++++
28 $titleGraph = json_decode($_POST['track'],TRUE);
29 $the_date_array = json_decode($_POST['dates'],TRUE);
30 $the_value_array = json_decode($_POST['values'],TRUE);
31 $the_item_names = json_decode($_POST['items'],TRUE);
32 $the_checked_cols = json_decode($_POST['thecheckboxes'],TRUE);
33 // ++++++/end get POSTed data
34 $laenge = count($the_date_array);
36 // set up colors for lines to plot
37 $line_colors[] = "#a40000";
38 $line_colors[] = "#5c3566";
39 $line_colors[] = "#204a87";
40 $line_colors[] = "#4e9a06";
41 $line_colors[] = "#babdb6";
42 $line_colors[] = "#0000FF";
43 $line_colors[] = "#DB1750";
48 // check if something was sent
49 // and quit if not
50 //-------------------------------
51 if ($the_checked_cols == NULL) {
52 // nothing to plot
53 echo "No item checked,\n";
54 echo "nothing to plot."; // DEBUG ONLY! COMMENT ME OUT!
55 exit;
57 // end check if NULL data
62 // get ideal y-axis-steps
63 if(!function_exists('getIdealYSteps')) {
64 function getIdealYSteps($a) {
65 if ($a>1000) {
66 return 400;
67 } else if ($a>500) {
68 return 200;
69 } else if ($a>100) {
70 return 40;
71 } else if ($a>50) {
72 return 20;
73 } else {
74 return 5;
76 } // end function getIdeal...
77 } // end if function_exist
82 // Prepare look and feel of data points
83 $def = new hollow_dot();
84 $def->size(4)->halo_size(3)->tooltip('#val#<br>#date:Y-m-d H:i#');
87 // Build and show the chart
88 $chart = new open_flash_chart();
89 $chart->set_title( new Title( $titleGraph ));
92 // do this for each checked data-column
93 //-----------------------------------
94 //#############################################################
95 foreach($the_checked_cols as $col) {
97 // reset loop-arrays
98 $the_values = array();
99 $the_dates = array();
100 $the_data = array();
103 // skip NULL or not-numeric entries
104 // check if values are numeric
105 // and change date into UNIX-format
106 for ($i = 0; $i < $laenge; $i++){
107 if( is_numeric($the_value_array[$col][$i]) ) {
108 // is numeric
109 $the_values[] = $the_value_array[$col][$i];
110 $the_dates[] = strtotime($the_date_array[$i]); // convert to UNIX-format
111 $the_data[] = new scatter_value(strtotime($the_date_array[$i]), $the_value_array[$col][$i]); // save into array for plotting
112 } else {
113 // is NOT numeric, do nothing
116 // -----------------------------------------------------------
117 // all graph-data are now in array $the_data
118 // UNIX times (for x-axis-range) are in array $the_dates
119 // -----------------------------------------------------------
122 $s_{$col} = new scatter_line( $line_colors[$col] , 2 );
123 $s_{$col}->set_default_dot_style( $def );
125 // Prepare and insert data
126 $s_{$col}->set_values( $the_data );
127 $s_{$col}-> set_key( $the_item_names[$col] , 10 );
128 $chart->add_element( $s_{$col} );
129 } // end foreach data-column-------------------------------------
130 //###############################################################
134 // get ranges
135 //--------------------------------------
136 // dates (for x-axis)
137 $the_sort = $the_dates;# // UNIX.time
138 sort($the_sort);
139 $lowest = $the_sort[0];
140 rsort($the_sort);
141 $highest = $the_sort[0];
143 // get maximum value (for y-axis)
144 $the_sort = $the_value_array;
145 foreach($the_checked_cols as $col) {
146 rsort($the_sort[$col]);
147 $maxima[] = $the_sort[$col][0];
149 rsort($maxima);
150 $maximum = $maxima[0];
151 //-----/end get ranges -----------------
154 // Prepare the x-axis
155 $x = new x_axis();
156 $x->set_range( $lowest, $highest );
158 // Calculate the steps and visible steps
159 $step= ($highest - $lowest)/60;
160 $step_vis=2;
162 // do not allow steps to be less than 30 minutes
163 if ($step < 26400) { # 86400
164 $step = 26400;
165 $step_vis=1;
168 $x->set_steps($step);
169 $labels = new x_axis_labels();
170 $labels->text('#date:Y-m-d#');
171 $labels->set_steps($step);
172 $labels->visible_steps($step_vis);
173 $labels->rotate(90);
174 $x->set_labels($labels);
176 // Prepare the y-axis
177 $y = new y_axis(); // $maximum is already set above
179 // set the range and y-step
180 $y->set_range( 0 , $maximum + getIdealYSteps( $maximum ) );
181 $y->set_steps( getIdealYSteps( $maximum ) );
183 # $chart->add_element( $s );
184 $chart->set_x_axis( $x );
185 $chart->add_y_axis( $y );
188 // echo a pretty ofc-string anyway
189 echo $chart->toPrettyString();