/ui/obminclude/Artichow-1.1.0-php4+5/php5/LinePlot.class.php

https://github.com/goldoraf/OBM · PHP · 591 lines · 238 code · 117 blank · 236 comment · 18 complexity · d7b3db2efb2b792cc18114c9fbedfc18 MD5 · raw file

  1. <?php
  2. /*
  3. * This work is hereby released into the Public Domain.
  4. * To view a copy of the public domain dedication,
  5. * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
  6. * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
  7. *
  8. */
  9. require_once dirname(__FILE__)."/Plot.class.php";
  10. /* <php4> */
  11. define("LINEPLOT_LINE", 0);
  12. define("LINEPLOT_MIDDLE", 1);
  13. /* </php4> */
  14. /**
  15. * LinePlot
  16. *
  17. * @package Artichow
  18. */
  19. class awLinePlot extends awPlot implements awLegendable {
  20. /**
  21. * Add marks to your line plot
  22. *
  23. * @var Mark
  24. */
  25. public $mark;
  26. /**
  27. * Labels on your line plot
  28. *
  29. * @var Label
  30. */
  31. public $label;
  32. /**
  33. * Filled areas
  34. *
  35. * @var bool
  36. */
  37. protected $areas = array();
  38. /**
  39. * Is the line hidden
  40. *
  41. * @var bool
  42. */
  43. protected $lineHide = FALSE;
  44. /**
  45. * Line color
  46. *
  47. * @var Color
  48. */
  49. protected $lineColor;
  50. /**
  51. * Line mode
  52. *
  53. * @var int
  54. */
  55. protected $lineMode = awLinePlot::LINE;
  56. /**
  57. * Line type
  58. *
  59. * @var int
  60. */
  61. protected $lineStyle = awLine::SOLID;
  62. /**
  63. * Line thickness
  64. *
  65. * @var int
  66. */
  67. protected $lineThickness = 1;
  68. /**
  69. * Line background
  70. *
  71. * @var Color, Gradient
  72. */
  73. protected $lineBackground;
  74. /**
  75. * Line mode
  76. *
  77. * @var int
  78. */
  79. const LINE = 0;
  80. /**
  81. * Line in the middle
  82. *
  83. * @var int
  84. */
  85. const MIDDLE = 1;
  86. /**
  87. * Construct a new awLinePlot
  88. *
  89. * @param array $values Some numeric values for Y axis
  90. * @param int $mode
  91. */
  92. public function __construct($values, $mode = awLinePlot::LINE) {
  93. parent::__construct();
  94. $this->mark = new awMark;
  95. $this->label = new awLabel;
  96. $this->lineMode = (int)$mode;
  97. $this->setValues($values);
  98. }
  99. /**
  100. * Hide line
  101. *
  102. * @param bool $hide
  103. */
  104. public function hideLine($hide) {
  105. $this->lineHide = (bool)$hide;
  106. }
  107. /**
  108. * Add a filled area
  109. *
  110. * @param int $start Begining of the area
  111. * @param int $end End of the area
  112. * @param mixed $background Background color or gradient of the area
  113. */
  114. public function setFilledArea($start, $stop, $background) {
  115. if($stop <= $start) {
  116. awImage::drawError("Class LinePlot: End position can not be greater than begin position in setFilledArea().");
  117. }
  118. $this->areas[] = array((int)$start, (int)$stop, $background);
  119. }
  120. /**
  121. * Change line color
  122. *
  123. * @param awColor $color
  124. */
  125. public function setColor(awColor $color) {
  126. $this->lineColor = $color;
  127. }
  128. /**
  129. * Change line style
  130. *
  131. * @param int $style
  132. */
  133. public function setStyle($style) {
  134. $this->lineStyle = (int)$style;
  135. }
  136. /**
  137. * Change line tickness
  138. *
  139. * @param int $tickness
  140. */
  141. public function setThickness($tickness) {
  142. $this->lineThickness = (int)$tickness;
  143. }
  144. /**
  145. * Change line background color
  146. *
  147. * @param awColor $color
  148. */
  149. public function setFillColor(awColor $color) {
  150. $this->lineBackground = $color;
  151. }
  152. /**
  153. * Change line background gradient
  154. *
  155. * @param awGradient $gradient
  156. */
  157. public function setFillGradient(awGradient $gradient) {
  158. $this->lineBackground = $gradient;
  159. }
  160. /**
  161. * Get the line thickness
  162. *
  163. * @return int
  164. */
  165. public function getLegendLineThickness() {
  166. return $this->lineThickness;
  167. }
  168. /**
  169. * Get the line type
  170. *
  171. * @return int
  172. */
  173. public function getLegendLineStyle() {
  174. return $this->lineStyle;
  175. }
  176. /**
  177. * Get the color of line
  178. *
  179. * @return Color
  180. */
  181. public function getLegendLineColor() {
  182. return $this->lineColor;
  183. }
  184. /**
  185. * Get the background color or gradient of an element of the component
  186. *
  187. * @return Color, Gradient
  188. */
  189. public function getLegendBackground() {
  190. return $this->lineBackground;
  191. }
  192. /**
  193. * Get a mark object
  194. *
  195. * @return Mark
  196. */
  197. public function getLegendMark() {
  198. return $this->mark;
  199. }
  200. public function drawComponent(awDriver $driver, $x1, $y1, $x2, $y2, $aliasing) {
  201. $max = $this->getRealYMax();
  202. $min = $this->getRealYMin();
  203. // Get start and stop values
  204. list($start, $stop) = $this->getLimit();
  205. if($this->lineMode === awLinePlot::MIDDLE) {
  206. $inc = $this->xAxis->getDistance(0, 1) / 2;
  207. } else {
  208. $inc = 0;
  209. }
  210. // Build the polygon
  211. $polygon = new awPolygon;
  212. for($key = $start; $key <= $stop; $key++) {
  213. $value = $this->datay[$key];
  214. if($value !== NULL) {
  215. $p = awAxis::toPosition($this->xAxis, $this->yAxis, new awPoint($key, $value));
  216. $p = $p->move($inc, 0);
  217. $polygon->set($key, $p);
  218. }
  219. }
  220. // Draw backgrounds
  221. if($this->lineBackground instanceof awColor or $this->lineBackground instanceof awGradient) {
  222. $backgroundPolygon = new awPolygon;
  223. $p = $this->xAxisPoint($start);
  224. $p = $p->move($inc, 0);
  225. $backgroundPolygon->append($p);
  226. // Add others points
  227. foreach($polygon->all() as $point) {
  228. $backgroundPolygon->append(clone $point);
  229. }
  230. $p = $this->xAxisPoint($stop);
  231. $p = $p->move($inc, 0);
  232. $backgroundPolygon->append($p);
  233. // Draw polygon background
  234. $driver->filledPolygon($this->lineBackground, $backgroundPolygon);
  235. }
  236. $this->drawArea($driver, $polygon);
  237. // Draw line
  238. $prev = NULL;
  239. // Line color
  240. if($this->lineHide === FALSE) {
  241. if($this->lineColor === NULL) {
  242. $this->lineColor = new awColor(0, 0, 0);
  243. }
  244. foreach($polygon->all() as $point) {
  245. if($prev !== NULL) {
  246. $driver->line(
  247. $this->lineColor,
  248. new awLine(
  249. $prev,
  250. $point,
  251. $this->lineStyle,
  252. $this->lineThickness
  253. )
  254. );
  255. }
  256. $prev = $point;
  257. }
  258. }
  259. // Draw marks and labels
  260. foreach($polygon->all() as $key => $point) {
  261. $this->mark->draw($driver, $point);
  262. $this->label->draw($driver, $point, $key);
  263. }
  264. }
  265. protected function drawArea(awDriver $driver, awPolygon $polygon) {
  266. $starts = array();
  267. foreach($this->areas as $area) {
  268. list($start) = $area;
  269. $starts[$start] = TRUE;
  270. }
  271. // Draw filled areas
  272. foreach($this->areas as $area) {
  273. list($start, $stop, $background) = $area;
  274. $polygonArea = new awPolygon;
  275. $p = $this->xAxisPoint($start);
  276. $polygonArea->append($p);
  277. for($i = $start; $i <= $stop; $i++) {
  278. $p = clone $polygon->get($i);
  279. if($i === $stop and array_key_exists($stop, $starts)) {
  280. $p = $p->move(-1, 0);
  281. }
  282. $polygonArea->append($p);
  283. }
  284. $p = $this->xAxisPoint($stop);
  285. if(array_key_exists($stop, $starts)) {
  286. $p = $p->move(-1, 0);
  287. }
  288. $polygonArea->append($p);
  289. // Draw area
  290. $driver->filledPolygon($background, $polygonArea);
  291. }
  292. }
  293. public function getXAxisNumber() {
  294. if($this->lineMode === awLinePlot::MIDDLE) {
  295. return count($this->datay) + 1;
  296. } else {
  297. return count($this->datay);
  298. }
  299. }
  300. protected function xAxisPoint($position) {
  301. $y = $this->xAxisZero ? 0 : $this->getRealYMin();
  302. return awAxis::toPosition($this->xAxis, $this->yAxis, new awPoint($position, $y));
  303. }
  304. public function getXCenter() {
  305. return ($this->lineMode === awLinePlot::MIDDLE);
  306. }
  307. }
  308. registerClass('LinePlot');
  309. /**
  310. * Simple LinePlot
  311. * Useful to draw simple horizontal lines
  312. *
  313. * @package Artichow
  314. */
  315. class awSimpleLinePlot extends awPlot implements awLegendable {
  316. /**
  317. * Line color
  318. *
  319. * @var Color
  320. */
  321. protected $lineColor;
  322. /**
  323. * Line start
  324. *
  325. * @var int
  326. */
  327. protected $lineStart;
  328. /**
  329. * Line stop
  330. *
  331. * @var int
  332. */
  333. protected $lineStop;
  334. /**
  335. * Line value
  336. *
  337. * @var flaot
  338. */
  339. protected $lineValue;
  340. /**
  341. * Line mode
  342. *
  343. * @var int
  344. */
  345. protected $lineMode = awLinePlot::LINE;
  346. /**
  347. * Line type
  348. *
  349. * @var int
  350. */
  351. protected $lineStyle = awLine::SOLID;
  352. /**
  353. * Line thickness
  354. *
  355. * @var int
  356. */
  357. protected $lineThickness = 1;
  358. /**
  359. * Line mode
  360. *
  361. * @var int
  362. */
  363. const LINE = 0;
  364. /**
  365. * Line in the middle
  366. *
  367. * @var int
  368. */
  369. const MIDDLE = 1;
  370. /**
  371. * Construct a new awLinePlot
  372. *
  373. * @param float $value A Y value
  374. * @param int $start Line start index
  375. * @param int $stop Line stop index
  376. * @param int $mode Line mode
  377. */
  378. public function __construct($value, $start, $stop, $mode = awLinePlot::LINE) {
  379. parent::__construct();
  380. $this->lineMode = (int)$mode;
  381. $this->lineStart = (int)$start;
  382. $this->lineStop = (int)$stop;
  383. $this->lineValue = (float)$value;
  384. $this->lineColor = new awColor(0, 0, 0);
  385. }
  386. /**
  387. * Change line color
  388. *
  389. * @param awColor $color
  390. */
  391. public function setColor(awColor $color) {
  392. $this->lineColor = $color;
  393. }
  394. /**
  395. * Change line style
  396. *
  397. * @param int $style
  398. */
  399. public function setStyle($style) {
  400. $this->lineStyle = (int)$style;
  401. }
  402. /**
  403. * Change line tickness
  404. *
  405. * @param int $tickness
  406. */
  407. public function setThickness($tickness) {
  408. $this->lineThickness = (int)$tickness;
  409. }
  410. /**
  411. * Get the line thickness
  412. *
  413. * @return int
  414. */
  415. public function getLegendLineThickness() {
  416. return $this->lineThickness;
  417. }
  418. /**
  419. * Get the line type
  420. *
  421. * @return int
  422. */
  423. public function getLegendLineStyle() {
  424. return $this->lineStyle;
  425. }
  426. /**
  427. * Get the color of line
  428. *
  429. * @return Color
  430. */
  431. public function getLegendLineColor() {
  432. return $this->lineColor;
  433. }
  434. public function getLegendBackground() {
  435. return NULL;
  436. }
  437. public function getLegendMark() {
  438. return NULL;
  439. }
  440. public function drawComponent(awDriver $driver, $x1, $y1, $x2, $y2, $aliasing) {
  441. if($this->lineMode === awLinePlot::MIDDLE) {
  442. $inc = $this->xAxis->getDistance(0, 1) / 2;
  443. } else {
  444. $inc = 0;
  445. }
  446. $p1 = awAxis::toPosition($this->xAxis, $this->yAxis, new awPoint($this->lineStart, $this->lineValue));
  447. $p2 = awAxis::toPosition($this->xAxis, $this->yAxis, new awPoint($this->lineStop, $this->lineValue));
  448. $driver->line(
  449. $this->lineColor,
  450. new awLine(
  451. $p1->move($inc, 0),
  452. $p2->move($inc, 0),
  453. $this->lineStyle,
  454. $this->lineThickness
  455. )
  456. );
  457. }
  458. public function getXAxisNumber() {
  459. if($this->lineMode === awLinePlot::MIDDLE) {
  460. return count($this->datay) + 1;
  461. } else {
  462. return count($this->datay);
  463. }
  464. }
  465. protected function xAxisPoint($position) {
  466. $y = $this->xAxisZero ? 0 : $this->getRealYMin();
  467. return awAxis::toPosition($this->xAxis, $this->yAxis, new awPoint($position, $y));
  468. }
  469. public function getXCenter() {
  470. return ($this->lineMode === awLinePlot::MIDDLE);
  471. }
  472. }
  473. registerClass('SimpleLinePlot');
  474. ?>