gpt4 book ai didi

java - 在图 TableView 中将日期显示为 X 轴不起作用(每次显示相同)

转载 作者:行者123 更新时间:2023-12-01 17:54:03 25 4
gpt4 key购买 nike

我正在尝试在 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数据库中的数据如下:

enter image description here

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

enter image description here

我做错了什么?我该如何解决这个问题?

最佳答案

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

graphView.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter()

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

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

关于java - 在图 TableView 中将日期显示为 X 轴不起作用(每次显示相同),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60749200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com