ソースを参照

Initial import

master
Chris Smith 14年前
コミット
aaffda131b
7個のファイルの変更1046行の追加0行の削除
  1. 1
    0
      .gitignore
  2. 47
    0
      data.php
  3. 46
    0
      graph.php
  4. 154
    0
      index.php
  5. 323
    0
      newergraph.php
  6. 319
    0
      newgraph.php
  7. 156
    0
      oldgraph.php

+ 1
- 0
.gitignore ファイルの表示

@@ -0,0 +1 @@
1
+/user-data.php

+ 47
- 0
data.php ファイルの表示

@@ -0,0 +1,47 @@
1
+<?PHP
2
+
3
+ require('user-data.php');
4
+
5
+ /* user-data.php contains user-supplied data.
6
+  *
7
+  * It should define a HEIGHT constant in metres, e.g.:
8
+  *
9
+  *   define('HEIGHT', 1.765);
10
+  *
11
+  * Weigh-ins are collected in a $data array, with one entry in kilograms per
12
+  * week, e.g.:
13
+  *
14
+  *   $data = array(100.0, 99.0, 98.0);
15
+  *
16
+  * If a weight is recorded as -1, the week will be skipped and an average of
17
+  * the nearest actual readings will be used. You can skip multiple weeks in
18
+  * a row.
19
+  *
20
+  * Targets (shown as crosses on the graph) can be defined in an array called
21
+  * $targets. Each entry in the array must be an array with two elements -
22
+  * the week number and the target BMI (not weight!). e.g.:
23
+  *
24
+  *   $targets = array(array(52, 70.0));
25
+  */
26
+
27
+ $skips = array();
28
+
29
+ // Replace -1 entries
30
+ for ($i = 0; $i < count($data); $i++) {
31
+  if ($data[$i] == -1) {
32
+   $skips[$i] = true;
33
+
34
+   $j = 1;
35
+   while (isset($data[$i + $j]) && $data[$i + $j] == -1) { $j++; }
36
+   $k = -1;
37
+   while (isset($data[$i + $k]) && $data[$i + $k] == -1) { $k--; }
38
+
39
+   if (isset($data[$i + $j])) {
40
+    $data[$i] = round($data[$i + $k] + ($data[$i + $j] - $data[$i + $k]) / ($j - $k), 1);
41
+   } else {
42
+    $data[$i] = $data[$i - 1];
43
+   }
44
+  }
45
+ }
46
+
47
+?>

+ 46
- 0
graph.php ファイルの表示

@@ -0,0 +1,46 @@
1
+<?PHP
2
+
3
+require('data.php');
4
+
5
+$tenp = array(123.4, 123.4);
6
+$twen = array(111.1, 111.1);
7
+$obe2 = array(113.4, 113.4);
8
+$over = array(97.2, 97.2);
9
+$norm = array(81, 81);
10
+$morb = array(129.6, 129.6);
11
+$lall = array(0, count($data));
12
+
13
+include ('jpgraph/src/jpgraph.php');
14
+include ('jpgraph/src/jpgraph_line.php');
15
+
16
+$graph = new Graph(800, 800, 'weight.png');
17
+$graph->setMargin(50, 50, 50, 50);
18
+$graph->legend->setPos(0.03, 0.1);
19
+$graph->title = new Text('Mass against time');
20
+$graph->title->align('center');
21
+$graph->title->setFont(FF_FONT2);
22
+$graph->title->setMargin(10);
23
+
24
+$line = new LinePlot($data);
25
+$line->mark->show();
26
+$line->mark->setType(MARK_UTRIANGLE);
27
+
28
+$graph->add($line);
29
+
30
+$graph->setscale('textlin', 78, 140, 0, count($data));
31
+$graph->setY2Scale('lin', 78.0 / (1.8*1.8), 140.0 / (1.8*1.8));
32
+$graph->ygrid->show(false);
33
+$graph->y2grid->show(true, true);
34
+
35
+$graph->xaxis->setTitle('Week number', 'middle');
36
+$graph->xaxis->setTitleMargin(15);
37
+
38
+$graph->yaxis->setTitle('Mass (KG)', 'middle');
39
+$graph->yaxis->setTitleMargin(32);
40
+
41
+$graph->y2axis->setTitle('BMI', 'middle');
42
+$graph->y2axis->setTitleMargin(30);
43
+
44
+$graph->stroke();
45
+
46
+?>

+ 154
- 0
index.php ファイルの表示

