9

I implemented line chart (MPAndroidChart library) for temperature report in my project.In X axis datetime should be plotted and Y axis temperature should be plotted.

I just added datetime as string in X axis label but it's collapsed. So please anyone guide me.

David Rawson
  • 20,912
  • 7
  • 88
  • 124
Shanmugapriyan M
  • 189
  • 1
  • 2
  • 12
  • I have implement same which you want can you, please Try [This.](http://stackoverflow.com/a/40806503/1343788) – Mehul Kabaria Jan 10 '17 at 04:49
  • Possible duplicate of [MPAndroidChart x-axis date/time label formatting](http://stackoverflow.com/questions/40803233/mpandroidchart-x-axis-date-time-label-formatting) – David Rawson Feb 10 '17 at 09:29

4 Answers4

6

Using version 3.0+ of the MPAndroidChart:

Set formatter to the x axis (created below):

// Formatter to adjust epoch time to readable date
lineChart.xAxis.setValueFormatter(new LineChartXAxisValueFormatter());

Create a new class LineChartXAxisValueFormatter:

public class LineChartXAxisValueFormatter extends IndexAxisValueFormatter {

    @Override
    public String getFormattedValue(float value) {

        // Convert float value to date string
        // Convert from seconds back to milliseconds to format time  to show to the user
        long emissionsMilliSince1970Time = ((long) value) * 1000;

        // Show time in local version
        Date timeMilliseconds = new Date(emissionsMilliSince1970Time);
        DateFormat dateTimeFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault());

        return dateTimeFormat.format(timeMilliseconds);
    }
}

When the entries are added to the chartDataArray they are added in seconds, not milliseconds, to avoid potential precision issues with inputting as a float (i.e. milliseconds divided by 1000).

chartDataArray.add(new Entry(secondsSince1970Float, yValueFloat));

Happy coding!

Ben
  • 3,346
  • 6
  • 32
  • 51
5

Try the following.

To set the X Axis

 XAxis xAxis = mChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setValueFormatter(new MyXAxisValueFormatter());
        xAxis.setLabelsToSkip(0);

Create a new class MyXAxisValueFormatter implement 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;
    }
}

Hope this helps

Ramesh R
  • 7,009
  • 4
  • 25
  • 38
Sagar Patil
  • 1,400
  • 15
  • 29
  • Hi for xaxis i added below can you please check whether it's correct or wrong ? ArrayList xAxisValues = new ArrayList<>(); for (int i = 0; i < chartList.size(); i++) { xAxisValues.add(Utils.parseDateForChart(chartList.get(i).getDateTime())); } – Shanmugapriyan M Jan 02 '17 at 12:15
  • Yes date is displayed. But the date is too long. – Sagar Patil Jan 02 '17 at 12:42
  • Yes How to avoid collapse ? – Shanmugapriyan M Jan 02 '17 at 12:43
  • Add this to your chart mChart.setDragEnabled(true); mChart.setPinchZoom(true); mChart.setDoubleTapToZoomEnabled(true); mChart.setHorizontalScrollBarEnabled(true); mChart.getViewPortHandler().setMaximumScaleX(5f); mChart.getViewPortHandler().setMaximumScaleY(5f); – Sagar Patil Jan 02 '17 at 12:49
  • I had implement same thing which you want. can you please, try This. http://stackoverflow.com/a/40806503/1343788 – Mehul Kabaria Jan 10 '17 at 04:52
  • @ShanmugapriyanM: have you check, its working or not ? – Mehul Kabaria Jan 11 '17 at 11:16
  • @MehulKabaria I checked your response but I can't make it work because I can't figure out `BinSensorData` and `SensorInterval.Interval`, if you could provide the code or an explanation, it would be appreciated. – MeknessiHamida Apr 04 '17 at 12:03
2

Further from @Ben's answer, if you are creating BarChart, and the time span of the bar is like an hour or a day, and you are supplying with millisecond or second data, you will end up getting the bars too thin to be visible. This is a bug posted in 2017 (https://github.com/PhilJay/MPAndroidChart/issues/2892) and remains unresolved to date unfortunately.

A workaround was proposed and it is to convert the millisecond values into your time span of the bar before setting then into BarEntry. My time span is a day, so I have the formatter as:

    static class BarChartXAxisValueFormatter extends IndexAxisValueFormatter {

    @Override
    public String getFormattedValue(float value) {

        // Convert float value to date string
        // Convert from days back to milliseconds to format time  to show to the user
        long emissionsMilliSince1970Time = TimeUnit.DAYS.toMillis((long)value);
        // Show time in local version
        Date timeMilliseconds = new Date(emissionsMilliSince1970Time);
        SimpleDateFormat dateTimeFormat = new SimpleDateFormat("MM-dd");

        return dateTimeFormat.format(timeMilliseconds);
    }
}

And I set the X axis with:

xAxis.setValueFormatter(new BarChartXAxisValueFormatter());

Then when setting the data to the bar, I have

new BarEntry(TimeUnit.MILLISECONDS.toDays((long)valX), valY).

LXJ
  • 1,180
  • 6
  • 18
0

If it still actually...

class DateAxisValueFormatter implements IAxisValueFormatter {
  private String[] mValues;

  SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.hh");

  public DateAxisValueFormatter(String[] values) {
      this.mValues = values; }


   @Override
   public String getFormattedValue(float value, AxisBase axis) {
        // "value" represents the position of the label on the axis (x or y)
        return mValues[(int) value];
      }
    }
  • And your must on Create put String[] values (public DateAxisValueFormatter(String[] values) ) where each value is a DateString. Data series Entries for X (new Entry(forX,forY)) must be a flat array = 0,1,2,3,4

  • Sorry, my poor English, I am from Russia. From 1988 main chief of RealTime Control System Developer Company for Hydro Power Plants (www.asu-epro.ru). Borodatov Michael miclosoft@mail.ru

Agilanbu
  • 2,747
  • 2
  • 28
  • 33