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.
Thanks!
You may like(simple bar chart)-
http://www.androidhub4you.com/2013/05/bar-chart-example-in-android-simple-bar.html
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>
You may like(simple bar chart)-
http://www.androidhub4you.com/2013/05/bar-chart-example-in-android-simple-bar.html