DataSeriesValues.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 (can be array with colors if dataseries have custom colors).
  53. *
  54. * @var string|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|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|string[] HEX color or array with HEX colors
  191. */
  192. public function getFillColor()
  193. {
  194. return $this->fillColor;
  195. }
  196. /**
  197. * Set fill color for series.
  198. *
  199. * @param string|string[] $color HEX color or array with HEX colors
  200. *
  201. * @return DataSeriesValues
  202. */
  203. public function setFillColor($color)
  204. {
  205. if (is_array($color)) {
  206. foreach ($color as $colorValue) {
  207. $this->validateColor($colorValue);
  208. }
  209. } else {
  210. $this->validateColor($color);
  211. }
  212. $this->fillColor = $color;
  213. return $this;
  214. }
  215. /**
  216. * Method for validating hex color.
  217. *
  218. * @param string $color value for color
  219. *
  220. * @throws \Exception thrown if color is invalid
  221. *
  222. * @return bool true if validation was successful
  223. */
  224. private function validateColor($color)
  225. {
  226. if (!preg_match('/^[a-f0-9]{6}$/i', $color)) {
  227. throw new Exception(sprintf('Invalid hex color for chart series (color: "%s")', $color));
  228. }
  229. return true;
  230. }
  231. /**
  232. * Get line width for series.
  233. *
  234. * @return int
  235. */
  236. public function getLineWidth()
  237. {
  238. return $this->lineWidth;
  239. }
  240. /**
  241. * Set line width for the series.
  242. *
  243. * @param int $width
  244. *
  245. * @return DataSeriesValues
  246. */
  247. public function setLineWidth($width)
  248. {
  249. $minWidth = 12700;
  250. $this->lineWidth = max($minWidth, $width);
  251. return $this;
  252. }
  253. /**
  254. * Identify if the Data Series is a multi-level or a simple series.
  255. *
  256. * @return null|bool
  257. */
  258. public function isMultiLevelSeries()
  259. {
  260. if (count($this->dataValues) > 0) {
  261. return is_array(array_values($this->dataValues)[0]);
  262. }
  263. return null;
  264. }
  265. /**
  266. * Return the level count of a multi-level Data Series.
  267. *
  268. * @return int
  269. */
  270. public function multiLevelCount()
  271. {
  272. $levelCount = 0;
  273. foreach ($this->dataValues as $dataValueSet) {
  274. $levelCount = max($levelCount, count($dataValueSet));
  275. }
  276. return $levelCount;
  277. }
  278. /**
  279. * Get Series Data Values.
  280. *
  281. * @return array of mixed
  282. */
  283. public function getDataValues()
  284. {
  285. return $this->dataValues;
  286. }
  287. /**
  288. * Get the first Series Data value.
  289. *
  290. * @return mixed
  291. */
  292. public function getDataValue()
  293. {
  294. $count = count($this->dataValues);
  295. if ($count == 0) {
  296. return null;
  297. } elseif ($count == 1) {
  298. return $this->dataValues[0];
  299. }
  300. return $this->dataValues;
  301. }
  302. /**
  303. * Set Series Data Values.
  304. *
  305. * @param array $dataValues
  306. *
  307. * @return DataSeriesValues
  308. */
  309. public function setDataValues($dataValues)
  310. {
  311. $this->dataValues = Functions::flattenArray($dataValues);
  312. $this->pointCount = count($dataValues);
  313. return $this;
  314. }
  315. public function refresh(Worksheet $worksheet, $flatten = true)
  316. {
  317. if ($this->dataSource !== null) {
  318. $calcEngine = Calculation::getInstance($worksheet->getParent());
  319. $newDataValues = Calculation::unwrapResult(
  320. $calcEngine->_calculateFormulaValue(
  321. '=' . $this->dataSource,
  322. null,
  323. $worksheet->getCell('A1')
  324. )
  325. );
  326. if ($flatten) {
  327. $this->dataValues = Functions::flattenArray($newDataValues);
  328. foreach ($this->dataValues as &$dataValue) {
  329. if ((!empty($dataValue)) && ($dataValue[0] == '#')) {
  330. $dataValue = 0.0;
  331. }
  332. }
  333. unset($dataValue);
  334. } else {
  335. list($worksheet, $cellRange) = Worksheet::extractSheetTitle($this->dataSource, true);
  336. $dimensions = Coordinate::rangeDimension(str_replace('$', '', $cellRange));
  337. if (($dimensions[0] == 1) || ($dimensions[1] == 1)) {
  338. $this->dataValues = Functions::flattenArray($newDataValues);
  339. } else {
  340. $newArray = array_values(array_shift($newDataValues));
  341. foreach ($newArray as $i => $newDataSet) {
  342. $newArray[$i] = [$newDataSet];
  343. }
  344. foreach ($newDataValues as $newDataSet) {
  345. $i = 0;
  346. foreach ($newDataSet as $newDataVal) {
  347. array_unshift($newArray[$i++], $newDataVal);
  348. }
  349. }
  350. $this->dataValues = $newArray;
  351. }
  352. }
  353. $this->pointCount = count($this->dataValues);
  354. }
  355. }
  356. }