Wednesday, December 28, 2011

Graph APIs for Android Platform: Part 1



For your help I have created few Graph APIs for android platform. Today I am sharing the Bar Graph APIs. This java class is a view which you can add to your Activity. The Bar graph this API create are of three type as shown in the figures(Norma, Stacked, Grouped). The details are given Below
Class BarGraphView
Super Class View
Constructors public BarGraphView(Context context)
public BarGraphView(Context context, int[] _data, int _graphWidth, int _graphHeight, int _graphOriginX, int _graphOriginY, String _yAxisLabel, String _barLabels[])
public BarGraphView(Context context, int[] _data, int _graphWidth, int _graphHeight, int _graphOriginX, int _graphOriginY, String _yAxisLabel, String _barLabels[], int _style, int _grouping)






package com.nhpc.nhpc;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Align;
import android.view.View;

class BarGraphView extends View {
    private int data[]={15,65,29,68,12,25};
    private int color[]={ 0xff3298CE, 0xffCB3398, 0xffFE9900, 0xff3266C8, 0xffCBCC65, 0xff029834, 0xffFFCC00, 0xffD70000, 0xff00CF60 };
    private int graphWidth=200; // Default Width of graph
    private int graphHeight=200; // Default graph height
    private int graphOriginX=60; // Default Origin coordinates X and Y
    private int graphOriginY=50;
    private String yAxisLabel = "Y Axis Label"; // Default label of Y axis
    private String barLabels[]= {"Bar1", "Bar2", "Bar3", "Bar4", "Bar5", "Bar6", "Bar7", "Bar8", "Bar9", "Bar10", "Bar11", "Bar12"};
    private int style = 0;
    private int grouping = 2;

        public BarGraphView(Context context) {
            super(context);
        }

        public BarGraphView(Context context, int[] _data, int _graphWidth, int _graphHeight, int _graphOriginX, int _graphOriginY, String _yAxisLabel, String _barLabels[]) {
            super(context);
            data = _data;
            graphWidth = _graphWidth;
            graphHeight = _graphHeight;
            graphOriginX = _graphOriginX;
            graphOriginY = _graphOriginY;
            yAxisLabel = _yAxisLabel;
        }
     
        public BarGraphView(Context context, int[] _data, int _graphWidth, int _graphHeight, int _graphOriginX, int _graphOriginY, String _yAxisLabel, String _barLabels[], int _style, int _grouping) {
            super(context);
            data = _data;
            graphWidth = _graphWidth;
            graphHeight = _graphHeight;
            graphOriginX = _graphOriginX;
            graphOriginY = _graphOriginY;
            yAxisLabel = _yAxisLabel;
            style = _style;
            grouping = _grouping;
        }
     
