Skip to content Skip to sidebar Skip to footer

How To Create Bar-chart With Different Color For Each Bar?

We are creating bar chart for file manager application, we have to display different color for each bar. Written following code globally. GraphicalView chartView = null; private Ca

Solution 1:

The bar color in a bar chart is given by its renderer. Each series has a renderer. In order to have a separate color for each bar you will have to add several series, each one having only one bar.

Solution 2:

Hi Have a look at this code.

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

publicclassMainActivityextendsActivity {
 LinearLayout linearChart;

 @OverrideprotectedvoidonCreate(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 (intj=0; j < colerloop.length; j++) {
   drawChart(1, colerloop[j], heightLoop[j]);
  }
 }

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

  for (intk=1; k <= count; k++) {
   Viewview=newView(this);
   view.setBackgroundColor(color);
   view.setLayoutParams(newLinearLayout.LayoutParams(25, height));
   LinearLayout.LayoutParamsparams= (LinearLayout.LayoutParams) view
     .getLayoutParams();
   params.setMargins(3, 0, 0, 0); // substitute parameters for left,// top, right, bottom
   view.setLayoutParams(params);
   linearChart.addView(view);
  }
 }
}

For more details please visit the below link.

http://www.androidhub4you.com/2013/05/custom-bar-chart-in-android-dynamic.html

If you follow above link your bar chart will be look like below image.

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGJBzFlTMDlAQzLeGuPxrBY8Oc4zXYmOH5Rn3sZbptxGR0MiVXIRcf8ML81J_JbiafDYWBt8g1oT6YsLlDPa3RNBvTjEEBhmbjr5TlbnA2vpcf01aDucykD2NYltx5gnydo9br3UFobj0/s1600/device-2013-05-26-171036.png

Post a Comment for "How To Create Bar-chart With Different Color For Each Bar?"