@@ -0,0 +1,154 @@
1
+<?PHP require('data.php'); ?>
2
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
3
+<html>
4
+ <head>
5
+  <title>Weight Tracking</title>
6
+  <style type="text/css">
7
+   table { width: 100%; border-collapse: collapse; margin-bottom: 10px; }
8
+   td, th { border: 1px solid black; padding: 5px; }
9
+   div#stats { margin-left: 810px; margin-right: 5px; padding-top: 1px; }
10
+   td { text-align: right; }
11
+   th { font-weight: bold; color: #fff; background-color: #000; border: 1px solid #666; }
12
+   tr.vbad td, td.bad { background-color: #f99; }
13
+   tr.bad td { background-color: #fdd; }
14
+   tr.ok td { background-color: #fff; }
15
+   tr.good td { background-color: #dfd; }
16
+   tr.vgood td, td.good { background-color: #9f9; }
17
+  </style>
18
+ </head>
19
+ <body>
20
+  <div id="graph" style="float: left;">
21
+   <img src="newergraph.php" alt="Graph of weight">
22
+  </div>
23
+  <div id="stats">
24
+   <table>
25
+    <tr>
26
+     <th rowspan="2">Wk</th><th rowspan="2">Mass</th><th rowspan="2">BMI</th>
27
+     <th colspan="2">&Delta;Mass</th><th colspan="2">&Sigma;&Delta;Mass</th>
28
+    </tr><tr>
29
+     <th>kg</th><th>st</th><th>kg</th><th>st</th>
30
+    </tr>
31
+<?PHP
32
+
33
+ $last = 0;
34
+ $first = 0;
35
+
36
+ $number = 18;
37
+ for ($i = count($data) - 1; $i > count($data) - $number; $i--) {
38
+  if (isset($skips[$i])) { $number++; }
39
+ }
40
+
41
+ foreach ($data as $week => $weight) {
42
+  $week++;
43
+  if ($first == 0) {
44
+   $first = $weight;
45
+  }
46
+
47
+  if ($last != 0) { $diffs[] = $weight - $last; }
48
+ 
49
+  if ($week > count($data) - $number) {
50
+  $diff = $weight - $last;
51
+
52
+  if ($diff > 0.5) {
53
+   $class = 'vbad';
54
+  } else if ($diff > 0) {
55
+   $class = 'bad';
56
+  } else if ($diff == 0) {
57
+   $class = 'ok';
58
+  } else if ($diff > -1) {
59
+   $class = 'good';
60
+  } else {
61
+   $class = 'vgood';
62
+  }
63
+
64
+  if (!isset($skips[$week - 1])) {
65
+  echo '<tr class="', $class, '"><td>', $week, '</td><td>', sprintf('%01.1f', $weight), '</td><td>';
66
+  echo sprintf('%01.1f', $weight / (HEIGHT * HEIGHT), 1), '</td><td>';
67
+
68
+  if ($last == 0) {
69
+   echo '-';
70
+  } else {
71
+   if ($weight - $last > 0) {
72
+    echo '<span style="color: red;">';
73
+   }
74
+   echo sprintf('%01.1f', $weight - $last);
75
+  }
76
+
77
+  echo '</td><td>';
78
+
79
+  if ($last == 0) {
80
+   echo '-';
81
+  } else {
82
+   if ($weight - $last > 0) {
83
+    echo '<span style="color: red;">';
84
+   }
85
+   echo sprintf('%01.1f', 0.157473044 * ($weight - $last));
86
+  }
87
+
88
+  echo '</td><td>', sprintf('%01.1f', $weight - $first), '</td>';
89
+  echo '<td>', sprintf('%01.1f', 0.157473044 * ($weight - $first)), '</td></tr>';
90
+  }
91
+ }
92
+  $last = $weight; 
93
+ }
94
+
95
+ $a1 = array_sum($diffs) / count($diffs);
96
+ $a2 = array_sum(array_slice($diffs, -4)) / 4;
97
+ $a3 = array_pop($diffs);
98
+
99
+ $bmi = $last / (HEIGHT * HEIGHT);
100
+ $target = 5 * floor($bmi/5);
101
+ $target2 = $target - 5;
102
+ $targs = array($target, $target2);
103
+
104
+ $tweeks = array();
105
+ foreach ($targets as $targ) {
106
+  list($tweek, $tbmi) = $targ;
107
+  if ($bmi > $tbmi && $tweek > $week) {
108
+   $tweeks[$tbmi] = $tweek;
109
+   $targs[] = $tbmi;
110
+  }
111
+ }
112
+ rsort($targs);
113
+
114
+ $target = array_shift($targs);
115
+ $target2 = array_shift($targs);
116
+ $b35 = $target * HEIGHT * HEIGHT - $last;
117
+ $b30 = ($target2) * HEIGHT * HEIGHT - $last;
118
+
119
+ $target_s = $target . ' (' . round($target * HEIGHT * HEIGHT, 1) . ')';
120
+ $target2_s = $target2 . ' (' . round($target2 * HEIGHT * HEIGHT, 1) . ')';
121
+
122
+?>
123
+   </table>
124
+   <table>
125
+    <tr><th rowspan="2">Timespan</th><th rowspan="2"><span style="border-top: 1px solid white;">&Delta;Mass</span></th><th colspan="2">ETA &rarr; BMI (KG)</th></tr>
126
+    <tr><th><?PHP echo $target_s; ?></th><th><?PHP echo $target2_s; ?></th></tr>
127
+    <tr><td>All time</td><td><?PHP printf('%01.1f', $a1); ?></td>
128
+     <td class="<?PHP if (isset($tweeks[$target])) { echo ($a1 < 0 && ceil(abs($b35 / $a1)) < $tweeks[$target] - count($data)) ? 'good' : 'bad'; } ?>">
129
+      <?PHP echo $a1 >= 0 ? 'Never' : date('d M y', strtotime('+'.ceil(abs($b35 / $a1)).'weeks')); ?></td>
130
+     <td class="<?PHP if (isset($tweeks[$target2])) { echo ($a1 < 0 && ceil(abs($b30 / $a1)) < $tweeks[$target2] - count($data)) ? 'good' : 'bad'; } ?>">
131
+      <?PHP echo $a1 >= 0 ? 'Never' : date('d M y', strtotime('+'.ceil(abs($b30 / $a1)).'weeks')); ?></td>
132
+    </tr>
133
+    <tr><td>Last month</td><td><?PHP printf('%01.1f', $a2); ?></td>
134
+     <td class="<?PHP if (isset($tweeks[$target])) { echo ($a2 < 0 && ceil(abs($b35 / $a2)) < $tweeks[$target] - count($data)) ? 'good' : 'bad'; } ?>">
135
+      <?PHP echo $a2 >= 0 ? 'Never' : date('d M y', strtotime('+'.ceil(abs($b35 / $a2)).'weeks')); ?></td>
136
+     <td class="<?PHP if (isset($tweeks[$target2])) { echo ($a2 < 0 && ceil(abs($b30 / $a2)) < $tweeks[$target2] - count($data)) ? 'good' : 'bad'; } ?>">
137
+      <?PHP echo $a2 >= 0 ? 'Never' : date('d M y', strtotime('+'.ceil(abs($b30 / $a2)).'weeks')); ?></td>
138
+    </tr>
139
+    <tr><td>Last week</td><td><?PHP printf('%01.1f', $a3); ?></td>
140
+     <td class="<?PHP if (isset($tweeks[$target])) { echo ($a3 < 0 && ceil(abs($b35 / $a3)) < $tweeks[$target] - count($data)) ? 'good' : 'bad'; } ?>">
141
+      <?PHP echo $a3 >= 0 ? 'Never' : date('d M y', strtotime('+'.ceil(abs($b35 / $a3)).'weeks')); ?></td>
142
+     <td class="<?PHP if (isset($tweeks[$target2])) { echo ($a3 < 0 && ceil(abs($b30 / $a3)) < $tweeks[$target2] - count($data)) ? 'good' : 'bad'; } ?>">
143
+      <?PHP echo $a3 >= 0 ? 'Never' : date('d M y', strtotime('+'.ceil(abs($b30 / $a3)).'weeks')); ?></td>
144
+    </tr>
145
+    <tr><th>Average</th><td><?PHP printf('%01.1f', ($a1 + $a2 + $a3)/3); ?></td>
146
+     <td class="<?PHP if (isset($tweeks[$target])) { echo ($a1 + $a2 + $a3 < 0 && ceil(abs($b35 / (($a1 + $a2 + $a3)/3))) < $tweeks[$target] - count($data)) ? 'good' : 'bad'; } ?>">
147
+      <?PHP echo $a1 + $a2 + $a3 >= 0 ? 'Never' : date('d M y', strtotime('+'.ceil(abs($b35 / (($a1 + $a2 + $a3)/3))).'weeks')); ?></td>
148
+     <td class="<?PHP if (isset($tweeks[$target2])) { echo ($a1 + $a2 + $a3 < 0 && ceil(abs($b30 / (($a1 + $a2 + $a3)/3))) < $tweeks[$target2] - count($data)) ? 'good' : 'bad'; } ?>">
149
+      <?PHP echo $a1 + $a2 + $a3 >= 0 ? 'Never' : date('d M y', strtotime('+'.ceil(abs($b30 / (($a1 + $a2 + $a3)/3))).'weeks')); ?></td>
150
+    </tr>
151
+   </table>
152
+  </div>
153
+ </body>
154
+</html>

+ 323
- 0
newergraph.php ファイルの表示

@@ -0,0 +1,323 @@
1
+<?PHP
2
+
3
+
4
+ function imagesmoothline ( $image , $x1 , $y1 , $x2 , $y2 , $color )
5
+ {
6
+  $colors = imagecolorsforindex ( $image , $color );
7
+  if ( $x1 == $x2 )
8
+  {
9
+   imageline ( $image , $x1 , $y1 , $x2 , $y2 , $color ); // Vertical line
10
+  }
11
+  else
12
+  {
13
+   $m = ( $y2 - $y1 ) / ( $x2 - $x1 );
14
+   $b = $y1 - $m * $x1;
15
+   if ( abs ( $m ) <= 1 )
16
+   {
17
+    $x = min ( $x1 , $x2 );
18
+    $endx = max ( $x1 , $x2 );
19
+    while ( $x <= $endx )
20
+    {
21
+     $y = $m * $x + $b;
22
+     $y == floor ( $y ) ? $ya = 1 : $ya = $y - floor ( $y );
23
+     $yb = ceil ( $y ) - $y;
24
+     $tempcolors = imagecolorsforindex ( $image , imagecolorat ( $image , $x , floor ( $y ) ) );
25
+     $tempcolors['red'] = $tempcolors['red'] * $ya + $colors['red'] * $yb;
26
+     $tempcolors['green'] = $tempcolors['green'] * $ya + $colors['green'] * $yb;
27
+     $tempcolors['blue'] = $tempcolors['blue'] * $ya + $colors['blue'] * $yb;
28
+     if ( imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) == -1 ) imagecolorallocate ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] );
29
+     imagesetpixel ( $image , $x , floor ( $y ) , imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) );
30
+     $tempcolors = imagecolorsforindex ( $image , imagecolorat ( $image , $x , ceil ( $y ) ) );
31
+     $tempcolors['red'] = $tempcolors['red'] * $yb + $colors['red'] * $ya;
32
+      $tempcolors['green'] = $tempcolors['green'] * $yb + $colors['green'] * $ya;
33
+     $tempcolors['blue'] = $tempcolors['blue'] * $yb + $colors['blue'] * $ya;
34
+     if ( imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) == -1 ) imagecolorallocate ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] );
35
+     imagesetpixel ( $image , $x , ceil ( $y ) , imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) );
36
+     $x ++;
37
+    }
38
+   }
39
+   else
40
+   {
41
+    $y = min ( $y1 , $y2 );
42
+    $endy = max ( $y1 , $y2 );
43
+    while ( $y <= $endy )
44
+    {
45
+     $x = ( $y - $b ) / $m;
46
+     $x == floor ( $x ) ? $xa = 1 : $xa = $x - floor ( $x );
47
+     $xb = ceil ( $x ) - $x;
48
+     $tempcolors = imagecolorsforindex ( $image , imagecolorat ( $image , floor ( $x ) , $y ) );
49
+     $tempcolors['red'] = $tempcolors['red'] * $xa + $colors['red'] * $xb;
50
+     $tempcolors['green'] = $tempcolors['green'] * $xa + $colors['green'] * $xb;
51
+     $tempcolors['blue'] = $tempcolors['blue'] * $xa + $colors['blue'] * $xb;
52
+     if ( imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) == -1 ) imagecolorallocate ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] );
53
+     imagesetpixel ( $image , floor ( $x ) , $y , imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) );
54
+     $tempcolors = imagecolorsforindex ( $image , imagecolorat ( $image , ceil ( $x ) , $y ) );
55
+     $tempcolors['red'] = $tempcolors['red'] * $xb + $colors['red'] * $xa;
56
+     $tempcolors['green'] = $tempcolors['green'] * $xb + $colors['green'] * $xa;
57
+     $tempcolors['blue'] = $tempcolors['blue'] * $xb + $colors['blue'] * $xa;
58
+     if ( imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) == -1 ) imagecolorallocate ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] );
59
+     imagesetpixel ( $image , ceil ( $x ) , $y , imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) );
60
+     $y ++;
61
+    }
62
+   }
63
+  }
64
+ }
65
+
66
+ function array_min($array) {
67
+  $min = 9999999999;
68
+
69
+  foreach ($array as $v) { $min = min($v, $min); }
70
+
71
+  return $min;
72
+ }
73
+
74
+ require_once('data.php');
75
+
76
+ define('GWIDTH', 800);
77
+ define('GHEIGHT', 792);
78
+ define('FONT', '/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf');
79
+
80
+ $maxtweek = 0; $mintbmi = 100;
81
+ foreach ($targets as $target) {
82
+  list($week, $bmi) = $target;
83
+  $maxtweek = max($week - 1, $maxtweek);
84
+  $mintbmi = min($mintbmi, $bmi);
85
+ }
86
+ $mintmass = $mintbmi * HEIGHT * HEIGHT;
87
+
88
+ define('XMIN', 0);
89
+ define('XMAX', max($maxtweek + 1, count($data)));
90
+
91
+ define('YMIN', ~1 & min($mintmass, floor(array_min($data)) - 10));
92
+ define('YMAX', 140);
93
+
94
+ $im = imagecreate(GWIDTH, GHEIGHT);
95
+
96
+ $black = imagecolorallocate($im, 0x00, 0x00, 0x00);
97
+ $grey  = imagecolorallocate($im, 0x66, 0x66, 0x66);
98
+ $white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
99
+ $red   = imagecolorallocate($im, 0xAA, 0x33, 0x33);
100
+
101
+ imagefill($im, 0, 0, $white);
102
+
103
+ //imagerectangle($im, 0, 0, GWIDTH - 1, GHEIGHT - 1, $black);
104
+ //imagefilledrectangle($im, 0, 0, GWIDTH - 1, 60, $black);
105
+ imagerectangle($im, 65, 45, GWIDTH - 55, GHEIGHT - 45, $black);
106
+
107
+ function dashLine($im, $y) {
108
+  $colour = imagecolorallocate($im, 0xEE, 0xEE, 0xEE);
109
+  //imageline($im, 67, $y, GWIDTH - 57, $y, $colour);
110
+ }
111
+
112
+ function dashLine2($im, $y) {
113
+  $colour = imagecolorallocate($im, 0xDD, 0xDD, 0xDD);
114
+  imageline($im, GWIDTH - 57, $y, 67, $y, $colour);
115
+ }
116
+
117
+ function fillBMI($im, $min, $max, $colour, $label) {
118
+  $omin = $min; $omax = $max;
119
+
120
+  $min = min(YMAX, max(YMIN, $min * HEIGHT * HEIGHT));
121
+  $max = max(YMIN, min(YMAX, $max * HEIGHT * HEIGHT));
122
+
123
+  if ($min == $max) { return; }
124
+
125
+  $y1 = GHEIGHT - 45 - ($min - YMIN) * ((GHEIGHT - 90) / (YMAX - YMIN));
126
+  $y2 = GHEIGHT - 45 - ($max - YMIN) * ((GHEIGHT - 90) / (YMAX - YMIN));
127
+
128
+  //imagefilledrectangle($im, 66, $max == YMAX ? $y2 + 1 : $y2, GWIDTH - 56, $y1 - 1, $colour);
129
+  imagefilledrectangle($im, 66, $max == YMAX ? $y2 + 1 : $y2, GWIDTH - 56, $y2 + 12, imagecolorallocate($im, 0xDD, 0xDD, 0xDD));
130
+  imageline($im, 66, $y2, GWIDTH - 56, $y2, imagecolorallocate($im, 0x00, 0x00, 0x00));
131
+  $bmi = $omax == 100 ? "$omin+" : "$omin-$omax";
132
+  imagestring($im, 1, 69, $y2 + 3, "BMI $bmi: '$label'", $black);
133
+ }
134
+
135
+ $colours = array(
136
+  imagecolorallocate($im, 0xFF, 0xFF, 0xFF),
137
+  imagecolorallocate($im, 0xE0, 0xFF, 0xFF),
138
+  imagecolorallocate($im, 0xC0, 0xDE, 0xEC),
139
+  imagecolorallocate($im, 0x87, 0xCE, 0xFA), 
140
+  imagecolorallocate($im, 0x81, 0xAD, 0xD2),
141
+  imagecolorallocate($im, 0x60, 0x80, 0xDF),
142
+  imagecolorallocate($im, 0x00, 0x00, 0xFF)
143
+ );
144
+
145
+ $bmis = array(
146
+  18.5,
147
+  25,
148
+  30,
149
+  35,
150
+  40,
151
+  100
152
+ );
153
+
154
+ for ($i = YMIN; $i < YMAX; $i += 2) {
155
+  $y = GHEIGHT - 45 - ($i - YMIN) * ((GHEIGHT - 90) / (YMAX - YMIN));
156
+
157
+  imageline($im, 63, $y, 66, $y, $black);
158
+  imagestring($im, 1, 45, $y - 4, STR_PAD($i, 3, ' ', STR_PAD_LEFT), $black);
159
+
160
+  if ($i % 2 == 0 && $i > YMIN) {
161
+   for ($j = 0; $j < count($bmis); $j++) {
162
+    if ($bmis[$j] > ($i / (HEIGHT * HEIGHT))) { break; }
163
+   }
164
+
165
+   dashLine($im, $y);
166
+  }
167
+ }
168
+
169
+ for ($i = ceil(YMIN / (HEIGHT*HEIGHT)); $i < ceil(YMAX / (HEIGHT*HEIGHT)); $i++) {
170
+  $y = GHEIGHT - 45 - ($i * HEIGHT * HEIGHT - YMIN) * ((GHEIGHT - 90) / (YMAX - YMIN));
171
+
172
+  imageline($im, GWIDTH - 56, $y, GWIDTH - 53, $y, $black);
173
+  imagestring($im, 1, GWIDTH - 50, $y - 4, $i, $black);
174
+
175
+  if ($i % 5 != 0) {
176
+   for ($j = 0; $j < count($bmis); $j++) {
177
+    if ($bmis[$j] > $i) { break; }
178
+   } 
179
+
180
+   dashLine2($im, $y);
181
+  }
182
+ }
183
+
184
+ fillBMI($im, 0, 18.5, $colours[0], "Underweight");
185
+ fillBMI($im, 18.5, 25, $colours[1], "Normal");
186
+ fillBMI($im, 25, 30, $colours[2], "Overweight");
187
+ fillBMI($im, 30, 35, $colours[3], "Obese class I");
188
+ fillBMI($im, 35, 40, $colours[4], "Obese class II");
189
+ fillBMI($im, 40, 100, $colours[5], "Obese class III");
190
+
191
+
192
+ $lx = $ly = 0;
193
+ $points = array();
194
+
195
+ for($i = 0; $i < XMAX; $i++) {
196
+  $x = 65 + ($i - XMIN) * ((GWIDTH - 120) / (XMAX - XMIN));
197
+
198
+  if ($lx != 0) {
199
+   //imageline($im, $lx, $ly, $x, $y, $ly > $y ? $red : $black);
200
+  }
201
+
202
+  imageline($im, $x, GHEIGHT - 46, $x, GHEIGHT - 43, $black);
203
+
204
+  if ($i % 2 == 0) {
205
+   imagestring($im, 1, $x - 2, GHEIGHT - 40, substr($i + 1, 0, 1), $black);
206
+   if ($i >= 9) {
207
+    imagestring($im, 1, $x - 2, GHEIGHT - 32, substr($i + 1, 1, 1), $black);
208
+    if ($i >= 99) {
209
+     imagestring($im, 1, $x - 2, GHEIGHT - 24, substr($i + 1, 2, 1), $black);
210
+    }
211
+   }
212
+  }
213
+
214
+  #imageline($im, $x - 4, $y - 4, $x + 4, $y + 4, $white);
215
+  #imageline($im, $x + 4, $y - 4, $x - 4, $y + 4, $white);
216
+
217
+  if (isset($data[$i])) {
218
+   $weight = $data[$i];
219
+   $y = GHEIGHT - 45 - ($weight - YMIN) * ((GHEIGHT - 90) / (YMAX - YMIN));
220
+   $points[] = array($x, $y);
221
+
222
+   if ($i % 5 == 4) {
223
+    //dashLine3($im, $x, $y, imagecolorallocatealpha($im, 0x66, 0x66, 0x66, 10));// $i % 5 == 4 ? $black : $grey);
224
+    //imagearc($im, $x, $y, 10, 10, 0, 360, $red);
225
+   }// else {
226
+    imagefilledarc($im, $x, $y, 3, 3, 0, 360, $red, IMG_ARC_PIE);
227
+   //}
228
+
229
+   $lx = $x; $ly = $y;
230
+  }
231
+ }
232
+
233
+ foreach ($targets as $target) {
234
+  list($week, $bmi) = $target;
235
+  $weight = $bmi * HEIGHT * HEIGHT;
236
+
237
+  if ($week > 0) {
238
+   $x = 65 + (($week - 1) - XMIN) * ((GWIDTH - 120) / (XMAX - XMIN));
239
+   $y = GHEIGHT - 45 - ($weight - YMIN) * ((GHEIGHT - 90) / (YMAX - YMIN));
240
+   imageline($im, $x - 3, $y - 3, $x + 3, $y + 3, $red);
241
+   imageline($im, $x - 3, $y + 3, $x + 3, $y - 3, $red);
242
+  }
243
+ }
244
+
245
+ $avg = array();
246
+ $avg[] = $points[0];
247
+ for ($i = 0; $i < XMAX - XMIN - 5; $i += 5) {
248
+  $tx = $ty = 0;
249
+  for ($j = 0; $j < 5; $j++) {
250
+   list($x, $y) = $points[$i + $j];
251
+   $tx += $x; $ty += $y;
252
+  }
253
+  $avg[] = array($tx/5, $ty/5);
254
+ }
255
+
256
+ $colours = array($red, $black);
257
+ foreach (array(/*$avg,*/ $points) as $points) {
258
+  $colour = array_shift($colours);
259
+  $lx = $ly = 0;
260
+  for ($i = 0; $i < count($points) - 1; $i++) {
261
+   $func = interpolate($points, $i, $i + 1);
262
+   list($x1,$y1) = $points[$i];
263
+   list($x2,$y2) = $points[$i + 1];
264
+   for ($t = 0; $t < 1; $t += 4 / ($x2 - $x1)) {
265
+    $x = round($x1 + ($x2 - $x1) * $t, 0);
266
+    $y = round(call_user_func($func, $t), 0);
267
+   
268
+    if ($lx != 0) {
269
+     imagesmoothline($im, $lx, $ly, $x, $y, $red); 
270
+    }
271
+    $lx = $x; $ly = $y;
272
+   }
273
+  }
274
+ }
275
+
276
+ function h00($t) { return 2 * pow($t, 3) - 3 * pow($t, 2) + 1; }
277
+ function h10($t) { return pow($t, 3) - 2 * pow($t, 2) + $t; }
278
+ function h01($t) { return -2 * pow($t, 3) + 3 * pow($t, 2); }
279
+ function h11($t) { return pow($t, 3) - pow($t, 2); }
280
+ function p($t, $p0, $m0, $p1, $m1, $h) { return h00($t) * $p0 + h10($t) * $h * $m0 + h01($t) * $p1 + h11($t) * $h * $m1; }
281
+
282
+ function getLineGradient($a, $b) {
283
+  list($x1,$y1) = $a;
284
+  list($x2,$y2) = $b;
285
+  $dx = $x2 - $x1;
286
+  $dy = $y2 - $y1;
287
+  if ($dx == 0) { return PHP_INT_MAX; }
288
+  return $dy/$dx;
289
+ }
290
+
291
+ function getPointGradient($cur, $last, $next) {
292
+  if ($last == null) { return getLineGradient($cur, $next); }
293
+  if ($next == null) { return getLineGradient($last, $cur); }
294
+  return (getLineGradient($cur, $next) + getLineGradient($last, $cur))/2;
295
+ }
296
+
297
+ function interpolate($data, $from, $to) {
298
+  $prev = isset($data[$from - 1]) ? $data[$from - 1] : null;
299
+  $next = isset($data[$to + 1]) ? $data[$to + 1] : null;
300
+
301
+  $one = $data[$from];
302
+  $two = $data[$to];
303
+
304
+  list($x1, $y1) = $one;
305
+  list($x2, $y2) = $two;
306
+
307
+  $h = $x2 - $x1;
308
+  $p0 = $y1; $p1 = $y2;
309
+  $m0 = getPointGradient($prev, $one, $two);
310
+  $m1 = getPointGradient($one, $two, $next);
311
+
312
+  return create_function('$t', 'return p($t, ' . $p0 . ', ' . $m0 . ', ' . $p1 . ', ' . $m1 . ', ' . $h . ');');
313
+ }
314
+
315
+ imagettftext($im, 8, 0, GWIDTH/2 - 50, GHEIGHT - 5, $black, FONT, 'Week number');
316
+ imagettftext($im, 8, 90, 30, GHEIGHT/2 + 20, $black, FONT, 'Mass (KG)');
317
+ imagettftext($im, 8, 270, GWIDTH - 25, GHEIGHT/2 - 40, $black, FONT, 'BMI (KG/m^2)');
318
+ imagettftext($im, 12, 0, GWIDTH/2 - 110, 30, $black, FONT, 'Graph of mass against time');
319
+
320
+ header('Content-type: image/png');
321
+ imagepng($im); 
322
+
323
+?>

