List of all Iphone and Blackberry Development codes in a one click Iphone,objective C,xcode,blackberry Development,iOS Development
Showing posts with label Bar Graph. Show all posts
Showing posts with label Bar Graph. Show all posts

Wednesday, May 2, 2012

BarGraph in Blackberry



















If you want to display a bar graph, then lets try this code sample. This will help you in drawing a bar graph in blackberry.

//BarScreen  - MainScreen class ---

import net.rim.device.api.ui.component.NullField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class BarScreen extends MainScreen
{
 
    private BarGraphField _bar;
    private boolean _barChanged = false;
    private MainScreen _barScreen;
 
    public BarScreen(long style)
    {
        super(style);
     
        _barScreen = this;
     
        //Graph data.
        VerticalFieldManager graphLayout = new VerticalFieldManager(VerticalFieldManager.FIELD_VCENTER);
        final int[] values = {60,50,40,30,20,10};
        final int[] colors = {0x00FF0000,0x007F525D , 0x00E77471, 0x00C8B560, 0x00ADDFFF,0};      
        _bar = new BarGraphField(values, colors, 100, 5, 30, BarGraphField.PADDING_MEDIUM, BarGraphField.PADDING_HIGH, true);
        //Add a focusable NullField for scrolling purposes.
        this.add(new NullField(NullField.FOCUSABLE));
        _bar.setPadding(20, 0, 0, 20);
        graphLayout.add(_bar);
        this.add(graphLayout);
        this.add(new NullField(NullField.FOCUSABLE));
     
    }
}


// BarGraphField  class--------

import net.rim.device.api.ui.Graphics;
import net.rim.device.api.util.Arrays;
import net.rim.device.api.ui.Font;

public class BarGraphField extends GraphField
{
    
    public static final int PADDING_NONE = 0;
    
    public static final int PADDING_LOW = 4;
  
    public static final int PADDING_MEDIUM = 8;
    
  
    public static final int PADDING_HIGH = 12;
    
    private int _scale, _increment, _barWidth, _barPadding, _scalePadding;
    private boolean _showScale;
    
  
    public BarGraphField(int[] values, int[] colors, int scale, int increment, int barWidth)
    {
        this(values, colors, scale, increment, barWidth, PADDING_MEDIUM, PADDING_MEDIUM, false);
    }
    
   
    public BarGraphField(int[] values, int[] colors, int scale, int increment, int barWidth, boolean showScale)
    {
        this(values, colors, scale, increment, barWidth, PADDING_MEDIUM, PADDING_MEDIUM, showScale);
    }
    
    
    public BarGraphField(int[] values, int[] colors, int scale, int increment, int barWidth, int barPadding, 
        int scalePadding, boolean showScale)
    {
        
        //Ensure the scale is greater than 0.
        if (scale <= 0)
            throw new IllegalArgumentException("Scale must be greater than 0.");
            
        //Ensure the increment is greater than 0.
        if (increment <= 0)
            throw new IllegalArgumentException("Increment must be greater than 0.");
            
        //Ensure the barWidth is greater than 0.
        if (barWidth <= 0)
            throw new IllegalArgumentException("Bar width must be greater than 0.");
            
        //Ensure barPadding is a valid value.
        switch (barPadding)
        {
            case PADDING_NONE:
            case PADDING_LOW:
            case PADDING_MEDIUM:
            case PADDING_HIGH:
            break;
            default:
                throw new IllegalArgumentException("Invalid bar padding specified");
        }
        
        //Ensure scalePadding is a valid value.
        switch (scalePadding)
        {
            case PADDING_NONE:
            case PADDING_LOW:
            case PADDING_MEDIUM:
            case PADDING_HIGH:
            break;
            default:
                throw new IllegalArgumentException("Invalid scale padding specified");
        }
        
        //Ensure the number of bar values match the number of colors.
        if (values.length != colors.length)
            throw new IllegalArgumentException("Non matching array lengths for values and colors.");
            
        //Ensure not values are greater than the scale value.
        for(int count = values.length - 1; count >= 0; --count)
        {
            if (values[count] > scale)
                throw new IllegalArgumentException("Value greater than scale.");
        }
        
        _values = values;
        _colors = colors;
        _scale = scale;
        _increment = increment;
        _barWidth = barWidth;
        _barPadding = barPadding;
        _scalePadding = scalePadding;
        _showScale = showScale;
        _width = calcWidth();
        _height = calcHeight();
    }
    
   

