如何在 MPAndroidChart 中添加 x 轴作为日期时间标签? [英] How to add x axis as datetime label in MPAndroidChart?

查看:125
本文介绍了如何在 MPAndroidChart 中添加 x 轴作为日期时间标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中为温度报告实现了折线图(MPAndroidChart 库).在 X 轴上应绘制日期时间,并应绘制 Y 轴温度.

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.

我刚刚在 X 轴标签中将日期时间添加为字符串,但它已折叠.所以请任何人指导我.

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

推荐答案

请尝试以下操作.

设置X轴

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

创建一个新类 MyXAxisValueFormatter 实现 XAxisValueFormatter

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

希望能帮到你

这篇关于如何在 MPAndroidChart 中添加 x 轴作为日期时间标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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