Search code examples
androidandroid-graphview

Why can not GraphView plot these points? If the second point has a x value less than first point


GraphView for android can not plot these points, if second point has a x value less than first point.

<com.jjoe64.graphview.GraphView
    android:id="@+id/GPH_graph"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/STL_Graph"/>


DataPoint[] DP_Array = new DataPoint[]{new DataPoint( 0, 0), new DataPoint(-10,10)};
LineH1 = new LineGraphSeries<>(DP_Array);
LineH1.setColor(getResources().getColor(R.color.black));
GPH_graph.addSeries(LineH1);

I want to plot these points like this picture

I want to plot these points like this picture


Solution

  • I solved the problem with following codes:

    List<LineGraphSeries<DataPoint>> Series;
    
    Series = SetSeries(DP_Array);
    
    for (int i = 0; i < Series.size(); i++)
    {
    
        GPH_graph.addSeries(Series.get(i));    
        
    }
    
    List<LineGraphSeries<DataPoint>> SetSeries(DataPoint[] AllData)
        {
    
            List<LineGraphSeries<DataPoint>> LineGraphSeriesList = new ArrayList<> ();
            DataPoint[] DP;
    
            boolean bol_Forward = true;
            int NumForward = 0;
            int NumBackward = 0;
    
            for(int i = 1;i < AllData.length;i++)
            {
    
                if(AllData[i].getX() >= AllData[i-1].getX())
                {
    
                    NumForward++;
                    bol_Forward = true;
    
                }
                else
                {
    
                    NumBackward++;
                    bol_Forward = false;
    
                }
    
                if(NumBackward>0 & NumForward>0)
                {
    
                    if(bol_Forward == false)
                    {
    
                        DP = new DataPoint[NumForward + 1];
                        for (int A = 0;A < NumForward + 1;A++)
                        {
    
                            DP[NumForward - A] = new DataPoint(AllData[i - 1 - A].getX(),AllData[i - 1 - A].getY());
    
                        }
    
                        LineGraphSeriesList.add(new LineGraphSeries<>(DP));
                        NumForward = 0;
    
                    }
                    else
                    {
    
                        DP = new DataPoint[NumBackward + 1];
                        for (int A = 0;A < NumBackward + 1;A++)
                        {
    
                            DP[A] = new DataPoint(AllData[i - 1 - A].getX(),AllData[i - 1 - A].getY());
    
                        }
    
                        LineGraphSeriesList.add(new LineGraphSeries<>(DP));
                        NumBackward = 0;
    
                    }
    
                    Num++;
    
                }
    
            }
    
            return LineGraphSeriesList;
    
        }