【问题标题】:Is there any option in Graph View to sort data before viewing?图表视图中是否有任何选项可以在查看之前对数据进行排序?
【发布时间】:2015-06-16 20:57:57
【问题描述】:

我收到此错误。

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

我正在使用上面的行获取数据,要添加什么以便我可以得到一个排序的系列。

【问题讨论】:

  • 显示您的代码以从数据库中获取结果,尤其是 setupAndShowGraph() 方法
  • 方法已添加,请查看。

标签: android graph android-graphview


【解决方案1】:

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

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

下面是完整的sn-p


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

}

【讨论】:

  • 嗨,我想我明白了,但它给出了错误,因为 x 轴值没有排序。而且我必须按照日期的升序对百分比的值进行排序。日期绘制在 x 轴上,百分比绘制在 y 轴上。
  • DatabaseSQLiteHelper.date.get(index)的返回类型是什么?
  • DatabaseSQLiteHelper.date.get(index)的返回类型是Integer。
猜你喜欢
  • 2015-09-19
  • 2020-06-30
  • 1970-01-01
  • 2015-09-18
  • 1970-01-01
  • 2019-11-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多