Android: GraphView How do I implement time in the X axis?

谁说我不能喝 提交于 2019-12-06 14:03:07

the trick is to just convert the time to seconds or milliseconds (unix time) and you it as X-value.

There's an example on the GraphView-Demos project. Take a look at:

https://github.com/jjoe64/GraphView-Demos/blob/master/src/com/jjoe64/graphviewdemos/CustomLabelFormatterActivity.java#L68

I had the same issue. The easiest method was to convert Time into milliseconds. Using a Gregorian Calendar might be more helpful as the methods in Date are deprecated.

Calendar cal=new GregorianCalendar(2014,6,12,9,30,0);
data[i]=new GraphViewData(cal.getTimeInMillis,VALUE);

Then set the CustomLabelFormatter.

graphView.setCustomLabelFormatter(new CustomLabelFormatter() {
        @Override
        public String formatLabel(double value, boolean isValueX) {
            // TODO Auto-generated method stub
            if (isValueX) {
                Date d = new Date((long) (value));
                return (dateFormat.format(d));
            }
            return "" + (int) value;
        }
    });

Time/Date appears in the format specified in dateFormat.

I know it is an old question, but I solved by using DefaultLabelFormatter().

graphView.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter() {
    @Override
    public String formatLabel(double value, boolean isValueX) {
        if (isValueX) {
            Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return formatter.format(value);
        }
        return super.formatLabel(value, isValueX);
    }
});

Based on the following video: https://www.youtube.com/watch?v=xt8gx7Z7yJU

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!