Na, im Prinzip musst du dir dafür nur den Code von btn_export_canvas() ansehen.
Die Methode wird aufgerufen, wenn der Export-Button gedrückt wurde.
Du kannst dir ja schnell einen
Tk::Button unter die Canvas machen:
my $export_button = $mw->Button(
-command => sub{ btn_export_canvas($mw, $ca) },
-text => 'export canvas',
)->pack();
-command bindet die Ausführung der Subroutine btn_export_canvas() an sich, d.h. wenn jemand auf den Button drückt, ruft er btn_export_canvas() auf. Aber das kennst du bestimmt schon.
In der Sub btn_export_canvas() brauchst du das MainWindow ($mw) für den Dialog und die Canvas, weil du von der die Methode postscript() aufrufst.
Versuch mal, den Code Zeile für Zeile nachzuvollziehen. Dann kannst du zu spezifischen Zeilen fragen, wenn was unklar ist.
Grüße, pktm
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
#!/usr/local/bin/perl -w
use strict;
use lib '../.';
use Tk;
use Tk::Graph;
use Date::Format;
use PostScript::Convert qw/psconvert/;
my $mw = Tk::MainWindow->new;
my $data = {
'one' => 0.1,
'two' => 0.1,
'three' => 10,
};
my $ca = $mw->Graph(
-type => 'HBARS',
# -shadowdepth => 5,
-padding => [50,50,50,50],
-light => [80,50,0],
-wire => 'gray',
-bg => 'white',
-threed => 10,
)->pack(-expand => 1,
-fill => 'both');
$ca->set($data); # Auf Daten anzeigen
my $export_button = $mw->Button(
-command => sub{ btn_export_canvas($mw, $ca) },
-text => 'export canvas',
)->pack();
Tk::MainLoop;
=head2 btn_export_canvas( $mw, $graph )
todo
=cut
sub btn_export_canvas {
my $mw = shift;
my $graph = shift;
# -- file dialog
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 @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