首页 文章

MPAndroidChart x轴日期/时间标签格式

提问于
浏览
9

Background

对于我的应用程序中的一些图表,我正在使用MPAndroidChart库 . 我的图表的所有水平轴都是基于时间的,它们可以跨越一整年,一个月,一周,一天或跨越一小时 . 它总是显示一个完整的周期,所以1月到12月,周一到周日,0:00-24:00等 . 轴的值始终是纪元时间戳(以秒为单位) .

Requirement

我希望x轴标签遵循以下规则:

year span的情况下

  • at 1st of month ;
    monthweek span的情况下
  • at start of day ;
    day span的情况下
  • 在任何 full hour (##:00)(不是全部);
    hour span上的任何 5 minute
  • .

Problem

我可以设置x轴的 granularity ,这样可以确保两个点之间的空间不小于颗粒度,但这可能意味着(在白天 Span 的情况下)第一个标签位于凌晨1:00,并且第二个是凌晨2:01,第三个是凌晨3:16,因为它符合(最小)60分钟的粒度 .

当前不正确的情况,理想情况下会像 [0:00, 3:00, 6:00, 9:00 ..]

Example of current situation

Question

有没有办法控制x轴标签的定位,以达到上述结果?

2 回答

  • 4

    我做了同样的事,试试这个,

    XAxis xAxis = mChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM_INSIDE);
        xAxis.setDrawGridLines(false);
        xAxis.setGranularity(1f); // only intervals of 1 day
        xAxis.setTypeface(mTfLight);
        xAxis.setTextSize(8);
        xAxis.setTextColor(ContextCompat.getColor(this, R.color.colorYellow));
        xAxis.setValueFormatter(new GraphXAxisValueFormatter(range, interval, slot));
    

    在你的情况下 range . 如果你想要一个月那么就有12个,如果是第7周等 .

    interval 你通过1 .

    slot 你必须通过,识别你的数据,如月,年,日,我使用enum为此 .

    public class GraphXAxisValueFormatter implements IAxisValueFormatter {
    
    private static int MINUTES_INTERVAL = 5;
    private String[] mValues;
    private int mInterval;
    private SensorInterval.Interval mSlot;
    
    public GraphXAxisValueFormatter(List<BinSensorData> range, int interval, SensorInterval.Interval slot) {
        mValues = new String[range.size()];
        mInterval = interval;
        mSlot = slot;
    
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < range.size(); i++) {
            calendar.setTimeInMillis(range.get(i).getTime());
    
            int unroundedMinutes = calendar.get(Calendar.MINUTE);
            int mod = unroundedMinutes % MINUTES_INTERVAL;
            calendar.add(Calendar.MINUTE, mod < 8 ? -mod : (MINUTES_INTERVAL - mod));
    
    
            String s = "";
    
            if (slot.equals(SensorInterval.Interval.HOUR) || slot.equals(SensorInterval.Interval.DAY))
                s = Util.getTimeFromTimestamp(calendar.getTimeInMillis());
            else if (slot.equals(SensorInterval.Interval.WEEK))
                s = Util.getDayFromTimestamp(calendar.getTimeInMillis());
            else if (slot.equals(SensorInterval.Interval.MONTH))
                s = Util.getMonthFromTimestamp(calendar.getTimeInMillis());
            else if (slot.equals(SensorInterval.Interval.YEAR))
                s = Util.getYearFromTimestamp(calendar.getTimeInMillis());
    
    
            Util.setLog("Time : "+s);
            mValues[i] = s;
        }
    }
    
    @Override
    public String getFormattedValue(float value, AxisBase axis) {
        Util.setLog("Value : "+ value);
        if (value % mInterval == 0 && value >= 0) {
            return mValues[(int) value % mValues.length];
        } else
            return "";
    
    }
    
    @Override
    public int getDecimalDigits() {
        return 0;
    }
    

    见:http://prntscr.com/dbn62x

  • 1

    一周中我有类似的问题......

    我的时间戳格式与您的格式相同,但是当它们被转换为浮动条目时,它们会失去太多精确度并给我不规则的间隔 .

    我用毫秒将时间戳转换为时间戳,只需几分钟,现在它完美无缺

    检查浮动转换是否没有弄乱你的精度,从那里你可以设置其余的参数,使你的间隔规则

相关问题