Is there any option in Graph View to sort data before viewing?

837 Views Asked by At

I am getting this error.

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

I am reading data from a database. Is it possible to sort it before?

Edit:

    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);

}

I have added the setupAndShowGraph() method. You can see in the line below,

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

I am fetching the data using the above line, what all to add so that I can get a sorted series.

1

There are 1 best solutions below

3
On

Make an array to hold the value of the series :

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

Fill the array :

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

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

Sort the array :

Arrays.sort(theValue);

Finally, Fill the GraphViewData :

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

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

Below is the complete snippet


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);

}