【问题标题】:Android GraphView incorrect X-axis steps when formatting labels格式化标签时Android GraphView不正确的X轴步骤
【发布时间】:2016-10-11 15:57:24
【问题描述】:

我正在使用 android-graphview 为天气预报应用绘制 Temps 和时间。

在我的选项卡“24 小时”中,我绘制了从当前时间到 24 小时后的数据。我有以 3 小时为单位的天气预报。 为了在 x 轴上实现正确的显示,首先我必须在一天变化时增加小时数。因此,如果当前时间是 18:00 绘制数据直到明天 18:00,那么我绘制的 x 轴值是 --> 18 21 24 27 30 33 36 39 42。 接下来,我使用标签格式化程序将 x 轴标签更改为 --> 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);

这是图表的图像:

resulting graph

【问题讨论】:

    标签: android graph android-graphview


    【解决方案1】:

    尝试通过禁用人工舍入

    graph.getGridLabelRenderer().setHumanRounding(false);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-28
      • 2017-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多