This repository was archived by the owner on Jan 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraphPanel.cpp
More file actions
136 lines (118 loc) · 4.13 KB
/
Copy pathGraphPanel.cpp
File metadata and controls
136 lines (118 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <wx/wx.h>
#include <ranges>
#include "GraphPanel.h"
#include "OsdSettings.h"
#include "wx/txtstrm.h"
GraphPanel::GraphPanel(wxWindow *parent, const wxSize size) : wxPanel(parent, wxID_ANY) {
// Set minimum size for the graph panel
// wxWindowBase::SetMinSize(size);
// wxWindowBase::SetMaxSize(size);
this->m_size = size.x;
this->m_currents.resize(size.x, 0);
this->m_voltages.resize(size.x, PowerDelivery::PD_NONE);
this->m_graph_style = settings.is_line_graph ? STYLE_LINE : STYLE_BAR;
// Set background color
//wxWindow::SetBackgroundColour(settings.color_bg);
wxWindow::SetBackgroundColour(wxColour(0, 0, 0));
// Bind paint event
Bind(wxEVT_PAINT, &GraphPanel::OnPaint, this);
}
void GraphPanel::add(const int current, const PowerDelivery::PD_VOLTS voltage) {
if (this->m_currents.size() != this->m_size) {
this->m_currents.resize(this->m_size);
this->m_voltages.resize(this->m_size);
}
if (voltage == PowerDelivery::PD_NONE) {
return;
}
this->m_currents.pop_front();
this->m_currents.push_back(current);
this->m_voltages.pop_front();
this->m_voltages.push_back(voltage);
this->m_current_max = maxDequeueItem(this->m_currents);
if (this->m_current_max >= 500) {
this->m_maxBarValue = (this->m_current_max / 1000 + 1) * 1000;
} else if (this->m_current_max >= 250) {
this->m_maxBarValue = 500;
} else if (this->m_current_max >= 100) {
this->m_maxBarValue = 250;
} else {
this->m_maxBarValue = 100;
}
int minCurrent = this->m_current_max;
for (const auto c: this->m_currents) {
if (c > 0 && c < minCurrent) {
minCurrent = c;
}
}
this->m_current_min = minCurrent;
if (IsShown()) {
Refresh(false);
}
Update();
}
void GraphPanel::SetGraphStyle(const graph_style_t style) {
this->m_graph_style = style;
if (IsShown()) {
Refresh(false);
}
}
void GraphPanel::GetMinMaxCurrent(int *min, int *max) const {
*min = this->m_current_min;
*max = this->m_current_max;
}
void GraphPanel::OnPaint(wxPaintEvent &event) {
wxPaintDC dc(this);
int width, height;
GetClientSize(&width, &height);
if (this->m_currents.size() != width) {
this->m_currents.resize(width);
this->m_voltages.resize(width);
this->m_size = width;
}
// Skip if no data or invalid dimensions
if (width <= 2 || height <= 2) {
std::cerr << "Bad client size " << width << "x" << height << std::endl;
return;
}
this->m_current_max = maxDequeueItem(this->m_currents);
wxPoint prevPoint;
for (int i = 0; i < this->m_size; ++i) {
auto pd_volts = this->m_voltages.at(i);
wxColour colour = settings.voltsRgb(pd_volts);
if (i > 1) {
auto pd_volts2 = this->m_voltages.at(i - 1);
if (pd_volts2 != pd_volts && pd_volts2 != PowerDelivery::PD_20V) {
colour = settings.voltsRgb(pd_volts2);
} else if (i < this->m_size - 1) {
pd_volts2 = this->m_voltages.at(i + 1);
if (pd_volts2 != pd_volts && pd_volts2 != PowerDelivery::PD_20V) {
colour = settings.voltsRgb(pd_volts2);
}
}
}
// Set both pen and brush for proper rectangle coloring
dc.SetPen(wxPen(colour, 1));
dc.SetBrush(wxBrush(colour)); // Add this line for filled rectangles
int barHeight = height * this->m_currents.at(i) / this->m_maxBarValue;
if (this->m_graph_style == STYLE_BAR) {
dc.DrawLine(i, height - barHeight, i, height);
//dc.DrawRectangle(i, height - barHeight, 1, barHeight);
} else {
// For lines, only pen is needed
if (i > 0) {
dc.DrawLine(prevPoint.x, prevPoint.y, i, height - barHeight);
}
prevPoint = wxPoint(i, height - barHeight);
}
}
}
int GraphPanel::maxDequeueItem(const std::deque<int> &deque) {
int max = 0;
for (auto current: deque) {
if (current > max) {
max = current;
}
}
return max;
}