简体   繁体   English

格式化标签时,Android GraphView不正确的X轴步骤

[英]Android GraphView incorrect X-axis steps when formatting labels

I am using android-graphview to plot Temps with time for a weather forecasting app. 我正在使用android-graphview来绘制天气预报应用的时间。

In my tab "24hours" I plot the data from the current time and until 24hours later. 在我的标签“24小时”中,我绘制了当前时间和24小时后的数据。 I have weather forecasts in 3-hour steps. 我有3小时的天气预报。 To achieve proper display on x-axis, first I have to keep the hours additively when the day changes. 为了在x轴上实现正确显示,首先我必须在日期变化时加上时间。 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. 因此,如果当前时间是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? " .. 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? System.out.println(“WHAT1?”..正确显示输入数据点对,小时数以3为单位增加,但System.out.println(“pout”..和“pour”显示x轴值增加以2为步长(虽然位于正确的位置)。如何在3小时步骤中显示标签?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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