[英]Android GraphView incorrect X-axis steps when formatting labels
我正在使用android-graphview來繪制天氣預報應用的時間。
在我的標簽“24小時”中,我繪制了當前時間和24小時后的數據。 我有3小時的天氣預報。 為了在x軸上實現正確顯示,首先我必須在日期變化時加上時間。 因此,如果當前時間是18:00將數據繪制到tommorow 18:00,那么我繪制的x軸值是 - > 18 21 24 27 30 33 36 39 42.接下來,我使用標簽格式化器來更改x -axis標簽到 - > 18 21 0 3 6 9 12 15 18.圖表在正確的點上正確顯示,但標簽的步驟為2而不是3,但范圍是正確的。
System.out.println(“WHAT1?”..正確顯示輸入數據點對,小時數以3為單位增加,但System.out.println(“pout”..和“pour”顯示x軸值增加以2為步長(雖然位於正確的位置)。如何在3小時步驟中顯示標簽?
我究竟做錯了什么? 這是代碼:
DataPoint b= new DataPoint(0,0);//test
DataPoint[] point_array= new DataPoint[9];
String[] time_label_24=new String[9];
GraphView graph = (GraphView) findViewById(R.id.graph);
hour=Integer.parseInt(Lista.get(x)[15]);
for(int i=x;i<=x+8;i++){
if((i-x)<Lista.size()) {
b = new DataPoint(hour, Float.parseFloat(Lista.get(i)[4]));
point_array[i - x] = b;
System.out.println("WHAT1? " + (i - x) + " " + point_array[i - x] + " " + hour);
time_label_24[i-x]=Lista.get(i)[15];
hour = hour + 3;
}
}
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(point_array);
graph.addSeries(series);
h=Integer.parseInt(Lista.get(x)[15]);
graph.getViewport().setXAxisBoundsManual(true);
//graph.getViewport().setMinX(Integer.parseInt(Lista.get(x)[15])-1);
//graph.getViewport().setMaxX(Integer.parseInt(Lista.get(x)[15])+8*3+1);
graph.getViewport().setMinX(Integer.parseInt(Lista.get(x)[15]));
graph.getViewport().setMaxX(Integer.parseInt(Lista.get(x)[15])+8*3);
graph.getGridLabelRenderer().setNumHorizontalLabels(9); //9
graph.getGridLabelRenderer().setTextSize(12f);
graph.getGridLabelRenderer().reloadStyles();
graph.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter() {
@Override
public String formatLabel(double value, boolean isValueX) {
int xylabel;
if (isValueX) {
if (value < 24) {
System.out.println("pout "+value+" " + String.valueOf(value));
xylabel = (int) Math.round(value);
return String.valueOf(xylabel);
} else{
System.out.println("pour "+value+" " + String.valueOf(value%24));
xylabel = (int) Math.round(value);
return String.valueOf(xylabel%24);
}
}else {
xylabel = (int) Math.round(value);
return String.valueOf(xylabel); // let graphview generate Y-axis label for us
}
}
});
////////
hour=Integer.parseInt(Lista.get(x)[15]);
for(int i=x;i<=x+8;i++){ // Same process for Temp FEEL
if((i-x)<Lista.size()) {
b = new DataPoint(hour, Float.parseFloat(Lista.get(i)[13]));
point_array[i - x] = b;
hour = hour + 3;
time_label_24[i-x]=Lista.get(i)[15];
System.out.println("What2? " + point_array[i - x]);
}
}
LineGraphSeries<DataPoint> series2 = new LineGraphSeries<DataPoint>(point_array);
graph.addSeries(series2);
series.setTitle("Temp");
series2.setTitle("Feel");
series.setColor(Color.WHITE);
series2.setColor(Color.RED);
graph.getLegendRenderer().setVisible(true);
graph.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP);
這是圖表的圖像:
嘗試通過禁用人工舍入
graph.getGridLabelRenderer().setHumanRounding(false);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.