在android中用sqlite表数据显示条形图?

时间:2012-11-20 09:42:55

标签: android database sqlite graph

我是Android的新手,在我的SQLite表中有图表,我希望数据显示X和Y轴上的数据。我的数据库值或是休闲:

productId      Qty

2100021        20
2100022        50

我想在X轴和Y轴上绘制Android中这些值的图形。

我写下面的代码

公共类SalesTrackingByCustomer扩展了Activity {

private DBAdapter db;
private Spinner spinner1, spinner2;
 private String[] prodctnames;
 private String[] prodctnames1;
 private double[] product_1;
 private double[] product_2;
 public int count;
 public String custid;
 public Sample customerdetails;
 public ChartCollection<String> collection;
 ChartArea area;
 ChartArea area1;

 private SimpleAdapter mSchedule;
 Vector<String> vec_custid=new Vector<String>();
 Vector<String> vec_oldno=new Vector<String>();
 Vector<String> vec_productid=new Vector<String>();
 Vector<String> vec_qty=new Vector<String>();
 private ListView mListView;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bycustomers);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
mListView=(ListView)findViewById(R.id.listView1);
prodctnames=new String[10];
product_1=new double[10];

prodctnames1=new String[10];
product_2=new double[10];

db=new DBAdapter(this);

final ChartView chartView = (ChartView) findViewById(R.id.chartView);

ChartPalette palette = new ChartPalette(0xffffd7e8);
chartView.setPalette(palette);
final ChartSeries product1 = new ChartSeries("P1", ChartTypes.Column);


spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub

        int position=spinner1.getSelectedItemPosition();
        System.out.println("=====ITEM POSITION======"+position);

        final String selected=arg0.getItemAtPosition(arg2).toString();
        System.out.println("=====IN SELECTED======"+selected);

        db.open();
        Cursor customer=db.fetchorderCustomername(selected);
        custid=customer.getString(0);
        System.out.println("=====CUSTOMER NAME ID======"+custid);
        Cursor cursor=db.fetchorderDetails(custid);
        count=cursor.getCount();
        System.out.println("=====COUNT======"+count);

               int i=0;
                while (i<count)
                {
                    String productid=cursor.getString(1);
                    prodctnames[i]=productid;

                    String qty=cursor.getString(2);
                    product_1[i]=Double.parseDouble(qty);
                    System.out.println("=====PROID======"+prodctnames[i]+"=====QUANTITY======"+product_1[i]);
                    cursor.moveToNext();
                    i++;

                }


                for (int j = 0; j < count; j++)
                {
                    ChartPoint point = product1.getPoints().addXY(j, product_1[j]);
                    point.setAxisLabel(prodctnames[j]);
                }

                if(position==0)
                {
                   chartView.refreshDrawableState();

                   chartView.getSeries().add(product1);


                   area = chartView.getAreas().get(0);

                area.getDefaultXAxis().setLabelsMode(ChartAxis.LabelsMode.SeriesLabels);

                }
                if(position==1)
                {

                   chartView.refreshDrawableState();


                   chartView.getSeries().add(product1);
                    area1 = chartView.getAreas().get(0);

                    area1.getDefaultXAxis().setLabelsMode(ChartAxis.LabelsMode.SeriesLabels);
                }

              //area.refresh();
                db.close();

                        }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
});

提前致谢。

1 个答案:

答案 0 :(得分:0)

您尝试添加具有相同名称的新区域。更改区域的名称。

参考:http://www.artfulbits.com/support/forum.aspx?g=posts&t=389