Chart.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Element;
  18. use PhpOffice\PhpWord\Style\Chart as ChartStyle;
  19. /**
  20. * Chart element
  21. *
  22. * @since 0.12.0
  23. */
  24. class Chart extends AbstractElement
  25. {
  26. /**
  27. * Is part of collection
  28. *
  29. * @var bool
  30. */
  31. protected $collectionRelation = true;
  32. /**
  33. * Type
  34. *
  35. * @var string
  36. */
  37. private $type = 'pie';
  38. /**
  39. * Series
  40. *
  41. * @var array
  42. */
  43. private $series = array();
  44. /**
  45. * Chart style
  46. *
  47. * @var \PhpOffice\PhpWord\Style\Chart
  48. */
  49. private $style;
  50. /**
  51. * Create new instance
  52. *
  53. * @param string $type
  54. * @param array $categories
  55. * @param array $values
  56. * @param array $style
  57. * @param null|mixed $seriesName
  58. */
  59. public function __construct($type, $categories, $values, $style = null, $seriesName = null)
  60. {
  61. $this->setType($type);
  62. $this->addSeries($categories, $values, $seriesName);
  63. $this->style = $this->setNewStyle(new ChartStyle(), $style, true);
  64. }
  65. /**
  66. * Get type
  67. *
  68. * @return string
  69. */
  70. public function getType()
  71. {
  72. return $this->type;
  73. }
  74. /**
  75. * Set type.
  76. *
  77. * @param string $value
  78. */
  79. public function setType($value)
  80. {
  81. $enum = array('pie', 'doughnut', 'line', 'bar', 'stacked_bar', 'percent_stacked_bar', 'column', 'stacked_column', 'percent_stacked_column', 'area', 'radar', 'scatter');
  82. $this->type = $this->setEnumVal($value, $enum, 'pie');
  83. }
  84. /**
  85. * Add series
  86. *
  87. * @param array $categories
  88. * @param array $values
  89. * @param null|mixed $name
  90. */
  91. public function addSeries($categories, $values, $name = null)
  92. {
  93. $this->series[] = array(
  94. 'categories' => $categories,
  95. 'values' => $values,
  96. 'name' => $name,
  97. );
  98. }
  99. /**
  100. * Get series
  101. *
  102. * @return array
  103. */
  104. public function getSeries()
  105. {
  106. return $this->series;
  107. }
  108. /**
  109. * Get chart style
  110. *
  111. * @return \PhpOffice\PhpWord\Style\Chart
  112. */
  113. public function getStyle()
  114. {
  115. return $this->style;
  116. }
  117. }