CWIS Developer Documentation

- Home
- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

Chart_Base.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: Chart_Base.php
4 #
5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2017 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
8 #
9
13 abstract class Chart_Base
14 {
15 # ---- PUBLIC INTERFACE --------------------------------------------------
16
26 public function LegendPosition($Position)
27 {
28 if (!in_array($Position,
29 [self::LEGEND_BOTTOM, self::LEGEND_RIGHT,
30 self::LEGEND_INSET, self::LEGEND_NONE]))
31 {
32 throw new Exception("Unsupported legend position: ".$Position);
33 }
34 $this->LegendPosition = $Position;
35 }
36
43 public function LegendLabels($LegendLabels)
44 {
45 $this->LegendLabels = $LegendLabels;
46 }
47
54 public function Colors($NewValue=NULL)
55 {
56 if (!is_null($NewValue))
57 {
58 $this->Colors = $NewValue;
59 }
60
61 return $this->Colors;
62 }
63
68 public function Height($NewValue)
69 {
70 if (!is_null($NewValue))
71 {
72 $this->Height = $NewValue;
73 }
74
75 return $this->Height;
76 }
77
82 public function Width($NewValue)
83 {
84 if (!is_null($NewValue))
85 {
86 $this->Width = $NewValue;
87 }
88
89 return $this->Width;
90 }
91
124 public function Display($ContainerId)
125 {
126 # be sure the necessary js and css are loaded
127 $GLOBALS["AF"]->RequireUIFile("d3.js");
128 $GLOBALS["AF"]->RequireUIFile("c3.js");
129 $GLOBALS["AF"]->RequireUIFile("c3.css");
130 $GLOBALS["AF"]->RequireUIFile("Chart_Base.css");
131
132 # declare the chart data that we will give to c3.generate
133 # for function callbacks, give the function a name ending with
134 # '_fn' and include the function name as a string
135 $this->Chart = [
136 "bindto" => "#".$ContainerId,
137 "size" => [
138 "height" => $this->Height,
139 "width" => $this->Width,
140 ],
141 "tooltip" => [
142 "format" => [
143 "name" => "tooltip_name_fn",
144 ],
145 ],
146 ];
147
148 # set up legend positioning
149 if ($this->LegendPosition == self::LEGEND_NONE)
150 {
151 $this->Chart["legend"]["show"] = FALSE;
152 }
153 else
154 {
155 $this->Chart["legend"]["position"] = $this->LegendPosition;
156 }
157
158 # if the user provided a color palette, set that up as will
159 if (!is_null($this->Colors()))
160 {
161 # sort user-provided colors into the correct order
162 $Palette = [];
163 foreach ($this->Data as $Label => $Value)
164 {
165 $Palette[]= isset($this->Colors[$Label]) ?
166 $this->Colors[$Label] :
167 $this->GenerateRgbColorString($Label);
168 }
169
170 $this->Chart["color"]["pattern"] = $Palette;
171 }
172
173 static::PrepareData();
174
175
176 // @codingStandardsIgnoreStart
177 ?><div id="<?= $ContainerId ?>" class="cw-<?= strtolower(get_called_class()) ?>"></div>
178 <script type="text/javascript">
179 $(document).ready(function(){
180 // define state variables for this chart
181 var label_lut = <?= json_encode($this->LabelLUT) ?>;
182 <?PHP static::DeclareStateVariables(); ?>
183
184 // define helper functions for this chart
185 function tooltip_name_fn(name, ratio, id, index) {
186 return name in label_lut ? label_lut[name] : name;
187 }
188 <?PHP static::DeclareHelperFunctions(); ?>
189
190 // get the chart spec data
191 var chart_spec = <?= json_encode($this->Chart) ?>;
192
193 // convert any strings that refer to functions into callable function refs
194 function eval_fns(obj){
195 for (var prop in obj) {
196 if (typeof obj[prop] == "object") {
197 eval_fns(obj[prop]);
198 } else if (typeof obj[prop] == "string" && obj[prop].match(/_fn$/)) {
199 obj[prop] = eval(obj[prop]);
200 }
201 }
202 }
203 eval_fns(chart_spec);
204
205 // generate the chart
206 c3.generate(chart_spec);
207 });
208 </script><?PHP
209 // @codingStandardsIgnoreEnd
210 }
211
212 # legend position constants
213 const LEGEND_BOTTOM = "bottom";
214 const LEGEND_RIGHT = "right";
215 const LEGEND_INSET = "inset";
216 const LEGEND_NONE = "none";
217
218 # ---- PRIVATE INTERFACE --------------------------------------------------
219
225 abstract protected function PrepareData();
226
231 protected function DeclareStateVariables()
232 {
233 return;
234 }
235
242 protected function DeclareHelperFunctions()
243 {
244 return;
245 }
246
252 protected function GenerateRgbColorString($DataIndex)
253 {
254 return "#".substr(md5($DataIndex), 0, 6);
255 }
256
261 protected function AddToChart($Data)
262 {
263 $this->AddToArray($this->Chart, $Data);
264 }
265
271 protected function AddToArray(&$Tgt, $Data)
272 {
273 foreach ($Data as $Key => $Val)
274 {
275 if (isset($Tgt[$Key]) &&
276 is_array($Tgt[$Key]) && is_array($Val))
277 {
278 $this->AddToArray($Tgt[$Key], $Val);
279 }
280 else
281 {
282 $Tgt[$Key] = $Val;
283 }
284 }
285 }
286
287 # data provided by caller
288 protected $Data = [];
289
290 # chart parameters that can be changed prior to generation
291 protected $LegendPosition = self::LEGEND_BOTTOM;
292 protected $Colors = NULL;
293 protected $LegendLabels = [];
294 protected $Height = 600;
295 protected $Width = 600;
296
297 # internal variables used to generate the chart
298 protected $LabelLUT = [];
299 protected $Chart = NULL;
300 }
Chart_Base\$Chart
$Chart
Definition: Chart_Base.php:299
Chart_Base\$LegendLabels
$LegendLabels
Definition: Chart_Base.php:293
Chart_Base
Base class for generating and displaying a chart.
Definition: Chart_Base.php:13
Chart_Base\LEGEND_INSET
const LEGEND_INSET
Definition: Chart_Base.php:215
Chart_Base\DeclareStateVariables
DeclareStateVariables()
Output var declarations for any js state variables needed in this chart&#39;s display helper functions...
Definition: Chart_Base.php:231
Chart_Base\Height
Height($NewValue)
Get/Set height of the chart including the legend.
Definition: Chart_Base.php:68
Chart_Base\Width
Width($NewValue)
Get/Set width of the chart including the legend.
Definition: Chart_Base.php:82
Chart_Base\LegendLabels
LegendLabels($LegendLabels)
Set shortened labels to be used in the legend of the chart.
Definition: Chart_Base.php:43
Chart_Base\$LabelLUT
$LabelLUT
Definition: Chart_Base.php:298
Chart_Base\Display
Display($ContainerId)
Display a chart.
Definition: Chart_Base.php:124
Chart_Base\$Width
$Width
Definition: Chart_Base.php:295
Chart_Base\$Colors
$Colors
Definition: Chart_Base.php:292
Chart_Base\DeclareHelperFunctions
DeclareHelperFunctions()
Output function definitions for any needed javascript display helper functions.
Definition: Chart_Base.php:242
PHP
PHP
Definition: RSSClient--Test.php:12
Chart_Base\AddToArray
AddToArray(&$Tgt, $Data)
Merge elements from a source array into a dest array.
Definition: Chart_Base.php:271
Chart_Base\AddToChart
AddToChart($Data)
Merge an array of settings into $this->Chart.
Definition: Chart_Base.php:261
Chart_Base\$Data
$Data
Definition: Chart_Base.php:288
Chart_Base\LEGEND_BOTTOM
const LEGEND_BOTTOM
Definition: Chart_Base.php:213
Chart_Base\$Height
$Height
Definition: Chart_Base.php:294
Chart_Base\Colors
Colors($NewValue=NULL)
Set color palette.
Definition: Chart_Base.php:54
Chart_Base\LEGEND_NONE
const LEGEND_NONE
Definition: Chart_Base.php:216
Chart_Base\$LegendPosition
$LegendPosition
Definition: Chart_Base.php:291
Chart_Base\LEGEND_RIGHT
const LEGEND_RIGHT
Definition: Chart_Base.php:214
Chart_Base\PrepareData
PrepareData()
Massage data provided by the user into an appropriate format for plotting and do any necessary tweaks...
Chart_Base\LegendPosition
LegendPosition($Position)
Set legend position.
Definition: Chart_Base.php:26
Chart_Base\GenerateRgbColorString
GenerateRgbColorString($DataIndex)
Get RGB hex color when no color supplied.
Definition: Chart_Base.php:252