c++ - Graphing issues in data output -
i have program taking input external source (right now, joystick), , plotting on graph. graph displays last 60 frames of data, 1-2 seconds.
here data input:
nextdatapoint(double x){ if (x > max){ max = x; } if (x < min){ min = x; } datainput.enqueue(x) //datainput qqueue<double> while (datainput.size() > 60){ datainput.dequeue(); } update(); //this triggers paint event }
here graphing function
graph function: //this draws min line , max line qpainter painter(this); int linedist = 25; qpen mypen(qt::black, 3); qpoint maxtext(10,20); painter.drawtext(maxtext, "max"); qpoint maxlineleft(0, linedist); qpoint maxlineright(width(), linedist); painter.drawline(maxlineleft, maxlineright); qpoint mintext(10, height()-10); painter.drawtext(mintext, "min"); qpoint minlineleft(0, height()-linedist); qpoint minlineright(width(), height()-linedist); //this draws actual graph mypen.setcolor(qt::blue); mypen.setwidth(2); painter.setpen(mypen); double dist = (double)(heigh() - 2*linedist); int stepsize = (int)((double)width() / 60.0); int heightstep = (max-min)/dist; double x; qpoint lastpoint(0,0); qpoint nextpoint(0,0); int = 0; if (!datainput.empty()){ //checks there data if (datainput.size() < 60) { //ignoring sake of brevety } else { x = datainput.at(i); x = max - x; // inverts data, necessary because (0,0) upper left corner x = (x-min)/heightstep; nextpoint.setx(0); nextpoint.sety(x+linedist); (i = 1; < 60; i++){ x = datainput.at(i); x = max - x; x = (x-min)/heightstep; lastpoint = nextpoint; nextpoint.setx(i*stepsize); nextpoint.sety(x+linedist); painter.drawline(lastpoint, nextpoint); } } }
so, 1 joystick testing with, program works without issue. move joystick, program draws blue line within bounds listed on screen. x input number between 0 , 65535, , after moving joystick around little bit system recognizes max , min, , proceeds function expected.
the second "joystick" less of joystick , more of pressure sensor. outputs either x = 1023 or x number between 5 , 9. in case however, blue line appears on same level max line, or when data output x in single digits, blue line appears below min. haven't been able figure out why case, first joystick can produce results of 0, never goes below minimum line. might causing particular issue?
two changes needed correct issues.
first, stepsize , heightstep both changed int double.
second, section
x = datainput.at(i); x = max - x; x = (x-min)/heightstep; nextpoint.setx(0); nextpoint.sety(x+linedist); (i = 1; < 60; i++){ x = datainput.at(i); x = max - x; x = (x-min)/heightstep; lastpoint = nextpoint; nextpoint.setx(i*stepsize); nextpoint.sety(x+linedist); painter.drawline(lastpoint, nextpoint); }
was changed to:
x = datainput.at(i); x = (x-min)/heightstep; nextpoint.setx(0); nextpoint.sety(dist-x+linedist); (i = 1; < 60; i++){ x = datainput.at(i); x = (x-min)/heightstep; lastpoint = nextpoint; nextpoint.setx(i*stepsize); nextpoint.sety(dist-x+linedist); painter.drawline(lastpoint, nextpoint); }
this solved issue , made system draw data between min , max lines.
Comments
Post a Comment