| 1 | package Chart::Clicker::Decoration::Legend::Tabular; | ||
| 2 | $Chart::Clicker::Decoration::Legend::Tabular::VERSION = '2.90'; | ||
| 3 | use Moose; | ||
| 4 | |||
| 5 | extends 'Chart::Clicker::Decoration::Legend'; | ||
| 6 | |||
| 7 | # ABSTRACT: Tabular version of Legend | ||
| 8 | |||
| 9 | use Graphics::Primitive::Font; | ||
| 10 | use Graphics::Primitive::Insets; | ||
| 11 | use Graphics::Primitive::TextBox; | ||
| 12 | use Graphics::Color::RGB; | ||
| 13 | |||
| 14 | use Layout::Manager::Grid; | ||
| 15 | |||
| 16 | |||
| 17 | has '+border' => ( | ||
| 18 | default => sub { | ||
| 19 | my $b = Graphics::Primitive::Border->new; | ||
| 20 | $b->color(Graphics::Color::RGB->new( red => 0, green => 0, blue => 0)); | ||
| 21 | $b->width(1); | ||
| 22 | return $b; | ||
| 23 | } | ||
| 24 | ); | ||
| 25 | |||
| 26 | |||
| 27 | has '+color' => ( | ||
| 28 | default => sub { Graphics::Color::RGB->new( red => 0, green => 0, blue => 0) } | ||
| 29 | ); | ||
| 30 | |||
| 31 | |||
| 32 | has 'data' => ( | ||
| 33 | is => 'rw', | ||
| 34 | isa => 'ArrayRef[ArrayRef[Str]]', | ||
| 35 | required => 1 | ||
| 36 | ); | ||
| 37 | |||
| 38 | |||
| 39 | has 'font' => ( | ||
| 40 | is => 'rw', | ||
| 41 | isa => 'Graphics::Primitive::Font', | ||
| 42 | default => sub { | ||
| 43 | Graphics::Primitive::Font->new | ||
| 44 | } | ||
| 45 | ); | ||
| 46 | |||
| 47 | |||
| 48 | has 'header' => ( | ||
| 49 | is => 'rw', | ||
| 50 | isa => 'ArrayRef[Str]', | ||
| 51 | predicate => 'has_header' | ||
| 52 | ); | ||
| 53 | |||
| 54 | |||
| 55 | has 'item_padding' => ( | ||
| 56 | is => 'rw', | ||
| 57 | isa => 'Graphics::Primitive::Insets', | ||
| 58 | default => sub { | ||
| 59 | Graphics::Primitive::Insets->new({ | ||
| 60 | top => 3, left => 3, bottom => 3, right => 5 | ||
| 61 | }) | ||
| 62 | } | ||
| 63 | ); | ||
| 64 | has '+layout_manager' => ( | ||
| 65 | default => sub { | ||
| 66 | my $self = shift; | ||
| 67 | my $header_rows = $self->has_header ? 1 : 0; | ||
| 68 | return Layout::Manager::Grid->new( | ||
| 69 | rows => scalar(@{ $self->data }) + $header_rows, | ||
| 70 | columns => scalar(@{ $self->data->[0] }) + 1 | ||
| 71 | ); | ||
| 72 | }, | ||
| 73 | lazy => 1 | ||
| 74 | ); | ||
| 75 | |||
| 76 | override('prepare', sub { | ||
| 77 | my ($self, $driver) = @_; | ||
| 78 | |||
| 79 | return if $self->component_count > 0; | ||
| 80 | |||
| 81 | my $ca = $self->clicker->color_allocator; | ||
| 82 | |||
| 83 | my $font = $self->font; | ||
| 84 | |||
| 85 | my $ii = $self->item_padding; | ||
| 86 | |||
| 87 | if($self->is_vertical) { | ||
| 88 | # This assumes you aren't changing the layout manager... | ||
| 89 | $self->layout_manager->anchor('north'); | ||
| 90 | } | ||
| 91 | |||
| 92 | my $row_offset = 0; | ||
| 93 | if($self->has_header) { | ||
| 94 | $row_offset = 1; | ||
| 95 | my $count = 0; | ||
| 96 | # foreach my $d (@{ $self->header }) { | ||
| 97 | for(0..scalar(@{ $self->header }) - 1) { | ||
| 98 | $self->add_component( | ||
| 99 | Graphics::Primitive::TextBox->new( | ||
| 100 | color => $self->color, | ||
| 101 | font => $font, | ||
| 102 | padding => $ii, | ||
| 103 | text => $self->header->[$_] | ||
| 104 | ), | ||
| 105 | { row => 0, column => $_ } | ||
| 106 | ); | ||
| 107 | $count++; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | my @data = @{ $self->data }; | ||
| 112 | |||
| 113 | my $count = 0; | ||
| 114 | foreach my $ds (@{ $self->clicker->datasets }) { | ||
| 115 | foreach my $s (@{ $ds->series }) { | ||
| 116 | |||
| 117 | my $color = $ca->next; | ||
| 118 | |||
| 119 | unless($s->has_name) { | ||
| 120 | $s->name("Series $count"); | ||
| 121 | } | ||
| 122 | |||
| 123 | my $tb = Graphics::Primitive::TextBox->new( | ||
| 124 | color => $color, | ||
| 125 | font => $font, | ||
| 126 | padding => $ii, | ||
| 127 | text => $s->name | ||
| 128 | ); | ||
| 129 | |||
| 130 | $self->add_component($tb, { row => $count + $row_offset, column => 0 }); | ||
| 131 | |||
| 132 | for(0..scalar(@{ $data[$count] }) - 1) { | ||
| 133 | $self->add_component( | ||
| 134 | Graphics::Primitive::TextBox->new( | ||
| 135 | color => $color, | ||
| 136 | font => $font, | ||
| 137 | padding => $ii, | ||
| 138 | text => $data[$count]->[$_] | ||
| 139 | ), | ||
| 140 | { row => $count + $row_offset, column => $_ + 1 } | ||
| 141 | ); | ||
| 142 | } | ||
| 143 | |||
| 144 | $count++; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | super; | ||
| 149 | |||
| 150 | $ca->reset; | ||
| 151 | }); | ||
| 152 | |||
| 153 | __PACKAGE__->meta->make_immutable; | ||
| 154 | |||
| 155 | no Moose; | ||
| 156 | |||
| 157 | 1; | ||
| 158 | |||
| 159 | __END__ |