21 #include <QVBoxLayout>
23 #include <qcustomplot.h>
26 const QPair<double, double> default_xrange(-0.1, 0.1);
27 const QPair<double, double> default_yrange(0.0, 1.1);
29 QPair<double, double> xRangeForValue(
double value);
30 QPair<double, double> xRangeForValues(
double value1,
double value2);
31 QPair<double, double> xRangeForValues(
const QVector<double>& xvec);
32 QPair<double, double> yRangeForValues(
const QVector<double>& yvec);
33 double optimalBarWidth(
double xmin,
double xmax,
int nbars = 1);
39 , m_plot(new QCustomPlot)
42 , m_resetAction(new QAction(this))
45 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
50 m_label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
51 m_label->setStyleSheet(
"background-color:white;");
54 QVBoxLayout* mainLayout =
new QVBoxLayout;
55 mainLayout->setMargin(0);
56 mainLayout->setSpacing(0);
57 mainLayout->addWidget(
m_plot, 1);
59 setLayout(mainLayout);
61 setStyleSheet(
"background-color:white;");
102 }
catch (
const std::exception& ex) {
105 QString message = QString(
"Wrong parameters\n\n").append(QString::fromStdString(ex.what()));
116 QPoint point =
event->pos();
117 double xPos =
m_plot->xAxis->pixelToCoord(point.x());
118 double yPos =
m_plot->yAxis->pixelToCoord(point.y());
120 if (
m_plot->xAxis->range().contains(xPos) &&
m_plot->yAxis->range().contains(yPos)) {
121 QString text = QString(
"[x:%1, y:%2]").arg(xPos).arg(yPos);
128 if (event->button() == Qt::RightButton) {
129 QPoint point =
event->globalPos();
153 m_plot->clearPlottables();
154 m_plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes
155 | QCP::iSelectLegend | QCP::iSelectPlottables);
156 m_plot->yAxis->setLabel(
"probability");
157 m_plot->xAxis2->setVisible(
true);
158 m_plot->yAxis2->setVisible(
true);
159 m_plot->xAxis2->setTickLabels(
false);
160 m_plot->yAxis2->setTickLabels(
false);
161 m_plot->xAxis2->setTicks(
false);
162 m_plot->yAxis2->setTicks(
false);
184 QVector<double> xPos = QVector<double>() << value;
185 QVector<double> yPos = QVector<double>() << 1.0;
188 plotVerticalLine(value, default_yrange.first, value, default_yrange.second);
196 double sigmafactor(0.0);
203 limits = limitsItem.createRealLimits();
210 std::vector<double> xp = dist->equidistantPoints(numberOfSamples, sigmafactor, limits);
211 std::vector<double> yp(xp.size());
212 std::transform(xp.begin(), xp.end(), yp.begin(),
213 [&](
double value) { return dist->probabilityDensity(value); });
214 double sumOfWeights = std::accumulate(yp.begin(), yp.end(), 0.0);
215 ASSERT(sumOfWeights != 0.0);
217 #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
218 QVector<double> xBar(xp.begin(), xp.end());
223 QVector<double> yBar(xBar.size());
224 std::transform(yp.begin(), yp.end(), yBar.begin(),
225 [&](
double value) { return value / sumOfWeights; });
230 auto xRange = xRangeForValues(xBar);
231 const int number_of_points = 400;
232 std::vector<double> xf =
233 dist->equidistantPointsInRange(number_of_points, xRange.first, xRange.second);
234 std::vector<double> yf(xf.size());
235 std::transform(xf.begin(), xf.end(), yf.begin(),
236 [&](
double value) { return dist->probabilityDensity(value); });
238 #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
239 QVector<double> xFunc(xf.begin(), xf.end());
243 QVector<double> yFunc(xFunc.size());
244 std::transform(yf.begin(), yf.end(), yFunc.begin(),
245 [&](
double value) { return value / sumOfWeights; });
251 const QPair<double, double>& yRange)
253 m_xRange = QCPRange(xRange.first, xRange.second);
254 m_yRange = QCPRange(yRange.first, yRange.second);
263 auto xRange = xRangeForValues(xbars);
264 auto yRange = yRangeForValues(ybars);
267 double barWidth(0.0);
268 if (xbars.size() == 1)
269 barWidth = optimalBarWidth(xRange.first, xRange.second, xbars.size());
271 barWidth = optimalBarWidth(xbars.front(), xbars.back(), xbars.size());
273 QCPBars* bars =
new QCPBars(
m_plot->xAxis,
m_plot->yAxis);
275 bars->setWidth(barWidth);
276 bars->setData(xbars, ybars);
281 auto xRange = xRangeForValues(xFunc);
282 auto yRange = yRangeForValues(yFunc);
286 m_plot->graph(0)->setData(xFunc, yFunc);
292 QCPItemLine* line =
new QCPItemLine(
m_plot);
294 QPen pen(color, 1, Qt::DashLine);
296 line->setSelectable(
true);
298 line->start->setCoords(xMin, yMin);
299 line->end->setCoords(xMax, yMax);
308 plotVerticalLine(value, default_yrange.first, value, default_yrange.second, Qt::red);
313 plotVerticalLine(value, default_yrange.first, value, default_yrange.second, Qt::red);
319 m_plot->xAxis->setLabel(xAxisName);
324 QPair<double, double> xRangeForValue(
double value)
326 const double range_factor(0.1);
328 double dr = (value == 0.0 ? 1.0 * range_factor : std::abs(value) * range_factor);
329 double xmin = value - dr;
330 double xmax = value + dr;
332 return QPair<double, double>(xmin, xmax);
337 QPair<double, double> xRangeForValues(
double value1,
double value2)
339 const double range_factor(0.1);
340 double dr = (value2 - value1) * range_factor;
343 return QPair<double, double>(value1 - dr, value2 + dr);
346 QPair<double, double> xRangeForValues(
const QVector<double>& xvec)
349 return xvec.size() == 1 ? xRangeForValue(xvec.front())
350 : xRangeForValues(xvec.front(), xvec.back());
353 QPair<double, double> yRangeForValues(
const QVector<double>& yvec)
355 const double range_factor(1.1);
356 double ymax = *std::max_element(yvec.begin(), yvec.end());
357 return QPair<double, double>(default_yrange.first, ymax * range_factor);
362 double optimalBarWidth(
double xmin,
double xmax,
int nbars)
364 double optimalWidth = (xmax - xmin) / 40.;
365 double width = (xmax - xmin) / nbars;
367 return optimalWidth < width ? optimalWidth : width;
#define ASSERT(condition)
Defines class DistributionItem and several subclasses.
Defines classes representing one-dimensional distributions.
Defines RealLimitsItems's classes.
Defines class WarningSign.
virtual std::unique_ptr< IDistribution1D > createDistribution(double scale=1.0) const =0
static const QString P_LIMITS
static const QString P_NUMBER_OF_SAMPLES
static const QString P_SIGMA_FACTOR
void unsubscribe(const void *caller)
Cancells all subscribtion of given caller.
void setOnItemDestroy(std::function< void(SessionItem *)> f, const void *caller=0)
void setOnPropertyChange(std::function< void(QString)> f, const void *caller=0)
Limits for a real fit parameter.
bool hasUpperLimit() const
if has upper limit
double upperLimit() const
Returns upper limit.
double lowerLimit() const
Returns lower limit.
bool hasLowerLimit() const
if has lower limit
bool isTag(const QString &name) const
Returns true if tag is available.
T & groupItem(const QString &groupName) const
QString displayName() const
Get display name of item, append index if ambigue.
QVariant getItemValue(const QString &tag) const
Directly access value of item under given tag.
ModelMapper * mapper()
Returns the current model mapper of this item. Creates new one if necessary.
QString modelType() const
Get model type.
static const QString P_MEAN
The WarningSign controls appearance of WarningSignWidget on top of parent widget.
void clear()
Clears warning message;.
void setWarningMessage(const QString &warningMessage)
Shows warning sign on the screen.
QVector< double > fromStdVector(const std::vector< double > &data)