Sunday, May 26, 2013

Android Custom Bar Chart | Dynamic Chart with different color in Android | Bar Chart Sample Program in Android

Hello Friends,
I am sharing very important code for draw bar chart in Android in different color and size.
I have one more article on simple bar chart but this one is more good.

1)Print Screen


2)MainActivity

package com.manish.barchartdemo;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
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);
  int colerloop[] = { 1, 2, 2, 2, 3, 3, 3, 3, 1, 1 };
  int heightLoop[] = { 300, 200, 200, 200, 100, 100, 100, 100, 300, 300 };
  for (int j = 0; j < colerloop.length; j++) {
   drawChart(1, colerloop[j], heightLoop[j]);
  }
 }

 public void drawChart(int count, int color, int height) {
  System.out.println(count + color + height);
  if (color == 3) {
   color = Color.RED;
  } else if (color == 1) {
   color = Color.BLUE;
  } else if (color == 2) {
   color = Color.GREEN;
  }

  for (int k = 1; k <= count; k++) {
   View view = new View(this);
   view.setBackgroundColor(color);
   view.setLayoutParams(new LinearLayout.LayoutParams(25, height));
   LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view
     .getLayoutParams();
   params.setMargins(3, 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:gravity="bottom"
        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(simple bar chart)-
http://www.androidhub4you.com/2013/05/bar-chart-example-in-android-simple-bar.html

6 comments :

  1. Hi manish sir can you send zip file to me. actually i am working on a project.....

    how to create the dynamic bar-chart (that means providing rating it will automatically change the bar-chart) this is my problem plz help me sir..............
    This is my Email Id:Basha9.pattan@gmail.com

    ReplyDelete
  2. Hi please copy paste from above, very simple code. And yes it will work for you just change data according you.

    ReplyDelete
  3. Replies
    1. create a dynamic textview on that bar view. using relative layout.

      Delete
    2. what do you mean? using relative layout?

      Thank you for the reply.

      Delete
  4. Hi. Thanks for the tutorial.. it helped me a lot.

    But I have one question.. How can I use this code considering a dynamic data but making it work like percentage data. i.e. 20% investment of a company during a specific month or year.. You know what I mean?

    Thanks in advance man and keep up the good work. :)

    ReplyDelete