5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2002-2014 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
12 const TYPE_DATE_BAR = 2;
16 const ERROR_UNKNOWN_TYPE = 3;
28 function __construct($GraphType, array $GraphData)
30 if (count($GraphData)==0)
32 $this->Status = self::NO_DATA;
34 elseif ($GraphType == self::TYPE_DATE ||
35 $GraphType == self::TYPE_DATE_BAR )
37 $this->Status = self::OK;
38 # Convert input data into a form that Javascript will deal with
40 if ($GraphType == self::TYPE_DATE)
42 $this->Data = $this->ToJsFormat($GraphData);
44 elseif ($GraphType == self::TYPE_DATE_BAR)
46 # Summarize the search data passed in into daily, weekly, monthly:
48 $WeeklyData = array();
49 $MonthlyData = array();
51 foreach ($GraphData as $Xval => $Yvals)
53 $DailyTS = strtotime( date(
'Y-m-d', $Xval) );
54 # Find the Monday preceeding this day for the weekly TS:
55 $WeeklyTS = $DailyTS - 86400* (date(
'N',$Xval) - 1);
56 $MonthlyTS = strtotime( date(
'Y-m-01', $Xval) );
58 $this->AddToArray( $DailyData, $DailyTS, $Yvals );
59 $this->AddToArray( $WeeklyData, $WeeklyTS, $Yvals );
60 $this->AddToArray( $MonthlyData, $MonthlyTS , $Yvals);
64 "Daily" => $this->ToJsFormat( $DailyData ),
65 "Weekly" => $this->ToJsFormat( $WeeklyData ),
66 "Monthly"=> $this->ToJsFormat( $MonthlyData ) );
69 $this->Type = $GraphType;
74 $this->LabelChars = strlen(max( array_map(
'max', $GraphData) ) ) ;
77 $this->LeftMargin = 5;
78 $this->RightMargin = 50;
79 $this->BottomMargin = 40;
81 $this->LabelFontSize = 14;
83 $this->Legend = array();
85 $this->Scale = self::DAILY;
90 $this->Status = self::ERROR_UNKNOWN_TYPE;
98 function Status(){
return $this->Status; }
104 function XLabel($Label){ $this->XLabel = $Label; }
110 function YLabel($Label){ $this->YLabel = $Label; }
116 function LabelFontSize($X){ $this->LabelFontSize = $X; }
122 function TopMargin($X) { $this->TopMargin = $X; }
128 function RightMargin($X) { $this->RightMargin = $X; }
134 function BottomMargin($X) { $this->BottomMargin = $X; }
140 function LeftMargin($X) { $this->LeftMargin = $X; }
146 function Width($X) { $this->Width = $X; }
152 function Height($X) { $this->Height = $X; }
158 function Legend($X) { $this->Legend = $X; }
164 function Scale($X) { $this->Scale = $X; }
170 function Title($X) { $this->Title = $X; }
177 if ($this->Status == self::NO_DATA)
180 print
"<p><em>No data to display</em></p>";
184 if ($this->Status != self::OK)
187 $ChartNumber = self::GetNumber();
192 <?
PHP if($ChartNumber==0) { ?>
193 <script type=
"text/javascript" src=
"<?PHP $AF->PUIFile("d3.js
"); ?>"></script>
194 <script type=
"text/javascript" src=
"<?PHP $AF->PUIFile("CW-Graph.js
"); ?>"></script>
196 .cw-chart { width: 100%; }
197 .cw-chart-button { padding: 5px; margin-bottom: 15px; cursor: pointer; }
198 svg { font: 12px sans-serif;}
199 .axis { shape-rendering: crispEdges; }
201 .axis path, .axis line {
206 .x.axis path { stroke: #000; }
207 .x.axis line { stroke: #fff; stroke-opacity: .5; }
208 .y.axis line { stroke: #ddd; }
226 .focus { fill: black; }
228 .line.graph_color0 { stroke: #4D7588; }
229 .line.graph_color1 { stroke: ##975078; }
231 .area.graph_color0 { fill: #4D7588; }
232 .area.graph_color1 { fill: #975078; }
233 .area.graph_color2 { fill: #818181; }
234 .area.graph_color3 { fill: #5CAA5C; }
235 .area.graph_color4 { fill: #DDBC6D; }
241 <?
PHP if ($this->Type == self::TYPE_DATE) { ?>
242 .x-label<?PHP print($ChartNumber); ?> { font-weight: bold; font-size: <?
PHP print $this->LabelFontSize; ?>px; }
243 .y-label<?PHP print($ChartNumber); ?> { font-weight: bold; font-size: <?
PHP print $this->LabelFontSize; ?>px; }
244 <?
PHP } elseif ($this->Type == self::TYPE_DATE_BAR) { ?>
245 .x-label<?PHP print($ChartNumber); ?>a { font-weight: bold; font-size: <?
PHP print $this->LabelFontSize; ?>px; }
246 .y-label<?PHP print($ChartNumber); ?>a { font-weight: bold; font-size: <?
PHP print $this->LabelFontSize; ?>px; }
248 .x-label<?PHP print($ChartNumber); ?>b { font-weight: bold; font-size: <?
PHP print $this->LabelFontSize; ?>px; }
249 .y-label<?PHP print($ChartNumber); ?>b { font-weight: bold; font-size: <?
PHP print $this->LabelFontSize; ?>px; }
251 .x-label<?PHP print($ChartNumber); ?>c { font-weight: bold; font-size: <?
PHP print $this->LabelFontSize; ?>px; }
252 .y-label<?PHP print($ChartNumber); ?>c { font-weight: bold; font-size: <?
PHP print $this->LabelFontSize; ?>px; }
256 <?
PHP if ($this->Type == self::TYPE_DATE) { ?>
257 <?
PHP print $this->Title; ?>
258 <div
class=
"cw-chart" id=
"chart<?PHP print $ChartNumber; ?>"></div>
259 <?
PHP } elseif ($this->Type == self::TYPE_DATE_BAR) { ?>
260 <div style=
"max-width: <?PHP print $this->Width; ?>px;">
263 <span style=
"float: right; margin-top: 3px; padding-right: <?PHP print $this->LabelChars + 1 ; ?>em; margin-right: <?PHP print $this->RightMargin; ?>px; ">
264 <span
class=
"cw-chart-button" id=
"cw-chart-button<?PHP print $ChartNumber; ?>a"
265 <?
PHP if ($this->Scale == self::DAILY) { ?> style=
"font-weight: bold;" <?
PHP } ?>
267 <span
class=
"cw-chart-button" id=
"cw-chart-button<?PHP print $ChartNumber; ?>b"
268 <?
PHP if ($this->Scale == self::WEEKLY) { ?> style=
"font-weight: bold;" <?
PHP } ?>
270 <span
class=
"cw-chart-button" id=
"cw-chart-button<?PHP print $ChartNumber; ?>c"
271 <?
PHP if ($this->Scale == self::MONTHLY) { ?> style=
"font-weight: bold;" <?
PHP } ?>
274 <?
PHP print $this->Title; ?>
277 <div
id=
"chart<?PHP print $ChartNumber; ?>">
278 <div
class=
"cw-chart" id=
"chart<?PHP print $ChartNumber; ?>a"
279 <?
PHP if ($this->Scale != self::DAILY) { ?> style=
"display: none;" <?
PHP } ?> ></div>
280 <div
class=
"cw-chart" id=
"chart<?PHP print $ChartNumber; ?>b"
281 <?
PHP if ($this->Scale != self::WEEKLY) { ?> style=
"display: none;" <?
PHP } ?> ></div>
282 <div
class=
"cw-chart" id=
"chart<?PHP print $ChartNumber; ?>c"
283 <?
PHP if ($this->Scale != self::MONTHLY) { ?> style=
"display: none;" <?
PHP } ?> ></div>
286 <script type=
"text/javascript">
287 jQuery(
'#cw-chart-button<?PHP print $ChartNumber; ?>a').click(
function(){
288 jQuery(
'#cw-chart-button<?PHP print $ChartNumber; ?>a').css(
'font-weight',
'bold');
289 jQuery(
'#cw-chart-button<?PHP print $ChartNumber; ?>b').css(
'font-weight',
'normal');
290 jQuery(
'#cw-chart-button<?PHP print $ChartNumber; ?>c').css(
'font-weight',
'normal');
291 jQuery(
'#chart<?PHP print $ChartNumber; ?>a').show();
292 jQuery(
'#chart<?PHP print $ChartNumber; ?>b').hide();
293 jQuery(
'#chart<?PHP print $ChartNumber; ?>c').hide();
295 jQuery(
'#cw-chart-button<?PHP print $ChartNumber; ?>b').click(
function(){
296 jQuery(
'#cw-chart-button<?PHP print $ChartNumber; ?>a').css(
'font-weight',
'normal');
297 jQuery(
'#cw-chart-button<?PHP print $ChartNumber; ?>b').css(
'font-weight',
'bold');
298 jQuery(
'#cw-chart-button<?PHP print $ChartNumber; ?>c').css(
'font-weight',
'normal');
299 jQuery(
'#chart<?PHP print $ChartNumber; ?>a').hide();
300 jQuery(
'#chart<?PHP print $ChartNumber; ?>b').show();
301 jQuery(
'#chart<?PHP print $ChartNumber; ?>c').hide();
303 jQuery(
'#cw-chart-button<?PHP print $ChartNumber; ?>c').click(
function(){
304 jQuery(
'#cw-chart-button<?PHP print $ChartNumber; ?>a').css(
'font-weight',
'normal');
305 jQuery(
'#cw-chart-button<?PHP print $ChartNumber; ?>b').css(
'font-weight',
'normal');
306 jQuery(
'#cw-chart-button<?PHP print $ChartNumber; ?>c').css(
'font-weight',
'bold');
307 jQuery(
'#chart<?PHP print $ChartNumber; ?>a').hide();
308 jQuery(
'#chart<?PHP print $ChartNumber; ?>b').hide();
309 jQuery(
'#chart<?PHP print $ChartNumber; ?>c').show();
314 <script type=
"text/javascript">
315 <?
PHP if ($this->Type == self::TYPE_DATE ) { ?>
316 jQuery(document).ready(
function(){
317 new LineDateGraph(<?
PHP print($ChartNumber); ?>,
318 <?
PHP print(json_encode($this->Data)); ?>,
319 "<?PHP print($this->XLabel); ?>",
320 "<?PHP print($this->YLabel); ?>",
321 {top: <?
PHP print $this->TopMargin; ?>, right: <?
PHP print $this->RightMargin; ?>,
322 bottom: <?
PHP print $this->BottomMargin; ?>, left: <?
PHP print $this->LeftMargin; ?> },
323 <?
PHP print($this->Width); ?>, <?
PHP print($this->Height); ?>, <?
PHP print(json_encode($this->Legend)); ?> ); });
324 <?
PHP }
else if ($this->Type == self::TYPE_DATE_BAR) { ?>
325 jQuery(document).ready(
function(){
327 "<?PHP print($ChartNumber); ?>a",
328 <?
PHP print(json_encode($this->Data[
"Daily"])); ?>,
329 "<?PHP print($this->XLabel); ?>",
330 "<?PHP print($this->YLabel); ?> (Daily)",
331 {top: <?
PHP print $this->TopMargin; ?>, right: <?
PHP print $this->RightMargin; ?>,
332 bottom: <?
PHP print $this->BottomMargin; ?>, left: <?
PHP print $this->LeftMargin; ?> },
333 <?
PHP print($this->Width); ?>, <?
PHP print($this->Height); ?>, <?
PHP print(json_encode($this->Legend)); ?>,
334 <?
PHP print(22*3600); ?>); });
335 jQuery(document).ready(
function(){
337 "<?PHP print($ChartNumber); ?>b",
338 <?
PHP print(json_encode($this->Data[
"Weekly"])); ?>,
339 "<?PHP print($this->XLabel); ?>",
340 "<?PHP print($this->YLabel); ?> (Weekly)",
341 {top: <?
PHP print $this->TopMargin; ?>, right: <?
PHP print $this->RightMargin; ?>,
342 bottom: <?
PHP print $this->BottomMargin; ?>, left: <?
PHP print $this->LeftMargin; ?> },
343 <?
PHP print($this->Width); ?>, <?
PHP print($this->Height); ?>, <?
PHP print(json_encode($this->Legend)); ?>,
344 <?
PHP print(7*86400); ?>); });
345 jQuery(document).ready(
function(){
347 "<?PHP print($ChartNumber); ?>c",
348 <?
PHP print(json_encode($this->Data[
"Monthly"])); ?>,
349 "<?PHP print($this->XLabel); ?>",
350 "<?PHP print($this->YLabel); ?> (Monthly)",
351 {top: <?
PHP print $this->TopMargin; ?>, right: <?
PHP print $this->RightMargin; ?>,
352 bottom: <?
PHP print $this->BottomMargin; ?>, left: <?
PHP print $this->LeftMargin; ?> },
353 <?
PHP print($this->Width); ?>, <?
PHP print($this->Height); ?>, <?
PHP print(json_encode($this->Legend)); ?>,
354 <?
PHP print(28*86400); ?>); });
362 private static $ChartNumber = 0;
363 private function GetNumber(){
return self::$ChartNumber++; }
373 private function AddToArray(&$Array, $Key, $Value)
375 if (!isset($Array[$Key]))
376 $Array[$Key] = $Value;
378 $Array[$Key] = array_map(
function($a,$b){
return $a+$b; },
379 $Array[$Key], $Value );
390 private function ToJsFormat($Data, $IsDate=TRUE)
393 foreach ($Data as $Xval => $Yvals)
399 $DataRow[
"X"] = 1000 * $Xval;
401 $DataRow[
"X"] = $Xval;
404 foreach ($Yvals as $Yval)
405 $DataRow[
"Y".$Count++ ] = $Yval;
407 $Result []= $DataRow;