PageRenderTime 56ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/Chart.php

https://gitlab.com/goolic/PrestaShop
PHP | 194 lines | 138 code | 23 blank | 33 comment | 24 complexity | 1e65abba8edf5190826bb56c2797cddf MD5 | raw file
  1. <?php
  2. /**
  3. * 2007-2015 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2015 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. class ChartCore
  27. {
  28. protected static $poolId = 0;
  29. protected $width = 600;
  30. protected $height = 300;
  31. /* Time mode */
  32. protected $timeMode = false;
  33. protected $from;
  34. protected $to;
  35. protected $format;
  36. protected $granularity;
  37. protected $curves = array();
  38. /** @prototype void public static function init(void) */
  39. public static function init()
  40. {
  41. if (!self::$poolId) {
  42. ++self::$poolId;
  43. return true;
  44. }
  45. }
  46. /** @prototype void public function __construct() */
  47. public function __construct()
  48. {
  49. ++self::$poolId;
  50. }
  51. /** @prototype void public function setSize(int $width, int $height) */
  52. public function setSize($width, $height)
  53. {
  54. $this->width = (int)$width;
  55. $this->height = (int)$height;
  56. }
  57. /** @prototype void public function setTimeMode($from, $to, $granularity) */
  58. public function setTimeMode($from, $to, $granularity)
  59. {
  60. $this->granularity = $granularity;
  61. if (Validate::isDate($from)) {
  62. $from = strtotime($from);
  63. }
  64. $this->from = $from;
  65. if (Validate::isDate($to)) {
  66. $to = strtotime($to);
  67. }
  68. $this->to = $to;
  69. if ($granularity == 'd') {
  70. $this->format = '%d/%m/%y';
  71. }
  72. if ($granularity == 'w') {
  73. $this->format = '%d/%m/%y';
  74. }
  75. if ($granularity == 'm') {
  76. $this->format = '%m/%y';
  77. }
  78. if ($granularity == 'y') {
  79. $this->format = '%y';
  80. }
  81. $this->timeMode = true;
  82. }
  83. public function getCurve($i)
  84. {
  85. if (!array_key_exists($i, $this->curves)) {
  86. $this->curves[$i] = new Curve();
  87. }
  88. return $this->curves[$i];
  89. }
  90. /** @prototype void public function display() */
  91. public function display()
  92. {
  93. echo $this->fetch();
  94. }
  95. public function fetch()
  96. {
  97. if ($this->timeMode) {
  98. $options = 'xaxis:{mode:"time",timeformat:\''.addslashes($this->format).'\',min:'.$this->from.'000,max:'.$this->to.'000}';
  99. if ($this->granularity == 'd') {
  100. foreach ($this->curves as $curve) {
  101. /** @var Curve $curve */
  102. for ($i = $this->from; $i <= $this->to; $i = strtotime('+1 day', $i)) {
  103. if (!$curve->getPoint($i)) {
  104. $curve->setPoint($i, 0);
  105. }
  106. }
  107. }
  108. }
  109. }
  110. $jsCurves = array();
  111. foreach ($this->curves as $curve) {
  112. $jsCurves[] = $curve->getValues($this->timeMode);
  113. }
  114. if (count($jsCurves)) {
  115. return '
  116. <div id="flot'.self::$poolId.'" style="width:'.$this->width.'px;height:'.$this->height.'px"></div>
  117. <script type="text/javascript">
  118. $(function () {
  119. $.plot($(\'#flot'.self::$poolId.'\'), ['.implode(',', $jsCurves).'], {'.$options.'});
  120. });
  121. </script>';
  122. } else {
  123. return ErrorFacade::Display(PS_ERROR_UNDEFINED, 'No values for this chart.');
  124. }
  125. }
  126. }
  127. class Curve
  128. {
  129. protected $values = array();
  130. protected $label;
  131. protected $type;
  132. /** @prototype void public function setValues($values) */
  133. public function setValues($values)
  134. {
  135. $this->values = $values;
  136. }
  137. public function getValues($time_mode = false)
  138. {
  139. ksort($this->values);
  140. $string = '';
  141. foreach ($this->values as $key => $value) {
  142. $string .= '['.addslashes((string)$key).($time_mode ? '000' : '').','.(float)$value.'],';
  143. }
  144. return '{data:['.rtrim($string, ',').']'.(!empty($this->label) ? ',label:"'.$this->label.'"' : '').''.(!empty($this->type) ? ','.$this->type : '').'}';
  145. }
  146. /** @prototype void public function setPoint(float $x, float $y) */
  147. public function setPoint($x, $y)
  148. {
  149. $this->values[(string)$x] = (float)$y;
  150. }
  151. public function setLabel($label)
  152. {
  153. $this->label = $label;
  154. }
  155. public function setType($type)
  156. {
  157. $this->type = '';
  158. if ($type == 'bars') {
  159. $this->type = 'bars:{show:true,lineWidth:10}';
  160. }
  161. if ($type == 'steps') {
  162. $this->type = 'lines:{show:true,steps:true}';
  163. }
  164. }
  165. public function getPoint($x)
  166. {
  167. if (array_key_exists((string)$x, $this->values)) {
  168. return $this->values[(string)$x];
  169. }
  170. }
  171. }