MPAndroidChart x轴日期/时间标签格式 [英] MPAndroidChart x-axis date/time label formatting
问题描述
背景
对于我的应用程序中的某些图表,我使用MPAndroidChart库。我的图形的所有横轴都是基于时间的,它们可以跨越一整个月,一个月,一个星期,一天或一个小时。它始终显示一整个时期,所以1月至12月,周一至周日,0:00 - 24:00等。轴的值始终为时间 - 时间戳(以秒为单位)。
要求
我希望x轴标签遵循以下规则:
$在年份跨度
问题
我可以设置x轴的粒度
,这确保了粒度之间的两个点之间的空间不会更小,但是这可能意味着(在日间的情况下)第一个标签是在凌晨1点,第二个标签在上午2:01和第rd是在3:16 am,因为它符合(至少)60分钟的粒度。
当前不正确的情况,这将是理想的情况,像 [0:00,3:00,6:00,9:00 ..]
问题
有没有办法控制x轴标签可以实现上述的结果?
我做了同样的事情,尝试这个,
XAxis xAxis = mChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM_INSIDE);
xAxis.setDrawGridLines(false);
xAxis.setGranularity(1f); //只有1天的间隔
xAxis.setTypeface(mTfLight);
xAxis.setTextSize(8);
xAxis.setTextColor(ContextCompat.getColor(this,R.color.colorYellow));
xAxis.setValueFormatter(new GraphXAxisValueFormatter(range,interval,slot));在这个范围内的
。如果你想要月份,那么在第7周等的情况下,有12个。
在间隔
你通过1。
在插槽
你必须通过,识别你的数据,如月,年,日,我有使用枚举为此。
public class GraphXAxisValueFormatter实现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.getInstance(); (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&&&> = 0){
return mValues [(int)value%mValues.length];
} else
return;
}
@Override
public int getDecimalDigits(){
return 0;
}
请参阅: http://prntscr.com/dbn62x
Background
For some charts in my app, I'm using the MPAndroidChart library. All horizontal axis' of my graphs are time based, they can span a full year, a month, a week, a day or span one hour. It always shows a full period, so January-December, Monday-Sunday, 0:00 - 24:00 etc. The value of the axis is always the epoch-timestamp (in seconds).
Requirement
I want the x-axis labels to follow these rules:
- at 1st of month in case of year span;
- at start of day in case of month or week span;
- on any full hour (##:00) (not perse all) in case of a day span;
- on any 5 minute point on an hour span.
Problem
I can set the granularity
of the x-axis, which makes sure there is no less space between two points then the granularity says, but that can mean that (in case of day span) the first label is at 1:00am, and the second at 2:01am and the third is at 3:16am, since that fits a granularity of (minimum) 60 minutes.
Current incorrect situation, which would ideally be something like [0:00, 3:00, 6:00, 9:00 ..]
Question
Is there a way to control the positioning of the x-axis labels to achieve the results above?
I have done same thing, Try this,
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));
in this range
in your case. If you want month then there is 12, in case of week 7 etc.
in interval
you pass 1.
in slot
you have to pass, identification of your data like month, year, day, i have use enum for this.
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;
}
See: http://prntscr.com/dbn62x
这篇关于MPAndroidChart x轴日期/时间标签格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!