-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPowerCurvePlot.cpp
More file actions
131 lines (114 loc) · 4.01 KB
/
PowerCurvePlot.cpp
File metadata and controls
131 lines (114 loc) · 4.01 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
#include "PowerCurvePlot.h"
#include <cmath>
PowerCurvePlot::PowerCurvePlot(QWidget *parent)
: QCustomPlot( parent )
{
init();
replot();
}
void PowerCurvePlot::init()
{
setBackground( QBrush( QColor( 0, 0, 0, 0 ) ) );
axisRect(0)->setBackground( QBrush( QColor( 255, 255, 255 ) ) );
axisRect(0)->setMargins( QMargins( 0, 0, 0, 0 ) );
xAxis->setPadding( 0 );
xAxis2->setPadding( 0 );
yAxis->setPadding( 0 );
yAxis2->setPadding( 0 );
// Font
QFont font = xAxis->labelFont();
#ifdef __APPLE__
font.setPointSize( 10 );
#endif
// Darkmode font color
if( palette().background().color().redF() < 0.5 )
{
xAxis->setLabelColor( Qt::white );
yAxis->setLabelColor( Qt::white );
yAxis2->setLabelColor( Qt::white );
xAxis->setTickLabelColor( Qt::white );
yAxis->setTickLabelColor( Qt::white );
yAxis2->setTickLabelColor( Qt::white );
xAxis->setTickPen( QPen( Qt::gray ) );
yAxis->setTickPen( QPen( Qt::gray ) );
yAxis2->setTickPen( QPen( Qt::gray ) );
xAxis->setSubTickPen( QPen( Qt::gray ) );
yAxis->setSubTickPen( QPen( Qt::gray ) );
yAxis2->setSubTickPen( QPen( Qt::gray ) );
legend->setTextColor( Qt::white );
axisRect(0)->setBackground( QColor( 64, 64, 64 ) );
}
// X Axis and grid - logarithmic scale
//xAxis->setScaleType( QCPAxis::stLogarithmic );
xAxis->setLabelFont( font );
xAxis->setTickLabelFont( font );
xAxis->grid()->setSubGridVisible( false );
xAxis->grid()->setPen( QPen( Qt::lightGray, 0, Qt::SolidLine ) );
xAxis->setTickLength( 0, 8 );
xAxis->setLabelPadding( 0 );
xAxis->setTickLabelPadding( 0 );
xAxis->setOffset( 2 );
// Y Axis left and grid
yAxis->setLabelFont( font );
yAxis->setTickLabelFont( font );
yAxis->grid()->setSubGridVisible( true );
yAxis->grid()->setPen( QPen( Qt::lightGray, 0, Qt::SolidLine ) );
yAxis->grid()->setSubGridPen( QPen( Qt::lightGray, 0, Qt::DotLine ) );
yAxis->setTickLength( 0, 8 );
yAxis->setSubTickLength( 0, 4 );
yAxis->setLabelPadding( 5 );
yAxis->setTickLabelPadding( 0 );
yAxis->setOffset( 2 );
}
void PowerCurvePlot::setData(QVector<QPair<double, double> > pwrCurve)
{
clearPlottables();
if( pwrCurve.empty() )
{
replot();
return;
}
double minPower = 0.0;
double maxPower = pwrCurve.at(0).second;
for( int i = 0; i < pwrCurve.size(); i++ )
{
if( pwrCurve.at(i).second > maxPower )
maxPower = pwrCurve.at(i).second;
}
// Add padding to power range
double powerRange = maxPower - minPower;
if( powerRange < 1 ) powerRange = 1;
maxPower += powerRange * 0.1;
// Create graph
QCPGraph *graph = addGraph();
graph->setPen( QPen( QColor( 255, 0, 0 ), 1 ) );
graph->setBrush( QBrush( QColor( 255, 0, 0, 30 ) ) );
// Add data points directly (no transformation needed)
QVector<double> xData, yData;
for( int i = 0; i < pwrCurve.size(); i++ )
{
xData.append( log1p( pwrCurve.at(i).first > 0 ? pwrCurve.at(i).first : 1 ) );
yData.append( pwrCurve.at(i).second );
}
graph->setData( xData, yData );
// Set axis ranges
xAxis->setRange( xData.first(), xData.last() );
yAxis->setRange( minPower, maxPower );
// Set axis labels
//xAxis->setLabel( tr( "Time" ) );
yAxis->setLabel( tr( "Power (W)" ) );
QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
xAxis->setTicker(textTicker);
for( int i = 0; i < pwrCurve.size(); i++ )
{
QString interval;
if( pwrCurve.at(i).first < 60 )
interval = QString::number( pwrCurve.at(i).first ) + "\nsec";
else if( pwrCurve.at(i).first < 3600 )
interval = QString::number( pwrCurve.at(i).first / 60 ) + "\nmin";
else
interval = QString::number( pwrCurve.at(i).first / 3600 ) + "\nh";
textTicker->addTick( xData.at(i), interval );
}
replot();
}