我以这个https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/LineChartTime.java为例 我适应了我的需要。问题在于,要在X轴上绘制数据,它需要一个浮点数,然后从long转换为float会丢失数据。该示例中的技巧是将毫秒时间转换为HOURLY,这样就不会丢失数据,但是我需要转换为DAILY,我尝试过但无法获得结果
我尝试将TimeUnit.HOURS.toMillis((long) value);
更改为TimeUnit.DAYS.toMillis((long) value);
,并添加了timeInHours = TimeUnit.DAYS.toMillis(mDate.getTime());
public class ChartDailyActivity extends AppCompatActivity {
private LineChart chart;
DBOpenHelper mDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chart_daily);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setChart();
}
public void setChart(){
chart = findViewById(R.id.chart1);
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.TOP_INSIDE);
xAxis.setTextSize(10f);
xAxis.setTextColor(Color.WHITE);
xAxis.setDrawAxisLine(false);
xAxis.setDrawGridLines(true);
xAxis.setTextColor(Color.rgb(255, 192, 56));
xAxis.setCenterAxisLabels(true);
//xAxis.setSpaceMin(1000f*60f*60f*24f);
xAxis.setValueFormatter(new IAxisValueFormatter() {
private final SimpleDateFormat mFormat = new SimpleDateFormat("dd/MM/yy", Locale.ITALIAN);
@Override
public String getFormattedValue(float value, AxisBase axis) {
long millis = TimeUnit.DAYS.toMillis((long) value);
return mFormat.format(new Date(millis));
}
});
mDB = new DBOpenHelper(this);
YAxis leftAxis = chart.getAxisLeft();
leftAxis.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
leftAxis.setTextColor(ColorTemplate.getHoloBlue());
leftAxis.setDrawGridLines(true);
leftAxis.setGranularityEnabled(true);
leftAxis.setAxisMinimum(0f);
leftAxis.setAxisMaximum((float) mDB.queryMaxTotaleBevuto());
leftAxis.setYOffset(-9f);
leftAxis.setTextColor(Color.rgb(255, 192, 56));
YAxis rightAxis = chart.getAxisRight();
rightAxis.setEnabled(false);
setData();
}
private void setData() {
ArrayList<Entry> values = new ArrayList<>();
ArrayList xList = new ArrayList<Integer>();
ArrayList yList = new ArrayList<String>();
DBOpenHelper mDB = new DBOpenHelper(this);
xList = mDB.queryTotaleBevutoData();
yList = mDB.queryTotaleBevuto();
float flX, flY;
int intX, intY;
for(int x=0; x<xList.size(); x++){
String givenDateString = (String)xList.get(x);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
long timeInHours = 0;
try {
Date mDate = sdf.parse(givenDateString);
//Log.d("XLOGGIN:", "Date = " + givenDateString );
timeInHours = TimeUnit.DAYS.toMillis(mDate.getTime());
//timeInMilliseconds = mDate.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
intX = (int)timeInHours;
intY = (int)yList.get(x);
flX = (float)intX;
flY = (float)intY;
/*
Log.d("XLOGGIN:Long", Long.toString(timeInMilliseconds));
Log.d("XLOGGIN:intX", Integer.toString(intX));
Log.d("XLOGGIN:flX", Float.toString(flX));
*/
values.add(new Entry(flX, flY));
}
// create a dataset and give it a type
LineDataSet set1 = new LineDataSet(values, "Drinked ml/Day");
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
set1.setColor(ColorTemplate.getHoloBlue());
set1.setValueTextColor(ColorTemplate.getHoloBlue());
set1.setLineWidth(1.5f);
set1.setDrawCircles(false);
set1.setDrawValues(false);
set1.setFillAlpha(65);
set1.setFillColor(ColorTemplate.getHoloBlue());
set1.setHighLightColor(Color.rgb(244, 117, 117));
set1.setDrawCircleHole(false);
// create a data object with the data sets
LineData data = new LineData(set1);
data.setValueTextColor(Color.WHITE);
data.setValueTextSize(9f);
// set data
chart.setData(data);
}
}
图表绘制了错误的日期时间(所有日期均为1970年1月1日),并且该线为水平线
答案 0 :(得分:0)
您没有将时间戳转换为等效日期。这是您应该如何做(在setData()期间):
try {
Date mDate = sdf.parse(givenDateString);
//Log.d("XLOGGIN:", "Date = " + givenDateString );
timeInDays = TimeUnit.MILLISECONDS.toDays(mDate.getTime());
//timeInMilliseconds = mDate.getTime();
} catch (ParseException e) {
e.printStackTrace();
}