在Android中绘制图表

3

我想在Android上绘制一个图表,使X轴显示月份,如Jan、Feb、March、Apr、May、Jun、Aug、Sep、Oct、Nov和Dec,Y轴显示每个月的支出金额。

我有一段代码可以在Android上绘制图表:

mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
        Number[] series1Numbers = { 1, 8, 5, 2, 7, 4 };
        Number[] series2Numbers = { 4, 6, 3, 8, 2, 10 };
        XYSeries series1 = new SimpleXYSeries(Arrays.asList(series1Numbers),
        SimpleXYSeries.ArrayFormat.Y_VALS_ONLY,"Series1");
        XYSeries series2 = new SimpleXYSeries(Arrays.asList(series2Numbers),
        SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series2");

        LineAndPointFormatter series1Format = new LineAndPointFormatter(
                Color.rgb(0, 200, 0), // line color
                Color.rgb(0, 100, 0), // point color
                Color.rgb(150, 190, 150)); // fill color (optional)
                mySimpleXYPlot.addSeries(series1, series1Format);
                mySimpleXYPlot.addSeries(
                series2,
                new LineAndPointFormatter(Color.rgb(0, 0, 200),
                Color.rgb(0, 0, 100), Color.rgb(150, 150, 190)));
                mySimpleXYPlot.setTicksPerRangeLabel(3);
                }

但是我希望在X轴上显示月份,在Y轴上显示他们在该月份花费的特定金额。这些值来自数据库,以mon的形式表示他们花钱的月份,Name包含他们在该月份花费的值。

try {
        myDB = this.openOrCreateDatabase("Expense_db", MODE_PRIVATE, null);

        Cursor c = myDB.rawQuery("SELECT project_android.Month AS Month_Real, " +
                  "SUM(Total) AS OrderTotal FROM project_android " +
                  "WHERE Year='"+value1+"' " +
                  "GROUP BY project_android.Month " , null);

                     int Column1 = c.getColumnIndex("Month_Real");
                     int Column2 = c.getColumnIndex("OrderTotal");
                     //int current=0;
                  // Check if our result was valid.
                     c.moveToFirst();
                     if (c != null) {
                      // Loop through all Results
                     do {
                       mon = c.getString(Column1);               
                       Name = c.getDouble(Column2);
                       Log.i(TAG, "Testing_value_graph_month: " + Name + " | " + mon + " " );

                     }while(c.moveToNext());
                     c.close();
                     }
                     }
    catch(Exception e) {
        Log.e("Error", "Error", e);
       } finally {
        if (myDB != null)
         myDB.close();
       }

任何帮助都将不胜感激..!


任何建议将不胜感激,因为我正在尝试解决问题,但不确定该如何继续! - arsenal
1
为什么不使用一些类库,比如这些:https://dev59.com/t2w15IYBdhLWcg3wIoRM#7507700 - Aman Alam
1个回答

1

我使用了这段代码:

点击这里

来满足同样的需求,希望能对你有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接