DataSeries.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Chart;
  3. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  4. class DataSeries
  5. {
  6. const TYPE_BARCHART = 'barChart';
  7. const TYPE_BARCHART_3D = 'bar3DChart';
  8. const TYPE_LINECHART = 'lineChart';
  9. const TYPE_LINECHART_3D = 'line3DChart';
  10. const TYPE_AREACHART = 'areaChart';
  11. const TYPE_AREACHART_3D = 'area3DChart';
  12. const TYPE_PIECHART = 'pieChart';
  13. const TYPE_PIECHART_3D = 'pie3DChart';
  14. const TYPE_DOUGHNUTCHART = 'doughnutChart';
  15. const TYPE_DONUTCHART = self::TYPE_DOUGHNUTCHART; // Synonym
  16. const TYPE_SCATTERCHART = 'scatterChart';
  17. const TYPE_SURFACECHART = 'surfaceChart';
  18. const TYPE_SURFACECHART_3D = 'surface3DChart';
  19. const TYPE_RADARCHART = 'radarChart';
  20. const TYPE_BUBBLECHART = 'bubbleChart';
  21. const TYPE_STOCKCHART = 'stockChart';
  22. const TYPE_CANDLECHART = self::TYPE_STOCKCHART; // Synonym
  23. const GROUPING_CLUSTERED = 'clustered';
  24. const GROUPING_STACKED = 'stacked';
  25. const GROUPING_PERCENT_STACKED = 'percentStacked';
  26. const GROUPING_STANDARD = 'standard';
  27. const DIRECTION_BAR = 'bar';
  28. const DIRECTION_HORIZONTAL = self::DIRECTION_BAR;
  29. const DIRECTION_COL = 'col';
  30. const DIRECTION_COLUMN = self::DIRECTION_COL;
  31. const DIRECTION_VERTICAL = self::DIRECTION_COL;
  32. const STYLE_LINEMARKER = 'lineMarker';
  33. const STYLE_SMOOTHMARKER = 'smoothMarker';
  34. const STYLE_MARKER = 'marker';
  35. const STYLE_FILLED = 'filled';
  36. /**
  37. * Series Plot Type.
  38. *
  39. * @var string
  40. */
  41. private $plotType;
  42. /**
  43. * Plot Grouping Type.
  44. *
  45. * @var string
  46. */
  47. private $plotGrouping;
  48. /**
  49. * Plot Direction.
  50. *
  51. * @var string
  52. */
  53. private $plotDirection;
  54. /**
  55. * Plot Style.
  56. *
  57. * @var null|string
  58. */
  59. private $plotStyle;
  60. /**
  61. * Order of plots in Series.
  62. *
  63. * @var array of integer
  64. */
  65. private $plotOrder = [];
  66. /**
  67. * Plot Label.
  68. *
  69. * @var array of DataSeriesValues
  70. */
  71. private $plotLabel = [];
  72. /**
  73. * Plot Category.
  74. *
  75. * @var array of DataSeriesValues
  76. */
  77. private $plotCategory = [];
  78. /**
  79. * Smooth Line.
  80. *
  81. * @var bool
  82. */
  83. private $smoothLine;
  84. /**
  85. * Plot Values.
  86. *
  87. * @var array of DataSeriesValues
  88. */
  89. private $plotValues = [];
  90. /**
  91. * Create a new DataSeries.
  92. *
  93. * @param null|mixed $plotType
  94. * @param null|mixed $plotGrouping
  95. * @param int[] $plotOrder
  96. * @param DataSeriesValues[] $plotLabel
  97. * @param DataSeriesValues[] $plotCategory
  98. * @param DataSeriesValues[] $plotValues
  99. * @param null|string $plotDirection
  100. * @param bool $smoothLine
  101. * @param null|string $plotStyle
  102. */
  103. public function __construct($plotType = null, $plotGrouping = null, array $plotOrder = [], array $plotLabel = [], array $plotCategory = [], array $plotValues = [], $plotDirection = null, $smoothLine = false, $plotStyle = null)
  104. {
  105. $this->plotType = $plotType;
  106. $this->plotGrouping = $plotGrouping;
  107. $this->plotOrder = $plotOrder;
  108. $keys = array_keys($plotValues);
  109. $this->plotValues = $plotValues;
  110. if ((count($plotLabel) == 0) || ($plotLabel[$keys[0]] === null)) {
  111. $plotLabel[$keys[0]] = new DataSeriesValues();
  112. }
  113. $this->plotLabel = $plotLabel;
  114. if ((count($plotCategory) == 0) || ($plotCategory[$keys[0]] === null)) {
  115. $plotCategory[$keys[0]] = new DataSeriesValues();
  116. }
  117. $this->plotCategory = $plotCategory;
  118. $this->smoothLine = $smoothLine;
  119. $this->plotStyle = $plotStyle;
  120. if ($plotDirection === null) {
  121. $plotDirection = self::DIRECTION_COL;
  122. }
  123. $this->plotDirection = $plotDirection;
  124. }
  125. /**
  126. * Get Plot Type.
  127. *
  128. * @return string
  129. */
  130. public function getPlotType()
  131. {
  132. return $this->plotType;
  133. }
  134. /**
  135. * Set Plot Type.
  136. *
  137. * @param string $plotType
  138. *
  139. * @return DataSeries
  140. */
  141. public function setPlotType($plotType)
  142. {
  143. $this->plotType = $plotType;
  144. return $this;
  145. }
  146. /**
  147. * Get Plot Grouping Type.
  148. *
  149. * @return string
  150. */
  151. public function getPlotGrouping()
  152. {
  153. return $this->plotGrouping;
  154. }
  155. /**
  156. * Set Plot Grouping Type.
  157. *
  158. * @param string $groupingType
  159. *
  160. * @return DataSeries
  161. */
  162. public function setPlotGrouping($groupingType)
  163. {
  164. $this->plotGrouping = $groupingType;
  165. return $this;
  166. }
  167. /**
  168. * Get Plot Direction.
  169. *
  170. * @return string
  171. */
  172. public function getPlotDirection()
  173. {
  174. return $this->plotDirection;
  175. }
  176. /**
  177. * Set Plot Direction.
  178. *
  179. * @param string $plotDirection
  180. *
  181. * @return DataSeries
  182. */
  183. public function setPlotDirection($plotDirection)
  184. {
  185. $this->plotDirection = $plotDirection;
  186. return $this;
  187. }
  188. /**
  189. * Get Plot Order.
  190. *
  191. * @return string
  192. */
  193. public function getPlotOrder()
  194. {
  195. return $this->plotOrder;
  196. }
  197. /**
  198. * Get Plot Labels.
  199. *
  200. * @return array of DataSeriesValues
  201. */
  202. public function getPlotLabels()
  203. {
  204. return $this->plotLabel;
  205. }
  206. /**
  207. * Get Plot Label by Index.
  208. *
  209. * @param mixed $index
  210. *
  211. * @return DataSeriesValues
  212. */
  213. public function getPlotLabelByIndex($index)
  214. {
  215. $keys = array_keys($this->plotLabel);
  216. if (in_array($index, $keys)) {
  217. return $this->plotLabel[$index];
  218. } elseif (isset($keys[$index])) {
  219. return $this->plotLabel[$keys[$index]];
  220. }
  221. return false;
  222. }
  223. /**
  224. * Get Plot Categories.
  225. *
  226. * @return array of DataSeriesValues
  227. */
  228. public function getPlotCategories()
  229. {
  230. return $this->plotCategory;
  231. }
  232. /**
  233. * Get Plot Category by Index.
  234. *
  235. * @param mixed $index
  236. *
  237. * @return DataSeriesValues
  238. */
  239. public function getPlotCategoryByIndex($index)
  240. {
  241. $keys = array_keys($this->plotCategory);
  242. if (in_array($index, $keys)) {
  243. return $this->plotCategory[$index];
  244. } elseif (isset($keys[$index])) {
  245. return $this->plotCategory[$keys[$index]];
  246. }
  247. return false;
  248. }
  249. /**
  250. * Get Plot Style.
  251. *
  252. * @return null|string
  253. */
  254. public function getPlotStyle()
  255. {
  256. return $this->plotStyle;
  257. }
  258. /**
  259. * Set Plot Style.
  260. *
  261. * @param null|string $plotStyle
  262. *
  263. * @return DataSeries
  264. */
  265. public function setPlotStyle($plotStyle)
  266. {
  267. $this->plotStyle = $plotStyle;
  268. return $this;
  269. }
  270. /**
  271. * Get Plot Values.
  272. *
  273. * @return array of DataSeriesValues
  274. */
  275. public function getPlotValues()
  276. {
  277. return $this->plotValues;
  278. }
  279. /**
  280. * Get Plot Values by Index.
  281. *
  282. * @param mixed $index
  283. *
  284. * @return DataSeriesValues
  285. */
  286. public function getPlotValuesByIndex($index)
  287. {
  288. $keys = array_keys($this->plotValues);
  289. if (in_array($index, $keys)) {
  290. return $this->plotValues[$index];
  291. } elseif (isset($keys[$index])) {
  292. return $this->plotValues[$keys[$index]];
  293. }
  294. return false;
  295. }
  296. /**
  297. * Get Number of Plot Series.
  298. *
  299. * @return int
  300. */
  301. public function getPlotSeriesCount()
  302. {
  303. return count($this->plotValues);
  304. }
  305. /**
  306. * Get Smooth Line.
  307. *
  308. * @return bool
  309. */
  310. public function getSmoothLine()
  311. {
  312. return $this->smoothLine;
  313. }
  314. /**
  315. * Set Smooth Line.
  316. *
  317. * @param bool $smoothLine
  318. *
  319. * @return DataSeries
  320. */
  321. public function setSmoothLine($smoothLine)
  322. {
  323. $this->smoothLine = $smoothLine;
  324. return $this;
  325. }
  326. public function refresh(Worksheet $worksheet)
  327. {
  328. foreach ($this->plotValues as $plotValues) {
  329. if ($plotValues !== null) {
  330. $plotValues->refresh($worksheet, true);
  331. }
  332. }
  333. foreach ($this->plotLabel as $plotValues) {
  334. if ($plotValues !== null) {
  335. $plotValues->refresh($worksheet, true);
  336. }
  337. }
  338. foreach ($this->plotCategory as $plotValues) {
  339. if ($plotValues !== null) {
  340. $plotValues->refresh($worksheet, false);
  341. }
  342. }
  343. }
  344. }