        @Override
        public void onDraw(Canvas canvas) {
        Paint paint = new Paint();
       
        if(graphHeight<200)
        paint.setTextSize(10);
        else
        paint.setTextSize(20);
        //int _width = getWidth()-(getWidth()-(graphOriginX+graphWidth)); // set the right hand extent of graph
        int _width = (graphOriginX+graphWidth); // set the right hand extent of graph
        if(getWidth() - _width <10)
        _width = getWidth()-20;
        int _height = (int)(getHeight()-graphOriginY-graphHeight);// set the top extent of graph
        if(_height<20)
        _height=20;
        int originX = graphOriginX; // set the left hand extent
        int originY = getHeight()-graphOriginY; // set the bottom extent
       
        paint.setColor(Color.WHITE);
        float padding;
        if(style == 0 || style == 11)
        padding = (float) (((float)(_width-originX)/(float)data.length)*0.20);
        else
        padding = (float) (((float)(_width-originX)/(float)((float)(data.length)/(float)grouping))*0.20);
       
        //Drawing Y Axis
        Rect yAxis = new Rect(originX,(int)(_height),originX+2,originY);
        canvas.drawRect(yAxis, paint);
       
        //Draw grid parallel to X-Axis
        int maxVal = ((int)(getMaximum(data)/10)+1)*10;
        paint.setColor(Color.WHITE);
       
        float ratio = (float) ((float)(originY-_height)/(float)maxVal); // i value is equal to this much height
        float scale = (float)maxVal*ratio/10;
       
        for(int i=1;i<=10;i++){
        paint.setColor(Color.LTGRAY);
        if(maxVal<10)
        canvas.drawText((maxVal*i/10)+"", originX-15, originY-(scale*i)+5, paint);
        else if(maxVal<100)
        canvas.drawText((maxVal*i/10)+"", originX-25, originY-(scale*i)+5, paint);
        else
        canvas.drawText((maxVal*i/10)+"", originX-35, originY-(scale*i)+5, paint);
        Rect grid = new Rect(originX,(int)(originY-1-(scale*i)),_width,(int)(originY-(scale*i)));
        if(i<10)
            canvas.drawRect(grid, paint);
        }
       
       
       
        paint.setTextAlign(Align.CENTER);
        int tempX = originX + (int)padding;
        int heightBar;
        int j=0;
        //Draw Bars
        if(style == 0)
        {
        for(int i=0;i<data.length;i++)
        {
        if(padding*4 <10)
        paint.setTextSize(8);
        else
        paint.setTextSize(15);
        paint.setColor(color[i%color.length]);
        heightBar=(int) (((originY-(_height))-(data[i]*ratio))+(_height));
        canvas.drawText(data[i]+"", tempX+padding*2, heightBar-5, paint);
        canvas.drawText(barLabels[i], tempX+padding*2, originY+20, paint);
        Rect bar = new Rect( tempX ,heightBar,(int)(tempX+padding*4),originY);
            canvas.drawRect(bar, paint);
            tempX = (int)(tempX+padding*5);
        }
        }
        else if(style == 11){
        for(int i=0;i<data.length;i++)
        {
        if(i>0)
            if(i%grouping == 0)
            tempX = (int)(tempX+padding*(grouping+4));
            else
            tempX = (int)(tempX+padding*4);
        paint.setColor(color[(i%grouping)%color.length]);
        heightBar=(int) (((originY-(_height))-(data[i]*ratio))+(_height));
        canvas.drawText(data[i]+"", tempX+padding*2, heightBar-5, paint);
        Rect bar = new Rect( tempX ,heightBar,(int)(tempX+padding*4),originY);
            canvas.drawRect(bar, paint);
            paint.setColor(Color.WHITE);
        if(i%grouping == 0)
        canvas.drawText(barLabels[j++], (float) (tempX+2.5*grouping*padding), originY+20, paint);
        }
        }
        else
        {
        int temp=0;
        for(int i=0;i<data.length;i++)
        {
            if(i>0 && i%grouping == 0)
            {
            tempX = (int)(tempX + padding*5);
            temp = 0;
            }
        paint.setColor(color[(i%grouping)%color.length]);
        if(i%grouping == 0)
        heightBar=(int) ((originY)-(data[i]*ratio));
        else
        heightBar=(int) ((temp)-(data[i]*ratio));
       
        Rect bar = new Rect( tempX ,heightBar,(int)(tempX+padding*4),(temp==0)?originY:temp);
        temp=heightBar;
        canvas.drawRect(bar, paint);
        paint.setColor(Color.BLACK);
        canvas.drawText(data[i]+"", tempX+padding*2, heightBar+20, paint);
        paint.setColor(Color.WHITE);
        if(i%grouping == 0)
        canvas.drawText(barLabels[j++], (float) (tempX+2*padding), originY+20, paint);
        }
        }
        //Drawing X Axis
        paint.setColor(Color.WHITE);
        Rect xAxis = new Rect(originX,originY-2,_width,originY);
        canvas.drawRect(xAxis, paint);
       
        //Drawing Y-axis label
        Rect rect = new Rect();
        paint.getTextBounds(yAxisLabel, 0, yAxisLabel.length(), rect);
        paint.setColor(Color.WHITE);
       
        paint.setStyle(Paint.Style.FILL);
        if(maxVal<10)
        {
        canvas.rotate(-90, graphOriginX-25, getHeight()-graphOriginY-graphHeight/2); // rotating the text
        canvas.drawText(yAxisLabel, graphOriginX-20, getHeight()-graphOriginY-graphHeight/2, paint);
        }
    else if(maxVal<100)
    {
    canvas.rotate(-90, graphOriginX-35, getHeight()-graphOriginY-graphHeight/2); // rotating the text
    canvas.drawText(yAxisLabel, graphOriginX-30, getHeight()-graphOriginY-graphHeight/2, paint);
    }
    else
    {
    canvas.rotate(-90, graphOriginX-45, getHeight()-graphOriginY-graphHeight/2); // rotating the text
    canvas.drawText(yAxisLabel, graphOriginX-40, getHeight()-graphOriginY-graphHeight/2, paint);
    }
        }
     
        public int getMaximum(int arr[])
        {
        int max = 0;
        if(style == 0 || style == 11)
        {
        max = arr[0];
        for(int i=1;i<arr.length;i++)
        {
        if(max<arr[i])
        max = arr[i];
        }
        }
        else
        {
        for(int i=0;i<grouping;i++)
        max+=arr[i];
        int temp=0;
        for(int i=grouping;i<arr.length;i++)
        {
        temp=temp+arr[i];
        if((i+1)%grouping ==0)
        {
        if(max<temp)
        {
        max=temp;
        }
        temp=0;
        }
       
        }
        }
       
        return max;
        }
    }


4
Feb

No comments:

Post a Comment