android - GraphView For循环不绘制数据

标签 android for-loop android-graphview linegraph

我正在开发一个应用程序,它通过 XML 文件进行解析,收集日期和值,并使用 GraphView 使用循环遍历的 for 循环和数据点数组列表绘制此数据。我可以获得图表以生成具有日期和值范围的 x 轴和 y 轴,但未绘制任何点。

我可以在 Android Monitor 中看到正在生成数据点,它们只是没有绘制。我的代码肯定有问题,但我无法弄清楚。有什么想法吗?

更新:我应该补充一点,我的 XML 文件具有同一日期的多个值(数据每 15 分钟收集一次)。我的猜测是我需要对数组中的这些值进行平均,以便每个日期只有一个值。这可能是影响 GraphView 的原因。

公共(public)类 GraphActivity 扩展 AppCompatActivity {

GraphView graphView;
LineGraphSeries<DataPoint> series;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

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

    graphView = (GraphView) findViewById(R.id.graph);
    try {
        InputStream is = GraphActivity.this.getResources()
                .getAssets().open("smallmonth.xml");
        ArrayList<GraphDateEntry> dateOnlyEntrySeries = new GraphDateParsing2().parse(is);

        InputStream is1 = GraphActivity.this.getResources()
                .getAssets().open("smallmonth.xml");
        ArrayList<GraphValueEntry> valueOnlySeries = new GraphValueParsing().parse(is1);

        for (int i = 0; i < dateOnlyEntrySeries.size(); i++) {

            Double value = Double.parseDouble(valueOnlySeries.get(i).value);
            String stringDate = dateOnlyEntrySeries.get(i).date;

            DataPoint dateAndValue = new DataPoint(new SimpleDateFormat("yyyy-MM-dd").parse(stringDate), value);

            series = new LineGraphSeries<>();
            series.appendData(dateAndValue, true, dateOnlyEntrySeries.size());
        }

        graphView.addSeries(series);
        series.setColor(Color.BLACK);
        series.setThickness(8);

        graphView.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter() {
            @Override
            public String formatLabel(double value, boolean isValueX) {
                if (isValueX) {
                    Log.d("formatLabel: ", sdf.format(new Date((long) value)));
                    return sdf.format(new Date((long) value));
                } else {
                    return super.formatLabel(value, isValueX);
                }
            }
        });

    } catch (IOException ie) {
        ie.printStackTrace();
    } catch (ParseException pe) {
        pe.printStackTrace();
    }
}

最佳答案

我想通了。我首先遍历我的数组并创建没有任何重复的新数组。然后我使用这些数组创建了一个 DataPoints 数组并将其添加到图中:

            for (int j = 0; j < dateOnlyEntrySeries.size() - 1; j++) {
                String stringValue = valueOnlySeries.get(j).value;
                String stringDate = dateOnlyEntrySeries.get(j).date;
                String stringDate2 = dateOnlyEntrySeries.get(j + 1).date;

                finalDate = dateOnlyEntrySeries.get(dateOnlyEntrySeries.size() - 1).date;
                if (!(stringDate.equalsIgnoreCase(stringDate2))) {

                    array3.add(stringDate);
                    array4.add(stringValue);
                    n = n + 1;
                }
            }

            dp = new DataPoint[array3.size()];
            int arraySize = array3.size();

            for (int i = 0; i < array3.size(); i++) {
                Double newDoubleValue = Double.parseDouble(array4.get(i));
                String newStringDate = array3.get(i);

                dp[i] = new DataPoint(new SimpleDateFormat("yyyy-MM-dd").parse(newStringDate), newDoubleValue);
            }

            finalDoubleValue = Double.parseDouble(valueOnlySeries.get(valueOnlySeries.size() - 1).value);

            DataPoint finalDataPoint = new DataPoint(
                    new SimpleDateFormat("yyyy-MM-dd").parse(finalDate), finalDoubleValue);


            series = new LineGraphSeries<>(dp);

            series.appendData(finalDataPoint, true, 30);
            graphView.addSeries(series); 

我对 GraphView 设置进行了一些调整,以正确标记轴并根据数据调整图表大小,但我成功了。

关于android - GraphView For循环不绘制数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47352585/

相关文章:

java - 如何通过textview在屏幕上显示android中发生的生命周期事件

android - 如何在Android中的字符串中插入新行

javascript - 使用 ">>>"运算符改进循环?

c# - for 和 foreach 在闭包方面有什么区别

Android GraphView 项目因实时更新而卡住

java - Android 警报对话框在我认为应该显示的时候没有显示

android - android 中的 gridview 选择器。如何?

python - 平均功能不是平均而是重置

android - 无法在 android 中添加 addView

java - GraphView水平日期标签无法换行