2014-01-19 54 views
1

我覺得有點難以弄清楚如何在Android的圖表的X軸中實現時間?Android:GraphView我如何在X軸上實現時間?

這是我的代碼:

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); 
      } 
     } 

即使我添加Y軸值,我無法弄清楚如何時間值添加到X軸?

回答

1

訣竅是將時間轉換爲秒或毫秒(unix時間),並將其作爲X值。

GraphView-Demos項目中有一個例子。看一看:

https://github.com/jjoe64/GraphView-Demos/blob/master/src/com/jjoe64/graphviewdemos/CustomLabelFormatterActivity.java#L68

+1

鏈接越來越404 - 它應該可能是https://github.com/jjoe64/GraphView-Demos/blob/a38c1ffa47ef34bc34fed1e80d7caac8e18f87e4/app/src/main/java/com/jjoe64/graphview_demos/fragments/DateAsXAxis.java –

2

我有同樣的問題。最簡單的方法是將時間轉換爲毫秒。由於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; 
     } 
    }); 

時間/日期以dateFormat指定的格式顯示。

相關問題