Android GraphView在X轴上只渲染一个日期

问题描述 投票:0回答:1

我从数据库中获取股票日期,其中公司的股票历史记录保存在数据库列中。每天的数据以{date,stock_value}格式显示,并通过“\ n”与其他日期分开。

    String[] allStocks = history.split("\n");
    String[] singleStock;

    ArrayList<DataPoint> points = new ArrayList<>();
    GraphView graph = (GraphView) findViewById(R.id.graph);

    for(int i=0; i<3; i++)
        {
            singleStock = allStocks[i].split(",");
            Date date = new Date(Long.parseLong(singleStock[0]));
            System.out.println(date);
            points.add(new DataPoint(new Date(Long.parseLong(singleStock[0])), Float.parseFloat(singleStock[1])));
        }

    DataPoint[] dbPoint = points.toArray(new DataPoint[points.size()]);
    System.out.println(points);
    LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(dbPoint);
    graph.addSeries(series);
    graph.getGridLabelRenderer().setLabelFormatter(new DateAsXAxisLabelFormatter(this));
    graph.getGridLabelRenderer().setNumHorizontalLabels(2); 
    // set manual x bounds to have nice steps

    graph.getViewport().setXAxisBoundsManual(true);

    // as we use dates as labels, the human rounding to nice readable numbers
    // is not necessary
    graph.getGridLabelRenderer().setHumanRounding(false);

以下是点变量的内容:[[1.4887386E12 / 117.55000305175781],[1.4881338E12 / 118.62000274658203],[1.4876154E12 / 118.79000091552734]]

该图显示为:The graph appears only for one date

这是我图表的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="match_parent"
 android:layout_height="match_parent">

 <com.jjoe64.graphview.GraphView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/graph" />

 </LinearLayout>

图表中只显示一个日期。当我从'GraphView'尝试日期的示例代码时,一切都按预期工作。如果我的代码中有任何缺陷,请告诉我。

android android-graphview
1个回答
0
投票

我之前没有使用过GraphView,但是我发现的日期示例似乎是将水平标签的数量设置为比绘制的项目数量多一个,所以我会尝试:

graph.getGridLabelRenderer().setNumHorizontalLabels(points.size()+1);
© www.soinside.com 2019 - 2024. All rights reserved.