Graph.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /* GNU FM -- a free network service for sharing your music listening habits
  3. Copyright (C) 2009 Free Software Foundation, Inc
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Affero General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. require_once($install_path . '/database.php');
  16. require_once($install_path . '/data/Artist.php');
  17. require_once($install_path . '/data/Album.php');
  18. require_once($install_path . '/data/Server.php');
  19. require_once($install_path . '/data/Statistic.php');
  20. /**
  21. * Represents graph object, extended for specific implementations, currently
  22. * acts as a data only object but intention is to delegate as much functionality
  23. * with respect to the propagation of the graph objects themselves to this
  24. * class. Many methods are, therefore, included with this view in mind.
  25. *
  26. * @see GraphTypes.php for implementations
  27. **/
  28. class Graph {
  29. public $data, $data_buffer;
  30. public $user, $type, $renderer, $label_renderer;
  31. public $max_x_axis, $max_y_axis;
  32. public $tick_interval = 20;
  33. public $x_axis_label = 'X axis', $y_axis_label = 'Y Axis';
  34. /* Multidimensional array to allow for multiple series of data. */
  35. protected $graph_types = array(array());
  36. public static $DEFAULT_GRAPH_TYPE = 0;
  37. function __construct($user = null, $type = null) {
  38. $this->user = $user;
  39. $this->resetData();
  40. /* @todo: iterate through $type to determine renderer requirements */
  41. switch($type){}
  42. }
  43. /**
  44. * Resets internal data object.
  45. **/
  46. protected function resetData() {
  47. $this->data = array(array(array()));
  48. }
  49. /**
  50. * Returns the graph renderer as defined at object instantiation.
  51. **/
  52. public function getGraphRenderer() {
  53. return $this->graph_types[$this->renderer];
  54. }
  55. /**
  56. * Returns x-axis label.
  57. **/
  58. public function getXAxis() {
  59. return $this->x_axis_label;
  60. }
  61. /**
  62. * Returns y-axis label.
  63. **/
  64. public function getYAxis() {
  65. return $this->y_axis_label;
  66. }
  67. /**
  68. * Sets the axes labels of the current object.
  69. * @param $x = x-axis label.
  70. * @param $y = y-axis label.
  71. **/
  72. public function setAxisLabels($x = NULL, $y = NULL)
  73. {
  74. $this->x_axis_label = ($x === NULL) ? $this->x_axis_label : $x;
  75. $this->y_axis_label = ($y === NULL) ? $this->y_axis_label : $y;
  76. }
  77. /**
  78. * Returns the JS array built from the internal data respresentation, not
  79. * used at present by used to hide the internal building method.
  80. * @return String JS array.
  81. * @see buildJsDataArray().
  82. **/
  83. public function getJsDataArray() {
  84. return $this->buildJsDataArray();
  85. }
  86. /**
  87. * Sets the internal data source to a new object.
  88. * @param $data = new data object for the Graph
  89. **/
  90. public function setDataSource($data = NULL)
  91. {
  92. if (($data === NULL) && (! isEmpty($data))) return;
  93. $this->data = $data;
  94. }
  95. /**
  96. * Sets the maximum value of x-axis, both the maximum, rounded value of the
  97. * x-axis ticks and also determining a round, suitable tick interval.
  98. */
  99. protected function setMaxX($raw)
  100. {
  101. $this->max_x_axis = round($raw + 100, -2);
  102. $this->tick_interval = ($this->max_x_axis / 10);
  103. }
  104. /**
  105. * Iterates through the multi-dimensional array $data to create a string
  106. * JS array representation of multiple series of data for the Graph object.
  107. * @param $inverse Boolean TRUE if data is being represented horizontally.
  108. * @return String JS multi-dimensional array.
  109. **/
  110. protected function buildJsDataArray($inverse = FALSE) {
  111. $temp = '[';
  112. foreach ($this->data as $i => $series)
  113. {
  114. $temp .= '[';
  115. if ($inverse) $i = 0;
  116. foreach ($series as $j => $set)
  117. {
  118. foreach ($set as $k => $node)
  119. {
  120. /* Determine if the node is numeric, if not, escape. */
  121. $temp .= '['.((! is_numeric($node))
  122. ? '\''.addslashes($node).'\''
  123. : $node);
  124. /* @TODO: check $node for len > 1, if so tokenise string */
  125. if ($inverse) $temp .= ',' . ++$i;
  126. $temp .= '],';
  127. }
  128. $temp = rtrim($temp, ',');
  129. }
  130. $temp .= '],';
  131. }
  132. $temp = rtrim($temp, ',');
  133. $temp .= ']';
  134. return $temp;
  135. }
  136. protected function buildJsSingleArray($source) {
  137. $temp = '[';
  138. foreach ($source as $i => $node)
  139. {
  140. $temp .= ((! is_numeric($node)) ? '\''.$node.'\'' : $node) . ',';
  141. }
  142. $temp = rtrim($temp, ',') . ']';
  143. return $temp;
  144. }
  145. }