QTradingView 1.0.0
A high-performance charting library built with C++ and Qt.
Loading...
Searching...
No Matches
Chart.h
1/*
2 * Copyright (c) 2025 Dhruvan Gnanadhandayuthapani
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23#ifndef QTRADINGVIEW_CHART_H
24#define QTRADINGVIEW_CHART_H
25
26#include <QObject>
27#include <QWidget>
28#include <QRectF>
29#include <QTimer>
30#include <QPointF>
31#include <QEvent>
32#include <QMouseEvent>
33#include <QWheelEvent>
34#include <QKeyEvent>
35#include <QPaintEvent>
36#include <QResizeEvent>
37#include <vector>
38#include <memory>
39#include <QPinchGesture>
40
41#include "QTradingView/Pane.h"
42#include "QTradingView/ViewPort.h"
43#include "QTradingView/qtradingview_global.h"
44#include "QTradingView/style/ChartTheme.h"
45#include "renderer/AxisRenderer.h"
46#include "renderer/AxisRenderer.h"
47#include "renderer/GridRenderer.h"
48#include "renderer/CrosshairRenderer.h"
49
50class QPainter;
51
52namespace QTradingView {
53
73class QTRADINGVIEW_EXPORT Chart : public QWidget
74{
75 Q_OBJECT
76
77public:
82 explicit Chart(QWidget *parent = nullptr);
83
87 ~Chart() override;
88
94 std::shared_ptr<Pane> addPane(double heightRatio = 1.0);
95
100 void removePane(Pane* pane);
101
106 Pane* mainPane();
107
112 std::vector<Pane*> panes() const;
113
118 ViewPort& viewport();
119
124 void setTheme(const ChartTheme& theme);
125
130 const ChartTheme& theme() const;
131
135 void calculateLayout();
136
141 void pan(int indexDelta);
142
148 void zoom(int indexDelta, int anchorIndex);
149
153 void fitToData();
154
159 void showLastNPoints(int n);
160
164 void show();
165
170 void setCrosshairVisible(bool visible);
171
176 bool isCrosshairVisible() const;
177
182 void setCrosshairPosition(const QPointF& position);
183
188 QPointF crosshairPosition() const;
189
194 AxisRenderer& axis();
195
201
207
208protected:
209 void paintEvent(QPaintEvent* event) override;
210 void resizeEvent(QResizeEvent* event) override;
211 void wheelEvent(QWheelEvent* event) override;
212 void mousePressEvent(QMouseEvent* event) override;
213 void mouseMoveEvent(QMouseEvent* event) override;
214 void mouseReleaseEvent(QMouseEvent* event) override;
215 void mouseDoubleClickEvent(QMouseEvent* event) override;
216 void leaveEvent(QEvent* event) override;
217 bool event(QEvent* event) override;
218 void keyPressEvent(QKeyEvent *event) override;
219
220private:
221 // Gesture handling
222 void handlePinchGesture(QPinchGesture* gesture);
223 void handlePanGesture(QPanGesture* gesture);
224
225 // Render
226 void render(QPainter* painter);
227
228 // Axis region helpers
229 QRectF leftAxisRect() const;
230 QRectF rightAxisRect() const;
231 QRectF xAxisRect() const;
232
233 // Pane helpers
234 Pane* paneAtPosition(const QPointF& position) const;
235 int paneBorderAtPosition(const QPointF& position, double threshold = 5.0) const;
236
237 // Chart logic members
238 std::vector<std::shared_ptr<Pane>> m_panes;
239 ViewPort m_viewport;
240 ChartTheme m_theme;
241
242 // Mouse/interaction state
243 bool m_isPanning;
244 QPoint m_lastMousePos;
245 int m_lastMouseIndex;
246 double m_initialVisibleCount;
247 double m_wheelDeltaAccumulator = 0;
248#ifdef EMSCRIPTEN
249 static constexpr int WHEEL_THRESHOLD = 72;
250#else
251 static constexpr int WHEEL_THRESHOLD = 120;
252#endif
253
254 enum class DragMode { None, ChartPan, YAxisZoom, XAxisZoom, PaneResize };
255 DragMode m_dragMode;
256 Pane* m_dragPane;
257 double m_dragStartValue;
258 int m_resizingBorderIndex;
259 double m_minPaneHeight;
260
261 // Renderers
262 AxisRenderer m_axisRenderer;
263 GridRenderer m_gridRenderer;
264 CrosshairRenderer m_crosshairRenderer;
265
266 int m_leftAxisWidth;
267 int m_rightAxisWidth;
268 int m_xAxisHeight;
269
270 // Crosshair state
271 bool m_crosshairVisible;
272 QPointF m_crosshairPosition;
273
274 // Update throttling
275 QTimer m_updateTimer;
276 bool m_pendingUpdate = false;
277 static constexpr int UPDATE_INTERVAL_MS = 16; // ~60 FPS (1000/60 ≈ 16ms)
278 void setupUpdateTimer();
279 void onUpdateTimerTick();
280 void scheduleUpdate();
281};
282
283} // namespace QTradingView
284
285#endif // QTRADINGVIEW_CHART_H
Renders chart axes, ticks, and labels for QTradingView.
Definition AxisRenderer.h:63
Defines the color and font theme for chart rendering.
Definition ChartTheme.h:38
Top level chart container for QTradingView.
Definition Chart.h:74
CrosshairRenderer & crosshair()
Access the crosshair renderer for customization.
~Chart() override
Destroys the Chart object.
GridRenderer & grid()
Access the grid renderer for customization.
Renders crosshair lines and labels on the chart.
Definition CrosshairRenderer.h:49
Renders grid lines on the chart background.
Definition GridRenderer.h:44
Represents a chart pane that contains one or more data series.
Definition Pane.h:44
Manages the visible range and pixel mapping for chart bars.
Definition ViewPort.h:39