GraphView.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #pragma once
  2. #include <QWidget>
  3. #include <QPaintEvent>
  4. #include <QPen>
  5. #include <QPainter>
  6. #include <vector>
  7. class GraphView;
  8. struct GraphSeries {
  9. std::vector<QPointF>* data;
  10. double minX, maxX;
  11. double minY, maxY;
  12. QString name;
  13. QColor color;
  14. int lineWidth = 2;
  15. double circleRadius = 1.0;
  16. enum class SeriesType{ Line, Dot, LineDot};
  17. SeriesType type;
  18. std::pair<std::vector<QPointF>::iterator, std::vector<QPointF>::iterator> getFirstLastIndexInSeriesThatsInBound(int minX, int maxX);
  19. };
  20. struct Bound {
  21. double minX = 0, minY = 0, maxX = 0, maxY = 0;
  22. enum class Change{MoveLeft, MoveRight, MoveUp, MoveDown, ZoomOut, ZoomIn, Reset};
  23. Bound() {}
  24. Bound(double minX, double maxX, double minY, double maxY):minX(minX), maxX(maxX), minY(minY), maxY(maxY){}
  25. void move(GraphView* widget,const Change& type, const double changePercentage = 0.1);
  26. };
  27. class GraphView :public QWidget
  28. {
  29. Q_OBJECT
  30. public:
  31. GraphView(QWidget* parent, QString title, Bound fixedBound);
  32. ~GraphView();
  33. QString title;
  34. Bound fixedBound;
  35. private:
  36. bool useFixedBound;
  37. void paintEvent(QPaintEvent* event) override;
  38. //Data
  39. std::vector<GraphSeries> graphSeriesVec;
  40. Bound totalBound;
  41. Bound actualBound = totalBound;
  42. double rangeGraphX = 0, rangeGraphY = 0;
  43. //Visualization
  44. QString xAxisNumbers[11];
  45. QString yAxisNumbers[11];
  46. double hueoffset;
  47. QPen linePen, rectPen, axisPen;
  48. public:
  49. void setUseFixedBound(bool value);
  50. void generateAndAddRandomLine();
  51. void addLine(std::vector<QPointF>& line);
  52. void addLine(std::vector<QPointF>& line, QColor color);
  53. void addDots(std::vector<QPointF>& dots);
  54. void addDots(std::vector<QPointF>& dots, QColor color);
  55. void calculateRangeXY();
  56. void generateAxisNumberStrings();
  57. private:
  58. void calculateTotalGraphBound();
  59. void GraphView::calculateMinMaxXY(GraphSeries& lgs);
  60. void addSeries(std::vector<QPointF>& line, QColor color, GraphSeries::SeriesType type);
  61. QColor generateNextColorForGraph();
  62. QPointF transformPoint(QPointF& point, double stregth_factorX, double stregth_factorY) const;
  63. bool inBoundX(QPointF& point);
  64. void keyPressEvent(QKeyEvent* event) override;
  65. void wheelEvent(QWheelEvent* event) override;
  66. };