2

我有一个显示 3 个条形的条形图。以不同宽度显示的三个条形图。如何设置所有条的宽度相等?

这是我的条形图的快照

在此处输入图像描述

请任何人建议!

任何帮助将不胜感激!!

4

2 回答 2

3

看起来您有 2 个系列的数据。一个系列有 2 件,绿色的只有一件。当您在系列中只有一项时,使用以下方法设置条形宽度可能是有意义的:

renderer.setBarWidth(width);

另一种解决方案是在您的系列中添加至少两个项目。

于 2013-03-27T08:01:00.150 回答
-1
@SuppressLint("ParserError")
public class MainActivity extends Activity {
    private GraphicalView BarChartView;




    @SuppressLint("ParserError")
    @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (BarChartView==null )
        {

        XYMultipleSeriesRenderer renderer = buildBarRenderer();
        setChartSettings(renderer);
        BarChartView  = ChartFactory.getBarChartView(this, getBarDataset(), renderer, Type.DEFAULT);
        LinearLayout playout = (LinearLayout) findViewById(R.id.graph2);
        playout.addView(BarChartView);
        ;

        }
        else
        {

            BarChartView.repaint();
            BarChartView.setVisibility(View.GONE);


        }
    }

    private void setChartSettings(XYMultipleSeriesRenderer renderer)
    {
      renderer.setYTitle("Hits");
      renderer.setXAxisMin(0);
      renderer.setXAxisMax(7);
      renderer.setYAxisMin(0);
      renderer.setYAxisMax(900);

      }
    private XYMultipleSeriesDataset getBarDataset()
    {
            int[] y = {624,430,712};
            XYSeries series1 = new XYSeries("A");
            XYSeries series2 = new XYSeries("B");
            XYSeries series3 = new XYSeries("C");



            XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();


                    series1.add(1,y[0]);
                    dataset.addSeries(series1);

                    series2.add(2,y[1]);
                    dataset.addSeries(series2);

                    series3.add(3,y[2]);
                    dataset.addSeries(series3);



              return dataset;

    }
    protected XYMultipleSeriesRenderer buildBarRenderer() {
        XYMultipleSeriesRenderer mrenderer = new XYMultipleSeriesRenderer();

        double[] range = {0,5,0,5};

        mrenderer.setAxisTitleTextSize(16);
        mrenderer.setChartTitleTextSize(20);
         mrenderer.setLabelsTextSize(15);
         mrenderer.setLegendTextSize(15);
         mrenderer.setBarSpacing(0);
         mrenderer.setXLabels(1);
         mrenderer.addXTextLabel(2.5, "");
         mrenderer.setMargins(new int[] {20, 30, 15, 0});
         mrenderer.setAxesColor(Color.WHITE);
         mrenderer.setChartTitle("");
         mrenderer.setXTitle("");
         mrenderer.setInitialRange(range, 1);


         XYSeriesRenderer renderer1 = new XYSeriesRenderer();
         renderer1.setColor(Color.GREEN);
         XYSeriesRenderer renderer2 = new XYSeriesRenderer();
         renderer2.setColor(Color.GREEN);
         XYSeriesRenderer renderer3 = new XYSeriesRenderer();
         renderer3.setColor(Color.BLUE);


         mrenderer.addSeriesRenderer(renderer1);
         mrenderer.addSeriesRenderer(renderer2);
         mrenderer.addSeriesRenderer(renderer3);



          return mrenderer;

    }




}

这是xml文件

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainActivity"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout android:id="@+id/graph2"
       android:layout_width="160dip"
        android:layout_height="200dip"
        android:scaleType="fitEnd"
        android:layout_marginTop="40dip"
        android:layout_marginLeft="790dip"/>

  </RelativeLayout>

试试看 !!享受

于 2013-03-27T06:34:01.370 回答