Thread Perl TK: jpg aus chart erzeugen (13 answers)
Opened by perln00b at 2010-06-29 19:18

pktm
 2010-06-29 22:45
#139212 #139212
User since
2003-08-07
2921 Artikel
BenutzerIn
[Homepage]
user image
Es hat mich mal wieder nicht losgelassen :)

Hier ist ein Beispiel. Leider kann das Modul keine jpgs erzeugen :-s

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/perl

package TkApp;

use base qw(Class::Accessor);

use strict;
use warnings;
use FileHandle;
use Data::Dumper qw/Dumper/;
use Date::Format;
use Tk;
use Tk::Graph;
use PostScript::Convert qw/psconvert/;

__PACKAGE__->follow_best_practice();
__PACKAGE__->mk_accessors(qw(mw graph));

our $VERSION = 0.1;

=head1 NAME

TkApp - Demonstration of the export functionality of a canvas. Allows for
different export formats than ps via L<PostScript::Convert>.

=head1 SYNOPSIS

use strict;
use warnings;

my $app = TkApp->new();
$app->run();

=head1 DESCRIPTION

A Tk::Canvas provides the method L<Tk::Canvas/postscript> to export the
contents to postscript. This code provides an example of how to work with
that method and how to allow for other export formats as postscript. Other
export formats are provided via L<PostScript::Convert>. Unfortunately, it's
lacking support for jpg files. Please feel free to commit a patch :)

=head2 EXPORT

None by default.


=head1 METHODS

=head2 new()

Ctor. Calls _build_gui.

=cut

sub new {
my $class = shift;

my $self = bless({}, $class);

my $mw = Tk::MainWindow->new();
$self->set_mw($mw);

$self->_build_gui();

return $self;
} # /new




=head2 _build_gui()

Build GUI. Set up widgets etc. Positioning is done here, too.

Private method. Don't call it directly, as this will be done by new().

=cut

sub _build_gui {
my $self = shift;
my $mw = $self->get_mw();


# -- menu
my $menuitems = [
[Cascade => "~File", -menuitems =>
[
[Button => "~ExportCanvas", -command => sub{
return $self->btn_export_canvas();
}],
[Separator => ""],
[Button => "~Exit", -command => sub{ exit(0); }],
],
],
];
my $menu = $mw->Menu(-menuitems => $menuitems);
$mw->configure(-menu => $menu);


# -- graph
my $data = {
Sleep => 51,
Work => 135,
Access => 124,
mySQL => 5
};

my $ca = $mw->Graph(
-type => 'BARS',
)->pack(
-expand => 1,
-fill => 'both',
);
$ca->set($data);
$self->set_graph($ca);

} # /_build_gui




=head2 btn_export_canvas()

Menu item for canvas export has been clicked. Display a file dialog.
Evaluate response. If filename is given, export the contents of the canvas to
this file. Returns 1 on successfull export, 0 otherwise.

If no file is given, export has been aborted. This will return 0.

TODO: this code is ugly. Someone split up the dialog and the export in separate
methods.
TODO: allow for more export formats, evaluate chosen filetype.
FIXME: the export as png file isn't readable.

=cut

sub btn_export_canvas {
my $self = shift;

# -- file dialog
my $mw = $self->get_mw();
my $types = [ ['PNG files', '.png'],
['All Files', '*'],];
my $output_path = $mw->getSaveFile(
-filetypes => $types,
-initialfile => time2str("%Y-%m-%d", time()) . '-export',
-defaultextension => '.png',
);

return 0 unless $output_path;


# -- actual export
my $graph = $self->get_graph();
my @all_items = $graph->find('all');
my @bbox = $graph->bbox(@all_items);

my $ps = $graph->postscript(
'-x' => $bbox[0],
'-y' => $bbox[1],
-width => $bbox[2] - $bbox[0],
-height => $bbox[3] - $bbox[1],
);
psconvert(\$ps, $output_path, format => 'png');

return 1;
} # /btn_export_canvas




=head2 run()

This actually starts the application, including the gui event loop.

=cut

sub run {
my $self = shift;
my $mw = $self->get_mw();

$mw->MainLoop();
} # /run




=head1 SEE ALSO

L<Tk::Canvas>, L<PostScript::Convert>.

Inspired by a thread at L<http://www.perl-community.de>.

=head1 AUTHOR

A. U. Thor, E<lt>a.u.thor@a.galaxy.far.far.awayE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2009 by A. U. Thor

This library is free software; you can redistribute it and/or modify it under
the same terms as Perl itself, either Perl version 5.8.8 or, at your option,
any later version of Perl 5 you may have available.

=cut

1;

use strict;
use warnings;

my $app = TkApp->new();
$app->run();


Grüße, pktm
Last edited: 2010-07-03 00:11:02 +0200 (CEST)
http://www.intergastro-service.de (mein erstes CMS :) )

View full thread Perl TK: jpg aus chart erzeugen