0

I'm trying to plot the value in GraphView and for 2nd time i'm trying plot the another set of data in the same graphview but I'm getting some extra unwanted line in the graphview.

in the above picture Red colored marked line is the extra line I'm getting while plotting the graph.

I'm using com.jjoe64.graphview.GraphView

Here is my sample code , when ever i click on OK button it will start plotting the graph.

public class ABIModeActivity extends AppCompatActivity {

private LineGraphSeries<DataPoint> mSeries2;
private double graph2LastXValue = 5d;
static GraphView graph2;
Context context;
Button okbutton;
CountDownTimer cTimer = null;
int i=0;
int[] BP_Data=new int[]{115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,113,105,76,36,7,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,33,73,105,112,114,113,84,32,22,5,1,0,0,0,0,0,1,8,37,76,95,112,111,90,51,12,2,0,0,0,0,2,21,59,83,105,113,112,105,62,48,17,2,7,33,73,105,112,114,113,84,32,22,5,1,0,0,0,0,0,1,8,37,76,95,112,111,90,51,12,2,0,2,21,59,83,105,113,112,105,62,48,17,2,2,21,59,83,105,113,112,105,62,48,17,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide();
    setContentView(R.layout.activity_abimode);

    //I'm using Timer to simulate the live data graph which I'm getting in my actual device.
    //Timer is used only for simulation of live data
    cTimer = new CountDownTimer(100000, 20) {
        public void onTick(long millisUntilFinished) {
            graph2LastXValue += 1d;
            drawgraph(graph2LastXValue,BP_Data[i]);
            i++;
            if(i==180){
                cTimer.cancel();//Stop the live graph plotting when it reached the end of data
                i=0;
            }
        }
        public void onFinish() {

        }
    };

    okbutton =(Button) findViewById(R.id.okbutton);
    okbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view)
        {
            cTimer.start();// to start plotting the graph data with the timer
        }
    });
    //Actuall graph realted code starting
    graph2 = (GraphView) findViewById(R.id.graph2);
    mSeries2 = new LineGraphSeries<>();
    mSeries2.setThickness(3);
    graph2.getViewport().setXAxisBoundsManual(true);
    graph2.getViewport().setYAxisBoundsManual(true);
    graph2.getViewport().setMinX(0);
    graph2.getViewport().setMaxX(200);
    graph2.getViewport().setMinY(0);
    graph2.getViewport().setMaxY(251);
    graph2.getGridLabelRenderer().setTextSize(0);
    graph2.getViewport().setScrollable(true); // enables horizontal scrolling
    graph2.getViewport().setScrollableY(true); // enables vertical scrolling
    graph2.getViewport().setDrawBorder(false);
    graph2.getViewport().setBackgroundColor(Color.TRANSPARENT);
    graph2.addSeries(mSeries2);
    context = this;
}

public void drawgraph(Double graphvalue,int incrementValue)//plotting the value in the graph
{
    mSeries2.appendData(new DataPoint(graphvalue, incrementValue), true, 200);
}

}

this red colored marked line is the thing i dont want.

Ragul Murugan
  • 37
  • 1
  • 10
  • Please add some sample code of what you wrote so people can help you better. I believe you need to create a new series or reset it to prevent this unwanted effect. – svahidhoss May 03 '18 at 22:06
  • Hai, Thank you for your Reply , I have updated my code i tried to reset but i didn't work. it meant i cant able to plot the graph after resetting. – Ragul Murugan May 08 '18 at 05:21
  • I tested your code and I am not seeing the line you just explained. You have a line y values valued at 150 and then the sinusoidal values which matches BP_Data array data. – svahidhoss May 09 '18 at 15:13
  • actually its a stored data, but in above picture is live data from BP modules. if you press ok button two/three time you can see the previous graph also , i need fresh graph every time when i press ok button.i have updated my picture for sample. – Ragul Murugan May 10 '18 at 13:55
  • I see. I believe I found what you were looking for. Check my answer and let me know if that works for you! – svahidhoss May 10 '18 at 17:20
  • Thank you, You solved my problem. – Ragul Murugan May 11 '18 at 07:03
  • Hai I'm getting another problem now, i have updated my question can you check it? – Ragul Murugan May 11 '18 at 07:11
  • Hi, happy it resolved your problem! Can you please create ask another question and provide a link to it here? I will be more than happy to help. – svahidhoss May 11 '18 at 16:38
  • Thank you, here is the link for my next post https://stackoverflow.com/questions/50323632/while-writing-data-to-the-graph-in-a-graphview-getting-some-the-extra-line-how – Ragul Murugan May 14 '18 at 05:45

1 Answers1

0

If you need a newly drawn graph everytime you press the button, you need to add the following change to your onTick method:

@Override
public void onTick(long millisUntilFinished) {
    graph2LastXValue += 1d;
    if (i == 0) {
        mSeries2.resetData(new DataPoint[]{new DataPoint(graph2LastXValue, BP_Data[i])});
    }
    drawgraph(graph2LastXValue, BP_Data[i]);
    i++;
    if (i == 180) {
        cTimer.cancel();//Stop the live graph plotting when it reached the end of data
        i = 0;
    }
}

This will reset your graph every time the value of i has been reset earlier and gives you the desired result.

svahidhoss
  • 333
  • 2
  • 11