patterncppMinor
Qt Custom Scatterplot
Viewed 0 times
customscatterplotstackoverflow
Problem
I have created a custom scatterplot in Qt and am wondering if there is a more efficient way to do it. Here is a sketch of the problem:
The layout of the plotting area consists of a rectangular structure starting at [0,0] in the top left corner and expands by 250 pixels to the bottom right corner [250,250]. So the y-coordinate grows downwards and the x-coordinate to the right.
The task consists of mapping two vectors
To solve this problem I choose the following approach (pseudo code):
-
transform A and B by:
and the implementation:
.h file
.cpp
main.cpp
```
#include "painter.h"
#include
int main(int arg
The layout of the plotting area consists of a rectangular structure starting at [0,0] in the top left corner and expands by 250 pixels to the bottom right corner [250,250]. So the y-coordinate grows downwards and the x-coordinate to the right.
The task consists of mapping two vectors
(A[i],B[i]) holding numerical data onto this rectangular structure such that the lowest x,y coordinates are in the bottom left corner and the highest x,y coordinates are in the top right corner.To solve this problem I choose the following approach (pseudo code):
- calculate the min and max for (A and B);
- calculate the spread (max - min) for (A and B);
-
transform A and B by:
foreach (i in A) {
(i - min(A) / spread(a))* 250
}
foreach(j in B) {
(250 - ((j - min(B) / spread(B))*250
}and the implementation:
.h file
#include
#include
#include
#include
#include
#include
#include
#include
namespace Ui {
class paintEr;
}
class paintEr : public QDialog
{
Q_OBJECT
public:
explicit paintEr(QWidget *parent = 0);
~paintEr();
private slots:
void addPoint();
private:
Ui::paintEr *ui;
QGraphicsScene *scatter;
protected:
void paintEvent(QPaintEvent *event);
};.cpp
#include "painter.h"
#include "ui_painter.h"
paintEr::paintEr(QWidget *parent) :
QDialog(parent),
ui(new Ui::paintEr)
{
ui->setupUi(this);
}
void paintEr::paintEvent(QPaintEvent *event){
scatter = new QGraphicsScene(this);
ui->graphicsView->setSceneRect(0,0,250,250);
ui->graphicsView->setScene(scatter);
}
void paintEr::addPoint(){
QPen greenPen(Qt::green);
QBrush greenBrush(Qt::green);
QVector a,b,c,d;
a addEllipse(c[i], d[i], 5, 5, greenPen, greenBrush);
}
}
paintEr::~paintEr()
{
delete ui;
}main.cpp
```
#include "painter.h"
#include
int main(int arg
Solution
You should separate the actual data from the logic. Make your function capable of working on any
Variable names
You should be able to get both the min and max with one pass over the list. You'll probably have to write a custom mapreduce-style function.
For the circles in the
QVectorq of QPairs (or pair of QVectors but that's not as normalized).Variable names
a,b,c,d don't make sense. Especially since a and b really ought to be input parameters.You should be able to get both the min and max with one pass over the list. You'll probably have to write a custom mapreduce-style function.
For the circles in the
QGraphicsScene: Are they supposed to have their center at the point in question, or the topleft of their bounding rect?Context
StackExchange Code Review Q#84483, answer score: 3
Revisions (0)
No revisions yet.