Sunday, May 26, 2013

Android Bar Chart Example | Simple Bar Chart Demo in Android | Charts in Android, Java

Hello Friends!
Today i am going to share very simple code for Draw Bar Chart in Android.
*No Library needed.
*No Canvas needed.
Just copy paste below code and enjoy :)

1)Print Screen


2)MainActivity


package com.manish.barchartdemo;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
LinearLayout linearChart;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		linearChart=(LinearLayout)findViewById(R.id.linearChart);
		drawChart(5);
	}

	
	public void drawChart(int count) {
		System.out.println(count);
		
		for (int k = 1; k <= count; k++) {
			View view = new View(this);
			view.setBackgroundColor(Color.parseColor("#ff6233"));
			view.setLayoutParams(new LinearLayout.LayoutParams(30, 400));
			LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view
					.getLayoutParams();
			params.setMargins(5, 0, 0, 0); // substitute parameters for left,top, right, bottom
			view.setLayoutParams(params);
			linearChart.addView(view);
		}
	}
}

3)activity_main


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/linearChart"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/view"
        android:layout_marginTop="50dp" >
    </LinearLayout>

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:layout_alignParentBottom="true"
        android:background="#00FF00" />

</RelativeLayout>

Thanks!

You may like(Custom Bar Chart)-
http://www.androidhub4you.com/2013/05/custom-bar-chart-in-android-dynamic.html


10 comments:

  1. can you tell me how i could plot a graph from a wave file..

    ReplyDelete
  2. hi ,
    manish chart not working , i am suresh from chennai can u give me a detail code...

    ReplyDelete
    Replies
    1. please recheck ones because it is very simple and small demo and should work..And this is full code..

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
  4. can you help , how to create dynamic legend for charts

    ReplyDelete
  5. hello dear how can possible define Linear layout inside relative layout it show error


    -->









    ReplyDelete
  6. Don't waste your time. This code produces a single column in the view. Guess the "should work" statement is correct

    ReplyDelete
  7. Don't waste your time. This code produces a single column in the view. Guess the "should work" statement is correct

    ReplyDelete