首页 > 解决方案 > GraphView 不显示折线图

问题描述

xml

<?xml version="1.0" encoding="utf-8"?>
<com.jjoe64.graphview.GraphView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dip"
    android:id="@+id/graph">

</com.jjoe64.graphview.GraphView>

java

LineGraphSeries<DataPoint> series;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_graph_weight);

    GraphView graph = findViewById(R.id.graph);
    for (int i = 0; i < FragmentDiaryWeight.weightDiaryList.size(); i++) {
        series = new LineGraphSeries<>(new DataPoint[]{
                new DataPoint(Integer.parseInt(FragmentDiaryWeight.weightDiaryList.get(i).getmDay()), Integer.parseInt(FragmentDiaryWeight.weightDiaryList.get(i).getmWeight())),
        });
        graph.addSeries(series);
    }
}

我知道数据是从 Array 收集的weightDiaryList,但图表线没有显示。如果我将 x 和 y 替换为 5 和 10,则一切正常。

如果我使用series.setDrawDataPoints(true),点显示正确。

我该如何解决?如何显示此图表的线条?

标签: javaarraylistandroid-graphview

解决方案


GraphView graph = findViewById(R.id.graph);
    for (int i = 0; i < FragmentDiaryWeight.weightDiaryList.size(); i++) {
        series = new LineGraphSeries<>(new DataPoint[]{
                new DataPoint(Integer.parseInt(FragmentDiaryWeight.weightDiaryList.get(i).getmDay()), Integer.parseInt(FragmentDiaryWeight.weightDiaryList.get(i).getmWeight())),
        });
        
    }
graph.addSeries(series);

推荐阅读