如何调整android中条形图中的条形图?

时间:2012-08-24 11:31:29

标签: android bar-chart

我现在正在尝试使用achartengine在android中创建一个条形图,希望我已创建它,但问题是我创建的条形显示距离每个条形图越来越近,我需要显示那些条形图以下订单(ord1,ord2等,...)都在x轴上?

任何人都可以让我知道解决这个问题的方法吗?

下面的图片会说清楚,请找到它

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.WHITE, 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(15);

    renderer.setLegendTextSize(15);

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

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

}

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 :(得分:1)

您正在使用一些参数调用setChartSettings,这些参数将X轴视觉范围设置为0到100之间,Y轴设置相同。您可以调整这些参数,以便更好地放松显示。

您也可以使用renderer.setBarSpacing()来调整条形图中条形之间的间距。