DataSeriesValues.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. /**
  3. * PHPExcel_Chart_DataSeriesValues
  4. *
  5. * Copyright (c) 2006 - 2015 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 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. class PHPExcel_Chart_DataSeriesValues
  28. {
  29. const DATASERIES_TYPE_STRING = 'String';
  30. const DATASERIES_TYPE_NUMBER = 'Number';
  31. private static $dataTypeValues = array(
  32. self::DATASERIES_TYPE_STRING,
  33. self::DATASERIES_TYPE_NUMBER,
  34. );
  35. /**
  36. * Series Data Type
  37. *
  38. * @var string
  39. */
  40. private $dataType;
  41. /**
  42. * Series Data Source
  43. *
  44. * @var string
  45. */
  46. private $dataSource;
  47. /**
  48. * Format Code
  49. *
  50. * @var string
  51. */
  52. private $formatCode;
  53. /**
  54. * Series Point Marker
  55. *
  56. * @var string
  57. */
  58. private $pointMarker;
  59. /**
  60. * Point Count (The number of datapoints in the dataseries)
  61. *
  62. * @var integer
  63. */
  64. private $pointCount = 0;
  65. /**
  66. * Data Values
  67. *
  68. * @var array of mixed
  69. */
  70. private $dataValues = array();
  71. /**
  72. * Create a new PHPExcel_Chart_DataSeriesValues object
  73. */
  74. public function __construct($dataType = self::DATASERIES_TYPE_NUMBER, $dataSource = null, $formatCode = null, $pointCount = 0, $dataValues = array(), $marker = null)
  75. {
  76. $this->setDataType($dataType);
  77. $this->dataSource = $dataSource;
  78. $this->formatCode = $formatCode;
  79. $this->pointCount = $pointCount;
  80. $this->dataValues = $dataValues;
  81. $this->pointMarker = $marker;
  82. }
  83. /**
  84. * Get Series Data Type
  85. *
  86. * @return string
  87. */
  88. public function getDataType()
  89. {
  90. return $this->dataType;
  91. }
  92. /**
  93. * Set Series Data Type
  94. *
  95. * @param string $dataType Datatype of this data series
  96. * Typical values are:
  97. * PHPExcel_Chart_DataSeriesValues::DATASERIES_TYPE_STRING
  98. * Normally used for axis point values
  99. * PHPExcel_Chart_DataSeriesValues::DATASERIES_TYPE_NUMBER
  100. * Normally used for chart data values
  101. * @return PHPExcel_Chart_DataSeriesValues
  102. */
  103. public function setDataType($dataType = self::DATASERIES_TYPE_NUMBER)
  104. {
  105. if (!in_array($dataType, self::$dataTypeValues)) {
  106. throw new PHPExcel_Chart_Exception('Invalid datatype for chart data series values');
  107. }
  108. $this->dataType = $dataType;
  109. return $this;
  110. }
  111. /**
  112. * Get Series Data Source (formula)
  113. *
  114. * @return string
  115. */
  116. public function getDataSource()
  117. {
  118. return $this->dataSource;
  119. }
  120. /**
  121. * Set Series Data Source (formula)
  122. *
  123. * @param string $dataSource
  124. * @return PHPExcel_Chart_DataSeriesValues
  125. */
  126. public function setDataSource($dataSource = null, $refreshDataValues = true)
  127. {
  128. $this->dataSource = $dataSource;
  129. if ($refreshDataValues) {
  130. // TO DO
  131. }
  132. return $this;
  133. }
  134. /**
  135. * Get Point Marker
  136. *
  137. * @return string
  138. */
  139. public function getPointMarker()
  140. {
  141. return $this->pointMarker;
  142. }
  143. /**
  144. * Set Point Marker
  145. *
  146. * @param string $marker
  147. * @return PHPExcel_Chart_DataSeriesValues
  148. */
  149. public function setPointMarker($marker = null)
  150. {
  151. $this->pointMarker = $marker;
  152. return $this;
  153. }
  154. /**
  155. * Get Series Format Code
  156. *
  157. * @return string
  158. */
  159. public function getFormatCode()
  160. {
  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. {
  171. $this->formatCode = $formatCode;
  172. return $this;
  173. }
  174. /**
  175. * Get Series Point Count
  176. *
  177. * @return integer
  178. */
  179. public function getPointCount()
  180. {
  181. return $this->pointCount;
  182. }
  183. /**
  184. * Identify if the Data Series is a multi-level or a simple series
  185. *
  186. * @return boolean
  187. */
  188. public function isMultiLevelSeries()
  189. {
  190. if (count($this->dataValues) > 0) {
  191. return is_array($this->dataValues[0]);
  192. }
  193. return null;
  194. }
  195. /**
  196. * Return the level count of a multi-level Data Series
  197. *
  198. * @return boolean
  199. */
  200. public function multiLevelCount()
  201. {
  202. $levelCount = 0;
  203. foreach ($this->dataValues as $dataValueSet) {
  204. $levelCount = max($levelCount, count($dataValueSet));
  205. }
  206. return $levelCount;
  207. }
  208. /**
  209. * Get Series Data Values
  210. *
  211. * @return array of mixed
  212. */
  213. public function getDataValues()
  214. {
  215. return $this->dataValues;
  216. }
  217. /**
  218. * Get the first Series Data value
  219. *
  220. * @return mixed
  221. */
  222. public function getDataValue()
  223. {
  224. $count = count($this->dataValues);
  225. if ($count == 0) {
  226. return null;
  227. } elseif ($count == 1) {
  228. return $this->dataValues[0];
  229. }
  230. return $this->dataValues;
  231. }
  232. /**
  233. * Set Series Data Values
  234. *
  235. * @param array $dataValues
  236. * @param boolean $refreshDataSource
  237. * TRUE - refresh the value of dataSource based on the values of $dataValues
  238. * FALSE - don't change the value of dataSource
  239. * @return PHPExcel_Chart_DataSeriesValues
  240. */
  241. public function setDataValues($dataValues = array(), $refreshDataSource = true)
  242. {
  243. $this->dataValues = PHPExcel_Calculation_Functions::flattenArray($dataValues);
  244. $this->pointCount = count($dataValues);
  245. if ($refreshDataSource) {
  246. // TO DO
  247. }
  248. return $this;
  249. }
  250. private function stripNulls($var)
  251. {
  252. return $var !== null;
  253. }
  254. public function refresh(PHPExcel_Worksheet $worksheet, $flatten = true)
  255. {
  256. if ($this->dataSource !== null) {
  257. $calcEngine = PHPExcel_Calculation::getInstance($worksheet->getParent());
  258. $newDataValues = PHPExcel_Calculation::unwrapResult(
  259. $calcEngine->_calculateFormulaValue(
  260. '='.$this->dataSource,
  261. null,
  262. $worksheet->getCell('A1')
  263. )
  264. );
  265. if ($flatten) {
  266. $this->dataValues = PHPExcel_Calculation_Functions::flattenArray($newDataValues);
  267. foreach ($this->dataValues as &$dataValue) {
  268. if ((!empty($dataValue)) && ($dataValue[0] == '#')) {
  269. $dataValue = 0.0;
  270. }
  271. }
  272. unset($dataValue);
  273. } else {
  274. $cellRange = explode('!', $this->dataSource);
  275. if (count($cellRange) > 1) {
  276. list(, $cellRange) = $cellRange;
  277. }
  278. $dimensions = PHPExcel_Cell::rangeDimension(str_replace('$', '', $cellRange));
  279. if (($dimensions[0] == 1) || ($dimensions[1] == 1)) {
  280. $this->dataValues = PHPExcel_Calculation_Functions::flattenArray($newDataValues);
  281. } else {
  282. $newArray = array_values(array_shift($newDataValues));
  283. foreach ($newArray as $i => $newDataSet) {
  284. $newArray[$i] = array($newDataSet);
  285. }
  286. foreach ($newDataValues as $newDataSet) {
  287. $i = 0;
  288. foreach ($newDataSet as $newDataVal) {
  289. array_unshift($newArray[$i++], $newDataVal);
  290. }
  291. }
  292. $this->dataValues = $newArray;
  293. }
  294. }
  295. $this->pointCount = count($this->dataValues);
  296. }
  297. }
  298. }