Debian Codegraph @

avogadro/qtgui/chartwidget.h

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #ifndef AVOGADRO_QTGUI_CHARTWIDGET_H
7 #define AVOGADRO_QTGUI_CHARTWIDGET_H
8
9 #include "avogadroqtguiexport.h"
10
11 #include <QWidget>
12
13 #include <array>
14 #include <string>
15
16 namespace Avogadro::QtGui {
17
18 /**
19 * @brief A color with 4 unsigned bytes (RGBA)
20 */
21 typedef std::array<unsigned char, 4> color4ub;
22
23 /**
24 * @class ChartWidget chartwidget.h <avogadro/qtgui/chartwidget.h>
25 * @brief A Qt widget for displaying charts via JKQtPlotter
26 *
27 * A wrapper around the JKQtPlotter library, providing a widget
28 * for displaying line charts, with multiple series, configurable
29 * axes, and a legend, etc.
30 */
31 class AVOGADROQTGUI_EXPORT ChartWidget : public QWidget
32 {
33 Q_OBJECT
34
35 public:
36 explicit ChartWidget(QWidget* p = nullptr);
37 ~ChartWidget() override;
38
39 /**
40 * @brief Axis enumeration
41 */
42 enum class Axis
43 {
44 x,
45 y
46 };
47
48 /**
49 * @brief Legend location enumeration, relative to the plot
50 */
51 enum class LegendLocation
52 {
53 None,
54 TopLeft,
55 TopRight,
56 BottomLeft,
57 BottomRight
58 };
59
60 /**
61 * @brief Add a plot to the chart
62 * @param x The x values
63 * @param y The y values
64 * @param color The color of the line
65 * @param xName The name of the x axis
66 * @param yName The name of the y axis
67 * @return True if successful
68 */
69 bool addPlot(const std::vector<float>& x, const std::vector<float>& y,
70 const color4ub& color = color4ub{ 0, 0, 0, 255 },
71 const QString& xName = "x", const QString& yName = "y");
72
73 /**
74 * @brief Add a series to an existing chart
75 * @param y The y values
76 * @param color The color of the line
77 * @param name The name of the series
78 * @return True if successful
79 */
80 bool addSeries(const std::vector<float>& y,
81 const color4ub& color = color4ub{ 0, 0, 0, 255 },
82 const QString& name = "");
83
84 /**
85 * @brief Add multiple lines to the chart
86 * @param plotData The data for the plots, column[0] is x, rest are y
87 * @param color The color of the lines
88 * @param names The names of the lines (including for the x axis)
89 * @return True if successful
90 */
91 bool addPlots(const std::vector<std::vector<float>>& plotData,
92 const color4ub& color = color4ub{ 0, 0, 0, 255 },
93 const QStringList& names = {});
94
95 /**
96 * @brief Clear all plots and data
97 */
98 void clearPlots();
99
100 /**
101 * @brief Set the title of the x axis
102 * @param title The title
103 */
104 void setXAxisTitle(const QString& title);
105
106 /**
107 * @brief Set the title of the y axis
108 * @param title The title
109 */
110 void setYAxisTitle(const QString& title);
111
112 /**
113 * @brief Set the custom tick positions and labels for an axis
114 * @param a The axis
115 * @param tickPositions The tick positions
116 * @param tickLabels The tick labels
117 * @return True if successful
118 */
119 void setTickLabels(Axis a, const std::vector<float>& tickPositions,
120 const QStringList& tickLabels);
121
122 /**
123 * @brief Set the default axis limits
124 * @param a The axis
125 * @param min The minimum value
126 * @param max The maximum value
127 * @return True if successful
128 *
129 * If minimum is bigger than maximum, the axis will be reversed
130 */
131 void setAxisLimits(Axis a, float min, float max);
132
133 /**
134 * @brief Set the default x-axis limits
135 * @param min The minimum value
136 * @param max The maximum value
137 * @return True if successful
138 *
139 * If minimum is bigger than maximum, the axis will be reversed
140 */
141 void setXAxisLimits(float min, float max);
142 /**
143 * @brief Set the default y-axis limits
144 * @param min The minimum value
145 * @param max The maximum value
146 * @return True if successful
147 *
148 * If minimum is bigger than maximum, the axis will be reversed
149 */
150 void setYAxisLimits(float min, float max);
151
152 /**
153 * @brief Set the font size for the ticks and axis titles
154 * @param size The font size
155 * @return True if successful
156 */
157 void setFontSize(int size = 14);
158 /**
159 * @brief Set the line width of the graph lines (in pt)
160 * @param width The line width
161 * @return True if successful
162 */
163 void setLineWidth(float width = 1.0);
164
165 /**
166 * @brief Set to logarithmic scale
167 * @param a The axis
168 * @param logScale True for log scale, false for linear
169 * @return True if successful
170 */
171 void setAxisLogScale(Axis a, bool logScale);
172
173 /**
174 * @brief Set the number of digits to display in tick labels
175 * @param a The axis
176 * @param digits The number of digits
177 * @return True if successful
178 */
179 void setAxisDigits(Axis a, int digits = 2);
180
181 /**
182 * @brief Label peaks in the plot
183 * @param yColumn The column of the y values
184 * @param threshold The threshold for peak detection
185 * @param windowSize The window size for peak detection (in samples along the
186 * x-axis)
187 */
188 void labelPeaks(int yColumn = 1, float threshold = 1.0, int windowSize = 5);
189
190 /**
191 * @brief Set the color of an axis
192 * @param a The axis
193 * @param color The color
194 */
195 void setAxisColor(Axis a, const color4ub& color);
196 /**
197 * @brief Set the color of the plot area
198 * @param color The color
199 */
200 void setBackgroundColor(const color4ub& color);
201
202 /**
203 * @brief Set the location of the legend
204 * @param location The location
205 */
206 void setLegendLocation(LegendLocation location);
207
208 /**
209 * @brief Set the visibility of data points
210 * @param visible True to show data points, false to hide them
211 */
212 void setShowPoints(bool visible) { m_showPoints = visible; }
213
214 protected:
215 void changeEvent(QEvent* event) override;
216
217 signals:
218 /**
219 * @brief Emitted when the user single-clicks on the chart
220 */
221 void clicked(float x, float y, Qt::KeyboardModifiers modifiers);
222
223 public slots:
224 /**
225 * @brief register a callback for when the user clicks on the chart
226 */
227 void plotClicked(double x, double y, Qt::KeyboardModifiers modifiers,
228 Qt::MouseButton button);
229
230 /** @brief Reset the zoom level of the plot (auto-scale to data)
231 */
232 void resetZoom();
233
234 private:
235 void renderViews();
236 float m_lineWidth = 1.0;
237 bool m_showPoints = false;
238
239 // private members
240 class ChartWidgetImpl;
241 ChartWidgetImpl* m_impl;
242 };
243
244 } // namespace Avogadro::QtGui
245
246 #endif // AVOGADRO_QTGUI_CHARTWIDGET_H