如何以编程方式在Android中绘制条形图?

时间:2011-10-14 13:33:05

标签: android

可以根据x轴和y轴上的数据在android中绘制条形图

4 个答案:

答案 0 :(得分:6)

charts4j是另一个好的图表API。它使用Google图表服务器生成图表并将其作为图像返回给您。

答案 1 :(得分:4)

我认为这个图书馆可以为您提供帮助:achartengine

答案 2 :(得分:3)

我过去曾成功使用AndroidPlot来制作折线图,但他们也支持条形图。你可以试一试。

答案 3 :(得分:2)

    public class BarchartsampleActivity extends Activity {
    /** Called when the activity is first created. */

    static XYMultipleSeriesDataset dataset;
    static XYMultipleSeriesRenderer renderer;
    static XYSeriesRenderer rendererSeries;
    static CategorySeries series;
    static GraphicalView mChartView;
    String xvalstring1,xvalstring2,xvalstring3,xvalstring4,xvalstring5;
    int xvalint1,xvalint2,xvalint3,xvalint4,xvalint5;


     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LinearLayout layout = (LinearLayout) findViewById(R.id.linearlayout);
        Intent intent=getIntent();
        xvalstring1=intent.getStringExtra("xvalstring1").toString().trim();
        xvalstring2=intent.getStringExtra("xvalstring2").toString().trim();
        xvalstring3=intent.getStringExtra("xvalstring3").toString().trim();
        xvalstring4=intent.getStringExtra("xvalstring4").toString().trim();
        xvalstring5=intent.getStringExtra("xvalstring5").toString().trim();     
        xvalint1=Integer.parseInt(xvalstring1);
        xvalint2=Integer.parseInt(xvalstring2);
        xvalint3=Integer.parseInt(xvalstring3);
        xvalint4=Integer.parseInt(xvalstring4);
        xvalint5=Integer.parseInt(xvalstring5);

     String[] titles = new String[] { "USER INPUTS" };
        List<double[]> x = new ArrayList<double[]>();
        x.add(new double[] { 1, 2, 3, 4, 5});
        List<double[]> values = new ArrayList<double[]>();
        values
                .add(new double[] { xvalint1,xvalint2,xvalint3,xvalint4,xvalint5 });
        dataset = new XYMultipleSeriesDataset();
        renderer = new XYMultipleSeriesRenderer();
        renderer.setChartTitle("USER INPUTS");
        renderer.setXTitle("X");
        renderer.setYTitle("Y");
        renderer.setXLabelsAlign(Align.RIGHT);
        renderer.setYLabelsAlign(Align.RIGHT);
        renderer.setXAxisMin(0);
        renderer.setXAxisMax(10);
        renderer.setYAxisMin(0);
        renderer.setYAxisMax(40);
        renderer.setClickEnabled(false);
        renderer.setExternalZoomEnabled(false);
        renderer.setZoomButtonsVisible(true);
        renderer.setPanLimits(new double[] { 0,5,0,100 });
        renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });
        renderer.setShowGrid(true);
        /*renderer.setPanEnabled(false, false);
        renderer.setZoomEnabled(false, false);*/
        renderer.setBarSpacing(1.5);
        renderer.setLabelsColor(Color.BLUE);
        renderer.setAxisTitleTextSize(16);
        renderer.setChartTitleTextSize(20);
        renderer.setLabelsTextSize(15);
        renderer.setLegendTextSize(15);
        renderer.setMargins(new int[] {20, 30, 15, 20});
        renderer.setBackgroundColor(Color.GREEN);
        renderer.setMarginsColor(Color.GREEN);
        rendererSeries = new XYSeriesRenderer();
        rendererSeries.setColor(Color.GREEN);
        rendererSeries.setDisplayChartValues(true);
        renderer.addSeriesRenderer(rendererSeries);

      if (mChartView == null) {
            Log.d("Oncreate ", "if (mChartView == null)");
            mChartView = ChartFactory.getBarChartView(this, mDataset(titles,
                    x, values), renderer, Type.DEFAULT);
            layout.addView(mChartView, new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        } else {
//          mChartView.repaint();
            Log.d("Oncreate ", "if (mChartView != null)");
        }
    }    
    private XYMultipleSeriesDataset mDataset(String[] titles,
            List<double[]> xValues, List<double[]> yValues) {
        // TODO Auto-generated method stub
        XYMultipleSeriesDataset dataset1 = new XYMultipleSeriesDataset();
        addXYSeries(dataset1, titles, xValues, yValues, 0);
        return dataset1;
    }
    private void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles,
            List<double[]> xValues, List<double[]> yValues, int scale) {
        // TODO Auto-generated method stub

        int length = titles.length;
        for (int i = 0; i < length; i++) {
            XYSeries series = new XYSeries(titles[i], scale);
            double[] xV = xValues.get(i);
            double[] yV = yValues.get(i);
            int seriesLength = xV.length;
            for (int k = 0; k < seriesLength; k++) {
                series.add(xV[k], yV[k]);



            }
            dataset.addSeries(series);
        }

    }
}