CWIS Developer Documentation
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 
54  # label type constants
55  const LABEL_PERCENT = "Percent";
56  const LABEL_RAW = "Raw";
57  const LABEL_NAME = "Name";
58 
59  # ---- PRIVATE INTERFACE --------------------------------------------------
60 
64  protected function PrepareData()
65  {
66  $this->Chart["data"]["columns"] = [];
67  foreach ($this->Data as $Label => $Value)
68  {
69  if (isset($this->LegendLabels[$Label]))
70  {
71  $MyLabel = $this->LegendLabels[$Label];
72  $this->LabelLUT[$MyLabel] = $Label;
73  }
74  else
75  {
76  $MyLabel = $Label;
77  }
78 
79  $this->Chart["data"]["columns"][]= [$MyLabel, $Value];
80  }
81 
82  $this->AddToChart([
83  "data" => [
84  "type" => "pie",
85  ],
86  "pie" => [
87  "label" => [
88  "format" => "label_format_fn",
89  ],
90  ],
91  ]);
92  }
93 
98  protected function DeclareHelperFunctions()
99  {
100  // @codingStandardsIgnoreStart
101  ?>
102  function tooltip_value_fn(value, ratio, id, index) {
103  return value + " ("+(new Number(100*ratio)).toFixed(<?= $this->Precision ?>)+"%)";
104  }
105 
106  function label_format_fn(value, ratio, id, index) {
107  <?PHP if ($this->SliceLabelType == self::LABEL_PERCENT) { ?>
108  return (new Number(100*ratio)).toFixed(<?= $this->Precision ?>)+"%";
109  <?PHP } elseif ($this->SliceLabelType == self::LABEL_RAW){ ?>
110  return value;
111  <?PHP } elseif ($this->SliceLabelType == self::LABEL_NAME){ ?>
112  return id;
113  <?PHP } ?>
114  }
115  <?PHP
116  // @codingStandardsIgnoreEnd
117  }
118 
119  private $SliceLabelType = self::LABEL_PERCENT;
120  private $Precision = 1;
121 }
PrepareData()
Prepare data for display.
Definition: PieChart.php:64
const LABEL_PERCENT
Definition: PieChart.php:55
Base class for generating and displaying a chart.
Definition: Chart_Base.php:13
LegendLabels($LegendLabels)
Set shortened labels to be used in the legend of the chart.
Definition: Chart_Base.php:43
SliceLabelType($LabelType)
Set the style for slice labels.
Definition: PieChart.php:43
__construct($Data)
Class constructor.
Definition: PieChart.php:21
const LABEL_RAW
Definition: PieChart.php:56
AddToChart($Data)
Merge an array of settings into $this->Chart.
Definition: Chart_Base.php:261
const LABEL_NAME
Definition: PieChart.php:57
PercentPrecision($Prec)
Set the precision used to display percentages.
Definition: PieChart.php:30
Class for generating and displaying a pie chart.
Definition: PieChart.php:13
DeclareHelperFunctions()
Output javascript that declares helper functions used to display the chart.
Definition: PieChart.php:98