DataSeriesValues.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2013 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Chart
  23. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.9, 2013-06-02
  26. */
  27. /**
  28. * PHPExcel_Chart_DataSeriesValues
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Chart
  32. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Chart_DataSeriesValues
  35. {
  36. const DATASERIES_TYPE_STRING = 'String';
  37. const DATASERIES_TYPE_NUMBER = 'Number';
  38. private static $_dataTypeValues = array(
  39. self::DATASERIES_TYPE_STRING,
  40. self::DATASERIES_TYPE_NUMBER,
  41. );
  42. /**
  43. * Series Data Type
  44. *
  45. * @var string
  46. */
  47. private $_dataType = null;
  48. /**
  49. * Series Data Source
  50. *
  51. * @var string
  52. */
  53. private $_dataSource = null;
  54. /**
  55. * Format Code
  56. *
  57. * @var string
  58. */
  59. private $_formatCode = null;
  60. /**
  61. * Series Point Marker
  62. *
  63. * @var string
  64. */
  65. private $_marker = null;
  66. /**
  67. * Point Count (The number of datapoints in the dataseries)
  68. *
  69. * @var integer
  70. */
  71. private $_pointCount = 0;
  72. /**
  73. * Data Values
  74. *
  75. * @var array of mixed
  76. */
  77. private $_dataValues = array();
  78. /**
  79. * Create a new PHPExcel_Chart_DataSeriesValues object
  80. */
  81. public function __construct($dataType = self::DATASERIES_TYPE_NUMBER, $dataSource = null, $formatCode = null, $pointCount = 0, $dataValues = array(), $marker = null)
  82. {
  83. $this->setDataType($dataType);
  84. $this->_dataSource = $dataSource;
  85. $this->_formatCode = $formatCode;
  86. $this->_pointCount = $pointCount;
  87. $this->_dataValues = $dataValues;
  88. $this->_marker = $marker;
  89. }
  90. /**
  91. * Get Series Data Type
  92. *
  93. * @return string
  94. */
  95. public function getDataType() {
  96. return $this->_dataType;
  97. }
  98. /**
  99. * Set Series Data Type
  100. *
  101. * @param string $dataType Datatype of this data series
  102. * Typical values are:
  103. * PHPExcel_Chart_DataSeriesValues::DATASERIES_TYPE_STRING
  104. * Normally used for axis point values
  105. * PHPExcel_Chart_DataSeriesValues::DATASERIES_TYPE_NUMBER
  106. * Normally used for chart data values
  107. * @return PHPExcel_Chart_DataSeriesValues
  108. */
  109. public function setDataType($dataType = self::DATASERIES_TYPE_NUMBER) {
  110. if (!in_array($dataType, self::$_dataTypeValues)) {
  111. throw new PHPExcel_Chart_Exception('Invalid datatype for chart data series values');
  112. }
  113. $this->_dataType = $dataType;
  114. return $this;
  115. }
  116. /**
  117. * Get Series Data Source (formula)
  118. *
  119. * @return string
  120. */
  121. public function getDataSource() {
  122. return $this->_dataSource;
  123. }
  124. /**
  125. * Set Series Data Source (formula)
  126. *
  127. * @param string $dataSource
  128. * @return PHPExcel_Chart_DataSeriesValues
  129. */
  130. public function setDataSource($dataSource = null, $refreshDataValues = true) {
  131. $this->_dataSource = $dataSource;
  132. if ($refreshDataValues) {
  133. // TO DO
  134. }
  135. return $this;
  136. }
  137. /**
  138. * Get Point Marker
  139. *
  140. * @return string
  141. */
  142. public function getPointMarker() {
  143. return $this->_marker;
  144. }
  145. /**
  146. * Set Point Marker
  147. *
  148. * @param string $marker
  149. * @return PHPExcel_Chart_DataSeriesValues
  150. */
  151. public function setPointMarker($marker = null) {
  152. $this->_marker = $marker;
  153. return $this;
  154. }
  155. /**
  156. * Get Series Format Code
  157. *
  158. * @return string
  159. */
  160. public function getFormatCode() {
  161. return $this->_formatCode;
  162. }
  163. /**
  164. * Set Series Format Code
  165. *
  166. * @param string $formatCode
  167. * @return PHPExcel_Chart_DataSeriesValues
  168. */
  169. public function setFormatCode($formatCode = null) {
  170. $this->_formatCode = $formatCode;
  171. return $this;
  172. }
  173. /**
  174. * Get Series Point Count
  175. *
  176. * @return integer
  177. */
  178. public function getPointCount() {
  179. return $this->_pointCount;
  180. }
  181. /**
  182. * Identify if the Data Series is a multi-level or a simple series
  183. *
  184. * @return boolean
  185. */
  186. public function isMultiLevelSeries() {
  187. if (count($this->_dataValues) > 0) {
  188. return is_array($this->_dataValues[0]);
  189. }
  190. return null;
  191. }
  192. /**
  193. * Return the level count of a multi-level Data Series
  194. *
  195. * @return boolean
  196. */
  197. public function multiLevelCount() {
  198. $levelCount = 0;
  199. foreach($this->_dataValues as $dataValueSet) {
  200. $levelCount = max($levelCount,count($dataValueSet));
  201. }
  202. return $levelCount;
  203. }
  204. /**
  205. * Get Series Data Values
  206. *
  207. * @return array of mixed
  208. */
  209. public function getDataValues() {
  210. return $this->_dataValues;
  211. }
  212. /**
  213. * Get the first Series Data value
  214. *
  215. * @return mixed
  216. */
  217. public function getDataValue() {
  218. $count = count($this->_dataValues);
  219. if ($count == 0) {
  220. return null;
  221. } elseif ($count == 1) {
  222. return $this->_dataValues[0];
  223. }
  224. return $this->_dataValues;
  225. }
  226. /**
  227. * Set Series Data Values
  228. *
  229. * @param array $dataValues
  230. * @param boolean $refreshDataSource
  231. * TRUE - refresh the value of _dataSource based on the values of $dataValues
  232. * FALSE - don't change the value of _dataSource
  233. * @return PHPExcel_Chart_DataSeriesValues
  234. */
  235. public function setDataValues($dataValues = array(), $refreshDataSource = TRUE) {
  236. $this->_dataValues = PHPExcel_Calculation_Functions::flattenArray($dataValues);
  237. $this->_pointCount = count($dataValues);
  238. if ($refreshDataSource) {
  239. // TO DO
  240. }
  241. return $this;
  242. }
  243. private function _stripNulls($var) {
  244. return $var !== NULL;
  245. }
  246. public function refresh(PHPExcel_Worksheet $worksheet, $flatten = TRUE) {
  247. if ($this->_dataSource !== NULL) {
  248. $calcEngine = PHPExcel_Calculation::getInstance($worksheet->getParent());
  249. $newDataValues = PHPExcel_Calculation::_unwrapResult(
  250. $calcEngine->_calculateFormulaValue(
  251. '='.$this->_dataSource,
  252. NULL,
  253. $worksheet->getCell('A1')
  254. )
  255. );
  256. if ($flatten) {
  257. $this->_dataValues = PHPExcel_Calculation_Functions::flattenArray($newDataValues);
  258. foreach($this->_dataValues as &$dataValue) {
  259. if ((!empty($dataValue)) && ($dataValue[0] == '#')) {
  260. $dataValue = 0.0;
  261. }
  262. }
  263. unset($dataValue);
  264. } else {
  265. $cellRange = explode('!',$this->_dataSource);
  266. if (count($cellRange) > 1) {
  267. list(,$cellRange) = $cellRange;
  268. }
  269. $dimensions = PHPExcel_Cell::rangeDimension(str_replace('$','',$cellRange));
  270. if (($dimensions[0] == 1) || ($dimensions[1] == 1)) {
  271. $this->_dataValues = PHPExcel_Calculation_Functions::flattenArray($newDataValues);
  272. } else {
  273. $newArray = array_values(array_shift($newDataValues));
  274. foreach($newArray as $i => $newDataSet) {
  275. $newArray[$i] = array($newDataSet);
  276. }
  277. foreach($newDataValues as $newDataSet) {
  278. $i = 0;
  279. foreach($newDataSet as $newDataVal) {
  280. array_unshift($newArray[$i++],$newDataVal);
  281. }
  282. }
  283. $this->_dataValues = $newArray;
  284. }
  285. }
  286. $this->_pointCount = count($this->_dataValues);
  287. }
  288. }
  289. }