How to create bar-chart with different color for each bar?

∥☆過路亽.° 提交于 2020-01-16 20:27:11

问题


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 CategorySeries series = new CategorySeries("Space");
private XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
private XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
ArrayList<String> names = new ArrayList<String>();
ArrayList<String> sizes = new ArrayList<String>();

Written following code to display different colors.

     public void getBarChart(){
        for(int i = 0;i < name.length; i++){
        series.add(name[i],values[i]);
        multiRenderer.addXTextLabel(i, name[i]);
    }     
    dataset.addSeries(series.toXYSeries()); 
    XYSeriesRenderer renderer = new XYSeriesRenderer();
    renderer.setColor(Color.rgb(0, 255, 255));
    renderer.setFillPoints(true);
    renderer.setLineWidth(2);
    renderer.setDisplayChartValues(true);

    multiRenderer.setXLabels(0);
    multiRenderer.setChartTitle("Chart Used Space");
    multiRenderer.setXTitle("Name of Directory");
    multiRenderer.setAxisTitleTextSize((float)18);
    multiRenderer.setChartTitleTextSize((float)20);
    multiRenderer.setYTitle("Amount of Space in MB");
    multiRenderer.setXLabelsAngle((float)35);
    multiRenderer.setLabelsTextSize((float)15);
    multiRenderer.setXLabelsAlign(Align.LEFT);

    multiRenderer.setZoomButtonsVisible(true);
    multiRenderer.setBarSpacing((float) 1.0);
    multiRenderer.addSeriesRenderer(renderer);
        chartView = ChartFactory.getBarChartView(getBaseContext(), dataset, multiRenderer, org.achartengine.chart.BarChart.Type.DEFAULT);
    }

回答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.




回答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;

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);
  }
 }
}

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.

http://4.bp.blogspot.com/-V7SwZZIRPo8/UaH3pR99oSI/AAAAAAAAAhY/N8etB_mAzec/s1600/device-2013-05-26-171036.png



来源:https://stackoverflow.com/questions/16715028/how-to-create-bar-chart-with-different-color-for-each-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!