我在我的項目中實現了用於溫度報告的折線圖(MPAndroidChart庫)。應繪製X軸日期時間並繪製Y軸溫度。如何在MPAndroidChart中將x軸添加爲日期時間標籤?
我只是在X軸標籤中添加了datetime作爲字符串,但它已摺疊。所以請任何人指導我。
我在我的項目中實現了用於溫度報告的折線圖(MPAndroidChart庫)。應繪製X軸日期時間並繪製Y軸溫度。如何在MPAndroidChart中將x軸添加爲日期時間標籤?
我只是在X軸標籤中添加了datetime作爲字符串,但它已摺疊。所以請任何人指導我。
請嘗試以下操作。
要設置X軸
XAxis xAxis = mChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setValueFormatter(new MyXAxisValueFormatter());
xAxis.setLabelsToSkip(0);
創建一個新類MyXAxisValueFormatter
實現XAxisValueFormatter
public class MyXAxisValueFormatter implements XAxisValueFormatter {
@Override
public String getXValue(String dateInMillisecons, int index, ViewPortHandler viewPortHandler) {
try{
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM");
return sdf.format(new Date(Long.parseLong(dateInMillisecons)));
}
catch (Exception e)
{
return dateInMillisecons;
}
}
希望這有助於
嗨,對於我在下面添加的xaxis,你能檢查一下它是正確還是錯誤? ArrayList
顯示是的日期。但是這個日期太長了。 –
是如何避免崩潰? –
我有實現同樣要可你,請 嘗試[這。](http://stackoverflow.com/a/40806503/1343788) –
[MPAndroidChart x軸日期/時間標籤格式化]的可能重複(http://stackoverflow.com/questions/40803233/mpandroidchart-x-axis-date-time-label-formatting) –