+ 319
- 0
newgraph.php ファイルの表示

@@ -0,0 +1,319 @@
1
+<?PHP
2
+
3
+
4
+ function imagesmoothline ( $image , $x1 , $y1 , $x2 , $y2 , $color )
5
+ {
6
+  $colors = imagecolorsforindex ( $image , $color );
7
+  if ( $x1 == $x2 )
8
+  {
9
+   imageline ( $image , $x1 , $y1 , $x2 , $y2 , $color ); // Vertical line
10
+  }
11
+  else
12
+  {
13
+   $m = ( $y2 - $y1 ) / ( $x2 - $x1 );
14
+   $b = $y1 - $m * $x1;
15
+   if ( abs ( $m ) <= 1 )
16
+   {
17
+    $x = min ( $x1 , $x2 );
18
+    $endx = max ( $x1 , $x2 );
19
+    while ( $x <= $endx )
20
+    {
21
+     $y = $m * $x + $b;
22
+     $y == floor ( $y ) ? $ya = 1 : $ya = $y - floor ( $y );
23
+     $yb = ceil ( $y ) - $y;
24
+     $tempcolors = imagecolorsforindex ( $image , imagecolorat ( $image , $x , floor ( $y ) ) );
25
+     $tempcolors['red'] = $tempcolors['red'] * $ya + $colors['red'] * $yb;
26
+     $tempcolors['green'] = $tempcolors['green'] * $ya + $colors['green'] * $yb;
27
+     $tempcolors['blue'] = $tempcolors['blue'] * $ya + $colors['blue'] * $yb;
28
+     if ( imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) == -1 ) imagecolorallocate ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] );
29
+     imagesetpixel ( $image , $x , floor ( $y ) , imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) );
30
+     $tempcolors = imagecolorsforindex ( $image , imagecolorat ( $image , $x , ceil ( $y ) ) );
31
+     $tempcolors['red'] = $tempcolors['red'] * $yb + $colors['red'] * $ya;
32
+      $tempcolors['green'] = $tempcolors['green'] * $yb + $colors['green'] * $ya;
33
+     $tempcolors['blue'] = $tempcolors['blue'] * $yb + $colors['blue'] * $ya;
34
+     if ( imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) == -1 ) imagecolorallocate ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] );
35
+     imagesetpixel ( $image , $x , ceil ( $y ) , imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) );
36
+     $x ++;
37
+    }
38
+   }
39
+   else
40
+   {
41
+    $y = min ( $y1 , $y2 );
42
+    $endy = max ( $y1 , $y2 );
43
+    while ( $y <= $endy )
44
+    {
45
+     $x = ( $y - $b ) / $m;
46
+     $x == floor ( $x ) ? $xa = 1 : $xa = $x - floor ( $x );
47
+     $xb = ceil ( $x ) - $x;
48
+     $tempcolors = imagecolorsforindex ( $image , imagecolorat ( $image , floor ( $x ) , $y ) );
49
+     $tempcolors['red'] = $tempcolors['red'] * $xa + $colors['red'] * $xb;
50
+     $tempcolors['green'] = $tempcolors['green'] * $xa + $colors['green'] * $xb;
51
+     $tempcolors['blue'] = $tempcolors['blue'] * $xa + $colors['blue'] * $xb;
52
+     if ( imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) == -1 ) imagecolorallocate ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] );
53
+     imagesetpixel ( $image , floor ( $x ) , $y , imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) );
54
+     $tempcolors = imagecolorsforindex ( $image , imagecolorat ( $image , ceil ( $x ) , $y ) );
55
+     $tempcolors['red'] = $tempcolors['red'] * $xb + $colors['red'] * $xa;
56
+     $tempcolors['green'] = $tempcolors['green'] * $xb + $colors['green'] * $xa;
57
+     $tempcolors['blue'] = $tempcolors['blue'] * $xb + $colors['blue'] * $xa;
58
+     if ( imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) == -1 ) imagecolorallocate ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] );
59
+     imagesetpixel ( $image , ceil ( $x ) , $y , imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) );
60
+     $y ++;
61
+    }
62
+   }
63
+  }
64
+ }
65
+
66
+ function array_min($array) {
67
+  $min = 9999999999;
68
+
69
+  foreach ($array as $v) { $min = min($v, $min); }
70
+
71
+  return $min;
72
+ }
73
+
74
+ require_once('data.php');
75
+
76
+ define('GWIDTH', 800);
77
+ define('GHEIGHT', 792);
78
+
79
+ $maxtweek = 0; $mintbmi = 100;
80
+ foreach ($targets as $target) {
81
+  list($week, $bmi) = $target;
82
+  $maxtweek = max($week - 1, $maxtweek);
83
+  $mintbmi = min($mintbmi, $bmi);
84
+ }
85
+ $mintmass = $mintbmi * HEIGHT * HEIGHT;
86
+
87
+ define('XMIN', 0);
88
+ define('XMAX', max($maxtweek + 1, count($data)));
89
+
90
+ define('YMIN', ~1 & min($mintmass, floor(array_min($data)) - 10));
91
+ define('YMAX', 140);
92
+
93
+ $im = imagecreate(GWIDTH, GHEIGHT);
94
+
95
+ $black = imagecolorallocate($im, 0x00, 0x00, 0x00);
96
+ $grey  = imagecolorallocate($im, 0x66, 0x66, 0x66);
97
+ $white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
98
+ $red   = imagecolorallocate($im, 0xAA, 0x33, 0x33);
99
+
100
+ imagefill($im, 0, 0, $white);
101
+
102
+ //imagerectangle($im, 0, 0, GWIDTH - 1, GHEIGHT - 1, $black);
103
+ //imagefilledrectangle($im, 0, 0, GWIDTH - 1, 60, $black);
104
+ imagerectangle($im, 65, 45, GWIDTH - 55, GHEIGHT - 45, $black);
105
+
106
+ function dashLine($im, $y, $colour) {
107
+  $n = 3;
108
+  for ($i = 69; $i < GWIDTH - 59; $i += $n) {
109
+   imageline($im, $i, $y, $i, $y, $colour);
110
+   $n = 1 + ceil((($i - 60) / 3) / 20);
111
+  }
112
+ }
113
+
114
+ function dashLine2($im, $y, $colour) {
115
+  $n = 3;
116
+  for ($i = GWIDTH - 59; $i > 69; $i -= $n) {
117
+   imageline($im, $i, $y, $i, $y, $colour);
118
+   $n = 1 + ceil(((GWIDTH - ($i + 59)) / 3) / 20);
119
+  }
120
+ }
121
+
122
+ function fillBMI($im, $min, $max, $colour, $label) {
123
+  $omin = $min; $omax = $max;
124
+
125
+  $min = min(YMAX, max(YMIN, $min * HEIGHT * HEIGHT));
126
+  $max = max(YMIN, min(YMAX, $max * HEIGHT * HEIGHT));
127
+
128
+  if ($min == $max) { return; }
129
+
130
+  $y1 = GHEIGHT - 45 - ($min - YMIN) * ((GHEIGHT - 90) / (YMAX - YMIN));
131
+  $y2 = GHEIGHT - 45 - ($max - YMIN) * ((GHEIGHT - 90) / (YMAX - YMIN));
132
+
133
+  imagefilledrectangle($im, 66, $max == YMAX ? $y2 + 1 : $y2, GWIDTH - 56, $y1 - 1, $colour);
134
+  imageline($im, 66, $y2, GWIDTH - 56, $y2, imagecolorallocate($im, 0x00, 0x00, 0x00));
135
+  $bmi = $omax == 100 ? "$omin+" : "$omin-$omax";
136
+  imagestring($im, 1, 69, $y2 + 3, "BMI $bmi: '$label'", $black);
137
+ }
138
+
139
+ $colours = array(
140
+  imagecolorallocate($im, 0xFF, 0xFF, 0xFF),
141
+  imagecolorallocate($im, 0xE0, 0xFF, 0xFF),
142
+  imagecolorallocate($im, 0xC0, 0xDE, 0xEC),
143
+  imagecolorallocate($im, 0x87, 0xCE, 0xFA), 
144
+  imagecolorallocate($im, 0x81, 0xAD, 0xD2),
145
+  imagecolorallocate($im, 0x60, 0x80, 0xDF),
146
+  imagecolorallocate($im, 0x00, 0x00, 0xFF)
147
+ );
148
+
149
+ $bmis = array(
150
+  18.5,
151
+  25,
152
+  30,
153
+  35,
154
+  40,
155
+  100
156
+ );
157
+
158
+ fillBMI($im, 0, 18.5, $colours[0], "Underweight");
159
+ fillBMI($im, 18.5, 25, $colours[1], "Normal");
160
+ fillBMI($im, 25, 30, $colours[2], "Overweight");
161
+ fillBMI($im, 30, 35, $colours[3], "Obese class I");
162
+ fillBMI($im, 35, 40, $colours[4], "Obese class II");
163
+ fillBMI($im, 40, 100, $colours[5], "Obese class III");
164
+
165
+ for ($i = YMIN; $i < YMAX; $i += 2) {
166
+  $y = GHEIGHT - 45 - ($i - YMIN) * ((GHEIGHT - 90) / (YMAX - YMIN));
167
+
168
+  imageline($im, 63, $y, 66, $y, $black);
169
+  imagestring($im, 1, 45, $y - 4, STR_PAD($i, 3, ' ', STR_PAD_LEFT), $black);
170
+
171
+  if ($i % 2 == 0 && $i > YMIN) {
172
+   for ($j = 0; $j < count($bmis); $j++) {
173
+    if ($bmis[$j] > ($i / (HEIGHT * HEIGHT))) { break; }
174
+   }
175
+
176
+   dashLine($im, $y, $colours[$j+1]);
177
+  }
178
+ }
179
+
180
+ for ($i = ceil(YMIN / (HEIGHT*HEIGHT)); $i < ceil(YMAX / (HEIGHT*HEIGHT)); $i++) {
181
+  $y = GHEIGHT - 45 - ($i * HEIGHT * HEIGHT - YMIN) * ((GHEIGHT - 90) / (YMAX - YMIN));
182
+
183
+  imageline($im, GWIDTH - 56, $y, GWIDTH - 53, $y, $black);
184
+  imagestring($im, 1, GWIDTH - 50, $y - 4, $i, $black);
185
+
186
+  if ($i % 5 != 0) {
187
+   for ($j = 0; $j < count($bmis); $j++) {
188
+    if ($bmis[$j] > $i) { break; }
189
+   } 
190
+
191
+   dashLine2($im, $y, $colours[$j+1]);
192
+  }
193
+ }
194
+
195
+ $lx = $ly = 0;
196
+ $points = array();
197
+
198
+ for($i = 0; $i < XMAX; $i++) {
199
+  $x = 65 + ($i - XMIN) * ((GWIDTH - 120) / (XMAX - XMIN));
200
+
201
+  if ($lx != 0) {
202
+   //imageline($im, $lx, $ly, $x, $y, $ly > $y ? $red : $black);
203
+  }
204
+
205
+  imageline($im, $x, GHEIGHT - 46, $x, GHEIGHT - 43, $black);
206
+  imagestring($im, 1, $x - 2, GHEIGHT - 40, substr($i + 1, 0, 1), $black);
207
+  if ($i >= 9) {
208
+   imagestring($im, 1, $x - 2, GHEIGHT - 32, substr($i + 1, 1, 1), $black);
209
+  }
210
+  #imageline($im, $x - 4, $y - 4, $x + 4, $y + 4, $white);
211
+  #imageline($im, $x + 4, $y - 4, $x - 4, $y + 4, $white);
212
+
213
+  if (isset($data[$i])) {
214
+   $weight = $data[$i];
215
+   $y = GHEIGHT - 45 - ($weight - YMIN) * ((GHEIGHT - 90) / (YMAX - YMIN));
216
+   $points[] = array($x, $y);
217
+
218
+   if ($i % 5 == 4) {
219
+    //dashLine3($im, $x, $y, imagecolorallocatealpha($im, 0x66, 0x66, 0x66, 10));// $i % 5 == 4 ? $black : $grey);
220
+    //imagearc($im, $x, $y, 10, 10, 0, 360, $red);
221
+   }// else {
222
+    imagefilledarc($im, $x, $y, 3, 3, 0, 360, $red, IMG_ARC_PIE);
223
+   //}
224
+
225
+   $lx = $x; $ly = $y;
226
+  }
227
+ }
228
+
229
+ foreach ($targets as $target) {
230
+  list($week, $bmi) = $target;
231
+  $weight = $bmi * HEIGHT * HEIGHT;
232
+
233
+  if ($week > 0) {
234
+   $x = 65 + (($week - 1) - XMIN) * ((GWIDTH - 120) / (XMAX - XMIN));
235
+   $y = GHEIGHT - 45 - ($weight - YMIN) * ((GHEIGHT - 90) / (YMAX - YMIN));
236
+   imageline($im, $x - 4, $y - 4, $x + 4, $y + 4, $red);
237
+   imageline($im, $x - 4, $y + 4, $x + 4, $y - 4, $red);
238
+  }
239
+ }
240
+
241
+ $avg = array();
242
+ $avg[] = $points[0];
243
+ for ($i = 0; $i < XMAX - XMIN - 5; $i += 5) {
244
+  $tx = $ty = 0;
245
+  for ($j = 0; $j < 5; $j++) {
246
+   list($x, $y) = $points[$i + $j];
247
+   $tx += $x; $ty += $y;
248
+  }
249
+  $avg[] = array($tx/5, $ty/5);
250
+ }
251
+
252
+ $colours = array($red, $black);
253
+ foreach (array(/*$avg,*/ $points) as $points) {
254
+  $colour = array_shift($colours);
255
+  $lx = $ly = 0;
256
+  for ($i = 0; $i < count($points) - 1; $i++) {
257
+   $func = interpolate($points, $i, $i + 1);
258
+   list($x1,$y1) = $points[$i];
259
+   list($x2,$y2) = $points[$i + 1];
260
+   for ($t = 0; $t < 1; $t += 4 / ($x2 - $x1)) {
261
+    $x = round($x1 + ($x2 - $x1) * $t, 0);
262
+    $y = round(call_user_func($func, $t), 0);
263
+   
264
+    if ($lx != 0) {
265
+     imagesmoothline($im, $lx, $ly, $x, $y, $red); 
266
+    }
267
+    $lx = $x; $ly = $y;
268
+   }
269
+  }
270
+ }
271
+
272
+ function h00($t) { return 2 * pow($t, 3) - 3 * pow($t, 2) + 1; }
273
+ function h10($t) { return pow($t, 3) - 2 * pow($t, 2) + $t; }
274
+ function h01($t) { return -2 * pow($t, 3) + 3 * pow($t, 2); }
275
+ function h11($t) { return pow($t, 3) - pow($t, 2); }
276
+ function p($t, $p0, $m0, $p1, $m1, $h) { return h00($t) * $p0 + h10($t) * $h * $m0 + h01($t) * $p1 + h11($t) * $h * $m1; }
277
+
278
+ function getLineGradient($a, $b) {
279
+  list($x1,$y1) = $a;
280
+  list($x2,$y2) = $b;
281
+  $dx = $x2 - $x1;
282
+  $dy = $y2 - $y1;
283
+  if ($dx == 0) { return PHP_INT_MAX; }
284
+  return $dy/$dx;
285
+ }
286
+
287
+ function getPointGradient($cur, $last, $next) {
288
+  if ($last == null) { return getLineGradient($cur, $next); }
289
+  if ($next == null) { return getLineGradient($last, $cur); }
290
+  return (getLineGradient($cur, $next) + getLineGradient($last, $cur))/2;
291
+ }
292
+
293
+ function interpolate($data, $from, $to) {
294
+  $prev = isset($data[$from - 1]) ? $data[$from - 1] : null;
295
+  $next = isset($data[$to + 1]) ? $data[$to + 1] : null;
296
+
297
+  $one = $data[$from];
298
+  $two = $data[$to];
299
+
300
+  list($x1, $y1) = $one;
301
+  list($x2, $y2) = $two;
302
+
303
+  $h = $x2 - $x1;
304
+  $p0 = $y1; $p1 = $y2;
305
+  $m0 = getPointGradient($prev, $one, $two);
306
+  $m1 = getPointGradient($one, $two, $next);
307
+
308
+  return create_function('$t', 'return p($t, ' . $p0 . ', ' . $m0 . ', ' . $p1 . ', ' . $m1 . ', ' . $h . ');');
309
+ }
310
+
311
+ imagestring($im, 3, GWIDTH/2 - 50, GHEIGHT - 20, 'Week number', $black);
312
+ imagestringup($im, 3, 10, GHEIGHT/2 + 20, 'Mass (KG)', $black);
313
+ imagestringup($im, 3, GWIDTH - 20, GHEIGHT/2 + 50, 'BMI (KG/m^2)', $black);
314
+ imagestring($im, 5, GWIDTH/2 - 110, 10, 'Graph of mass against time', $black);
315
+
316
+ header('Content-type: image/png');
317
+ imagepng($im); 
318
+
319
+?>

+ 156
- 0
oldgraph.php ファイルの表示

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

読み込み中…
キャンセル
保存