在查看之前,Graph View中是否有任何选项可以对数据进行排序?

时间:2015-06-16 20:57:57

标签: android graph android-graphview

我收到此错误。

Process: com.peacecorps.malaria, PID: 3721
    java.lang.IllegalArgumentException: The order of the values is not correct. X-Values have to be ordered ASC. First the lowest x value and at least the highest x value.
            at com.jjoe64.graphview.GraphViewSeries.checkValueOrder(GraphViewSeries.java:200)
            at com.jjoe64.graphview.GraphViewSeries.<init>(GraphViewSeries.java:74)
            at com.peacecorps.malaria.SecondAnalyticFragment.setupAndShowGraph(SecondAnalyticFragment.java:192)
            at com.peacecorps.malaria.SecondAnalyticFragment.onCreateView(SecondAnalyticFragment.java:90)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
            at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
            at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
            at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1761)
            at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:1896)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1960)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2369)
            at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1719)
            at android.app.Activity.dispatchTouchEvent(Activity.java:2742)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2330)
            at android.view.View.dispatchPointerEvent(View.java:8666)
            at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4123)
            at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3989)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
            at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3680)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
            at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3737)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
            at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5807)
            at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5781)
            at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5752)
            at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5897)
            at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
            at android.os.MessageQueue.native

我正在从数据库中读取数据。以前可以对它进行排序吗?

编辑:

    public void setupAndShowGraph() {
    GraphViewData graphViewData[] = new GraphViewData[DatabaseSQLiteHelper.percentage.size()];
    for (int index = 0; index < DatabaseSQLiteHelper.percentage.size(); index++) {

        graphViewData[index] = new GraphViewData(DatabaseSQLiteHelper.date.get(index), Double.parseDouble("" + DatabaseSQLiteHelper.percentage.get(index)));
    }
    drugGraphSeries = new GraphViewSeries(graphViewData);

    GraphView lineGraphView = new LineGraphView(getActivity(), "");

    lineGraphView.getGraphViewStyle().setGridColor(getResources().getColor(R.color.golden_brown));
    lineGraphView.getGraphViewStyle().setHorizontalLabelsColor(getResources().getColor(R.color.golden_brown));
    lineGraphView.getGraphViewStyle().setVerticalLabelsColor(getResources().getColor(R.color.golden_brown));
    lineGraphView.getGraphViewStyle().setTextSize(15.0F);
    lineGraphView.setScrollable(true);
    lineGraphView.setScalable(true);

    ((LineGraphView) lineGraphView).setDrawBackground(true);
    lineGraphView.addSeries(drugGraphSeries);

    LinearLayout linearLayout = (LinearLayout) rootView.findViewById(R.id.graphView);
    linearLayout.addView(lineGraphView);

}

我添加了setupAndShowGraph()方法。您可以在下面的行中看到

     graphViewData[index] = new GraphViewData(DatabaseSQLiteHelper.date.get(index), Double.parseDouble("" + DatabaseSQLiteHelper.percentage.get(index)));

我使用上面的行获取数据,所有要添加的内容,以便我可以获得一个已排序的系列。

1 个答案:

答案 0 :(得分:0)

创建一个数组来保存系列的值:

Double[] valueArray = new Double[DatabaseSQLiteHelper.percentage.size()];

填充数组:

for (int index = 0; index < DatabaseSQLiteHelper.percentage.size(); index++) {

    valueArray[index] = Double.parseDouble("" + DatabaseSQLiteHelper.percentage.get(index));
}

对数组进行排序:

Arrays.sort(theValue);

最后,填写GraphViewData:

for (int index = 0; index < DatabaseSQLiteHelper.percentage.size(); index++) {

    graphViewData[index] = new GraphViewData(DatabaseSQLiteHelper.date.get(index), valueArray[index]);
}
drugGraphSeries = new GraphViewSeries(graphViewData);

以下是完整代码段

public void setupAndShowGraph() {

    Double[] valueArray = new Double[DatabaseSQLiteHelper.percentage.size()]; // Added line

    GraphViewData graphViewData[] = new GraphViewData[DatabaseSQLiteHelper.percentage.size()];

    // Fill the value array
    for (int index = 0; index < DatabaseSQLiteHelper.percentage.size(); index++) {

        valueArray[index] = Double.parseDouble("" + DatabaseSQLiteHelper.percentage.get(index));
    }

    // Sort the array 
    Arrays.sort(valueArray);

    // fill the graphViewData with the valueArray 
    for (int index = 0; index < DatabaseSQLiteHelper.percentage.size(); index++) {

        graphViewData[index] = new GraphViewData(DatabaseSQLiteHelper.date.get(index), valueArray[index]);
    }
    drugGraphSeries = new GraphViewSeries(graphViewData);

    GraphView lineGraphView = new LineGraphView(getActivity(), "");

    lineGraphView.getGraphViewStyle().setGridColor(getResources().getColor(R.color.golden_brown));
    lineGraphView.getGraphViewStyle().setHorizontalLabelsColor(getResources().getColor(R.color.golden_brown));
    lineGraphView.getGraphViewStyle().setVerticalLabelsColor(getResources().getColor(R.color.golden_brown));
    lineGraphView.getGraphViewStyle().setTextSize(15.0F);
    lineGraphView.setScrollable(true);
    lineGraphView.setScalable(true);

    ((LineGraphView) lineGraphView).setDrawBackground(true);
    lineGraphView.addSeries(drugGraphSeries);

    LinearLayout linearLayout = (LinearLayout) rootView.findViewById(R.id.graphView);
    linearLayout.addView(lineGraphView);

}