在MPAndroidChart中的x轴上格式化日期标签 [英] Format date labels on x axis in MPAndroidChart

查看:120
本文介绍了在MPAndroidChart中的x轴上格式化日期标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景和问题

我正在尝试使用MPAndroidChart库在折线图中提供一些温度值和日期,这些温度值和日期记录在CSV文件中.但是,我不完全了解如何在X轴上格式化日期值.我尝试使用IAxisValueFormatter,但现在它仅以奇怪的格式(2609.03.14:09:12:25)呈现CSV文件中的两个值.CSV文件中的值如下所示:"19.7 C,2017-12-09 12:59:43" ,依此类推.由于Entry想要(float,float)创建一个入口点,也许这会影响精度吗?有什么建议可以解决这个问题吗?

I´m trying to present a number of temperature values and dates logged in a CSV file in a line graph with the MPAndroidChart library. However I don´t fully understand how to format my date values on the X axis. I tried to use the IAxisValueFormatter but right now it only presents two values from my CSV file with a weird formatting (2609.03.14:09:12:25). The values in the CSV file looks like this: "19.7 C,2017-12-09 12:59:43" and so on. Since the Entry wants (float, float) to create an entry point maybe this is messing with the precision? Any suggestions how to solve this problem?

要求

如果可能的话,我希望所有条目都以折线图的形式显示在X轴上,并带有相应的日期,就像在CSV文件中记录的那样.

I want all my entries to be presented in the line graph with the corresponding dates on the X axis pretty much as it is logged in the CSV file if this is possible.

我现在的onCreate方法:

public class MainActivity extends AppCompatActivity {

LineChart mLineChart;
InputStream inputStream;
String [] sensorData;
ArrayList<Entry> entries = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mLineChart = findViewById(R.id.linegraph);

    importData();

    LineDataSet dataSet = new LineDataSet(entries, "Temperatures");
    LineData lineData = new LineData(dataSet);
    mLineChart.setData(lineData);
    dataSet.setColor(Color.RED);
    dataSet.setDrawCircles(false);
    dataSet.setDrawValues(false);
    dataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
    mLineChart.getDescription().setText("");
    mLineChart.getLegend().setEnabled(false);
    mLineChart.invalidate();


    YAxis leftAxis = mLineChart.getAxisLeft();
    leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
    leftAxis.setAxisMinimum(0f);
    leftAxis.setAxisMaximum(30f);


    YAxis rightAxis = mLineChart.getAxisRight();
    rightAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
    rightAxis.setAxisMinimum(0f);
    rightAxis.setAxisMaximum(30f);

    XAxis xAxis = mLineChart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setDrawGridLines(false);
    xAxis.setAxisMinimum(0f);
    xAxis.setLabelCount(5);
    xAxis.setAxisMaximum(400f);
    xAxis.setGranularity(1f);
    xAxis.setGranularityEnabled(true);
    xAxis.setValueFormatter(new DateAxisValueFormatter(null));
}

我用于导入和创建条目的代码:

   public void importData() {

inputStream = getResources().openRawResource(R.raw.sensor_vardagsrum);
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    try {
        String csvLine;

            while ((csvLine = reader.readLine()) != null) {

            sensorData = csvLine.split(",");

            sensorData[0] = sensorData[0].substring(0, sensorData[0].length() - 1).replaceAll("\\s+", "");
            sensorData[1] = sensorData[1].replaceAll("\\D", "");

                    float temp = Float.parseFloat(sensorData[0]);
                    float date = Float.parseFloat(sensorData[1]);

                    entries.add(new Entry(date, temp));

                }

                System.out.println(entries);
        } catch (IOException ex) {
         throw new RuntimeException("Error in reading CSV file" + ex);
        }
    }
}

我的IAxisValueFormatter:

class DateAxisValueFormatter implements IAxisValueFormatter {
private String[] mValues;

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

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

    @Override
public String getFormattedValue(float value, AxisBase axis) {
    return sdf.format(new Date((long) value));
    }
}

这是当前输出的样子:

推荐答案

由于日期是浮点型,因此需要将其恢复为日期",因此此处不传递null,而是创建新的 IAxisValueFormatter 对象并实现方法 getFormattedValue ,这是我的解决方案:

Since you date is float value you need to make it back to Date so here don't pass null, but create new IAxisValueFormatter object and implement method getFormattedValue and here is my solution:

Date d = new Date(Float.valueOf(value).longValue());
String date = new SimpleDateFormat("dd-MM", Locale.YOUR_REGION).format(d);

希望它对您有用!

这篇关于在MPAndroidChart中的x轴上格式化日期标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