Bar Chart in Android With out any Built in jars

Main Acitvity


package com.rakhi577.chart;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity
{
	private LinearLayout lay;
	HorizontalListView listview;
	private double highest;
	private int[] grossheight; 
	private int[] netheight;
		private Double[] grossSal= {15000.0,15000.0,15000.25,15000.1,
								15000.0,15000.0,15000.0,15000.0,
								15000.25,15000.1,15000.0,15000.0};
	
	private Double[] netSal = {12000.0,13000.0,14000.25,10000.1,
								10000.0,9000.0,12000.0,13000.0,
								14000.25,10000.1,10000.0,9000.0};

	private String[] datelabel = {"Jan 12","Feb 12","Mar 12",
								  "Apr 12","May 12","Jun 12",
								  "Jul 12","Aug 12","Sep 12",
								  "Oct 12","Nov 12","Dec 12"};
	

	public void onCreate(Bundle savedInstance)
	{
		super.onCreate(savedInstance);
		setContentView(R.layout.main);
		lay = (LinearLayout)findViewById(R.id.linearlay);
		listview = (HorizontalListView) findViewById(R.id.listview);
        
		List<Double> b = Arrays.asList(grossSal);
        highest = (Collections.max(b));

        netheight = new int[netSal.length];
        grossheight= new int[grossSal.length];
    	//updateSizeInfo();
		
    }
	
	public class bsAdapter extends BaseAdapter
    {
        Activity cntx;
        String[] array;
        public bsAdapter(Activity context,String[] arr)
        {
            // TODO Auto-generated constructor stub
            this.cntx=context;
            this.array = arr;

        }

        public int getCount()
        {
            // TODO Auto-generated method stub
            return array.length;
        }

        public Object getItem(int position)
        {
            // TODO Auto-generated method stub
            return array[position];
        }

        public long getItemId(int position)
        {
            // TODO Auto-generated method stub
            return array.length;
        }

        public View getView(final int position, View convertView, ViewGroup parent)
        {
            View row=null;
            LayoutInflater inflater=cntx.getLayoutInflater();
            row=inflater.inflate(R.layout.simplerow, null);
            
            DecimalFormat df = new DecimalFormat("#.##");
            final TextView title	=	(TextView)row.findViewById(R.id.title);
            TextView tvcol1	=	(TextView)row.findViewById(R.id.colortext01);
            TextView tvcol2	=	(TextView)row.findViewById(R.id.colortext02);
            
            TextView gt		=	(TextView)row.findViewById(R.id.text01);
            TextView nt		=	(TextView)row.findViewById(R.id.text02);
            
            tvcol1.setHeight(grossheight[position]);
            tvcol2.setHeight(netheight[position]);
            title.setText(datelabel[position]);
            
            gt.setText(df.format(grossSal[position]/1000)+" k");
            nt.setText(df.format(netSal[position]/1000)+" k");
            
            tvcol1.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					Toast.makeText(MainActivity.this, "Month/Year: "+title.getText().toString()+"\nGross Sal: "+grossSal[position], Toast.LENGTH_SHORT).show();
				}
			});
            
            tvcol2.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					Toast.makeText(MainActivity.this, "Month/Year: "+title.getText().toString()+"\nNet Sal: "+netSal[position], Toast.LENGTH_SHORT).show();
				}
			});
            
        return row;
        }
    }
	
	@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
        updateSizeInfo();
    }
	private void updateSizeInfo() {
		
		/** This is onWindowFocusChanged method is used to allow the chart to
		 * update the chart according to the orientation.
		 * Here h is the integer value which can be updated with the orientation
		 */
		int h;
		if(getResources().getConfiguration().orientation == 1)
		{
			h = (int) (lay.getHeight()*0.5);
			if(h == 0)
			{
				h = 200;
			}
		}
		else
		{
			h = (int) (lay.getHeight()*0.3);
			if(h == 0)
			{
				h = 130;
			}
		}
		for(int i=0;i<netSal.length;i++) 
    	{
			netheight[i] = (int)((h*netSal[i])/highest);
    		grossheight[i] = (int)((h*grossSal[i])/highest);
    		System.out.println("net width[i] "+netheight[i]+"gross width[i] "+grossheight[i]);
    	}
    	listview.setAdapter(new bsAdapter(this,datelabel));
	}
	
}

