DataSeriesValues.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Chart;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. use PhpOffice\PhpSpreadsheet\Calculation\Functions;
  5. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  6. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  7. class DataSeriesValues
  8. {
  9. const DATASERIES_TYPE_STRING = 'String';
  10. const DATASERIES_TYPE_NUMBER = 'Number';
  11. private static $dataTypeValues = [
  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 int
  43. */
  44. private $pointCount = 0;
  45. /**
  46. * Data Values.
  47. *
  48. * @var array of mixed
  49. */
  50. private $dataValues = [];
  51. /**
  52. * Fill color.
  53. *
  54. * @var string
  55. */
  56. private $fillColor;
  57. /**
  58. * Line Width.
  59. *
  60. * @var int
  61. */
  62. private $lineWidth = 12700;
  63. /**
  64. * Create a new DataSeriesValues object.
  65. *
  66. * @param string $dataType
  67. * @param string $dataSource
  68. * @param null|mixed $formatCode
  69. * @param int $pointCount
  70. * @param mixed $dataValues
  71. * @param null|mixed $marker
  72. * @param null|string $fillColor
  73. */
  74. public function __construct($dataType = self::DATASERIES_TYPE_NUMBER, $dataSource = null, $formatCode = null, $pointCount = 0, $dataValues = [], $marker = null, $fillColor = 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. $this->fillColor = $fillColor;
  83. }
  84. /**
  85. * Get Series Data Type.
  86. *
  87. * @return string
  88. */
  89. public function getDataType()
  90. {
  91. return $this->dataType;
  92. }
  93. /**
  94. * Set Series Data Type.
  95. *
  96. * @param string $dataType Datatype of this data series
  97. * Typical values are:
  98. * DataSeriesValues::DATASERIES_TYPE_STRING
  99. * Normally used for axis point values
  100. * DataSeriesValues::DATASERIES_TYPE_NUMBER
  101. * Normally used for chart data values
  102. *
  103. * @throws Exception
  104. *
  105. * @return DataSeriesValues
  106. */
  107. public function setDataType($dataType)
  108. {
  109. if (!in_array($dataType, self::$dataTypeValues)) {
  110. throw new Exception('Invalid datatype for chart data series values');
  111. }
  112. $this->dataType = $dataType;
  113. return $this;
  114. }
  115. /**
  116. * Get Series Data Source (formula).
  117. *
  118. * @return string
  119. */
  120. public function getDataSource()
  121. {
  122. return $this->dataSource;
  123. }
  124. /**
  125. * Set Series Data Source (formula).
  126. *
  127. * @param string $dataSource
  128. *
  129. * @return DataSeriesValues
  130. */
  131. public function setDataSource($dataSource)
  132. {
  133. $this->dataSource = $dataSource;
  134. return $this;
  135. }
  136. /**
  137. * Get Point Marker.
  138. *
  139. * @return string
  140. */
  141. public function getPointMarker()
  142. {
  143. return $this->pointMarker;
  144. }
  145. /**
  146. * Set Point Marker.
  147. *
  148. * @param string $marker
  149. *
  150. * @return DataSeriesValues
  151. */
  152. public function setPointMarker($marker)
  153. {
  154. $this->pointMarker = $marker;
  155. return $this;
  156. }
  157. /**
  158. * Get Series Format Code.
  159. *
  160. * @return string
  161. */
  162. public function getFormatCode()
  163. {
  164. return $this->formatCode;
  165. }
  166. /**
  167. * Set Series Format Code.
  168. *
  169. * @param string $formatCode
  170. *
  171. * @return DataSeriesValues
  172. */
  173. public function setFormatCode($formatCode)
  174. {
  175. $this->formatCode = $formatCode;
  176. return $this;
  177. }
  178. /**
  179. * Get Series Point Count.
  180. *
  181. * @return int
  182. */
  183. public function getPointCount()
  184. {
  185. return $this->pointCount;
  186. }
  187. /**
  188. * Get fill color.
  189. *
  190. * @return string HEX color
  191. */
  192. public function getFillColor()
  193. {
  194. return $this->fillColor;
  195. }
  196. /**
  197. * Set fill color for series.
  198. *
  199. * @param string $color HEX color
  200. *
  201. * @return DataSeriesValues
  202. */
  203. public function setFillColor($color)
  204. {
  205. if (!preg_match('/^[a-f0-9]{6}$/i', $color)) {
  206. throw new Exception('Invalid hex color for chart series');
  207. }
  208. $this->fillColor = $color;
  209. return $this;
  210. }
  211. /**
  212. * Get line width for series.
  213. *
  214. * @return int
  215. */
  216. public function getLineWidth()
  217. {
  218. return $this->lineWidth;
  219. }
  220. /**
  221. * Set line width for the series.
  222. *
  223. * @param int $width
  224. *
  225. * @return DataSeriesValues
  226. */
  227. public function setLineWidth($width)
  228. {
  229. $minWidth = 12700;
  230. $this->lineWidth = max($minWidth, $width);
  231. return $this;
  232. }
  233. /**
  234. * Identify if the Data Series is a multi-level or a simple series.
  235. *
  236. * @return null|bool
  237. */
  238. public function isMultiLevelSeries()
  239. {
  240. if (count($this->dataValues) > 0) {
  241. return is_array($this->dataValues[0]);
  242. }
  243. return null;
  244. }
  245. /**
  246. * Return the level count of a multi-level Data Series.
  247. *
  248. * @return int
  249. */
  250. public function multiLevelCount()
  251. {
  252. $levelCount = 0;
  253. foreach ($this->dataValues as $dataValueSet) {
  254. $levelCount = max($levelCount, count($dataValueSet));
  255. }
  256. return $levelCount;
  257. }
  258. /**
  259. * Get Series Data Values.
  260. *
  261. * @return array of mixed
  262. */
  263. public function getDataValues()
  264. {
  265. return $this->dataValues;
  266. }
  267. /**
  268. * Get the first Series Data value.
  269. *
  270. * @return mixed
  271. */
  272. public function getDataValue()
  273. {
  274. $count = count($this->dataValues);
  275. if ($count == 0) {
  276. return null;
  277. } elseif ($count == 1) {
  278. return $this->dataValues[0];
  279. }
  280. return $this->dataValues;
  281. }
  282. /**
  283. * Set Series Data Values.
  284. *
  285. * @param array $dataValues
  286. *
  287. * @return DataSeriesValues
  288. */
  289. public function setDataValues($dataValues)
  290. {
  291. $this->dataValues = Functions::flattenArray($dataValues);
  292. $this->pointCount = count($dataValues);
  293. return $this;
  294. }
  295. public function refresh(Worksheet $worksheet, $flatten = true)
  296. {
  297. if ($this->dataSource !== null) {
  298. $calcEngine = Calculation::getInstance($worksheet->getParent());
  299. $newDataValues = Calculation::unwrapResult(
  300. $calcEngine->_calculateFormulaValue(
  301. '=' . $this->dataSource,
  302. null,
  303. $worksheet->getCell('A1')
  304. )
  305. );
  306. if ($flatten) {
  307. $this->dataValues = Functions::flattenArray($newDataValues);
  308. foreach ($this->dataValues as &$dataValue) {
  309. if ((!empty($dataValue)) && ($dataValue[0] == '#')) {
  310. $dataValue = 0.0;
  311. }
  312. }
  313. unset($dataValue);
  314. } else {
  315. $cellRange = explode('!', $this->dataSource);
  316. if (count($cellRange) > 1) {
  317. list(, $cellRange) = $cellRange;
  318. }
  319. $dimensions = Coordinate::rangeDimension(str_replace('$', '', $cellRange));
  320. if (($dimensions[0] == 1) || ($dimensions[1] == 1)) {
  321. $this->dataValues = Functions::flattenArray($newDataValues);
  322. } else {
  323. $newArray = array_values(array_shift($newDataValues));
  324. foreach ($newArray as $i => $newDataSet) {
  325. $newArray[$i] = [$newDataSet];
  326. }
  327. foreach ($newDataValues as $newDataSet) {
  328. $i = 0;
  329. foreach ($newDataSet as $newDataVal) {
  330. array_unshift($newArray[$i++], $newDataVal);
  331. }
  332. }
  333. $this->dataValues = $newArray;
  334. }
  335. }
  336. $this->pointCount = count($this->dataValues);
  337. }
  338. }
  339. }