如何为每个栏创建不同颜色的条形图?

时间:2013-05-23 13:19:59

标签: android achartengine

我们正在为文件管理器应用程序创建条形图,我们必须为每个条形图显示不同的颜色。

全球编写以下代码。

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

编写以下代码以显示不同的颜色。

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

2 个答案:

答案 0 :(得分:0)

条形图中的条形颜色由其渲染器给出。每个系列都有一个渲染器。为了为每个条形图分别设置一个颜色,您必须添加几个系列,每个系列只有一个条形。

答案 1 :(得分:0)

嗨,看看这段代码吧。

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

有关详细信息,请访问以下链接。

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

如果您按照上面的链接,您的条形图将如下图所示。

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