Xml Layout for this activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#fff"
  android:id="@+id/linearlay"
  >
  
    <TextView
         android:layout_width="fill_parent"
         android:layout_height="wrap_content" 
         android:padding="10dp"
         android:textSize="16sp"
         android:gravity="center"
         android:layout_gravity="center"
         android:textColor="#000"
    	 android:text="Bar Chart with out any jar"/>
  
    <RelativeLayout 
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:padding="15dp"
         android:background="#ddd"
         android:orientation="horizontal">
    
     		<TextView
     		    android:id="@+id/a1"
	            android:layout_width="10dp"
	            android:layout_height="10dp" 
	            android:layout_marginLeft="10dp" 
	            android:textSize="16sp"
	            android:gravity="center_horizontal"
	            android:layout_centerVertical="true"
	            android:layout_gravity="center"
	            android:background="#53933f"
          		android:text=""/>
	     
	     	<TextView
	     	    android:id="@+id/a2"
	     	    android:layout_toRightOf="@+id/a1"
	     	    android:layout_centerVertical="true"
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content" 
	            android:layout_marginLeft="10dp"
	            android:textSize="16sp"
	            android:textColor="#000"
	            android:gravity="center_horizontal"
	            android:layout_gravity="center"
          		android:text="Gross sal"/>
	     
	     	<TextView
	     	    android:id="@+id/a3"
	     	    android:layout_toRightOf="@+id/a2"
	     	    android:layout_centerVertical="true"
	            android:layout_width="10dp"
	            android:layout_height="10dp" 
	            android:layout_marginLeft="10dp"
	            android:textSize="16sp"
	            android:gravity="center_horizontal"
	            android:layout_gravity="center"
	            android:background="#FF9A39"
          		android:text=""/>
	     
	    	 <TextView
	    	    android:id="@+id/a4"
	     	    android:layout_toRightOf="@+id/a3"
	     	    android:layout_centerVertical="true"
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:layout_marginLeft="10dp" 
	            android:textSize="16sp"
	            android:textColor="#000"
	            android:gravity="center_horizontal"
	            android:layout_gravity="center"
          		android:text="Net sal"/>
     
     </RelativeLayout>
  
  <com.rakhi577.chart.HorizontalListView
  	android:id="@+id/listview"
  	android:layout_width="fill_parent"
  	android:layout_height="fill_parent"
  	android:background="#ddd"
  />
  
</LinearLayout>


Xml Layout for List Item

<?xml version="1.0" encoding="utf-8" ?> 
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:padding="10dp">
	 
	  <RelativeLayout 
        android:id="@+id/lin02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:layout_above="@+id/title"
        android:layout_gravity="center">
        
   		<TextView
   		    android:layout_alignParentLeft="true" 
   		    android:id="@+id/colortext01" 
            android:layout_width="30dp" 
            android:layout_height="wrap_content" 
            android:layout_gravity="bottom"
            android:layout_alignParentBottom="true"
            android:gravity="bottom" 
            android:padding="10dp" 
            android:textSize="16sp"
            android:background="#4CA613"/>   
            
   		<TextView
   		    android:layout_toRightOf="@+id/colortext01" 
   		    android:id="@+id/colortext02" 
            android:layout_width="30dp" 
            android:layout_height="wrap_content"
            android:gravity="bottom" 
            android:layout_gravity="bottom"
            android:layout_alignParentBottom="true"
            android:padding="10dp" 
            android:textSize="16sp"
            android:background="#FF9A39"/>
   		
   		<TextView android:id="@+id/text01" 
            android:layout_width="wrap_content" 
            android:layout_height="15dp"
            android:layout_above="@+id/colortext01"
            android:textColor="#000"
            android:textStyle="bold"
            android:textSize="13sp"/>
   		
   		<TextView android:id="@+id/text02" 
            android:layout_width="wrap_content" 
            android:layout_height="15dp"
            android:layout_above="@+id/colortext02"
            android:layout_toRightOf="@+id/colortext01"
            android:textColor="#000"
            android:textStyle="bold"
            android:textSize="13sp"/>
   		
  	</RelativeLayout>
  	<TextView 
	      android:id="@+id/title"
	      android:layout_width="60dp" 
	      android:layout_height="wrap_content"
	      android:textSize="14sp"
	      android:gravity="center_horizontal"
	      android:layout_alignParentBottom="true"
	      android:textColor="#000"/>
  </RelativeLayout>



Landscape Image

Portrait Image

source code

About these ads

9 thoughts on “Bar Chart in Android With out any Built in jars

  1. Puneet

    How to display the dynamic graph.
    ie suppose we have random sets of values and we need to keep displaying at every say 5 sec intervals….

    Reply
  2. Pingback: How to plot a bar chart in android? : Android Community - For Application Development

  3. Pingback: android4health

  4. Pingback: how can i implement bar chart : Android Community - For Application Development

  5. you liang

    Hi how do i enable the fading edge effect when using ur customlistview?

    i tried set the following attriubutes on the custom listview but it doesn’t work.
    android:background=”@layout/circle_area_white”
    android:requiresFadingEdge=”horizontal|vertical”

    android:fadingEdgeLength=”50dp”

    it normally work with normal listview.

    regards
    yl

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s