    private int calcWidth()
    {
        int theWidth;
        
        if (_showScale)
            theWidth = Font.getDefault().getAdvance(Integer.toString(_scale)) + 8 
                + (_values.length * _barWidth) + ((_values.length - 1) * _barPadding);
        else
            theWidth = 10 + (_values.length * _barWidth) + ((_values.length - 1) * _barPadding); 
       
        return theWidth;
    }
    private int calcHeight()
    {
        if (_showScale)
            return 2 + (Font.getDefault().getHeight() * (_scale / _increment));
        else if (_scalePadding == PADDING_NONE)
            return 2 + (PADDING_LOW * (_scale / _increment));
        else
            return 2 + (_scalePadding * (_scale / _increment));
    }
    
   protected void paint(Graphics graphics)
    {
        int count, tempX, tempY, leftOver, graphHeight = 0;
        
        int tickHeight;             //The height of the scale ticks.
        int tickIndent;             //The distance to indent the scale ticks.
        
        if (_showScale)
        {
            tickHeight = Font.getDefault().getHeight();
            //Subtract 4 from the width of the scale (2 pixels for the scale width and 2 pixels for spacing).
            tickIndent = Font.getDefault().getAdvance(Integer.toString(_scale)) - 4;
        }
        else
        {
            if (_scalePadding == PADDING_NONE)
                tickHeight = PADDING_LOW;
            else
                tickHeight = _scalePadding;
            
            tickIndent = 0;
        }
        
        //Wipe out any existing graph image data.
        graphics.clear();
        
        graphics.setColor(0);
        
        //In case the increment doesn't divide into the scale evenly.
        leftOver = _scale % _increment;
        tempY = 0;
        
        //Draw the scale.
        for (count = _scale; count > leftOver; count-=_increment)
        {
            graphics.fillRect(tickIndent, tempY, 6, 2);
            
            if (_showScale)
                graphics.drawText(Integer.toString(count), 0, tempY);
            
            tempY += tickHeight;
        }
        
        tempX = tickIndent + 6;
        
        //Draw the x axis.
        graphics.fillRect(tempX, tempY, _width - tempX, 2);
        
        //Draw the y axis.
        graphics.fillRect(tempX, 0, 2, _height);
        
        tempX = _width - _barWidth;
        
        //Draw the bars.
        for (count = _values.length - 1; count >= 0; --count)
        {
            graphics.setColor(_colors[count]);
            
            //Calculate the top bar as a percentage of the available graphing space
            //relating to the value.  Graphing space is _height - 2 (2 pixel y axis).
            tempY = _height - (((_height - 2) * _values[count]) / _scale);
            graphHeight = _height - tempY - 2;
            
            graphics.fillRect(tempX, tempY, _barWidth, graphHeight);
            
            //Calculate the next graph starting point.
            tempX -= (_barWidth + _barPadding);
        }
    }
    
   
    public int getBarPadding()
    {
        return _barPadding;
    }
    
    
    public int getBarWidth()
    {
        return _barWidth;
    }
    

   
    public int getIncrement()
    {
        return _increment;
    }
    
   
    public int getScale()
    {
        return _scale;
    }
    
  
    public int getScalePadding()
    {
        return _scalePadding;
    }
   
