PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/oldgraph.php

https://github.com/csmith/WeightTracker
PHP | 156 lines | 114 code | 38 blank | 4 comment | 22 complexity | b60af2a4efa8377106314d9b4e684867 MD5 | raw file
  1. <?PHP
  2. function array_min($array) {
  3. $min = 9999999999;
  4. foreach ($array as $v) { $min = min($v, $min); }
  5. return $min;
  6. }
  7. require_once('data.php');
  8. define('GWIDTH', 800);
  9. define('GHEIGHT', 792);
  10. define('XMIN', 0);
  11. define('XMAX', count($data));
  12. define('YMIN', ~1 & (floor(array_min($data))) - 10);
  13. define('YMAX', 140);
  14. $im = imagecreate(GWIDTH, GHEIGHT);
  15. $black = imagecolorallocate($im, 0x00, 0x00, 0x00);
  16. $grey = imagecolorallocate($im, 0xCC, 0xCC, 0xCC);
  17. $white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
  18. $red = imagecolorallocate($im, 0xAA, 0x33, 0x33);
  19. imagefill($im, 0, 0, $white);
  20. //imagerectangle($im, 0, 0, GWIDTH - 1, GHEIGHT - 1, $black);
  21. //imagefilledrectangle($im, 0, 0, GWIDTH - 1, 60, $black);
  22. imagerectangle($im, 75, 75, GWIDTH - 75, GHEIGHT - 75, $black);
  23. function dashLine($im, $y, $colour) {
  24. $n = 3;
  25. for ($i = 79; $i < GWIDTH - 79; $i += $n) {
  26. imageline($im, $i, $y, $i, $y, $colour);
  27. $n = 1 + ceil((($i - 70) / 3) / 20);
  28. }
  29. }
  30. function dashLine2($im, $y, $colour) {
  31. $n = 3;
  32. for ($i = GWIDTH - 79; $i > 79; $i -= $n) {
  33. imageline($im, $i, $y, $i, $y, $colour);
  34. $n = 1 + ceil(((GWIDTH - ($i + 79)) / 3) / 20);
  35. }
  36. }
  37. function fillBMI($im, $min, $max, $colour, $label) {
  38. $omin = $min; $omax = $max;
  39. $min = min(YMAX, max(YMIN, $min * HEIGHT * HEIGHT));
  40. $max = max(YMIN, min(YMAX, $max * HEIGHT * HEIGHT));
  41. if ($min == $max) { return; }
  42. $y1 = GHEIGHT - 75 - ($min - YMIN) * ((GHEIGHT - 150) / (YMAX - YMIN));
  43. $y2 = GHEIGHT - 75 - ($max - YMIN) * ((GHEIGHT - 150) / (YMAX - YMIN));
  44. imagefilledrectangle($im, 76, $max == YMAX ? $y2 + 1 : $y2, GWIDTH - 76, $y1 - 1, $colour);
  45. imageline($im, 76, $y2, GWIDTH - 76, $y2, imagecolorallocate($im, 0x00, 0x00, 0x00));
  46. $bmi = $omax == 100 ? "$omin+" : "$omin-$omax";
  47. imagestring($im, 1, 79, $y2 + 3, "BMI $bmi: '$label'", $black);
  48. }
  49. $colours = array(
  50. imagecolorallocate($im, 0xFF, 0xFF, 0xFF),
  51. imagecolorallocate($im, 0xE0, 0xFF, 0xFF),
  52. imagecolorallocate($im, 0xAD, 0xD8, 0xE1),
  53. imagecolorallocate($im, 0x87, 0xCE, 0xFA),
  54. imagecolorallocate($im, 0x81, 0xAD, 0xD2),
  55. imagecolorallocate($im, 0x60, 0x80, 0xDF),
  56. imagecolorallocate($im, 0x00, 0x00, 0xFF)
  57. );
  58. $bmis = array(
  59. 18.5,
  60. 25,
  61. 30,
  62. 35,
  63. 40,
  64. 100
  65. );
  66. fillBMI($im, 0, 18.5, $colours[0], "Underweight");
  67. fillBMI($im, 18.5, 25, $colours[1], "Normal");
  68. fillBMI($im, 25, 30, $colours[2], "Overweight");
  69. fillBMI($im, 30, 35, $colours[3], "Obese class I");
  70. fillBMI($im, 35, 40, $colours[4], "Obese class II");
  71. fillBMI($im, 40, 100, $colours[5], "Obese class III");
  72. for ($i = YMIN; $i < YMAX; $i += 2) {
  73. $y = GHEIGHT - 75 - ($i - YMIN) * ((GHEIGHT - 150) / (YMAX - YMIN));
  74. imageline($im, 73, $y, 76, $y, $black);
  75. imagestring($im, 1, 55, $y - 4, STR_PAD($i, 3, ' ', STR_PAD_LEFT), $black);
  76. if ($i % 2 == 0 && $i > YMIN) {
  77. for ($j = 0; $j < count($bmis); $j++) {
  78. if ($bmis[$j] > ($i / (HEIGHT * HEIGHT))) { break; }
  79. }
  80. dashLine($im, $y, $colours[$j+1]);
  81. }
  82. }
  83. for ($i = ceil(YMIN / (HEIGHT*HEIGHT)); $i < ceil(YMAX / (HEIGHT*HEIGHT)); $i++) {
  84. $y = GHEIGHT - 75 - ($i * HEIGHT * HEIGHT - YMIN) * ((GHEIGHT - 150) / (YMAX - YMIN));
  85. imageline($im, GWIDTH - 76, $y, GWIDTH - 73, $y, $black);
  86. imagestring($im, 1, GWIDTH - 70, $y - 4, $i, $black);
  87. if ($i % 5 != 0) {
  88. for ($j = 0; $j < count($bmis); $j++) {
  89. if ($bmis[$j] > $i) { break; }
  90. }
  91. dashLine2($im, $y, $colours[$j+1]);
  92. }
  93. }
  94. $lx = $ly = 0;
  95. foreach ($data as $i => $weight) {
  96. $y = GHEIGHT - 75 - ($weight - YMIN) * ((GHEIGHT - 150) / (YMAX - YMIN));
  97. $x = 75 + ($i - XMIN) * ((GWIDTH - 150) / (XMAX - XMIN));
  98. if ($lx != 0) {
  99. imageline($im, $lx, $ly, $x, $y, $ly > $y ? $red : $black);
  100. }
  101. imageline($im, $x, GHEIGHT - 76, $x, GHEIGHT - 73, $black);
  102. imagestring($im, 1, $x - 2, GHEIGHT - 70, $i + 1, $black);
  103. #imageline($im, $x - 4, $y - 4, $x + 4, $y + 4, $white);
  104. #imageline($im, $x + 4, $y - 4, $x - 4, $y + 4, $white);
  105. if ($i % 5 == 4) {
  106. imagerectangle($im, $x - 3, $y - 3, $x + 3, $y + 3, $black);
  107. } else {
  108. imagerectangle($im, $x - 2, $y - 2, $x + 2, $y + 2, $black);
  109. }
  110. $lx = $x; $ly = $y;
  111. }
  112. imagestring($im, 3, GWIDTH/2 - 50, GHEIGHT - 35, 'Week number', $black);
  113. imagestringup($im, 3, 20, GHEIGHT/2 + 20, 'Mass (KG)', $black);
  114. imagestringup($im, 3, GWIDTH - 30, GHEIGHT/2 + 50, 'BMI (KG/m^2)', $black);
  115. imagestring($im, 5, GWIDTH/2 - 110, 20, 'Graph of mass against time', $black);
  116. header('Content-type: image/png');
  117. imagepng($im);
  118. ?>