使用Perl和Gtk2(或Gtk3),我想在GUI应用程序中显示图形。
Chart :: Clicker看起来很有希望,但它似乎只写入文件。有没有办法把它变成GtkDrawingArea或类似的?
答案 0 :(得分:2)
经过大量的实验,我发现了它。
通过源阅读Chart :: Clicker,我发现$ chart->驱动程序是Cairo对象,而> surface是实际的Cairo表面。可以基于开罗表面直接绘制Gtk :: Image。
说明如何执行此操作的完整程序:
#!/usr/bin/perl -w
use strict;
use Gtk3 -init;
use Chart::Clicker;
use Chart::Clicker::Data::Series;
use Chart::Clicker::Data::DataSet;
my $win = Gtk3::Window->new;
my $img = Gtk3::Image->new;
$win->add($img);
my $chart = Chart::Clicker->new;
my $dataset = Chart::Clicker::Data::DataSet->new(
series => [Chart::Clicker::Data::Series->new(
keys => [1, 2, 3, 4, 5],
values => [2, 3, 5, 7, 11],
)],
);
$chart->add_to_datasets($dataset);
$chart->draw;
$img->set_from_surface($chart->driver->surface);
$win->signal_connect(destroy => sub{Gtk3->main_quit});
$win->show_all;
Gtk3::main;
<强>更新强>
On Gtk3&lt; 3.10,将包含set_from_surface
的行替换为:
my $loader = Gtk3::Gdk::PixbufLoader->new;
$chart->driver->surface->write_to_png_stream(sub {
$loader->write([unpack 'C*', $_[1]]);
});
$loader->close;
$img->set_from_pixbuf($loader->get_pixbuf);