YaST2 Developers Documentation: YCP UI Reference: Widgets



YCP UI Widget Reference

Back to the widget index

BarGraph Horizontal bar graph (optional widget)

Description

A horizontal bar graph for graphical display of proportions of integer values. Labels can be passed for each portion; they can include a "%1" placeholder where the current value will be inserted (sformat() -style) and newlines. If no labels are specified, only the values will be displayed. Specify empty labels to suppress this.

Note: This is a "special" widget, i.e. not all UIs necessarily support it. Check for availability with HasSpecialWidget(`BarGraph) before using it.

Arguments

list values the initial values (integer numbers)

Optional

list labels the labels for each part; use "%1" to include the current numeric value. May include newlines.

Special Properties

integer-list Values The numerical values of each segment.
string-list Labels The labels for each segment. "\n" allowed. Use "%1" as a placeholder for the current value.

Sample Usage

if ( HasSpecialWidget(`BarGraph) {... `BarGraph( [450, 100, 700], [ "Windows used\n%1 MB", "Windows free\n%1 MB", "Linux\n%1 MB" ] )

Examples

Example 1: BarGraph1.ycp


UI(``{
    if ( ! HasSpecialWidget(`BarGraph) )
    {
	OpenDialog(
		   `VBox( 
			 `Label("Error: This UI doesn't support the BarGraph widget!"),
			 `PushButton(`opt(`default), "&OK")
			 )
		   );
	UserInput();
	CloseDialog();
	
	return;
    }
    
    OpenDialog(
	       `VBox(
		     `HSpacing( 60 ),	// wider default size
		     `BarGraph( [450, 100, 700] ),
		     `PushButton(`opt(`default), "&OK")
		     )
	       );
    UserInput();
    CloseDialog();
})
		

Example 2: BarGraph2.ycp


UI(``{
    if ( ! HasSpecialWidget(`BarGraph) )
    {
	OpenDialog(
		   `VBox( 
			 `Label("Error: This UI doesn't support the BarGraph widget!"),
			 `PushButton(`opt(`default), "&OK")
			 )
		   );
	UserInput();
	CloseDialog();
	
	return;
    }
    
    OpenDialog(
	       `VBox(
		     `HSpacing(80),		// force width
		     `HBox(`opt(`debugLayout),
			   `BarGraph(
				     `opt(`vstretch),
				     [600, 350, 800],
				     [
				      "Windows\nused\n%1 MB",
				      "Windows\nfree\n%1 MB",
				      "Linux\n%1 MB"
				     ]
				     )
			   ),
		     `PushButton(`opt(`default), "&OK")
		     )
	       );
    UserInput();
    CloseDialog();
})
		

Example 3: BarGraph3.ycp


// Advanced BarGraph example:
//
// Create a dialog with a BarGraph with a number of segments
// and a "+" and a "-" button for each segment.

UI(``{
    // Check for availability of the BarGraph widget - this is necessary since
    // this is an optional widget that not all UIs need to support.
    
    if ( ! HasSpecialWidget(`BarGraph) )
    {
	// Pop up error message if the BarGraph widget is not available
	
	OpenDialog(
		   `VBox( 
			 `Label("Error: This UI doesn't support the BarGraph widget!"),
			 `PushButton(`opt(`default), "&OK")
			 )
		   );
	UserInput();
	CloseDialog();
	
	return;
    }



    // list	values = [ 100, 200, 300, 150, 250, 120, 200, 120 ];
    list	values = [ 100, 100, 100, 100, 100, 100, 100, 100 ];
    integer 	inc = 10;  // increment / decrement for each button press
    
    // Create the main dialog:
    //
    // One BarGraph at the top, below that two rows of equal sized (thus the
    // weights) buttons, below that a "close" button.
    //
    // The "+" / "-" -buttons use an integer value as their ID which can be
    // used to point to the index of the value to be changed. If the ID is
    // negative it means subtract rather than add.

    term plus_buttons =  `HBox();
    term minus_buttons = `HBox();
    integer i = 1;

    foreach( `val, values, ``{
	plus_buttons  = add( plus_buttons,  `HWeight( 1, `PushButton(`id( i), "+" ) ) );
	minus_buttons = add( minus_buttons, `HWeight( 1, `PushButton(`id(-i), "-" ) ) );
	i = i+1;
    });
    
    OpenDialog(
	       `VBox( 
		     `BarGraph(`id(`bar), values ),
		     plus_buttons,
		     minus_buttons,
		     `PushButton(`id(`close), `opt(`default), "&Close")
		     )
	       );

    
    // Event processing loop - left only via the "close" button
    // or the window manager close button / function.
    
    do
    {
	any button_id = UserInput();	// wait for button click

	if ( button_id != `close && button_id != `cancel )
	{
	    integer sign = 1;
	    
	    if ( button_id < 0 )
	    {
		sign	  = -1;
		button_id = -button_id;
	    }

	    // Loop over the values. Increment the value corresponding to the
	    // clicked button, decrement all others as to maintain the total
	    // sum of all values - or vice versa for negative button IDs
	    // (i.e. "-" buttons).
	    
	    list new_values = [];
	    integer i = 0;

	    while ( i < size( values ) )
	    {
		integer old_val = select( values, i, 0 );
		
		if ( i+1 == button_id )
		    new_values = add( new_values, old_val + (sign*inc) );
		else
		    new_values = add( new_values, old_val + (-sign *(inc/( size(values)-1))) );

		i = i+1;
	    }

	    values = new_values;
	    ChangeWidget(`id(`bar), `Values, values );
	}
	
    } while ( button_id != `close && button_id != `cancel );
    
    CloseDialog();
})
		

Back to the widget index


YaST2 Developers Documentation: YCP UI Reference: Widgets

Generated Mon Oct 14 13:59:15 2002