File: chart-demo.pl

package info (click to toggle)
libgtk-perl 0.7009-12
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,956 kB
  • ctags: 2,260
  • sloc: perl: 13,998; xml: 9,919; ansic: 2,894; makefile: 64; cpp: 45
file content (184 lines) | stat: -rw-r--r-- 3,974 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/perl -w

#TITLE: Chart demo
#REQUIRES: Gtk

# chart-demo.pl
#
# Gtk+/Perl chart demo.
#
# 19980813 PMC Created.
#
# Coding and mathematics are horrible, but this is just to demonstrate
# the use of colors and graphics contexts.

#use strict;

use Gtk;

init Gtk;

$pixmapChart = undef;
$width = 600;
$height = 240;

sub do_chart_expose {
  my (
    $widget,
    $event
  ) = @_;

  my (
    $event_area
  );

  $event_area = ${$event}{'area'};

  $widget->window->draw_pixmap(
    $widget->style->fg_gc('normal'),
    $pixmapChart,
    ${$event_area}[0],
    ${$event_area}[1],
    ${$event_area}[0],
    ${$event_area}[1],
    ${$event_area}[2],
    ${$event_area}[3]
  );

  0;
}

sub draw_chart {

  my ( $widget ) = @_;

  my ( $margin, $axemargin ) = ( 20, 20 );
  my ( $xticks, $yticks, $tickwidth ) = ( 10, 24, 5 );

  my $steps = 90;
  my ( $step );

  my ( $tick);

  my $red_gc = new Gtk::Gdk::GC ( $widget->window );
  my $green_gc = new Gtk::Gdk::GC ( $widget->window );
  my $blue_gc = new Gtk::Gdk::GC ( $widget->window );

  my $red_color = $widget->window->get_colormap->color_alloc( { red => 65000, green => 0, blue => 0 } );
  my $green_color = $widget->window->get_colormap->color_alloc( { red => 0, green => 65000, blue => 0 } );
  my $blue_color = $widget->window->get_colormap->color_alloc( { red => 0, green => 0, blue => 65000 } );

  $red_gc->set_foreground( $red_color );
  $green_gc->set_foreground( $green_color );
  $blue_gc->set_foreground( $blue_color );

  $pixmapChart = new Gtk::Gdk::Pixmap (
    $widget->window,
    $width, 
    $height, 
    -1
  );
  # clear the pixmap to white
  $pixmapChart->draw_rectangle(
    $widget->style->white_gc,
    1,
    0,
    0,
    $width,
    $height
  );

  $pixmapChart->draw_line(
    $blue_gc,
    $margin,
    $height - $margin - $axemargin,
    $width - $margin,
    $height - $margin - $axemargin
  );

  $pixmapChart->draw_line(
    $blue_gc,
    $margin + $axemargin,
    $margin,
    $margin + $axemargin,
    $height - $margin
  );

  $tickstep = ( $height - ( $margin * 2 ) - ( $axemargin * 2 ) ) / $xticks;
  for $tick ( 1 .. $xticks ) {
    $pixmapChart->draw_line(
      $red_gc,
      $margin + $axemargin - $tickwidth,
      $height - $margin - $axemargin - ( $tick * $tickstep ),
      $margin + $axemargin,
      $height - $margin - $axemargin - ( $tick * $tickstep )
    );
  }

  $tickstep = ( $width - ( $margin * 2 ) - ( $axemargin * 2 ) ) / $yticks;
  for $tick ( 1 .. $yticks ) {
    $pixmapChart->draw_line(
      $red_gc,
      $margin + $axemargin + ( $tick * $tickstep ),
      $height - $margin - $axemargin,
      $margin + $axemargin + ( $tick * $tickstep ),
      $height - $margin - $axemargin + $tickwidth
    );
  }

  $range = $width - 2 * $margin - 2 * $axemargin;
  $center = $height / 2;
  $old = $center;
  for $step ( 1 .. $range ) {
    $new = 3.14 / ( $range / 2 - $step + 0.000000001 ) * 20 + $center;
    if ( $new > ( $height - $margin - $axemargin ) ) {
      $new = $margin + $axemargin;
    }
    $pixmapChart->draw_line(
      $green_gc,
      $margin + $axemargin + $step,
      $old,
      $margin + $axemargin + $step + 1,
      $new
    );
    $old = $new;
  }

}

# main

  my (
    $windowMain,
    $vboxMain,
    $dareaChart
  );

  $windowMain = new Gtk::Window( 'toplevel' );

  $windowMain->signal_connect( 'destroy', sub { Gtk::main_quit( Gtk ); } );

  $windowMain->set_title( 'Chart drawing' );
  $windowMain->border_width( 0 );

  $vboxMain = new Gtk::VBox ( 0, 0 );
  $vboxMain->border_width( 10 );
  $windowMain->add( $vboxMain );
  $vboxMain->show;

  $dareaChart = new Gtk::DrawingArea;
  $dareaChart->signal_connect( 'expose_event', \&do_chart_expose );
  $dareaChart->size( $width, $height );
  $vboxMain->pack_start( $dareaChart, 1, 1, 0 );
  $dareaChart->set_events( [ 'exposure_mask' ] );
  $dareaChart->show;

  $dareaChart->realize;

  draw_chart ( $dareaChart );

  $windowMain->show;

  Gtk::main( Gtk );

# end