How to plot a bar chart in android?

落爺英雄遲暮 提交于 2019-12-05 22:31:03

You could use the following: achartengine, AndroidPlot or charts4j. If you want to do it programmatically, then here is a program:

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

}

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!