plot.cpp 5 KB
Newer Older
Tobias WEBER's avatar
Tobias WEBER committed
1
2
3
4
5
6
7
8
/**
 * plotter
 * @author Tobias Weber <tweber@ill.fr>
 * @date 15-Jun-2018
 * @license see 'LICENSE' file
 */

#include "plot.h"
9

Tobias WEBER's avatar
Tobias WEBER committed
10
#include <QtWidgets/QGridLayout>
11
12
#include <QtWidgets/QFileDialog>
#include <QtWidgets/QMessageBox>
Tobias WEBER's avatar
Tobias WEBER committed
13
14
15
16
17


using t_real = t_real_dat;


18
Plotter::Plotter(QWidget *parent, QSettings* pSettings) : QWidget(parent), m_pSettings(pSettings)
Tobias WEBER's avatar
Tobias WEBER committed
19
20
21
22
23
24
{
	auto *pGrid = new QGridLayout(this);
	pGrid->setHorizontalSpacing(4);
	pGrid->setVerticalSpacing(4);
	pGrid->setContentsMargins(0,0,0,0);
	pGrid->addWidget(m_pPlotter, 0, 0, 1, 1);
25
26
27
28
29
30
31
32

	m_pPlotContextMenu->setTitle("Plot");
	m_pPlotContextMenu->addAction("Export to Gnuplot...", this, &Plotter::SaveGpl);
	m_pPlotContextMenu->addAction("Save as PDF...", this, &Plotter::SavePDF);

	m_pPlotter->setContextMenuPolicy(Qt::CustomContextMenu);
	connect(m_pPlotter, &QCustomPlot::customContextMenuRequested, this, &Plotter::ShowPlotContextMenu);

Tobias WEBER's avatar
Tobias WEBER committed
33
34
35
36
37
}


Plotter::~Plotter()
{
Tobias WEBER's avatar
Tobias WEBER committed
38
39
40
41
	Clear();
}


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
/**
 * show the context menu for the plotter
 */
void Plotter::ShowPlotContextMenu(const QPoint& pt)
{
	auto ptGlob = m_pPlotter->mapToGlobal(pt);
	ptGlob.setY(ptGlob.y() + 8);
	m_pPlotContextMenu->popup(ptGlob);
}


/**
 * save plot as PDF file
 */
void Plotter::SavePDF()
{
	QString dirLast = "";
	if(m_pSettings)
		m_pSettings->value("dir_pdf", "").toString();

	QString file = QFileDialog::getSaveFileName(this, "Save as PDF", dirLast, "PDF Files (*.pdf *.PDF)");
	if(file=="")
		return;

	if(!m_pPlotter->savePdf(file))
	{
		QMessageBox::critical(this, "PDF Export", "Could not save PDF file.");
		return;
	}

	if(m_pSettings)
		m_pSettings->setValue("dir_pdf", QFileInfo(file).path());
}


/**
 * export plot to Gnuplot
 */
void Plotter::SaveGpl()
{
	QString dirLast = "";
	if(m_pSettings)
		m_pSettings->value("dir_gpl", "").toString();

	QString file = QFileDialog::getSaveFileName(this, "Export to Gnuplot", dirLast, "Gnuplot Files (*.gpl *.GPL)");
	if(file=="")
		return;

Tobias WEBER's avatar
Tobias WEBER committed
90
	if(!m_dataset.SaveGpl(file.toStdString()))
91
92
93
94
95
96
97
98
99
100
	{
		QMessageBox::critical(this, "Gnuplot Export", "Could not save Gnuplot file.");
		return;
	}

	if(m_pSettings)
		m_pSettings->setValue("dir_gpl", QFileInfo(file).path());
}


Tobias WEBER's avatar
Tobias WEBER committed
101
102
103
void Plotter::Clear()
{
	m_pPlotter->clearGraphs();
Tobias WEBER's avatar
Tobias WEBER committed
104
	m_dataset.clear();
Tobias WEBER's avatar
Tobias WEBER committed
105
106
107
108
109
110
111
112
113
114
115
116
}


