3 plot2D::plot2D(QWidget *parent){
4 setBackgroundRole(QPalette::Dark);
5 setAutoFillBackground(
true);
6 setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
7 setMouseTracking(
true);
10 dataarea.setRect(0,0,width(),height());
11 count = count1 =
count2 = 0;
17 setBackgroundRole(QPalette::Dark);
18 setAutoFillBackground(
true);
19 setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
20 setMouseTracking(
true);
23 dataarea.setRect(0,0,width(),height());
24 count = count1 =
count2 = 0;
28 xdata =
new double[count];
29 ydata =
new double[count];
30 memcpy(xdata,src->xdata,count*
sizeof(
double));
39 xdata1 =
new double[count1];
40 ydata1 =
new double[count1];
41 memcpy(xdata1,src->xdata1,count1*
sizeof(
double));
50 xdata2 =
new double[
count2];
52 memcpy(xdata2,src->xdata2,
count2*
sizeof(
double));
60 xbarlabel = src->xbarlabel;
61 ybarlabel = src->ybarlabel;
66 if(xdata != NULL)
delete xdata;
67 if(xdata1 != NULL)
delete xdata1;
68 if(xdata2 != NULL)
delete xdata2;
75 s0.bgcolor = QColor(Qt::white).rgba();
81 strcpy(
s0.font,
"Arial");
82 s0.fontcolor = QColor(Qt::black).rgba();
85 s0.height = this->height();
86 s0.latticecolor = QColor(0,0,0,100).rgba();
88 s0.width = this->width();
93 s0.singlecolor =
false;
94 s0.fillcolor = QColor(0,255,0,150).rgba();
96 s0.linecolor = QColor(Qt::black).rgba();
100 void plot2D::zoom_out(){
101 double x =
s->xmax-
s->xmin, y =
s->ymax-
s->ymin;
102 s->xmin =
s->xmin - x/2.;
103 s->xmax =
s->xmax + x/2.;
104 s->ymin =
s->ymin - y/2.;
105 s->ymax =
s->ymax + y/2.;
108 void plot2D::zoom_in(){
109 double x =
s->xmax-
s->xmin, y =
s->ymax-
s->ymin;
110 s->xmin =
s->xmin + x/4.;
111 s->xmax =
s->xmax - x/4.;
112 s->ymin =
s->ymin + y/4.;
113 s->ymax =
s->ymax - y/4.;
116 QSize plot2D::sizeHint()
const{
117 return QSize(250,100);
119 QSize plot2D::minimumSizeHint()
const{
120 return QSize(100,100);
122 void plot2D::paintEvent(QPaintEvent *){
124 p.drawPixmap(0,0,
pm);
126 p.setBrush(QBrush(QColor(0,255,0,100)));
127 p.setPen(palette().light().color());
128 p.drawRect(
zoombox.normalized());
130 if(marker !=
s->xmin){
131 p.setPen(QPen(Qt::black));
136 void plot2D::resizeEvent(QResizeEvent *){
139 void plot2D::mousePressEvent(QMouseEvent *event){
140 if(
dataarea.contains(event->pos())){
141 if( event->button() == Qt::LeftButton){
144 xold =
event->pos().x();
145 yold =
event->pos().y();
153 void plot2D::mouseMoveEvent(QMouseEvent *event){
156 if(event->pos().x()>xold)
157 zoombox.setWidth(event->pos().x()-xold);
159 zoombox.setLeft(event->pos().x());
160 zoombox.setWidth(xold-event->pos().x());
166 marker =
xtoxdata(event->pos().x());
170 void plot2D::mouseReleaseEvent(QMouseEvent *event){
171 if( event->button() == Qt::LeftButton
174 if(event->pos().x()!=xold){
175 if(event->pos().x()>xold)
176 zoombox.setWidth(event->pos().x()-xold);
178 zoombox.setLeft(event->pos().x());
179 zoombox.setWidth(xold-event->pos().x());
181 if(
zoombox.normalized().width() < 2 ||
zoombox.normalized().height()<2)
193 if(event->pos().y() == yold)
194 emit clicked(
xtoxdata(event->pos().x()));
197 void plot2D::keyPressEvent(QKeyEvent *event){
198 switch(event->key()){
211 emit keypressed(event);
215 pm = QPixmap(size());
217 p.fillRect(
pm.rect(),QColor::fromRgba(
s->bgcolor));
220 p.setRenderHint(QPainter::Antialiasing);
237 p->setFont(QFont(QString(
s->font),
s->fsnormal));
238 QFontMetricsF q(p->font());
239 float textheight = q.height();
240 QFontMetricsF qt(QFont(QString(
s->font),
s->fstitle));
242 float ticklength = textheight /2;
243 int daxmin =
s->margin + ticklength,
244 daxmax = this->width()-
s->margin,
246 daymax = this->height()-
s->margin-ticklength-
s->margin-textheight;
247 if(
s->drawtitle && !title.isEmpty())
248 daymin = daymin + titleheight +
s->margin;
249 if(
s->drawxbarlabel && !xbarlabel.isEmpty())
250 daymax = daymax -
s->margin - textheight;
251 if(
s->drawybarlabel && !ybarlabel.isEmpty())
252 daxmin = daxmin +
s->margin + textheight;
255 ysteps = floor((daymax-daymin-
s->margin)/(textheight +
s->margin));
256 double step = 1.*(
s->ymax-
s->ymin)/ysteps;
260 if(step > 0.1) ystep = 0.2;
261 if(step > 0.2) ystep = 0.25;
262 if(step > 0.25) ystep = 0.5;
263 if(step > 0.5) ystep = 1.;
265 ystep0 = ystep*ceil(
s->ymin/ystep);
266 ysteps = floor((
s->ymax-ystep0)/ystep);
268 QString a = QString(
"%1").arg(ystep0,0,
'f',qMax(0.,-
yprecission));
269 QString b = QString(
"%1").arg(ystep0+ysteps*ystep,0,
'f',qMax(0.,-
yprecission));
271 daxmin +=
s->margin + qMax(q.width(a), q.width(b));
275 int OK = 1, End = 0, tickcount = 0;
277 if(End){ tickcount--; OK = 0;}
279 step = 1.*(
s->xmax-
s->xmin)/tickcount;
280 xprecission = floor(log10(step));
281 step = step/pow(10,xprecission+1);
283 if(step > 0.1) xstep = 0.2;
284 if(step > 0.2) xstep = 0.25;
285 if(step > 0.25) xstep = 0.5;
286 if(step > 0.5) xstep = 1.;
287 xstep = xstep * pow(10,xprecission+1);
288 xstep0 = xstep*ceil(
s->xmin/xstep);
289 xsteps = qMax(1.,floor((
s->xmax-xstep0)/xstep));
290 ww = q.width(QString(
"-%1").arg(xstep0,0,
'f',qMax(0.,-xprecission)));
291 if( (daxmax-daxmin)/(xsteps) -
s->margin
292 < q.width(QString(
"-%1").arg(xstep0,0,
'f',qMax(0.,-xprecission))))
297 dataarea = QRect(daxmin,daymin,daxmax-daxmin,daymax-daymin);
299 p->setPen(QColor::fromRgba(
s->fontcolor));
304 for(
double y = ystep0; y <
s->ymax; y = y + ystep){
308 p->drawText(0,y0-textheight/2,
dataarea.left()-ticklength-
s->margin,textheight,
309 Qt::AlignRight,QString(
"%1").arg(y,0,
'f',qMax(0.,-
yprecission)));
311 if(!ybarlabel.isNull()){
315 p->drawText(0,
s->margin,-height(),textheight,Qt::AlignCenter,ybarlabel);
324 for(
double x = xstep0; x <
s->xmax; x = x + xstep){
328 QString t = QString(
"%1").arg(x,0,
'f',qMax(0.,-xprecission));
329 if(q.width(t)/2+x0+
s->margin < width())
330 p->drawText(x0-1000,
dataarea.bottom()+ticklength+
s->margin,2001,
331 textheight,Qt::AlignCenter,t);
333 if(
s->drawxbarlabel && !xbarlabel.isNull())
335 dataarea.width(),textheight,Qt::AlignCenter,xbarlabel);
338 if(
s->drawtitle && !title.isNull()){
339 p->setFont(QFont(QString(
s->font),
s->fstitle));
340 p->drawText(0,
s->margin,width(),titleheight,Qt::AlignCenter,title);
344 p->setPen(QColor::fromRgba(
s->latticecolor));
345 p->setClipRect(
dataarea.adjusted(+1,+1,-1,-1));
347 for(
double y = ystep0; y <
s->ymax; y = y + ystep){
352 for(
double x = xstep0; x <
s->xmax; x = x + xstep){
358 p->setClipRect(
dataarea.adjusted(+1,+1,-1,-1));
360 p->setPen(Qt::NoPen);
362 p->setBrush(QBrush(QColor::fromRgba(
s->fillcolor)));
367 dxmin = (
s->xmax-
s->xmin)/(
dataarea.width()/5);
370 p->setRenderHint(QPainter::Antialiasing);
372 for(
int i = 1; i < count; i++){
373 if( xdata[i]-xdata[iold] > dxmin
374 && ( (xdata[i] >=
s->xmin && xdata[i] <=
s->xmax)
375 ||(xdata[i-1]>=
s->xmin && xdata[i-1]<=
s->xmax))){
379 ||d[1].x()-d[0].x()>5){
386 slope = qAbs((
ydata[i]-
ydata[iold])/(xdata[i]-xdata[iold]))*0.001;
391 p->setPen(QPen(QColor::fromRgba(
s->linecolor),4,Qt::SolidLine,Qt::RoundCap));
392 p->drawLine(d[0],d[1]);
393 p->setPen(Qt::NoPen);
400 p->setPen(QPen(Qt::red,1.0));
403 for(
int i = 1; i < count1; i++){
404 tmin = qMin(tmin,
ydata1[i]);
405 tmax = qMax(tmax,
ydata1[i]);
407 tmin -= (tmax-tmin)/20;
408 tmax += (tmax-tmin)/20;
409 QPointF* P =
new QPointF[count1];
414 (
ydata1[0]-tmin)/(tmax-tmin));
415 for(
int i = 1; i < count1; i++)
416 if( (xdata1[i-1]/1000. >
s->xmin && xdata1[i-1]/1000. <
s->xmax)
417 ||(xdata1[i ]/1000. >
s->xmin && xdata1[i ]/1000. <
s->xmax)){
418 P[ci].setX(
xdatatox(xdata1[i]/1000.));
421 (
ydata1[i]-tmin)/(tmax-tmin));
425 P[ci].setX(
xdatatox(xdata1[count1-1]));
428 (
ydata1[count1-1]-tmin)/(tmax-tmin));
431 p->drawPolyline(P,ci);
434 p->setPen(QPen(Qt::blue,1.0));
437 for(
int i = 1; i <
count2; i++){
438 tmin = qMin(tmin,
ydata2[i]);
439 tmax = qMax(tmax,
ydata2[i]);
441 tmin -= (tmax-tmin)/20;
442 tmax += (tmax-tmin)/20;
443 QPointF* P =
new QPointF[
count2];
448 (
ydata2[0]-tmin)/(tmax-tmin));
449 for(
int i = 1; i <
count2; i++)
450 if( (xdata2[i-1]/1000. >
s->xmin && xdata2[i-1]/1000. <
s->xmax)
451 ||(xdata2[i ]/1000. >
s->xmin && xdata2[i ]/1000. <
s->xmax)){
452 P[ci].setX(
xdatatox(xdata2[i]/1000.));
455 (
ydata2[i]-tmin)/(tmax-tmin));
459 P[ci].setX(
xdatatox(xdata2[count2-1]));
462 (
ydata2[count2-1]-tmin)/(tmax-tmin));
465 p->drawPolyline(P,ci);
468 p->setClipRect(this->
rect());
470 void plot2D::set_labels(QString title, QString xbarlabel, QString ybarlabel){
472 this->xbarlabel = xbarlabel;
473 this->ybarlabel = ybarlabel;
476 set_data(dataxy,count,NULL,0,NULL,0);
479 double** data1xy,
int count1,
480 double** data2xy,
int count2){
481 if(xdata != NULL ||
ydata != NULL){
486 if(xdata1 != NULL ||
ydata1 != NULL){
491 if(xdata2 != NULL ||
ydata2 != NULL){
498 xdata =
new double[count];
499 ydata =
new double[count];
500 for(
int i= 0; i < count; i++){
501 xdata[i] = dataxy[0][i];
502 ydata[i] = dataxy[1][i];
506 xdata =
ydata = NULL;
507 this->count1 = count1;
509 xdata1 =
new double[count1];
510 ydata1 =
new double[count1];
511 for(
int i= 0; i < count1; i++){
512 xdata1[i] = data1xy[0][i];
513 ydata1[i] = data1xy[1][i];
520 xdata2 =
new double[
count2];
522 for(
int i= 0; i <
count2; i++){
523 xdata2[i] = data2xy[0][i];
524 ydata2[i] = data2xy[1][i];
539 if(x<0.03)
return QColor(255,255,255,150);
540 if(x<0.05)
return QColor(150,255,150,150);
541 if(x<0.07)
return QColor(0,255,0,150);
542 if(x<0.09)
return QColor(255,255,0,150);
543 if(x<0.12)
return QColor(255,0,0,150);
544 return QColor(0,0,0,150);
548 s->ymin =
s->ymax =
s->xmin =
s->xmax = 0;
550 s->ymax =
s->xmax = -1e300;
551 s->ymin =
s->xmin = +1e300;
553 for(
int i = 0; i < count; i++){
554 if(xdata[i] >
s->xmax)
s->xmax = xdata[i];
556 if(xdata[i] <
s->xmin)
s->xmin = xdata[i];
559 if(
s->xmin ==
s->xmax){
s->xmin--;
s->xmax++;}
560 if(
s->ymin ==
s->ymax){
s->ymin--;
s->ymax++;}
561 s->ymax += (
s->ymax-
s->ymin)/10.;
562 s->ymin -= (
s->ymax-
s->ymin)/20.;
571 double plot2D::get_xmin(){
574 void plot2D::set_size(
int w,
int h){
576 dataarea.setRect(0,0,width(),height());
579 void plot2D::save_to_png(QString filename){
580 pm.save(filename,
"PNG");
bool showreduceddata
average data over multiple screen pixels to sensfully show fill color
void call_zoom_repaint(double xmin, double xmax)
indicate wish to be repainted
void add_data(QPainter *p)
rendering subfunction
double xdatatox(double x)
transform data value to pic-px
ProfilSettings s0
intrinsic settings
QRect dataarea
coordinates of plot inset
holds data grid information
void autoset_axis()
autoscale plot to full data view
double ytoydata(int y)
transform pic-px to data value
double * ydata2
linedata blue
void add_axis_labels(QPainter *p)
rendering subfunction
void call_fullview()
indicate wish to be rescaled
QRect zoombox
rect to be drawn green when zoomed
void set_marker(double x)
draws vertical black line at x pos
double yprecission
axis label precission
QColor slopetocolor(double x)
calc fill color of profil data
int count2
data point count
double xtoxdata(int x)
transform pic-px to data value
plotting 2D-Data sets as the height profile of a track or poweranalysis data
QPixmap pm
plot pixel data
float titleheight
text size
ProfilSettings * s
assigned settings to be used
void refresh_pixmap()
repaints the plot's pixmap
double * ydata
profile data
void set_data(double **dataxy, int count)
setting one dataset and repaint plot
void init_s0()
initialize intrinsic settings
double ydatatoy(double y)
transform data value to pic-px
double * ydata1
linedata red
void settings(ProfilSettings *s)
asign settings to plot
void add_grid(QPainter *p)
rendering subfunction