Android图表视图日期轴无法正常显示

时间:2016-11-13 19:45:28

标签: android graph android-graphview

我在android上有一个graphView的问题,当我尝试在x轴上显示我的图表时,我得到了这个结果: graph

每次生成图表时x值都会发生变化,有时会显示图表中的条形图,有时候不是真正随机的。

这是我的代码:

            Cursor weeklySale = db.getWeeklySale(login);

            DataPoint[] dp= new DataPoint[weeklySale.getCount()];
            Date[] dates= new Date[weeklySale.getCount()];

            int i=0;
            while(weeklySale.moveToNext())
            {
                SimpleDateFormat sdf = new SimpleDateFormat("dd MM yyyy");
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.WEEK_OF_YEAR, Integer.valueOf(weeklySale.getString(1)));
                cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

                // Date c = new Date(weeklySale.getLong(1));
                dp[i]= new DataPoint(cal.getTime(), (long)weeklySale.getFloat(0));
                dates[i]= cal.getTime();

                System.out.println(weeklySale.getFloat(0)+" + "+weeklySale.getString(1)+" , "+sdf.format(cal.getTime()));
            }

            graph.removeAllSeries();

            GridLabelRenderer gridLabel = graph.getGridLabelRenderer();
            gridLabel.setHorizontalAxisTitle("Semaine");
            gridLabel.setVerticalAxisTitle("CA");

            LineGraphSeries series = new LineGraphSeries<DataPoint>(dp);
            series.setColor(Color.RED);
            graph.addSeries(series);

由于

1 个答案:

答案 0 :(得分:0)

尝试通过以下方式更改标签数量:

graph.getGridLabelRenderer().setNumHorizontalLabels()

此外,您应该将此行移出while循环以节省内存和性能: SimpleDateFormat sdf = new SimpleDateFormat("dd MM yyyy");