如何在android中更改条形图的背景颜色?

时间:2012-08-24 10:31:18

标签: android background charts

任何人都可以告诉我如何更改在Android中使用achartengine创建的条形图的背景颜色吗?

请参考下图。

enter image description here

请访问我的来源以供参考

BarActivity.java

 public class BarActivity extends Activity 
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    Intent intent = createIntent();
    startActivity(intent);
}


  public Intent createIntent() 

  {

String[] titles = new String[] { "Order's profit of the year", " " };
List<double[]> values = new ArrayList<double[]>();
values.add(new double[] { 29, 83, 47, 33 });
values.add(new double[] {});

int[] colors = new int[] { Color.BLUE, Color.BLACK};

XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
renderer.setOrientation(Orientation.HORIZONTAL);

setChartSettings(renderer, "Profit for the year 2012", " ", " ", 0,100, 0,100, Color.GRAY, Color.LTGRAY);

renderer.setXLabels(1);
renderer.setYLabels(10);

renderer.addXTextLabel(10.75, "ord1");
renderer.addXTextLabel(20.75, "ord2");
renderer.addXTextLabel(30.75, "ord3");
renderer.addXTextLabel(40.75, "ord4");

int length = renderer.getSeriesRendererCount();
for (int i = 0; i < length; i++) 
{
  SimpleSeriesRenderer seriesRenderer = renderer.getSeriesRendererAt(i);
  seriesRenderer.setDisplayChartValues(true);
  }

return ChartFactory.getBarChartIntent(this, buildBarDataset(titles, values), renderer,Type.DEFAULT);
}

    protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) 
    {

// creates a SeriesRenderer and initializes it with useful default values as well as colors

    XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();

    renderer.setAxisTitleTextSize(16);

    renderer.setChartTitleTextSize(20);

    renderer.setLabelsTextSize(10);

    renderer.setLegendTextSize(10);

    int length = colors.length;

    for (int i = 0; i < length; i++) 
    {
      SimpleSeriesRenderer r = new SimpleSeriesRenderer();
      r.setColor(colors[i]);
      renderer.addSeriesRenderer(r);
    }
    return renderer;
  }

    protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor,int labelsColor) 
{

  // sets lots of default values for this renderer

   renderer.setChartTitle(title);

   renderer.setXTitle(xTitle);
   renderer.setYTitle(yTitle);

   renderer.setXAxisMin(xMin);
   renderer.setXAxisMax(xMax);

   renderer.setYAxisMin(yMin);
   renderer.setYAxisMax(yMax);

   renderer.setAxesColor(axesColor);
   renderer.setLabelsColor(labelsColor);
}

protected XYMultipleSeriesDataset buildBarDataset(String[] titles, List<double[]> values) 
{

    // adds the axis titles and values into the dataset

    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    int length = titles.length;

    for (int i = 0; i < length; i++) 
    {
      CategorySeries series = new CategorySeries(titles[i]);
      double[] v = values.get(i);
      int seriesLength = v.length;
      for (int k = 0; k < seriesLength; k++) 
      {
        series.add(v[k]);
      }

      dataset.addSeries(series.toXYSeries());
     }
    return dataset;
    }
}

感谢您宝贵的时间!..

1 个答案:

答案 0 :(得分:4)

尝试

renderer.setApplyBackgroundColor(true);
renderer.setBackgroundColor(Color.WHITE);