2

如何在android中提及条形图的条之间的空间?

在此处输入图像描述 提前致谢!..

上面的图片会让你清楚我的怀疑。即。

蓝色条实际上不适合对应的 x 轴值,这里是 (Ord_1,Ord_2,Ord_3,Ord_4)。

我需要显示这些带有 x 轴值的条形。如何做到这一点?

请在下面找到我的来源

条形图.java

public class BarChart 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[] { "Profits per order", " " };

    List<double[]> values = new ArrayList<double[]>();

    values.add(new double[] { 6, 9, 21, 3 });
    values.add(new double[] { 0, 0, 0, 0});


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

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

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

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

    renderer.addXTextLabel(2.75, "Ord_1");
    renderer.addXTextLabel(6.75, "Ord_2");
    renderer.addXTextLabel(10.75, "Ord_3");
    renderer.addXTextLabel(14.75,"Ord_4");

    renderer.setBarSpacing(-1.75);

    int length = renderer.getSeriesRendererCount();

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

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

1 回答 1

2

您正在调用setChartSettings一些参数,这些参数将 X 轴视觉范围设置在 0 到 30 之间,并且 Y 轴的视觉范围相同。您可以调整这些参数以获得更好的放松显示。

renderer.setBarSpacing()用负值跟注。只需使用正数,从0.5f(这意味着间距等于钢筋厚度的一半)开始,然后根据您的需要进行调整。

于 2012-08-28T10:16:12.967 回答