ofc_pie.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. class pie_value
  3. {
  4. function pie_value( $value, $label )
  5. {
  6. $this->value = $value;
  7. $this->label = $label;
  8. }
  9. function set_colour( $colour )
  10. {
  11. $this->colour = $colour;
  12. }
  13. function set_label( $label, $label_colour, $font_size )
  14. {
  15. $this->label = $label;
  16. $tmp = 'label-colour';
  17. $this->$tmp = $label_colour;
  18. $tmp = 'font-size';
  19. $this->$tmp = $font_size;
  20. }
  21. function set_tooltip( $tip )
  22. {
  23. $this->tip = $tip;
  24. }
  25. function on_click( $event )
  26. {
  27. $tmp = 'on-click';
  28. $this->$tmp = $event;
  29. }
  30. /**
  31. * An object that inherits from base_pie_animation
  32. */
  33. function add_animation( $animation )
  34. {
  35. if( !isset( $this->animate ) )
  36. $this->animate = array();
  37. $this->animate[] = $animation;
  38. return $this;
  39. }
  40. }
  41. class base_pie_animation{}
  42. /**
  43. * fade the pie slice from $alpha (pie set_alpha) to 100% opaque.
  44. */
  45. class pie_fade extends base_pie_animation
  46. {
  47. function pie_fade()
  48. {
  49. $this->type="fade";
  50. }
  51. }
  52. /**
  53. * Bounce the pie slice out a little
  54. */
  55. class pie_bounce extends base_pie_animation
  56. {
  57. /**
  58. * @param $distance as integer, distance to bounce in pixels
  59. */
  60. function pie_bounce( $distance )
  61. {
  62. $this->type="bounce";
  63. $this->distance = $distance;
  64. }
  65. }
  66. /**
  67. * Make a pie chart and fill it with pie slices
  68. */
  69. class pie
  70. {
  71. function pie()
  72. {
  73. $this->type = 'pie';
  74. }
  75. function set_colours( $colours )
  76. {
  77. $this->colours = $colours;
  78. }
  79. /**
  80. * Sugar wrapped around set_colours
  81. */
  82. function colours( $colours )
  83. {
  84. $this->set_colours( $colours );
  85. return $this;
  86. }
  87. /**
  88. * @param $alpha as float (0-1) 0.75 = 3/4 visible
  89. */
  90. function set_alpha( $alpha )
  91. {
  92. $this->alpha = $alpha;
  93. }
  94. /**
  95. *sugar wrapped set_alpha
  96. **/
  97. function alpha( $alpha )
  98. {
  99. $this->set_alpha( $alpha );
  100. return $this;
  101. }
  102. /**
  103. * @param $v as array containing one of
  104. * - null
  105. * - real or integer number
  106. * - a pie_value object
  107. */
  108. function set_values( $v )
  109. {
  110. $this->values = $v;
  111. }
  112. /**
  113. * sugar for set_values
  114. */
  115. function values( $v )
  116. {
  117. $this->set_values( $v );
  118. return $this;
  119. }
  120. /**
  121. * HACK to keep old code working.
  122. */
  123. function set_animate( $bool )
  124. {
  125. if( $bool )
  126. $this->add_animation( new pie_fade() );
  127. }
  128. /**
  129. * An object that inherits from base_pie_animation
  130. */
  131. function add_animation( $animation )
  132. {
  133. if( !isset( $this->animate ) )
  134. $this->animate = array();
  135. $this->animate[] = $animation;
  136. return $this;
  137. }
  138. /**
  139. * @param $angle as real number
  140. */
  141. function set_start_angle( $angle )
  142. {
  143. $tmp = 'start-angle';
  144. $this->$tmp = $angle;
  145. }
  146. /**
  147. * sugar for set_start_angle
  148. */
  149. function start_angle($angle)
  150. {
  151. $this->set_start_angle( $angle );
  152. return $this;
  153. }
  154. /**
  155. * @param $tip as string. The tooltip text. May contain magic varibles
  156. */
  157. function set_tooltip( $tip )
  158. {
  159. $this->tip = $tip;
  160. }
  161. /**
  162. * sugar for set_tooltip
  163. */
  164. function tooltip( $tip )
  165. {
  166. $this->set_tooltip( $tip );
  167. return $this;
  168. }
  169. function set_gradient_fill()
  170. {
  171. $tmp = 'gradient-fill';
  172. $this->$tmp = true;
  173. }
  174. function gradient_fill()
  175. {
  176. $this->set_gradient_fill();
  177. return $this;
  178. }
  179. /**
  180. * By default each label is the same colour as the slice,
  181. * but you can ovveride that behaviour using this method.
  182. *
  183. * @param $label_colour as string HEX colour;
  184. */
  185. function set_label_colour( $label_colour )
  186. {
  187. $tmp = 'label-colour';
  188. $this->$tmp = $label_colour;
  189. }
  190. function label_colour( $label_colour )
  191. {
  192. $this->set_label_colour( $label_colour );
  193. return $this;
  194. }
  195. /**
  196. * Turn off the labels
  197. */
  198. function set_no_labels()
  199. {
  200. $tmp = 'no-labels';
  201. $this->$tmp = true;
  202. }
  203. function on_click( $event )
  204. {
  205. $tmp = 'on-click';
  206. $this->$tmp = $event;
  207. }
  208. /**
  209. * Fix the radius of the pie chart. Take a look at the magic variable #radius#
  210. * for helping figure out what radius to set it to.
  211. *
  212. * @param $radius as number
  213. */
  214. function radius( $radius )
  215. {
  216. $this->radius = $radius;
  217. return $this;
  218. }
  219. }