#!/usr/bin/perl use warnings; use strict; use 5.010; # // use Tk; use constant MESSAGE => 'Point a mouse over a bar to see the details'; my $width = 25; my $height = 200; my $space = 10; my $input = shift // 'xp.out'; open my $IN, '<', $input or die $!; my @rectangles; my ($oldest, $max, $min) = (0, 0, 0); while (my $line = <$IN>) { my ($date, $xp) = split ' ', $line; my ($year, $month) = split /-/, $date; my $x = $year * 12 + $month; $oldest ||= $x; $max = $xp if $xp > $max; $min = $xp if $xp < $min; push @rectangles, [ $x - $oldest, $xp, $date ]; } my $step = $height / $max; my $mw = 'MainWindow'->new(-title => 'Average XP per month'); my $info = MESSAGE; $mw->Label(-textvariable => \$info, -foreground => 'black', -background => 'white', -relief => 'ridge', )->pack(-pady => 5); my $canvas = $mw->Canvas(-background => 'white', -width => $width * (1 + $rectangles[-1][0]), -height => $height + 2 * $space + $step * abs $min, )->pack(-padx => 10, -pady => 10); for my $rect (@rectangles) { $canvas->createRectangle( $rect->[0] * $width, $space + $step * ($max - $rect->[1]), ++$rect->[0] * $width, $space + $height, -fill => (qw/blue cyan green yellow orange red/) [($rect->[1] + abs $min) / ($max + abs $min) * 5], -outline => 'black', -tag => "bar$rect->[0]", ); $canvas->bind("bar$rect->[0]", '' => sub { $info = "$rect->[2]: $rect->[1]" }); } $canvas->bind('all', '' => sub { $info = MESSAGE }); $mw->Button(-text => 'Quit', -command => sub { exit }, )->pack; MainLoop();