在android中显示栏之间的空格

时间:2012-08-28 06:30:36

标签: android bar-chart

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

enter image description here 在此先感谢!..

在图像上方会让你清楚我的怀疑。。,

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

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

请在下面找到我的来源

BarChart.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;
}
}

1 个答案:

答案 0 :(得分:2)

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

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