/**
 * show a dataset
 */
void Plotter::Plot(const Dataset &dataset)
{
	static const std::vector<unsigned> colors = {
		0xffff0000, 0xff0000ff, 0xff009900, 0xff000000,
	};

Tobias WEBER's avatar
Tobias WEBER committed
117
	Clear();
Tobias WEBER's avatar
Tobias WEBER committed
118
	m_dataset = dataset;
Tobias WEBER's avatar
Tobias WEBER committed
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

	t_real xmin = std::numeric_limits<t_real>::max();
	t_real xmax = -xmin;
	t_real ymin = std::numeric_limits<t_real>::max();
	t_real ymax = -xmin;

	bool labels_already_set = false;


	// iterate over (polarisation) channels
	for(std::size_t channel=0; channel<dataset.GetNumChannels(); ++channel)
	{
		const auto &data = dataset.GetChannel(channel);

		if(data.GetNumAxes()==0 || data.GetNumCounters()==0)
			continue;

		const auto &datx = data.GetAxis(0);
		const auto &daty = data.GetCounter(0);
		const auto &datyerr = data.GetCounterErrors(0);


		// graph
		auto *graph = m_pPlotter->addGraph();
		auto *graph_err = new QCPErrorBars(m_pPlotter->xAxis, m_pPlotter->yAxis);
		graph_err->setDataPlottable(graph);

		QPen pen = QPen(QColor(colors[channel % colors.size()]));
		QBrush brush(pen.color());
		t_real ptsize = 8;
		graph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, pen, brush, ptsize));
		graph->setPen(pen);
		graph_err->setSymbolGap(ptsize);


		// convert to qvector
		QVector<t_real> _datx, _daty, _datyerr;
		std::copy(datx.begin(), datx.end(), std::back_inserter(_datx));
		std::copy(daty.begin(), daty.end(), std::back_inserter(_daty));
		std::copy(datyerr.begin(), datyerr.end(), std::back_inserter(_datyerr));
	
		graph->setData(_datx, _daty);
		graph_err->setData(_datyerr);


		// ranges
		auto xminmax = std::minmax_element(datx.begin(), datx.end());
		auto yminmax = std::minmax_element(daty.begin(), daty.end());
		auto yerrminmax = std::minmax_element(datyerr.begin(), datyerr.end());

Tobias WEBER's avatar
Tobias WEBER committed
169
170
171
172
173
174
175
176
177
178
		if(xminmax.first != datx.end() && xminmax.second != datx.end())
		{
			xmin = std::min(*xminmax.first, xmin);
			xmax = std::max(*xminmax.second, xmax);
		}
		if(yminmax.first != daty.end() && yminmax.second != daty.end())
		{
			ymin = std::min(*yminmax.first - *yerrminmax.first, ymin);
			ymax = std::max(*yminmax.second + *yerrminmax.second, ymax);
		}
Tobias WEBER's avatar
Tobias WEBER committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195


		// labels
		if(!labels_already_set)
		{
			m_pPlotter->xAxis->setLabel(data.GetAxisName(channel).size() ? data.GetAxisName(channel).c_str() : "x");
			m_pPlotter->yAxis->setLabel("Counts");
			labels_already_set = true;
		}
	}


	m_pPlotter->xAxis->setRange(xmin, xmax);
	m_pPlotter->yAxis->setRange(ymin, ymax);

	m_pPlotter->replot();
}
Tobias WEBER's avatar
Tobias WEBER committed
196
197
198
199
200
201



// ----------------------------------------------------------------------------
// dock

202
203
PlotterDock::PlotterDock(QWidget* pParent, QSettings* pSettings)
	: QDockWidget(pParent), m_pPlot(std::make_unique<Plotter>(this, pSettings))
Tobias WEBER's avatar
Tobias WEBER committed
204
205
206
207
208
209
210
211
212
213
214
{
	this->setObjectName("plotter");
	this->setWindowTitle("Current Plot");
	this->setWidget(m_pPlot.get());
}

PlotterDock::~PlotterDock()
{
}

// ----------------------------------------------------------------------------