-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomplot.cpp
143 lines (122 loc) · 3.6 KB
/
customplot.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
#include "customplot.h"
#include "mainwindow.h"
#include <cstdlib>
CustomPlot::CustomPlot(QWidget* parent) : QCustomPlot(parent)
{
xAxis->setLabel("x");
yAxis->setLabel("y");
}
QColor CustomPlot::_color(int index)
{
return QColor(0x87 * index % 256, 0xf * index % 256, 0x57 * index % 256);
}
QCPGraph* CustomPlot::_newGraph(int index)
{
auto newGraph = addGraph();
auto color = _color(index);
newGraph->setLineStyle(QCPGraph::lsNone);
newGraph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, color, color, Settings::DataPointSize));
newGraph->setPen(QPen(color));
return newGraph;
}
QCPGraph* CustomPlot::_newPathGraph(int index, int size)
{
auto newGraph = addGraph();
auto color = _color(index);
newGraph->setLineStyle(QCPGraph::lsLine);
newGraph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssSquare, color, color, size));
newGraph->setPen(QPen(color));
return newGraph;
}
void CustomPlot::_fixAxes()
{
rescaleAxes();
_addAxisMargin(xAxis);
_addAxisMargin(yAxis);
}
void CustomPlot::_addAxisMargin(QCPAxis *axis)
{
auto range = axis->range();
range.lower -= 1;
range.upper += 1;
axis->setRange(range);
}
void CustomPlot::UpdatePlot()
{
int graphIndex = 0;
clearGraphs();
// create graph for each cluster
for (; graphIndex < model->K; ++graphIndex)
{
_newGraph(graphIndex);
}
// add points to graphs
for (int i = 0; i < model->Data.size(); ++i)
{
int cluster = model->Clusters[i];
auto currentGraph = graph(cluster);
auto point = model->Data[i];
currentGraph->addData(point.X(), point.Y());
}
// add paths
auto & groupPaths = model->GroupPaths;
// case with only one step
if (groupPaths.size() == 1)
{
for (int i = 0; i < model->K; ++i)
{
auto newGraph = _newPathGraph(i);
auto point = groupPaths[0][i];
newGraph->addData(point.X(), point.Y());
}
}
// with more than one point a graph is needed for every step
else if (groupPaths.size() > 1)
{
int i;
for (i = 0; i + 1< groupPaths.size(); ++i)
{
for (int j = 0; j < model->K; ++j)
{
auto newGraph = _newPathGraph(j);
auto point1 = groupPaths[i][j];
auto point2 = groupPaths[i + 1][j];
newGraph->addData(point1.X(), point1.Y());
newGraph->addData(point2.X(), point2.Y());
}
}
// make current centroids bigger
for (int j = 0; j < model->K; ++j)
{
auto newGraph = _newPathGraph(j, Settings::LastPathPointSize);
auto point = groupPaths[i][j];
newGraph->addData(point.X(), point.Y());
}
}
if (model->FixedAxes)
{
xAxis->setRange(model->rangeXMin, model->rangeXMax);
yAxis->setRange(model->rangeYMin, model->rangeYMax);
}
else
{
_fixAxes();
auto xRange = xAxis->range();
auto yRange = yAxis->range();
model->rangeXMin = xRange.lower;
model->rangeXMax = xRange.upper;
model->rangeYMin = yRange.lower;
model->rangeYMax = yRange.upper;
}
setAntialiasedElements(QCP::aeAll);
replot();
}
void CustomPlot::mousePressEvent(QMouseEvent *ev)
{
double x = xAxis->pixelToCoord(ev->x());
double y = yAxis->pixelToCoord(ev->y());
model->Data.push_back(DataPoint(x, y));
model->Clusters.push_back(rand() % model->K);
auto mainWindow = (MainWindow*) this->parent()->parent();
mainWindow->updatePlot();
}