    public boolean isScaleVisible()
    {
        return _showScale;
    }
    
    
    public void setBarPadding(int barPadding)
    {
        //Don't do anything unless the bar padding has changed.
        if (_barPadding != barPadding)
        {
            //Ensure barPadding is a valid value.
            switch (barPadding)
            {
                case PADDING_NONE:
                case PADDING_LOW:
                case PADDING_MEDIUM:
                case PADDING_HIGH:
                break;
                default:
                    throw new IllegalArgumentException("Invalid bar padding specified");
            }
            
            _barPadding = barPadding;
            _width = calcWidth();
        }
    }
    
    
    public void setBarWidth(int barWidth)
    {
        //Dont do anything unless the bar width has changed.
        if (_barWidth != barWidth)
        {
            //Ensure the barWidth is greater than 0.
            if (barWidth <= 0)
                throw new IllegalArgumentException("Bar width must be greater than 0.");
                
            _barWidth = barWidth;
            _width = calcWidth();
        }
    }
    
  
    public void setGraphData(int[] values, int[] colors)
    {
        //Don't do anything unless the data has changed.
        if (!Arrays.equals(_values, values) || !Arrays.equals(_colors, colors))
        {
            //Ensure the number of values match the number of colors.
            if (values.length != colors.length)
                throw new IllegalArgumentException("Non matching array lengths for bars and colors.");
                
            //Ensure not values are greater than the scale value.
            for(int count = values.length - 1; count >= 0; --count)
            {
                if (values[count] > _scale)
                    throw new IllegalArgumentException("Value greater than scale.");
            }                
                            
            _values = values;
            _colors = colors;

            _width = calcWidth();
            _height = calcHeight();
        }
    }
  
   
    public void setIncrement(int increment)
    {
        //Don't do anything unless the increment has changed.
        if (_increment != increment)
        {
            //Ensure the increment is greater than 0.
            if (increment <= 0)
                throw new IllegalArgumentException("Increment must be greater than 0.");
                
            //Only the height changes with an increment change.
            _increment = increment;
            _height = calcHeight();
        }
    }
    
   
    public void setScale(int scale)
    {
        ///Don't do anything unless the scale has changed.
        if (_scale != scale)
        {
            //Ensure the scale is greater than 0.
            if (scale <= 0)
                throw new IllegalArgumentException("Scale must be greater than 0.");
                
            //Ensure no values are greater than the scale value.
            for(int count = _values.length - 1; count >= 0; --count)
            {
                if (_values[count] > scale)
                    throw new IllegalArgumentException("Value greater than scale.");
            }
            
            _scale = scale;
            _width = calcWidth();
            _height = calcHeight();
        }
    }
    
   
    public void setScalePadding(int scalePadding)
    {
        //Don't do anything unless the scale padding has changed.
        if (_scalePadding != scalePadding)
        {
            //Ensure scalePadding is a valid value.
            switch (scalePadding)
            {
                case PADDING_NONE:
                case PADDING_LOW:
                case PADDING_MEDIUM:
                case PADDING_HIGH:
                break;
                default:
                    throw new IllegalArgumentException("Invalid scale padding specified");
            }
            
            _scalePadding = scalePadding;
            _height = calcHeight();
        }
    }
    
   
    public void setScaleVisible(boolean showScale)
    {
        //Don't do anything unless the flag has changed.
        if (_showScale != showScale)
        {
            _showScale = showScale;
            _width = calcWidth();
            _height = calcHeight();
        }
    }
}


//GraphField  class ------

import net.rim.device.api.ui.Field;
public abstract class GraphField extends Field
{
    protected int[] _values;
    
    
    protected int[] _colors;
   
    protected int _width;
    
    
    protected int _height;
    
   
    GraphField() 
    {    
        super();
    }
    
   
    public int getColor(int index)
    {
        return _colors[index];
    }
   
    public int[] getColors()
    {
        return _colors;
    }
   
    public int getValue(int index)
    {
        return _values[index];
    }
    
   
    public int[] getValues()
    {
        return _values;
    }
   
    public void setGraphData(int[] values, int[] colors)
    {
        _values = values;
        _colors = colors;
    }
    
    public int getPreferredHeight()
    {
    return _height;
    }
    
    public int getPreferredWidth()
    {
    return _width;
    }
  
    protected void layout(int width, int height)
    {
    int w, h;
   
    if (width >= _width)
    {
    w = _width;
    }
    else
    {
    w = width;
   
    }
   
    if (height >= _height)
    {
    h = _height;
    }
    else
    {
    h = height;
    }
   
    setExtent(w, h);
    }