I just discovered Chart::Strip (thanks, bigpresh)! Earlier this year, I was trying to lose a little weight. At first, I was weighing myself each week, so I wrote a little program with Chart::Clicker to make a graph of my progress. Just now, I re-wrote that program using Chart::Strip and it's much simpler. I think I was able to use more defaults with Chart::Strip. In the Chart::Clicker version, a lot of the code is fussing with the ranges and such. With Chart::Strip, I added a title, turned off the transparent background (I typically just viewed the results in eog, which shows a checkerboard for transparency) and made the line blue instead of green. The axis ranges and labels and all are from default values.
Perfect! My weight stabilized and I stopped paying attention, so the data just kind of ends. I don't really care how much I weigh, I just want my clothes to fit!
I put the Chart::Strip version of the code at the end of the post because it's kind of long. Well, the program isn't long at all, but I just stuck my data in the same file.
One thing to note is the fancy new Perl-5.14 use of push on an array reference. We would like to have just said
my $data;
but autovivify doesn't work when we do that. I couldn't really tell from that thread on p5p, but I hope they're going to fix this in Perl 5.16. In the mean time, we can just initialize it to an aref.
#!/usr/bin/env perl use v5.14; use autodie; use warnings; use Chart::Strip; use Data::Dump qw(dump); use Time::Local qw(timelocal_nocheck); my $file = shift // 'weight.png'; my $debug = shift // 0; my $data = []; while (readline DATA) { next unless /\A\s*(?<doy>\d+)\s*=>\s*(?<weight>[\d\.]+)/; push $data, {time => timelocal_nocheck(0,0,0,$+{doy},0,2011), value => $+{weight}, }; } dump $data if $debug; my $chart = Chart::Strip->new(title => q{Tim's weight (lbs)}, transparent => 0, ); $chart->add_data($data, {color => 'blue'}); open my $fh, '>', $file; $fh->print($chart->png); close $fh; system("xdg-open $file") == 0 or warn "eog $file\n"; __END__ 1 => 180.2, 8 => 173.8, 15 => 172.2, 22 => 171.2, 29 => 170.2, 36 => 168.2, 43 => 166.6, 50 => 168.2, 57 => 165.6, 64 => 165.4, 71 => 165.0, 78 => 163.6, 85 => 163.2, 92 => 161.8, 99 => 160.6, 106 => 162.4, 113 => 164.3, 120 => 162.6, 134 => 161.8, 141 => 158.8, 148 => 160.6, 155 => 158.2, 162 => 161.8, 169 => 162.4, 176 => 159.8, 183 => 162.6, 190 => 160.6, 197 => 160.8, 204 => 159.6, 211 => 159.8, 218 => 158.8,
Recent Comments