5 #pragma region Includes
9 #include "QCPExtended.h"
13 #include "QCPAxisExtended.h"
14 #include "ColorPicker.h"
15 #include "Utilities.h"
18 namespace Util = Utilities;
20 namespace DataVisualizerGUI {
26 template<
class T = DataFile>
28 #pragma region Constants
29 static const float ScatterDotSize;
33 static const float BoldPenWidth;
43 Graph(QWidget* parent);
55 void InitializeGraph(
const QRect& rect,
56 string xAxisLabel,
string yAxisLabel);
68 void AddPlot(T& data,
int upper = 0);
77 #pragma region Draw Methods
96 bool IsZoomed()
const;
111 void ClearFullViewport();
113 #pragma region Accessors
120 list<PlotData*>* GetData();
125 double GetXLower()
const;
130 double GetXUpper()
const;
135 double GetYLower()
const;
140 double GetYUpper()
const;
147 void SetXLower(
double val);
154 void SetXUpper(
double val);
161 void SetYLower(
double val);
168 void SetYUpper(
double val);
173 double GetFullXLower()
const;
178 double GetFullXUpper()
const;
183 double GetFullYLower()
const;
188 double GetFullYUpper()
const;
193 void SetFullXLower(
double val);
198 void SetFullXUpper(
double val);
203 void SetFullYLower(
double val);
208 void SetFullYUpper(
double val);
213 void SetXAxis(
double lower,
double upper);
218 void SetYAxis(
double lower,
double upper);
223 void SetXLabel(
string label);
228 void SetYLabel(
string label);
234 void SetAutomaticResize(
bool automaticallyResizes);
239 void SetBoldfacedSelect(
bool isBold);
241 #pragma region Plot Data and Drawing: Methods
251 virtual void ProcessPoints(T& data,
int upper = 0) = 0;
256 void RegisterPlot(
PlotData* plotData);
264 #pragma region User Events: Methods
268 void mousePressEvent(QMouseEvent *event);
273 void mouseMoveEvent(QMouseEvent *event);
278 void mouseReleaseEvent(QMouseEvent* event);
283 void wheelEvent(QWheelEvent *event);
288 void keyPressEvent(QKeyEvent* event);
293 void keyReleaseEvent(QKeyEvent* event);
298 QRect BoundingArea()
const;
300 #pragma region Plot Data and Drawing: Members
307 #pragma region Zooming with Mouse Selection: Members
312 #pragma region Plot Axises: Members
318 #pragma region User Events: Members
338 this->
Data_ =
new list<PlotData*>();
349 this->FullXLower_ = 0;
350 this->FullXUpper_ = 0;
351 this->FullYLower_ = 0;
352 this->FullYUpper_ = 0;
355 this->
SelectionBox_ =
new QRubberBand(QRubberBand::Rectangle,
this);
368 this->setFocusPolicy(Qt::FocusPolicy::StrongFocus);
378 for(
auto i = this->Data_->begin(),
379 j = this->Data_->end(); i != j; i++) {
383 this->Data_->clear();
397 this->PlottableList_.clear();
400 if(this->SelectionBox_ != NULL) {
401 delete this->SelectionBox_;
402 this->SelectionBox_ = NULL;
408 string xAxisLabel,
string yAxisLabel) {
411 this->setGeometry(rect);
414 this->SetXLabel(xAxisLabel);
415 this->SetYLabel(yAxisLabel);
425 this->ClearFullViewport();
428 this->ProcessPoints(data, upper);
435 this->Data_->remove(dataSet);
445 this->clearPlottables();
448 this->PlottableList_.clear();
451 this->setInteraction(QCustomPlot::iSelectPlottables);
457 this->xAxis->setLabel(this->XAxisLabel_.c_str());
458 this->yAxis->setLabel(this->YAxisLabel_.c_str());
461 this->xAxis->setRange(this->XLower_, this->XUpper_);
462 this->yAxis->setRange(this->YLower_, this->YUpper_);
465 if(this->RescaleAxis_)
477 this->xAxisEX_->SetIsZoomed(isZoomed = this->IsZoomed());
483 QScrollBar* scrollBar = this->xAxisEX_->GetScrollBar();
486 double viewRange = (this->XUpper_ - this->XLower_);
487 double fullRange = (this->FullXUpper_ - this->FullXLower_);
490 scrollBar->setRange(0, (
int)ceil(this->xAxis->coordToPixel(fullRange - viewRange)));
494 int sliderPos = (int)ceil(this->xAxis->coordToPixel(this->XLower_ - this->FullXLower_));
497 if((sliderPos <= scrollBar->maximum())
498 && (sliderPos >= scrollBar->minimum()))
499 scrollBar->setSliderPosition(sliderPos);
503 QCustomPlot::replot();
510 this->xAxis->setRange(this->XLower_ = this->FullXLower_,
511 this->XUpper_ = this->FullXUpper_);
512 this->yAxis->setRange(this->YLower_ = this->FullYLower_,
513 this->YUpper_ = this->FullYUpper_);
524 for(
auto i = this->Data_->begin(),
525 j = this->Data_->end(); i != j; i++) {
533 this->Data_->clear();
538 this->FullXLower_ = 0;
539 this->FullXUpper_ = 0;
540 this->FullYLower_ = 0;
541 this->FullYUpper_ = 0;
546 if((this->FullXLower_ != this->XLower_)
547 || (this->FullXUpper_ != this->XUpper_)
548 || (this->FullYLower_ != this->YLower_)
549 || (this->FullYUpper_ != this->YUpper_))
561 double xLower(0), xUpper(0), yLower(0), yUpper(0);
564 for(
auto i = this->Data_->begin(),
565 j = this->Data_->end(); i != j; i++) {
568 double iXLower = (*i)->GetXLower();
569 double iXUpper = (*i)->GetXUpper();
570 double iYLower = (*i)->GetYLower();
571 double iYUpper = (*i)->GetYUpper();
574 xLower = (xLower > iXLower) ? iXLower : xLower;
575 xUpper = (xUpper < iXUpper) ? iXUpper : xUpper;
576 yLower = (yLower > iYLower) ? iYLower : yLower;
577 yUpper = (yUpper < iYUpper) ? iYUpper : yUpper;
581 this->FullXLower_ = xLower;
582 this->FullXUpper_ = xUpper;
583 this->FullYLower_ = yLower;
584 this->FullYUpper_ = yUpper;
594 return this->XLower_;
599 return this->XUpper_;
604 return this->YLower_;
609 return this->YUpper_;
634 return this->FullXLower_;
638 double Graph<T>::GetFullXUpper()
const {
639 return this->FullXUpper_;
643 double Graph<T>::GetFullYLower()
const {
644 return this->FullYLower_;
648 double Graph<T>::GetFullYUpper()
const {
649 return this->FullYUpper_;
653 void Graph<T>::SetFullXLower(
double val) {
654 this->FullXLower_ = val;
658 void Graph<T>::SetFullXUpper(
double val) {
659 this->FullXUpper_ = val;
663 void Graph<T>::SetFullYLower(
double val) {
664 this->FullYLower_ = val;
668 void Graph<T>::SetFullYUpper(
double val) {
669 this->FullYUpper_ = val;
674 this->xAxis->setRange(lower, upper);
675 this->FullXLower_ = lower;
676 this->FullXUpper_ = upper;
681 this->yAxis->setRange(lower, upper);
682 this->FullYLower_ = lower;
683 this->FullYUpper_ = upper;
688 this->XAxisLabel_ = label;
693 this->YAxisLabel_ = label;
698 this->RescaleAxis_ = automaticallyResizes;
705 this->GetData()->push_back(plotData);
718 this->ColorPicker_.Reset();
721 int numOfNamedPlots = 0;
724 for(
auto i = this->Data_->begin(),
725 j = this->Data_->end(); i != j; i++) {
734 QPen drawPen = QPen(plotColor);
737 if(this->IsScatterPlot_) {
744 this->addPlottable(ScatterGraph);
745 ScatterGraph->
setData((*i)->X, (*i)->Y);
748 drawPen.setWidthF(((*i)->GetSelected()) ? BoldPenWidth : this->DefaultPenWidth_);
749 ScatterGraph->
setPen(drawPen);
756 tempPlottable = ScatterGraph;
765 this->addPlottable(CurveGraph);
766 CurveGraph->
setData((*i)->X, (*i)->Y);
769 drawPen.setWidthF(((*i)->GetSelected()) ? BoldPenWidth : this->DefaultPenWidth_);
770 CurveGraph->
setPen(drawPen);
774 tempPlottable = CurveGraph;
778 if((*i)->Name !=
"") {
781 tempPlottable->
setName((*i)->Name);
793 this->legend->setVisible((numOfNamedPlots > 0) ?
true :
false);
798 this->IsSelectBoldfaced_ = isBold;
805 QRect plotGeo = this->geometry();
806 return QRect(this->marginLeft(), this->marginTop(),
808 (plotGeo.height() - this->marginTop() - this->marginBottom()));
815 if(event->button() == Qt::MouseButton::LeftButton) {
818 this->Origin_ =
event->pos();
821 if(Util::CheckBoundaries(this->Origin_, this->BoundingArea())) {
822 this->SelectionBox_->setGeometry(QRect(this->Origin_, QSize()));
823 this->SelectionBox_->show();
826 this->IsMouseZooming_ =
true;
830 this->mDragging =
true;
835 if(event->button() == Qt::MouseButton::RightButton) {
842 QWidget::mousePressEvent(event);
849 emit mouseMove(event);
852 if(this->mDragging) {
855 if(this->IsMouseZooming_) {
858 QPoint currPos =
event->pos();
861 QRect boundingArea = this->BoundingArea();
864 this->SelectionBox_->setGeometry(QRect(this->Origin_,
865 Util::CheckPoint(currPos, boundingArea)).normalized());
871 if(this->IsPlotGrabbed_ && this->SelectedPlotData_) {
874 QPoint mp =
event->pos();
877 double xOffset = this->xAxis->pixelToCoord(mp.x())
878 - this->xAxis->pixelToCoord(this->Origin_.x());
879 double yOffset = this->yAxis->pixelToCoord(mp.y())
880 - this->yAxis->pixelToCoord(this->Origin_.y());
883 this->SelectedPlotData_->SetXYOffset(this->SelectedPlotData_->GetXOffset()
884 + xOffset, this->SelectedPlotData_->GetYOffset() + yOffset);
894 this->Origin_ =
event->pos();
899 QWidget::mouseMoveEvent(event);
906 if(event->button() == Qt::MouseButton::LeftButton) {
909 if(this->IsMouseZooming_) {
912 QRect selectArea = this->SelectionBox_->geometry();
915 this->XLower_ = this->xAxis->pixelToCoord(selectArea.x());
916 this->XUpper_ = this->xAxis->pixelToCoord(selectArea.x() + selectArea.width());
919 this->YLower_ = this->yAxis->pixelToCoord(selectArea.y());
920 this->YUpper_ = this->yAxis->pixelToCoord(selectArea.y() + selectArea.height());
926 this->SelectionBox_->hide();
929 this->IsMouseZooming_ =
false;
933 this->IsPlotGrabbed_ =
false;
936 this->mDragging =
false;
942 QWidget::mouseReleaseEvent(event);
947 emit mouseWheel(event);
950 QWidget::wheelEvent(event);
957 QWidget::keyPressEvent(event);
964 QWidget::keyReleaseEvent(event);
967 #endif // !__Graph_H__
QCPAbstractPlottable * SelectedPlot_
Pointer to a previously selected plot.
Definition: Graph.h:319
string XAxisLabel_
The label used to identify values on the x-axis.
Definition: Graph.h:315
Colors are picked from a special list of colors.
Definition: ColorPicker.h:31
ColorPicker ColorPicker_
Object used to choose plot colors from a preset list of colors.
Definition: Graph.h:303
bool IsSelectBoldfaced_
Indicates if selected plots should be drawn with boldface.
Definition: Graph.h:323
void mouseReleaseEvent(QMouseEvent *event)
Override the mouse release event to incorporate extra behavior.
Definition: Graph.h:903
list< PlotData * > * GetData()
Returns the pointer to the plot data.
Definition: Graph.h:588
The abstract base class for all data representing objects in a plot.
Definition: qcustomplot.h:357
map< QCPAbstractPlottable *, PlotData * > PlottableList_
List of the plots mapped with their data.
Definition: Graph.h:302
void Clear()
Safely clears all the data from the graph.
Definition: Graph.h:521
Extends the axis class to allow scrolling when zoomed.
Definition: QCPAxisExtended.h:16
void Refresh()
Clears and redraws the graph.
Definition: Graph.h:442
void SetAutomaticResize(bool automaticallyResizes)
Set or get the ability for the graph to resize itself along with changes in data. ...
Definition: Graph.h:697
bool IsPlotGrabbed_
Indicates that a plot is currently being selected.
Definition: Graph.h:321
void ShiftViewport(int value)
Shifts the viewport of the graph along the x axis.
Definition: QCPExtended.cpp:14
bool IsScatterPlot_
Changes the style of plot between curve and scatter.
Definition: Graph.h:305
void setData(QCPDataMap *data, bool copy=false)
Replaces the current data with the provided data.
Definition: qcustomplot.cpp:378
void AdjustRange()
Calculates the new bounds after a plot has been changed.
Definition: Graph.h:555
void setData(QCPCurveDataMap *data, bool copy=false)
Replaces the current data with the provided data.
Definition: qcustomplot.cpp:9250
void keyPressEvent(QKeyEvent *event)
Override the key press event to handle special behavior.
Definition: Graph.h:954
QRubberBand * SelectionBox_
Object used to visually identify the area the user selects.
Definition: Graph.h:308
double GetXLower() const
Returns the lower bound on the x-axis.
Definition: Graph.h:593
void setScatterSize(double size)
This defines how big (in pixels) single scatters are drawn, if scatter style (setScatterStyle) isn't ...
Definition: qcustomplot.cpp:607
QCPAxisExtended * xAxisEX_
Extended x-axis object with scrollbar.
Definition: Graph.h:313
QCPAxisExtended * yAxisEX_
Extended y-axis object with scrollbar.
Definition: Graph.h:314
void setLineStyle(LineStyle ls)
Sets how the single data points are connected in the plot or how they are represented visually apart ...
Definition: qcustomplot.cpp:584
double GetYLower() const
Returns the lower bound on the y-axis.
Definition: Graph.h:603
list< PlotData * > * Data_
List of data sets for individual plots.
Definition: Graph.h:301
void AddPlot(T &data, int upper=0)
Processes and stores points when called.
Definition: Graph.h:422
bool RescaleAxis_
Flag for indicating if the graph can resize with the change in data points.
Definition: Graph.h:304
string YAxisLabel_
The label used to identify values on the y-axis.
Definition: Graph.h:316
void SetYUpper(double val)
Assigns the y-axis' upper bound with a new value.
Definition: Graph.h:628
void setScatterStyle(QCP::ScatterStyle ss)
Sets the visual appearance of single data points in the plot.
Definition: qcustomplot.cpp:595
void InitializeGraph(const QRect &rect, string xAxisLabel, string yAxisLabel)
Sets up the dimensions and labels for graph.
Definition: Graph.h:407
void SetYLabel(string label)
Sets the labels for the y-axis.
Definition: Graph.h:692
void ResetView()
Resets the view port to the maximum viewable range.
Definition: Graph.h:507
QRect BoundingArea() const
Returns the bounding area of plot view area.
Definition: Graph.h:802
A plottable representing a parametric curve in a plot.
Definition: qcustomplot.h:580
double GetYUpper() const
Returns the upper bound on the y-axis.
Definition: Graph.h:608
bool IsMouseZooming_
Indicates if the user is making an area selection.
Definition: Graph.h:310
~Graph()
Destructor.
Definition: Graph.h:375
void SetYAxis(double lower, double upper)
Sets the full y-axis range that can be returned to at any time.
Definition: Graph.h:680
void RemovePlot(PlotData *dataSet)
Removes a specific data set from the graph.
Definition: Graph.h:432
void mousePressEvent(QMouseEvent *event)
Override the mouse press event to incorporate extra behavior.
Definition: Graph.h:812
QScrollBar * GetScrollBar()
Returns the reference to the scroll bar.
Definition: QCPAxisExtended.cpp:25
Extension of the QCustomPlot class that allows an implementation of the class with different event ha...
Definition: QCPExtended.h:17
void SetXUpper(double val)
Assigns the x-axis' upper bound with a new value.
Definition: Graph.h:618
void SetXLabel(string label)
Sets the labels for the x-axis.
Definition: Graph.h:687
void mouseMoveEvent(QMouseEvent *event)
Override the original mouse event to handle a different behavior.
Definition: Graph.h:846
void setPen(const QPen &pen)
The pen is used to draw basic lines that make up the plottable representation in the plot...
Definition: qcustomplot.cpp:8430
Structure for storing data for individual curves.
Definition: PlotData.h:15
void ClearFullViewport()
Wipes the full view port bounds.
Definition: Graph.h:537
A plottable representing a graph in a plot.
Definition: qcustomplot.h:447
void SetYLower(double val)
Assigns the y-axis' lower bound with a new value.
Definition: Graph.h:623
PlotData * SelectedPlotData_
Pointer to the plot data of a selected plot.
Definition: Graph.h:320
void wheelEvent(QWheelEvent *event)
Override the scroll wheel event.
Definition: Graph.h:946
void SetBoldfacedSelect(bool isBold)
Set if a plot will be boldfaced when selected.
Definition: Graph.h:797
Allows the drawing of a color from a list of presets.
Definition: ColorPicker.h:12
double GetXUpper() const
Returns the upper bound on the x-axis.
Definition: Graph.h:598
void replot()
Replot the graph area taking into account user interactions, such as zooming.
Definition: Graph.h:473
void SetXAxis(double lower, double upper)
Sets the full x-axis range that can be returned to at any time.
Definition: Graph.h:673
void AddPlots()
Adds a new graph item to the graph list.
Definition: Graph.h:715
void RegisterPlot(PlotData *plotData)
Stores new ranges, resets the plots, and stores new data into graph list.
Definition: Graph.h:702
QPoint Origin_
Point in the viewport that the user first clicks.
Definition: Graph.h:309
void keyReleaseEvent(QKeyEvent *event)
Override the key release event to handle special behavior.
Definition: Graph.h:961
a circle which is filled with the color of the pen (not the brush!)
Definition: qcustomplot.h:86
Handles the basic requirements for handling data and drawing said data to plots and curves...
Definition: Graph.h:27
void setName(const QString &name)
The name is the textual representation of this plottable as it is displayed in the QCPLegend of the p...
Definition: qcustomplot.cpp:8382
float DefaultPenWidth_
Stores the default pen width.
Definition: Graph.h:322
void SetXLower(double val)
Assigns the x-axis' lower bound with a new value.
Definition: Graph.h:613
bool IsZoomed() const
Returns a state indicating if the viewport is zoomed or not.
Definition: Graph.h:545