Created
June 30, 2010 20:08
-
-
Save jmhobbs/459148 to your computer and use it in GitHub Desktop.
A bit I wrote to visualize my weight.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Copyright (C) 2010 John Hobbs | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
// Get the max days back we want to look. | |
$max_days = 7; | |
if( isset( $_REQUEST['days'] ) and ! empty( $_REQUEST['days'] ) ) | |
$max_days = intval( $_REQUEST['days'] ); | |
$i = 0; | |
$lines = array(); | |
/* | |
This is a VERY EXACT DATA FORMAT. | |
One record per line, newest record on top (pre-pended) | |
A record looks like: 2010-06-29 200.0 | |
Example: | |
2010-06-30 200.0 | |
2010-06-29 202.6 | |
2010-06-28 200.0 | |
*/ | |
$fh = fopen( 'data.txt', 'r' ); | |
while( ! feof( $fh ) and ++$i <= $max_days ) { | |
$line = fgets( $fh ); | |
if( empty( $line ) ) { continue; } | |
array_unshift( $lines, $line ); | |
} | |
fclose( $fh ); | |
?> | |
<html> | |
<head> | |
<script type="text/javascript" src="http://www.google.com/jsapi"></script> | |
<script type="text/javascript"> | |
google.load( 'visualization', '1', { 'packages': [ 'corechart' ] } ); | |
google.setOnLoadCallback( drawChart ); | |
function drawChart() { | |
var data = new google.visualization.DataTable(); | |
data.addColumn( 'string', 'Date' ); | |
data.addColumn( 'number', 'Weight' ); | |
data.addRows( <?php echo count( $lines ); ?> ); | |
<?php | |
$i = 0; | |
foreach( $lines as $line ): | |
?> | |
data.setValue( <?php echo $i; ?>, 0, '<?php echo substr( $line, 0, 10 ); ?>' ); | |
data.setValue( <?php echo $i; ?>, 1, <?php echo floatval( substr( $line, 11 ) ); ?> ); | |
<?php | |
++$i; | |
endforeach; | |
?> | |
var chart = new google.visualization.LineChart( document.getElementById( 'chart_div' ) ); | |
chart.draw( data, { width: 800, height: 600, title: 'Weight Over Time' } ); | |
} | |
</script> | |
</head> | |
<body> | |
<div id="chart_div"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment