PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 448: How to dynamically scale a bar graph using scrollbars - Part 4 - x and y directions?
Written by kksou   
Saturday, 01 March 2008
Problem

This is in response to Paamayim's post titled 'smooth scrolling for a gtkimage drawing'.

He has set up a bar graph and wants to allow the user to dynamically change the scale of the x-axis and y-axis through the scrollbars as shown below.

As there are quite a number of techniques involved, to make it easier to follow, I've spread the techniques over 5 parts:

  • Part 1 - adds the vscrollbar
  • Part 2 - aligns the graph to bottom left
  • Part 3 - adds the hscrollbar
  • Part 4 (this article) - adds both vscrollbar and hscrollbar
  • Part 5 - use only one scrollbar to control scaling of both axis

In this Part 4, we will combine the codes from Part 2 and Part 3 so that the users now have the vscrollbar for scaling the y-axis and the hscrollbar for scaling the x-axis as shown below:

How to dynamically scale a bar graph using scrollbars - Part 4 - x and y directions?


Solution
  • Please refer to Part 2 for the set up of vscrollbar.
  • Please refer to Part 3 for the set up of hscrollbar.

Sample Code
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   
<?php
$window = new GtkWindow();
$window->set_title($argv[0]);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Dynamically scale a bar graph using scrollbars\n".
"Part 4: with both hscrollbar and vscrollbar");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$title->set_justify(Gtk::JUSTIFY_CENTER);
$alignment = new GtkAlignment(0.5, 0, 0, 0);
$alignment->add($title);
$vbox->pack_start($alignment, 0, 0);
$vbox->pack_start(new GtkLabel(), 0, 0);

setup_bargraph($vbox);

$window->show_all();
Gtk::main();

function setup_bargraph($vbox) {
    global $img;
    $vbox->pack_start($hbox = new GtkHBox());
    $hbox->pack_start($vbox2 = new GtkVBox());
    $img = new GtkImage();
    $vbox2->pack_start(new GtkVBox());
    $vbox2->pack_start($hbox2 = new GtkHBox(), 0);
    $hbox2->pack_start($img, 0);

    global $hadj, $vadj;
    $vadj = new GtkAdjustment(0.87, 0.87, 2, 0.1, .5);
    $vscrollbar = new GtkVScrollbar($vadj);
    $vscrollbar->connect('value-changed', 'on_value_changed');
    $hbox->pack_start($vscrollbar, 0);

    $hadj = new GtkAdjustment(0.5, 0.5, 2, 0.01, .5);
    $hscrollbar = new GtkHScrollbar($hadj);
    $hscrollbar->connect('value-changed', 'on_value_changed');
    $vbox->pack_start($hscrollbar, 0);

    $vadj->set_value(1);
    $hadj->set_value(1);
    $vadj->value_changed();

    $hbox->set_size_request(600, 400);
}

function draw_bargraph($x_scale, $y_scale) {
    global $img;
    $max_width = 360 * $x_scale;
    $max_height = 200 * $y_scale;
    $margin = 40;
    $im = imagecreate($max_width, $max_height);
    $white = imagecolorallocate($im,255,255,255);
    $black = imagecolorallocate($im,0,0,0);
    $blue = imagecolorallocate($im,0,0,255);

    $data = array('120','160','300','240');
    imageline($im, 10, 5, 10, $max_height-$margin, $black );
    imageline($im, 10, $max_height-$margin,
  • Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
  • Registration is free and immediate.
  • Have some doubt about the registration? Please read this forum article.
Explanation

The above code is a synthesis of the codes from Part 2 and Part 3.

  1. Note that we can handle both scrollbars with just one signal handler.
  2. Redraw the bar graph.

Note

This example serves mainly to illustrate the use of GtkAdjustment and GtkVScrollbar. I will leave it to you to optimize and polish the gd2 portion (for the display of the bar graph).


Related Links

User reviews

There are no user reviews yet.

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Copyright © 2006-2008. kksou.com. All Rights Reserved