How to set String value of date time on xAxis BarChart MPAndroidChart

前端 未结 1 898
失恋的感觉
失恋的感觉 2021-01-27 14:53

I\'m trying to show date time on X-Axis for my bar chart. I got this data from my UNIX timestamp firebase database. I have been trying to convert this UNIX timestamp to a human-

相关标签:
1条回答
  • 2021-01-27 15:00

    I added an index for x values as I said 0,1,3,4,5.. Then I created a list for the dates.

    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
         int count_test = 1;
         int index = 0;
         ArrayList<BarEntry> dataValue = new ArrayList<BarEntry>();
         ArrayList<Long> dateList = new ArrayList< Long >();
         if(dataSnapshot.hasChildren()){
               for (DataSnapshot myDataSnapshot : dataSnapshot.getChildren()){
                   datapoint DataPoint = myDataSnapshot.getValue(datapoint.class);
                   originalTimestamp = DataPoint.getDate();
                   dataValue.add(new BarEntry(index, DataPoint.getValue()));
                   referenceTimestamp = 1587772800;
                   convertedTimestamp = originalTimestamp - referenceTimestamp;
                   dateList.add(convertedTimestamp);
                   index++;
               }
             showChart(dataValue, dateList);
    
          } else {
              chart.clear();
              chart.invalidate();
          }
      }
    

    I changed the formatter to use the dateList:

    public class FooFormatter extends ValueFormatter {
    
    private ArrayList<Long> dateList; // minimum timestamp
    private DateFormat mDataFormat;
    private Date mDate;
    
    public FooFormatter(ArrayList<Long>  dateList) {
        dateList = dateList;
        this.mDataFormat = new SimpleDateFormat( "HH:mm", Locale.ENGLISH );
        this.mDate = new Date();
    }
    
    @Override
    public String getFormattedValue(float value) {
        if((int) value < 0 || (int)(value) > longArrayList.size() -1)
            return "";
    
        // Retrieve original timestamp
        long originalTimestamp = dateList(((int)value));
    
        // Convert timestamp
        return getDateString(originalTimestamp);
    }
    
    private String getDateString(long timestamp) {
        try {
            mDate.setTime(timestamp * 1000);
            return mDataFormat.format(mDate);
        } catch(Exception ex) {
            return "xx";
        }
    }
    
    }
    

    axisValueFormatter

    FooFormatter axisValueFormatter = new FooFormatter(dateList);
    
    0 讨论(0)
提交回复
热议问题