DataSeriesValues.php 8.2 KB

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