Android:GraphView如何在X轴上实现时间?
我发现在Android中如何在图形的X轴上实现时间有点困难 这是我的代码:Android:GraphView如何在X轴上实现时间?,android,android-graphview,Android,Android Graphview,我发现在Android中如何在图形的X轴上实现时间有点困难 这是我的代码: for (int i = 0; i < listSize; i++) { String[] onlyReading = mData.get(i).getReading().split(" "); readingList[i] = Double.parseDouble(onlyReading[0]);
for (int i = 0; i < listSize; i++)
{
String[] onlyReading = mData.get(i).getReading().split(" ");
readingList[i] = Double.parseDouble(onlyReading[0]);
String date = mData.get(i).getUnFormatedDate();
String[] temp = date.split(" ");
String[] dateTemp = null;
String[] timeTemp = null;
if(temp.length > 0)
{
dateTemp = temp[0].trim().split("-");
timeTemp = temp[1].trim().split(":");
Date dateObj = new Date();
String year = "0";
if(dateTemp != null)
{
dateObj.setDate(Integer.parseInt(dateTemp[0]));
dateObj.setMonth(Integer.parseInt(dateTemp[1]) - 1);
year = dateTemp[2].trim();
if(dateTemp[2].trim().length() == 4)
{
year = dateTemp[2].substring(2, 4);
}
dateObj.setYear(Integer.parseInt(year)+100);
}
if(timeTemp != null)
{
calendar = new GregorianCalendar(Integer.parseInt(year) + 2000, Integer.parseInt(dateTemp[1]) - 1, Integer.parseInt(dateTemp[0]), Integer.parseInt(timeTemp[0]), Integer.parseInt(timeTemp[1]));
}
dateObj = new Date(calendar.getTimeInMillis());
dateList[i] = dateObj;
}
}
if(dateList.length > 0)
{
//dates.clear();
//values.clear();
//readingData.clear();
//readingDate.clear();
GraphViewData[] data = new GraphViewData[dateList.length];
LineGraphView graphView = new LineGraphView(
getActivity() // context
, "" // heading
);
for (int i = 0; i < listSize; i++)
{
data[i] = new GraphViewData(Double.valueOf(i), readingList[i]);
}
GraphViewSeries exampleSeries = new GraphViewSeries(data);
graphView.addSeries(exampleSeries);
graphView.setDrawBackground(false);
((LineGraphView) graphView).setDrawDataPoints(true);
graphView.getGraphViewStyle().setGridColor(0);
graphView.getGraphViewStyle().setHorizontalLabelsColor(Color.WHITE);
graphView.getGraphViewStyle().setVerticalLabelsColor(Color.WHITE);
graphView.getGraphViewStyle().setNumHorizontalLabels(5);
graphView.getGraphViewStyle().setNumVerticalLabels(5);
graphView.setManualYAxisBounds(400, 0);
mGraphParent.addView(graphView);
}
}
for(int i=0;i0)
{
dateTemp=temp[0].trim().split(“-”);
timeTemp=temp[1].trim().split(“:”);
Date dateObj=新日期();
字符串year=“0”;
if(dateTemp!=null)
{
dateObj.setDate(Integer.parseInt(dateTemp[0]);
dateObj.setMonth(Integer.parseInt(dateTemp[1])-1);
年份=日期温度[2]。修剪();
if(dateTemp[2].trim().length()==4)
{
年份=日期温度[2]。子字符串(2,4);
}
dateObj.setYear(整数.parseInt(年份)+100);
}
if(timeTemp!=null)
{
日历=新的GregorianCalendar(Integer.parseInt(year)+2000,Integer.parseInt(dateTemp[1])-1,Integer.parseInt(dateTemp[0]),Integer.parseInt(timeTemp[0]),Integer.parseInt(timeTemp[1]);
}
dateObj=新日期(calendar.getTimeInMillis());
dateList[i]=dateObj;
}
}
如果(dateList.length>0)
{
//日期。清除();
//value.clear();
//readingData.clear();
//readingDate.clear();
GraphViewData[]数据=新的GraphViewData[dateList.length];
LineGraphView graphView=新建LineGraphView(
getActivity()//上下文
,“”//标题
);
for(int i=0;i
即使我添加了Y轴值,我也不知道如何将时间值添加到X轴?诀窍是将时间转换为秒或毫秒(unix时间),然后将其作为X轴值 有一个关于GraphView演示项目的例子。看看:
我也有同样的问题。最简单的方法是将时间转换为毫秒。使用公历可能更有帮助,因为Date中的方法已被弃用
Calendar cal=new GregorianCalendar(2014,6,12,9,30,0);
data[i]=new GraphViewData(cal.getTimeInMillis,VALUE);
然后设置CustomLabelFormatter
graphView.setCustomLabelFormatter(new CustomLabelFormatter() {
@Override
public String formatLabel(double value, boolean isValueX) {
// TODO Auto-generated method stub
if (isValueX) {
Date d = new Date((long) (value));
return (dateFormat.format(d));
}
return "" + (int) value;
}
});
graphView.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter() {
@Override
public String formatLabel(double value, boolean isValueX) {
if (isValueX) {
Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter.format(value);
}
return super.formatLabel(value, isValueX);
}
});
时间/日期以dateFormat中指定的格式显示。我知道这是一个老问题,但我使用DefaultLabelFormatter()解决了这个问题 根据以下视频:
link得到了404-应该是