3
require_once($ADEI_ROOTDIR . "/classes/jpgraph.php");
5
class histogramview extends VIEW {
6
var $title = "Histogram";
8
function __construct(REQUEST $req = NULL, $opts) {
9
//$this->shelldisplay = false;
10
parent::__construct($req, $opts);
11
$this->object = $this->req->GetProp("view_object", false);
14
$this->max_points = $this->GetOption('max_points', 500);
16
$this->max_points = $this->GetOption('max_points', 5000);
18
$this->min_width = $this->GetOption('min_width', 300);
19
$this->min_height = $this->GetOption('min_height', 300);
21
$this->num_bins = $this->GetOption('bins', array(0));
24
function GetOption($opt, $default = false) {
25
if (isset($this->options[$opt])) return $this->options[$opt];
29
function GetOptions() {
30
$req = $this->req->CreateGroupRequest();
31
$x = $req->GetProp("view_x", false);
32
if ($x) list($x_gid, $x_id) = explode(":", $x);
33
else list($x_gid, $x_id) = array(0, 0);
35
$rdr = $req->CreateReader();
36
$group = $rdr->CreateGroup();
37
$caches = $rdr->CreateCacheSet($group, $mask);
41
foreach ($caches as $key => $cachewrap) {
42
array_push($result, array("label" => $cachewrap->GetGroupTitle(), "disabled" => 1));
45
$list = $cachewrap->GetItemList();
46
foreach ($list as $id => $info) {
47
$title = $info['name'];
48
array_push($result, array("value" => "$gid:$id", "label" => " $title"));
55
foreach ($this->num_bins as $bin) {
56
array_push($bins, array("value" => $bin, "label" => ($bin?$bin:"Auto")));
59
$checkboxNorm = array("label" => _("Normalize"), "id" => "hist_norm");
60
if ($req->GetProp("view_hist_norm", 0)) $checkboxNorm["checked"] = "checked";
62
$checkboxGFit = array("label" => _("Gaussian Fit"), "id" => "hist_fit");
63
if ($req->GetProp("view_hist_fit", 0)) $checkboxGFit["checked"] = "checked";
66
array("select" => array("id" => "x", "label" => _("x"), "options" => $result)),
67
array("select" => array("id" => "bins" , "label" => _("Bins"), "options" => $bins)),
68
array("checkbox" => $checkboxNorm),
69
array("checkbox" => $checkboxGFit)
76
$req = $this->req->CreateDataRequest();
77
$x = $req->GetProp("view_x", false);
79
if (!$x) throw new ADEIException(translate("Parameter view_x is not set"));
81
list($x_gid, $x_id) = explode(":", $x);
84
$width = $req->GetProp($this->object . "_width", $this->min_width + 20) - 20;
85
if ($width < $this->min_width) $width = $this->min_width;
86
$height = $width - 40;//$req->GetProp($this->object . "_height", $this->min_height);
88
$width = $req->GetProp("page_width", $this->min_width + 5) - 5;
89
$height = $req->GetProp("page_height", $this->min_height);
90
if ($width < $this->min_width) $width = $this->min_width;
91
if ($height < $this->min_height) $height = $this->min_height;
95
$rdr = $req->CreateReader();
96
$group = $rdr->CreateGroup();
97
$caches = $rdr->CreateCacheSet($group, $mask);
100
$myreq = $this->req->CreateDataRequest();
101
$iv = $caches->CreateInterval($req, true);
102
$window_size = $iv->GetWindowSize();
103
$window_start = $iv->GetWindowStart();
104
$window_end = $iv->GetWindowEnd();
107
'limit' => $this->max_points,
114
foreach ($caches as $key => $cachewrap) {
115
if (($gid != $x_gid)) {
120
$resolution = $cachewrap->GetResolution();
121
$r = $resolution->Get($iv, $width);
123
$size = $resolution->GetWindowSize($r);
124
if (($size > 0)&&(($window_size / $size) > $this->max_points)) {
125
$new_r = $resolution->Larger($r);
126
if ($new_r !== false) $r = $new_r;
127
$size = $resolution->GetWindowSize($r);
130
$rescfg['resolution'] = $r;
132
$points = $cachewrap->GetIntervals($iv, $rescfg, CACHE::TRUNCATE_INTERVALS);
133
$operation_info = $points->GetOperationInfo();
135
if ($gid == $x_gid) $res_x = $size;
138
foreach($points as $t => $v) {
139
/* if (($t < $window_start)||(($t + $size) > $window_end)) {
142
if (($gid == $x_gid)&&(is_numeric($v['mean'.$x_id]))) {
143
if (!is_array($res[$t])) $res[$t] = array();
144
$res[$t]['x'] = $v['mean'.$x_id];
154
foreach ($res as $val) {
155
if (isset($val['x'])) array_push($x, $val['x']);
159
throw new ADEIException(translate("No data found"));
162
$bins = $req->GetProp("view_bins", 0);
163
if (!$bins) $bins = ceil(sqrt(sizeof($x)));
165
$norm = $req->GetProp("view_hist_norm", 0);
166
$fit = $req->GetProp("view_hist_fit", 0);
171
$step = ($max - $min) / $bins;
173
$coef = $norm?(1/($step * sizeof($x))):1;
175
$h = array_fill(0, $bins, 0);
176
foreach ($x as $val) {
177
$idx = ($val - $min)/$step;
178
if ($idx == $bins) $idx--;
182
for($i = 0 ; $i < $bins ; $i++)
183
array_push($t, sprintf("%3.1e", $min + $i*$step));
185
$tmp_file1 = ADEI::GetTmpFile();
187
$graph = new Graph($width,$height);
188
/* $title = "Resolution: $res_x";
190
$graph->title->SetFont(FF_ARIAL,FS_BOLD,10);
191
$graph->title->Set($title);*/
192
$graph->SetTickDensity(TICKD_SPARSE, TICKD_SPARSE);
193
$graph->img->SetMargin(55,5,10,20);
195
$graph->SetScale("textlin");
196
$graph->xaxis->SetPos("min");
197
$graph->yaxis->SetPos("min");
198
// $graph->xaxis->SetLabelFormat('%3.1e');
199
// if (abs(max($h))<9999 && (abs(min($h))>0.01)) $graph->yaxis->SetLabelFormat('%01.2f');
200
// else $graph->yaxis->SetLabelFormat('%3.1e');
201
$graph->xaxis->SetFont(FF_ARIAL,FS_NORMAL,8);
202
$graph->yaxis->SetFont(FF_ARIAL,FS_NORMAL,8);
203
// $graph->yaxis->HideFirstTickLabel();
205
$graph->xaxis->title->SetFont(FF_ARIAL,FS_BOLD);
206
$graph->yaxis->title->SetFont(FF_ARIAL,FS_BOLD);
208
//$graph->xaxis->title->Set($arr[0]['select']['options'][$x_idg]['label']);
209
if($bins > 8) $graph->xaxis->SetTextLabelInterval(ceil(($bins / 6)));
210
$graph->xaxis->SetTickLabels($t);
212
$bplot = new BarPlot($h);
217
$graph->yaxis->scale->SetGrace(14);
219
$mean = array_sum($x)/sizeof($x);
220
$stddev = stats_standard_deviation($x);
221
$var = stats_variance($x);
223
$re = 100 * $sigma/$mean;
227
$median = $x[(sizeof($x) - 1)/2];
229
$median = ($x[sizeof($x)/2 - 1] + $x[sizeof($x)/2])/2;
236
if ($norm) $coef = (1/(sqrt(2 * pi() * $var)));
237
else $coef = ((sizeof($x)*$step)/(sqrt(2 * pi() * $var)));
240
for($i = 0; $i <= $bins; $i++) {
241
$offset = $i * $step;
242
$y = $coef * exp(-pow($min + $offset - $mean, 2)/(2*$var));
243
array_push($xdata, $i);
244
array_push($ydata, $y);
245
$xi2 += (pow($y - $h[$i], 2)) / $y;
249
$lineplot = new LinePlot($ydata , $xdata);
250
$graph->Add($lineplot);
253
$char_sigma = SymChar::Get('sigma', false);
257
$txt->SetFont(FF_ARIAL,FS_BOLD,10);
258
if( $req->GetProp("view_GFit", false) == "true")
259
$txt->Set("m=$mean\n$char_sigma=$sigma\nRE=$RE%\nx^2=$xi2");//\ns=$stdDev
261
$txt->Set("m=$mean\n$char_sigma=$sigma\nRE=$RE%");//\ns=$stdDev
262
$txt->ParagraphAlign('right');
263
$txt->SetPos(0.96,0.1,'right');
264
//$txt->SetBox('white');
268
$graph->Stroke("$TMP_PATH/$tmp_file1");
272
array("img" => array("id" => $tmp_file1)),
273
array("info" => array(
274
array("title"=>_("From"), "value" => date('c', $iv->GetWindowStart())),
275
array("title"=>_("To"), "value" => date('c', $iv->GetWindowEnd())),
276
array("title"=>_("Resolution"), "value" => $res_x),
277
array("title"=>_("Bins"), "value" => $bins),
278
array("title"=>_("First Bin"), "value" => $min),
279
array("title"=>_("Last Bin"), "value" => $min + $bins * $step),
280
array("title"=>_("Mean"), "value" => $mean),
281
array("title"=>_("Median"), "value" => $median),
282
array("title"=>_("StdDev"), "value" => $stddev),
283
array("title"=>_("Sigma"), "value" => $sigma),
284
array("title"=>_("RE"), "value" => ($re . "%")),
288
array_push($res[1]["info"], array("title"=>_("xi2"), "value" => $xi2));
293
"img" => array("id" => $tmp_file)