简体   繁体   中英

Android GraphView incorrect X-axis steps when formatting labels

I am using android-graphview to plot Temps with time for a weather forecasting app.

In my tab "24hours" I plot the data from the current time and until 24hours later. I have weather forecasts in 3-hour steps. To achieve proper display on x-axis, first I have to keep the hours additively when the day changes. So if current time is 18:00 to plot the data until tommorow 18:00, then x-axis values I plot are --> 18 21 24 27 30 33 36 39 42. Next, I use the label formatter to change the x-axis Labels to --> 18 21 0 3 6 9 12 15 18. The graph is displayed correctly at the correct points, however the labels follow a step of 2 instead of 3 but the range is correct.

System.out.println("WHAT1? " .. shows the input data point pairs correctly with hours increasing in steps of 3, but System.out.println("pout " .. and "pour" show the x-axis values increase in steps of 2 (although being at the correct positions). How can I have the labels be displayed at the 3hour step?

What am I doing wrong? Here is the code:

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

Here is an image of the graph:

resulting graph

尝试通过禁用人工舍入

graph.getGridLabelRenderer().setHumanRounding(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM