/
dataplot.cpp
259 lines (222 loc) · 6.48 KB
/
dataplot.cpp
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* ---------------------------------------------------------------------------
** This software is furnished "as is", without technical
** support, and with no warranty, express or implied, as to its usefulness for
** any purpose.
**
** File Name: dataplot.cpp
**
** plots the data on a graph - aggralinx software
**
** Author: Michael W. Hoag
** Copyright Michael W. Hoag 2016
** Email: mike@ndtjames.com
** -------------------------------------------------------------------------*/
/*
* Window always stays at the back in Linux, check same in Windows?
*/
#include "dataplot.h"
/*
* Blind Constructor with no datapoints
* Mostly Sets Parameters
*/
DataPlot::DataPlot( QWidget* )
{
createClasses();
SetPlotParameters();
SetGridParameters();
SetCurveParameters();
SetRCurveParameters();
plotDataPoints<< QPointF (0.0,0.0);
}
/*
*Destructor makes sure everything is deleted
*/
DataPlot::~DataPlot()
{
//delete symbol creates a problem.
delete Curve;
delete Grid;
}
/*
* creates the new classes (allocates memory)
*/
void DataPlot::createClasses()
{
Curve = new QwtPlotCurve;
Grid = new QwtPlotGrid;
Symbol = new QwtSymbol( QwtSymbol::Ellipse,
QBrush( Qt::red ),
QPen( Qt::red, 2 ),
QSize( 8, 8 ) );
rCurve = new QwtPlotCurve;
rSymbol = new QwtSymbol(QwtSymbol::Ellipse,
QBrush( Qt::red ),
QPen( Qt::red, 2 ),
QSize( 8, 8 ));
}
/*
*loads the datapoint vector with data
*/
void DataPlot::createPoints(const QString& rawdata)
{
QString working = rawdata;
QString line = "";
QStringList datalines = rawdata.split("\n", QString::SkipEmptyParts);
plotDataPoints.clear();
foreach (line, datalines) {
if(!loadPlotDataPoints(line)) return;
}
displayGraph(plotDataPoints);
}
/*
* Draws the linear regression line
*/
void DataPlot::createRegLine(const QVector<QPointF>& in)
{
QVector<QPointF> regLineData;
Regression* analysis = new Regression();
for(qint64 i = 0; i < in.size(); ++i){
exp_data loaddata;
loaddata.x = in[i].x();
loaddata.y = in[i].y();
analysis->addData(loaddata);
}
regLineData << QPointF(0.0, analysis->offset())
<< QPointF(maxAD(), analysis->slope()*maxAD()
+ analysis->offset());
rCurve->setSamples( regLineData );
rCurve->attach( this );
/* Text output of lines parameters */
QString p = QString("Correlation Line:") + '\n' +
QString("Gain: ") + QString::number(analysis->slope(),'f', 4.4) + '\n' +
QString("Offset: ") + QString::number(analysis->offset(),'f', 4.3)+ '\n' +
QString("R Squared: ")+ QString::number(analysis->rsqu(),'f',4.4);
displayRegParameters(p);
rCurve->setLegendAttribute(QwtPlotCurve::LegendShowSymbol, false);
delete analysis;
}
/*
* Shows the graph
* Sets some parameters that are data dependant
*/
void DataPlot::displayGraph(const QVector<QPointF>& points)
{
setAxisScale(QwtPlot::yLeft, minAxisScale(), maxY(points));
setAxisScale(QwtPlot::xBottom, minAxisScale(), maxX(points));
Curve->setSamples( points );
Curve->attach(this);
createRegLine(plotDataPoints);
resize( 1200, 800 );
replot();
show();
setFocus();
}
/*
* set parameters for regression parameter text
*/
void DataPlot::displayRegParameters(const QString &in){
QwtPlotTextLabel* text_label = new QwtPlotTextLabel;
QwtText qwt_text(in);
QFont font = qwt_text.font();
font.setPointSize(14);
qwt_text.setFont(font);
qwt_text.setColor(Qt::black);
qwt_text.setRenderFlags( Qt::AlignLeft | Qt::AlignTop );
text_label->setText(qwt_text);
text_label->attach( this );
text_label->show();
}
/*
* loads datapoints from string
*/
bool DataPlot::loadPlotDataPoints(const QString& line)
{
bool success = false;
double dr = 0.0;
double dm = 0.0;
QString buffer;
QTextStream bufferstream(& buffer);
QStringList words = line.split(" ");
QStringList::iterator wordsiterator = words.end()-1;
buffer = *wordsiterator;
if(buffer.count('%') != 0){
buffer = buffer.remove('%');
bufferstream.setString(&buffer); //had to reset string in buffer in order to double result
bufferstream >> dm;
--wordsiterator;
buffer = *wordsiterator;
bufferstream.setString(&buffer); //reset string in buffer in order to get double output
bufferstream >> dr;
success = true;
}else{
QMessageBox::information(this,"Aggralinx",
tr("Data Not Entered!"));
}
plotDataPoints << QPointF(dr,dm);
return(success);
}
/*
* a constructor function to set general chart parameters
* */
void DataPlot::SetPlotParameters()
{
setTitle(tr("Aggralinx Regression"));
setAxisTitle( QwtPlot::xBottom, "Aggrameter Value");
setAxisTitle( QwtPlot::yLeft, "Moisture Value");
setCanvasBackground( Qt::white );
insertLegend( new QwtLegend() );
}
/*
* a constructor function to set general curve parameters
*/
void DataPlot::SetCurveParameters()
{
Curve->setTitle( "Data Points" );
Curve->setStyle(QwtPlotCurve::NoCurve);
Curve->setRenderHint( QwtPlotItem::RenderAntialiased, true );
Curve->setSymbol( Symbol );
}
/*
* a constructor function to set general grid parameters
*/
void DataPlot::SetGridParameters()
{
Grid->attach(this);
}
/*
* a constructor function to set general regression
* parameters
*/
void DataPlot::SetRCurveParameters()
{
rCurve->setStyle(QwtPlotCurve::Lines);
rCurve->setRenderHint( QwtPlotItem::RenderAntialiased, true );
}
/*
* to set scale of chart
*/
qreal DataPlot::maxY(const QVector<QPointF> &in)
{
qreal rvalue = 0.0;
qreal test = 0.0;
QVector<QPointF> buffer = in;
for(int i = 0; i<in.size();++i){
test = buffer[i].ry();
rvalue = qMax( test, rvalue);
}
return(rvalue);
}
/*
* to set scale of chart
*/
qreal DataPlot::maxX(const QVector<QPointF> &in)
{
qreal rvalue = 0.0;
qreal test = 0.0;
QVector<QPointF> buffer = in;
for(int i = 0; i<in.size();++i){
test = buffer[i].rx();
rvalue = qMax( test, rvalue);
}
return(rvalue);
}