Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | # |
michael@0 | 2 | # This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | # License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 5 | package URLTimingGraph; |
michael@0 | 6 | use strict; |
michael@0 | 7 | use GD; |
michael@0 | 8 | use GD::Graph::linespoints; |
michael@0 | 9 | use GD::Graph::points; |
michael@0 | 10 | use GD::Graph::lines; |
michael@0 | 11 | use GD::Graph::mixed; |
michael@0 | 12 | use GD::Graph::colour; |
michael@0 | 13 | use GD::Graph::Data; |
michael@0 | 14 | |
michael@0 | 15 | sub new { |
michael@0 | 16 | my $proto = shift; |
michael@0 | 17 | my $class = ref($proto) || $proto; |
michael@0 | 18 | my $self = {}; |
michael@0 | 19 | bless ($self, $class); |
michael@0 | 20 | $self->{data} = shift || die "No data."; |
michael@0 | 21 | my $args = shift || {}; |
michael@0 | 22 | $self->{cgimode} = $args->{cgimode} || 0; |
michael@0 | 23 | $self->{title} = $args->{title} || ""; |
michael@0 | 24 | $self->{types} = $args->{types} || ['lines', undef, undef, undef, undef, undef, undef]; |
michael@0 | 25 | $self->{dclrs} = $args->{dclrs} || [qw(lred)]; |
michael@0 | 26 | $self->{legend} = $args->{legend} || [qw(undef)]; |
michael@0 | 27 | $self->{y_max_value} = $args->{y_max_value} || 10000; |
michael@0 | 28 | $self->{width} = $args->{width} || 800; |
michael@0 | 29 | $self->{height} = $args->{height} || 720; |
michael@0 | 30 | return $self; |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | sub _set_standard_options { |
michael@0 | 34 | my $self = shift; |
michael@0 | 35 | $self->{graph}->set( |
michael@0 | 36 | x_label => '', |
michael@0 | 37 | y_label => 'Page Load Time (msec)', |
michael@0 | 38 | default_type => 'points', |
michael@0 | 39 | x_labels_vertical => 1, |
michael@0 | 40 | y_long_ticks => 1, |
michael@0 | 41 | x_tick_length => 8, |
michael@0 | 42 | x_long_ticks => 0, |
michael@0 | 43 | line_width => 2, |
michael@0 | 44 | marker_size => 3, |
michael@0 | 45 | markers => [8], |
michael@0 | 46 | show_values => 0, |
michael@0 | 47 | transparent => 0, |
michael@0 | 48 | interlaced => 1, |
michael@0 | 49 | skip_undef => 1, |
michael@0 | 50 | ) |
michael@0 | 51 | || warn $self->{graph}->error; |
michael@0 | 52 | $self->{graph}->set_title_font(GD::Font->Giant); |
michael@0 | 53 | $self->{graph}->set_x_label_font(GD::Font->Large); |
michael@0 | 54 | $self->{graph}->set_y_label_font(GD::Font->Large); |
michael@0 | 55 | $self->{graph}->set_x_axis_font(GD::Font->Large); |
michael@0 | 56 | $self->{graph}->set_y_axis_font(GD::Font->Large); |
michael@0 | 57 | $self->{graph}->set_legend_font(GD::Font->Giant); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | sub plot { |
michael@0 | 61 | my $self = shift; |
michael@0 | 62 | $self->{graph} = new GD::Graph::mixed($self->{width}, |
michael@0 | 63 | $self->{height}); |
michael@0 | 64 | $self->_set_standard_options(); |
michael@0 | 65 | |
michael@0 | 66 | $self->{graph}->set(title => $self->{title}, |
michael@0 | 67 | types => $self->{types}, |
michael@0 | 68 | y_max_value => $self->{y_max_value}, |
michael@0 | 69 | dclrs => $self->{dclrs}, |
michael@0 | 70 | ) |
michael@0 | 71 | || warn $self->{graph}->error; |
michael@0 | 72 | |
michael@0 | 73 | $self->{graph}->set_legend( @{$self->{legend}} ); |
michael@0 | 74 | |
michael@0 | 75 | # draw the graph image |
michael@0 | 76 | $self->{graph}->plot($self->{data}) || |
michael@0 | 77 | die $self->{graph}->error; |
michael@0 | 78 | |
michael@0 | 79 | # send it back to stdout (or browser) |
michael@0 | 80 | print "Content-type: image/png\n\n" if $self->{cgimode}; |
michael@0 | 81 | binmode STDOUT; |
michael@0 | 82 | print $self->{graph}->gd->png(); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | |
michael@0 | 86 | 1; #return true |