CWIS Developer Documentation

- Home
- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

PieChart.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: PieChart.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 class PieChart extends Chart_Base
14 {
15 # ---- PUBLIC INTERFACE --------------------------------------------------
16
21 public function __construct($Data)
22 {
23 $this->Data = $Data;
24 }
25
30 public function PercentPrecision($Prec)
31 {
32 $this->Precision = $Prec;
33 }
34
43 public function SliceLabelType($LabelType)
44 {
45 if (!in_array($LabelType,
46 [static::LABEL_PERCENT, static::LABEL_NAME, static::LABEL_RAW]))
47 {
48 throw new Exception("Unsupported slice label type: ".$LabelType);
49 }
50
51 $this->SliceLabelType = $LabelType;
52 }
53
61 public function TooltipType($LabelType)
62 {
63 if (!in_array($LabelType,
64 [static::TOOLTIP_PERCENT, static::TOOLTIP_VALUE,
65 static::TOOLTIP_BOTH]))
66 {
67 throw new Exception("Unsupported tooltip label type: ".$LabelType);
68 }
69
70 $this->TooltipLabelType = $LabelType;
71 }
72
73 # label type constants
74 const LABEL_PERCENT = "Percent";
75 const LABEL_RAW = "Raw";
76 const LABEL_NAME = "Name";
77
78 # tooltip type constants
79 const TOOLTIP_VALUE = "Value";
80 const TOOLTIP_PERCENT = "Percent";
81 const TOOLTIP_BOTH = "Both";
82
83 # ---- PRIVATE INTERFACE --------------------------------------------------
84
88 protected function PrepareData()
89 {
90 # see http://c3js.org/reference.html#data-columns for format of 'columns' element.
91 # since C3 always uses the label in 'columns' for the legend,
92 # we'll need to populate the TooltipLabels array that is keyed
93 # by legend label where values give the tooltip label
94 $this->Chart["data"]["columns"] = [];
95 foreach ($this->Data as $Index => $Value)
96 {
97 $Label = isset($this->Labels[$Index]) ?
98 $this->Labels[$Index] : $Index ;
99
100 if (isset($this->LegendLabels[$Index]))
101 {
102 $MyLabel = $this->LegendLabels[$Index];
103 $this->TooltipLabels[$MyLabel] = $Label;
104 }
105 else
106 {
107 $MyLabel = $Label;
108 }
109
110 $this->Chart["data"]["columns"][]= [$MyLabel, $Value];
111 }
112
113 $this->AddToChart([
114 "data" => [
115 "type" => "pie",
116 ],
117 "pie" => [
118 "label" => [
119 "format" => "label_format_fn",
120 ],
121 ],
122 "tooltip" => [
123 "format" => [
124 "value" => "tooltip_value_fn",
125 ],
126 ],
127 ]);
128 }
129
134 protected function DeclareHelperFunctions()
135 {
136 // @codingStandardsIgnoreStart
137 ?>
138 function tooltip_value_fn(value, ratio, id, index) {
139 <?PHP if ($this->TooltipLabelType == self::TOOLTIP_BOTH) { ?>
140 return (new Number(100*ratio)).toFixed(<?= $this->Precision ?>)+"%&nbsp;("+value+")";
141 <?PHP } elseif ($this->TooltipLabelType == self::TOOLTIP_PERCENT){ ?>
142 return (new Number(100*ratio)).toFixed(<?= $this->Precision ?>)+"%";
143 <?PHP } elseif ($this->TooltipLabelType == self::TOOLTIP_VALUE){ ?>
144 return value;
145 <?PHP } ?>
146 }
147
148 function label_format_fn(value, ratio, id, index) {
149 <?PHP if ($this->SliceLabelType == self::LABEL_PERCENT) { ?>
150 return (new Number(100*ratio)).toFixed(<?= $this->Precision ?>)+"%";
151 <?PHP } elseif ($this->SliceLabelType == self::LABEL_RAW){ ?>
152 return value;
153 <?PHP } elseif ($this->SliceLabelType == self::LABEL_NAME){ ?>
154 return id;
155 <?PHP } ?>
156 }
157 <?PHP
158 // @codingStandardsIgnoreEnd
159 }
160
161 private $SliceLabelType = self::LABEL_PERCENT;
162 private $TooltipLabelType = self::TOOLTIP_BOTH;
163 private $Precision = 1;
164 }
PieChart\PrepareData
PrepareData()
Prepare data for display.
Definition: PieChart.php:88
PieChart\LABEL_PERCENT
const LABEL_PERCENT
Definition: PieChart.php:74
Chart_Base
Base class for generating and displaying a chart.
Definition: Chart_Base.php:13
PieChart\TOOLTIP_PERCENT
const TOOLTIP_PERCENT
Definition: PieChart.php:80
Chart_Base\Labels
Labels($NewValue=NULL)
Get/set chart element labels (pie slices, bars, etc).
Definition: Chart_Base.php:57
Chart_Base\LegendLabels
LegendLabels($LegendLabels)
Set shortened labels to be used in the legend of the chart.
Definition: Chart_Base.php:43
PieChart\TOOLTIP_BOTH
const TOOLTIP_BOTH
Definition: PieChart.php:81
PieChart\SliceLabelType
SliceLabelType($LabelType)
Set the style for slice labels.
Definition: PieChart.php:43
PieChart\__construct
__construct($Data)
Class constructor.
Definition: PieChart.php:21
PHP
PHP
Definition: RSSClient--Test.php:12
PieChart\LABEL_RAW
const LABEL_RAW
Definition: PieChart.php:75
Chart_Base\AddToChart
AddToChart($Data)
Merge an array of settings into $this->Chart.
Definition: Chart_Base.php:298
Chart_Base\$Data
$Data
Definition: Chart_Base.php:325
PieChart\LABEL_NAME
const LABEL_NAME
Definition: PieChart.php:76
PieChart\PercentPrecision
PercentPrecision($Prec)
Set the precision used to display percentages.
Definition: PieChart.php:30
PieChart\TooltipType
TooltipType($LabelType)
Set the style for values shown in the tooltip.
Definition: PieChart.php:61
PieChart
Class for generating and displaying a pie chart.
Definition: PieChart.php:13
PieChart\TOOLTIP_VALUE
const TOOLTIP_VALUE
Definition: PieChart.php:79
PieChart\DeclareHelperFunctions
DeclareHelperFunctions()
Output javascript that declares helper functions used to display the chart.
Definition: PieChart.php:134