在图形 View 中将日期显示为X轴不起作用(每次都显示相同)

2019年6月30日 9点热度 0条评论

我正在尝试在graphview中创建一个在x轴日期和y轴上显示一个整数的图形。数据存储在sqlite数据库中。这是我的DatabaseHelper.java文件:

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME="records.db";
    public static final String TABLE_NAME="records_table";
    public static final String COL_1="date";
    public static final String COL_2="percentage";

    public DatabaseHelper(@Nullable Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_NAME + "(date DATE,percentage INTEGER)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    public void insertData(Integer percentage){
        SQLiteDatabase db= this.getWritableDatabase();
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
        Date date = new Date();
        ContentValues initialValues = new ContentValues();
        initialValues.put("date", dateFormat.format(date));
        initialValues.put("percentage",percentage);
        long rowId = db.insert(TABLE_NAME, null, initialValues);

    }
}

这是显示图形的文件
Statistics.java

public class Statistics extends AppCompatActivity {
    SQLiteDatabase db;

    GraphView graphView;
    LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[0]);
    SimpleDateFormat sdf=new SimpleDateFormat("MM-dd");



    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_statistics);
        graphView= (GraphView) findViewById(R.id.graph);
        graphView.addSeries(series);
        graphView.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter(){
            @Override
            public String formatLabel(double value,boolean isValueX){
                if (isValueX){
                    return sdf.format(new Date((long) value));
                } else {
                    return super.formatLabel(value,isValueX);
                }
            }
        });
        series.resetData(getDataPoint());
        exqInsert();
    }

    private void exqInsert() {
        series.resetData(getDataPoint());
        graphView.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter(){
            @Override
            public String formatLabel(double value,boolean isValueX){
                if (isValueX){
                    return sdf.format(new Date((long) value));
                } else {
                    return super.formatLabel(value,isValueX);
                }
            }
        });
    }

    @SuppressLint("WrongConstant")
    private DataPoint[] getDataPoint() {
        String[] columns= {"date","percentage"};
        db = openOrCreateDatabase("records.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
        Cursor cursor=db.query("records_table",columns,null,null,null,null,null);
        DataPoint[] dp=new DataPoint[cursor.getCount()];

        for (int i=0 ; i<cursor.getCount(); i++){
            cursor.moveToNext();
            dp[i]= new DataPoint(cursor.getLong(0),cursor.getInt(1));
        }

        return dp;
    }

}

我的records.db数据库中的数据如下:

但是我得到的图是这样的:

我究竟做错了什么?我怎样才能解决这个问题?

解决方案如下:

您正在使用用于显示货币的标签格式化程序,如下所示:

graphView.getGridLabelRenderer()。setLabelFormatter(新的DefaultLabelFormatter()

我建议尝试将此标签格式化程序用作XAxis日期

graphView
.getGridLabelRenderer()
.setLabelFormatter(new DateAsXAxisLabelFormatter(GraphActivity.this, new SimpleDateFormat(getResources().getString(R.string.graph_date_format))));