void Fit::setDataCurve(int curve, double start, double end) { if (!d_graph) return; if (d_n > 0) delete[] d_w; Filter::setDataCurve(curve, start, end); d_w = new double[d_n]; PlotCurve *plotCurve = dynamic_cast<PlotCurve *>(d_curve); DataCurve *dataCurve = dynamic_cast<DataCurve *>(d_curve); // if it is a DataCurve (coming from a Table) if (plotCurve && dataCurve && plotCurve->type() != GraphOptions::Function) { QList<DataCurve *> lst = dataCurve->errorBarsList(); foreach (DataCurve *c, lst) { QwtErrorPlotCurve *er = dynamic_cast<QwtErrorPlotCurve *>(c); if (er && !er->xErrors()) { d_weighting = Instrumental; for (int i = 0; i < d_n; i++) d_w[i] = er->errorValue(i); // d_w are equal to the error bar values weighting_dataset = er->title().text(); return; } }
QwtPlotCurve* Filter::addResultCurve(double *x, double *y) { ApplicationWindow *app = (ApplicationWindow *)parent(); QLocale locale = app->locale(); const QString tableName = app->generateUniqueName(QString(objectName())); QString dataSet; if (d_curve) dataSet = d_curve->title().text(); else dataSet = d_y_col_name; d_result_table = app->newHiddenTable(tableName, d_explanation + " " + tr("of") + " " + dataSet, d_points, 2); for (int i=0; i<d_points; i++){ d_result_table->setText(i, 0, locale.toString(x[i], 'e', app->d_decimal_digits)); d_result_table->setText(i, 1, locale.toString(y[i], 'e', app->d_decimal_digits)); } DataCurve *c = 0; if (d_graphics_display){ c = new DataCurve(d_result_table, tableName + "_1", tableName + "_2"); c->setData(x, y, d_points); c->setPen(QPen(ColorBox::color(d_curveColorIndex), 1)); if (!d_output_graph) d_output_graph = createOutputGraph()->activeGraph(); d_output_graph->insertPlotItem(c, Graph::Line); d_output_graph->updatePlot(); } return (QwtPlotCurve*)c; }
/** * Called from constructor. * Sets up tool based on the given curve. * @param curve :: [input] Curve to use for initialization * @returns :: success or failure */ bool PeakPickerTool::initializeFromCurve(PlotCurve *curve) { if (!curve) { return false; } DataCurve *dcurve = dynamic_cast<DataCurve *>(curve); if (dcurve) { m_wsName = dcurve->table()->name().section('-', 0, 0); m_spec = dcurve->table()->colName(0).section('_', 1, 1).mid(2).toInt(); } else { MantidMatrixCurve *mcurve = dynamic_cast<MantidMatrixCurve *>(curve); if (mcurve) { const QString title = mcurve->title().text(); if (title.contains("Workspace-Calc")) { // Don't set up from a fit curve return false; } else { // Set up the tool from this curve m_wsName = mcurve->workspaceName(); m_spec = mcurve->workspaceIndex(); m_shouldBeNormalised = mcurve->isDistribution() && mcurve->isNormalizable(); } } else { return false; } } return true; }
void BaselineDialog::createBaseline() { ApplicationWindow *app = (ApplicationWindow *)parent(); QPen pen = QPen(Qt::red); if (d_baseline){ pen = d_baseline->pen(); graph->removeCurve(d_baseline->title().text()); delete d_baseline; d_baseline = NULL; if (d_table){ d_table->askOnCloseEvent(false); app->closeWindow(d_table); d_table = NULL; } } disableBaselineTool(); if (btnAutomatic->isChecked()){ Interpolation *i = new Interpolation(app, graph, boxInputName->currentText(), boxInterpolationMethod->currentIndex()); i->setOutputPoints(boxPoints->value()); i->run(); delete i; d_baseline = graph->dataCurve(graph->curveCount() - 1); d_table = ((DataCurve *)d_baseline)->table(); } else if (btnEquation->isChecked()){ double start = graph->axisScaleDiv(QwtPlot::xBottom)->lowerBound(); double end = graph->axisScaleDiv(QwtPlot::xBottom)->upperBound(); DataCurve *c = graph->dataCurve(graph->curveIndex(boxInputName->currentText())); if (c){ start = c->minXValue(); end = c->maxXValue(); } d_baseline = graph->addFunction(QStringList() << boxEquation->text(), start, end, boxPoints->value(), "x", FunctionCurve::Normal, tr("Baseline")); } else if (btnDataset->isChecked()){ Table *t = app->table(boxTableName->currentText()); if (t){ int rows = t->numRows(); d_table = app->newTable(rows, 2); app->setWindowName(d_table, tr("Baseline")); app->hideWindow(d_table); int ycol = t->colIndex(boxColumnName->currentText()); int xcol = t->colX(ycol); for (int i = 0; i < rows; i++){ d_table->setText(i, 0, t->text(i, xcol)); d_table->setText(i, 1, t->text(i, ycol)); } d_baseline = graph->insertCurve(d_table, d_table->objectName() + "_2", Graph::Line, 0, -1); graph->setAutoScale(); } } if (d_baseline){ d_baseline->setPen(pen); graph->replot(); } }
void Plot::updateCurveLabels() { QList<QwtPlotItem *> curves = curvesList(); foreach(QwtPlotItem *i, curves){ DataCurve * dc = dynamic_cast<DataCurve *>(i); if(dc && i->rtti() != QwtPlotItem::Rtti_PlotSpectrogram && dc->type() != Graph::Function && dc->hasLabels()) dc->updateLabelsPosition(); }
void Correlation::addResultCurve() { ApplicationWindow *app = dynamic_cast<ApplicationWindow *>(parent()); if (!app) return; QLocale locale = app->locale(); if (d_n > d_table->numRows()) d_table->setNumRows(d_n); int cols = d_table->numCols(); int cols2 = cols+1; d_table->addCol(); d_table->addCol(); int n = d_n/2; QVarLengthArray<double> x_temp(d_n), y_temp(d_n);//double x_temp[d_n], y_temp[d_n]; for (int i = 0; i<d_n; i++){ double x = i - n; x_temp[i] = x; double y; if(i < n) y = d_x[n + i]; else y = d_x[i - n]; y_temp[i] = y; d_table->setText(i, cols, QString::number(x)); d_table->setText(i, cols2, locale.toString(y, 'g', app->d_decimal_digits)); } QStringList l = d_table->colNames().grep(tr("Lag")); QString id = QString::number((int)l.size()+1); QString label = objectName() + id; d_table->setColName(cols, tr("Lag") + id); d_table->setColName(cols2, label); d_table->setColPlotDesignation(cols, Table::X); d_table->setHeaderColType(); if (d_graphics_display){ if (!d_output_graph) d_output_graph = createOutputGraph()->activeGraph(); DataCurve *c = new DataCurve(d_table, d_table->colName(cols), d_table->colName(cols2)); c->setData(x_temp.data(), y_temp.data(), d_n);//c->setData(x_temp, y_temp, d_n); c->setPen(QPen(ColorBox::color(d_curveColorIndex), 1)); d_output_graph->insertPlotItem(c, Graph::Line); d_output_graph->updatePlot(); } }
void AssociationsDialog::changePlotAssociation(int curve, const QStringList& ass) { DataCurve *c = (DataCurve *)graph->dataCurve(curvesIndicesList[curve]); if (!c) return; if (c->plotAssociation() == ass) return; QStringList lst = ass; if (lst.count() == 1){ c->setTitle(lst[0]); if (c->type() == Graph::Box) ((BoxCurve*)c)->loadData(); else if (c->type() == Graph::Pie) ((PieCurve*)c)->loadData(); else if (c->type() == Graph::Histogram) ((QwtHistogram*)c)->loadData(); } else if (lst.count() == 2){ c->setXColumnName(lst[0].remove("(X)")); c->setTitle(lst[1].remove("(Y)")); c->loadData(); } else if (lst.count() == 3){//curve with error bars ErrorBarsCurve *er = (ErrorBarsCurve *)c; QString xColName = lst[0].remove("(X)"); QString yColName = lst[1].remove("(Y)"); QString erColName = lst[2].remove("(xErr)").remove("(yErr)"); DataCurve *master_curve = graph->masterCurve(xColName, yColName); if (!master_curve) return; int type = ErrorBarsCurve::Vertical; if (ass.join(",").contains("(xErr)")) type = ErrorBarsCurve::Horizontal; er->setDirection(type); er->setTitle(erColName); if (master_curve != er->masterCurve()) er->setMasterCurve(master_curve); else er->loadData(); } else if (lst.count() == 4){ VectorCurve *v = (VectorCurve *)c; v->setXColumnName(lst[0].remove("(X)")); v->setTitle(lst[1].remove("(Y)")); QString xEndCol = lst[2].remove("(X)").remove("(A)"); QString yEndCol = lst[3].remove("(Y)").remove("(M)"); if (v->vectorEndXAColName() != xEndCol || v->vectorEndYMColName() != yEndCol) v->setVectorEnd(xEndCol, yEndCol); else v->loadData(); } graph->notifyChanges(); }
void Convolution::addResultCurve() { ApplicationWindow *app = (ApplicationWindow *)parent(); if (!app) return; int cols = d_table->numCols(); int cols2 = cols+1; d_table->addCol(); d_table->addCol(); #ifdef Q_CC_MSVC QVarLengthArray<double> x_temp(d_n); #else double x_temp[d_n]; #endif QLocale locale = app->locale(); for (int i = 0; i<d_n; i++){ double x = i+1; x_temp[i] = x; d_table->setText(i, cols, QString::number(x)); d_table->setText(i, cols2, locale.toString(d_x[i], 'g', app->d_decimal_digits)); } QStringList l = d_table->colNames().grep(tr("Index")); QString id = QString::number((int)l.size()+1); QString label = objectName() + id; d_table->setColName(cols, tr("Index") + id); d_table->setColName(cols2, label); d_table->setColPlotDesignation(cols, Table::X); d_table->setHeaderColType(); if (d_graphics_display){ if (!d_output_graph) createOutputGraph(); DataCurve *c = new DataCurve(d_table, d_table->colName(cols), d_table->colName(cols2)); #ifdef Q_CC_MSVC c->setData(x_temp.data(), d_x, d_n); #else c->setData(x_temp, d_x, d_n); #endif c->setPen(QPen(ColorBox::color(d_curveColorIndex), 1)); d_output_graph->insertPlotItem(c, Graph::Line); d_output_graph->updatePlot(); } }
void BaselineDialog::setGraph(Graph *g) { if (!g) return; graph = g; DataCurve *c = g->dataCurve(0); if (c) boxPoints->setValue(c->dataSize()); boxInputName->addItems(g->analysableCurvesList()); if (g->rangeSelectorsEnabled()) boxInputName->setCurrentIndex(boxInputName->findText(g->curveRange(g->rangeSelectorTool()->selectedCurve()))); connect (graph, SIGNAL(destroyed()), this, SLOT(close())); connect (graph, SIGNAL(modifiedGraph()), this, SLOT(updateGraphCurves())); }
/** * Returns the index of the closest curve to a point on the canvas. * Also returns index of the nearest data point on that curve. * @param xpos :: x coordinate of a point on the canvas in pixels. * @param ypos :: y coordinate of a point on the canvas in pixels. * @param dist :: ? * @param point :: Output index of the nearest data point to the point with coordinates (xpos,ypos) */ int Plot::closestCurve(int xpos, int ypos, int &dist, int &point) { QwtScaleMap map[QwtPlot::axisCnt]; for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ ) map[axis] = canvasMap(axis); double dmin = std::numeric_limits<double>::max(); int key = -1; for (QMap<int, QwtPlotItem *>::iterator iter = d_curves.begin(); iter != d_curves.end(); ++iter ) { QwtPlotItem *item = (QwtPlotItem *)iter.data(); if (!item) continue; if(item->rtti() != QwtPlotItem::Rtti_PlotSpectrogram) { PlotCurve *c = (PlotCurve *)item; DataCurve *dc = dynamic_cast<DataCurve *>(item); if (dc) { if (c->type() != Graph::Function && dc->hasLabels() && dc->selectedLabels(QPoint(xpos, ypos))){ dist = 0; return iter.key(); } else dc->setLabelsSelected(false); } for (int i=0; i<c->dataSize(); i++) { double cx = map[c->xAxis()].xTransform(c->x(i)) - double(xpos); double cy = map[c->yAxis()].xTransform(c->y(i)) - double(ypos); double f = qwtSqr(cx) + qwtSqr(cy); if (f < dmin && c->type() != Graph::ErrorBars) { dmin = f; key = iter.key(); point = i; } } } } dist = static_cast<int>(sqrt(dmin)); return key; }
void Correlation::addResultCurve() { ApplicationWindow *app = (ApplicationWindow *)parent(); if (!app) return; int rows = d_table->numRows(); int cols = d_table->numCols(); int cols2 = cols+1; d_table->addCol(); d_table->addCol(); int n = rows/2; double x_temp[rows], y_temp[rows]; for (int i = 0; i<rows; i++) { x_temp[i] = i - n; if(i < n) y_temp[i] = d_x[d_n - n + i]; else y_temp[i] = d_x[i-n]; d_table->column(cols)->setValueAt(i, x_temp[i]); d_table->column(cols2)->setValueAt(i, y_temp[i]); } QStringList l = d_table->colNames().grep(tr("Lag")); QString id = QString::number((int)l.size()+1); QString label = name() + id; d_table->setColName(cols, tr("Lag") + id); d_table->setColName(cols2, label); d_table->setColPlotDesignation(cols, SciDAVis::X); MultiLayer *ml = app->newGraph(name() + tr("Plot")); if (!ml) return; DataCurve *c = new DataCurve(d_table, d_table->colName(cols), d_table->colName(cols2)); c->setData(x_temp, y_temp, rows); c->setPen(QPen(ColorBox::color(d_curveColorIndex), 1)); ml->activeGraph()->insertPlotItem(c, Graph::Line); ml->activeGraph()->updatePlot(); }
DataCurve::DataCurve(const DataCurve &c) : PlotCurve(c.title().text()), d_table(c.d_table), d_x_column(c.d_x_column), d_start_row(c.d_start_row), d_end_row(c.d_end_row), d_labels_column(c.d_labels_column), d_click_pos_x(c.d_click_pos_x), d_click_pos_y(c.d_click_pos_y), d_labels_color(c.d_labels_color), d_labels_font(c.d_labels_font), d_labels_angle(c.d_labels_angle), d_white_out_labels(c.d_white_out_labels), d_labels_align(c.d_labels_align), d_labels_x_offset(c.d_labels_x_offset), d_labels_y_offset(c.d_labels_y_offset), d_selected_label(c.d_selected_label) {}
void BaselineDialog::setGraph(Graph *g) { if (!g) return; graph = g; DataCurve *c = g->dataCurve(0); if (c) boxPoints->setValue(c->dataSize()); boxInputName->addItems(g->analysableCurvesList()); QString selectedCurve = g->selectedCurveTitle(); if (!selectedCurve.isEmpty()) boxInputName->setCurrentIndex(boxInputName->findText(selectedCurve)); connect (graph, SIGNAL(destroyed()), this, SLOT(close())); connect (graph, SIGNAL(modifiedGraph()), this, SLOT(updateGraphCurves())); }
void AssociationsDialog::changePlotAssociation(int curve, const QString &text) { DataCurve *c = (DataCurve *)graph->curve(curve); // c_keys[curve]); if (!c) return; if (c->plotAssociation() == text) return; QStringList lst = text.split(",", QString::SkipEmptyParts); if (lst.count() == 2) { c->setXColumnName(lst[0].remove("(X)")); c->setTitle(lst[1].remove("(Y)")); c->loadData(); } else if (lst.count() == 3) { // curve with error bars QwtErrorPlotCurve *er = (QwtErrorPlotCurve *)c; QString xColName = lst[0].remove("(X)"); QString yColName = lst[1].remove("(Y)"); QString erColName = lst[2].remove("(xErr)").remove("(yErr)"); DataCurve *master_curve = graph->masterCurve(xColName, yColName); if (!master_curve) return; int type = QwtErrorPlotCurve::Vertical; if (text.contains("(xErr)")) type = QwtErrorPlotCurve::Horizontal; er->setDirection(type); er->setTitle(erColName); if (master_curve != er->masterCurve()) er->setMasterCurve(master_curve); else er->loadData(); } else if (lst.count() == 4) { VectorCurve *v = (VectorCurve *)c; v->setXColumnName(lst[0].remove("(X)")); v->setTitle(lst[1].remove("(Y)")); QString xEndCol = lst[2].remove("(X)").remove("(A)"); QString yEndCol = lst[3].remove("(Y)").remove("(M)"); if (v->vectorEndXAColName() != xEndCol || v->vectorEndYMColName() != yEndCol) v->setVectorEnd(xEndCol, yEndCol); else v->loadData(); } graph->notifyChanges(); }
void CurveRangeDialog::accept() { if (!d_graph) return; int start = boxStart->value() - 1; int end = boxEnd->value() - 1; if (boxApplyToAll->isChecked()){ for (int i = 0; i < d_graph->curveCount(); i++){ DataCurve *c = d_graph->dataCurve(i); if (c) c->setRowRange(QMIN(start, end), QMAX(start, end)); } } else if (!d_curves.isEmpty()){ foreach(DataCurve *c, d_curves) c->setRowRange(QMIN(start, end), QMAX(start, end)); } d_graph->updatePlot(); d_graph->notifyChanges(); close(); }
void MultiPeakFit::generateFitCurve() { ApplicationWindow *app = (ApplicationWindow *)parent(); if (!d_gen_function) d_points = d_n; int i, j; int peaks_aux = d_peaks; if (d_peaks == 1) peaks_aux--; if (d_gen_function){ customizeFitResults(); if (d_graphics_display){ if (!d_output_graph) createOutputGraph(); if (d_peaks > 1) insertFitFunctionCurve(QString(objectName()) + tr("Fit"), 2); else insertFitFunctionCurve(QString(objectName()) + tr("Fit")); if (generate_peak_curves){ for (i = 0; i < peaks_aux; i++)//add the peak curves insertPeakFunctionCurve(i); } d_output_graph->replot(); } } else { gsl_matrix * m = gsl_matrix_alloc (d_points, d_peaks); if (!m){ QMessageBox::warning(app, tr("QtiPlot - Fit Error"), tr("Could not allocate enough memory for the fit curves!")); return; } double *X = (double *)malloc(d_points*sizeof(double)); if (!X){ memoryErrorMessage(); return; } double *Y = (double *)malloc(d_points*sizeof(double)); if (!Y){ memoryErrorMessage(); free(X); return; } QString tableName = app->generateUniqueName(tr("Fit")); QString dataSet; if (d_curve) dataSet = d_curve->title().text(); else dataSet = d_y_col_name; QString label = d_explanation + " " + tr("fit of") + " " + dataSet; d_result_table = app->newHiddenTable(tableName, label, d_points, peaks_aux + 2); QStringList header = QStringList() << "1"; for (i = 0; i<peaks_aux; i++) header << tr("peak") + QString::number(i+1); header << "2"; d_result_table->setHeader(header); QLocale locale = app->locale(); for (i = 0; i<d_points; i++){ X[i] = d_x[i]; d_result_table->setText(i, 0, locale.toString(X[i], 'e', d_prec)); double yi=0; for (j=0; j<d_peaks; j++){ double diff = X[i] - d_results[3*j + 1]; double w = d_results[3*j + 2]; double y_aux = 0; if (d_profile == Gauss) y_aux += sqrt(M_2_PI)*d_results[3*j]/w*exp(-2*diff*diff/(w*w)); else y_aux += M_2_PI*d_results[3*j]*w/(4*diff*diff+w*w); yi += y_aux; y_aux += d_results[d_p - 1]; d_result_table->setText(i, j+1, locale.toString(y_aux, 'e', d_prec)); gsl_matrix_set(m, i, j, y_aux); } Y[i] = yi + d_results[d_p - 1];//add offset if (d_peaks > 1) d_result_table->setText(i, d_peaks+1, locale.toString(Y[i], 'e', d_prec)); } customizeFitResults(); if (d_graphics_display){ if (!d_output_graph) createOutputGraph(); label = tableName + "_2"; DataCurve *c = new DataCurve(d_result_table, tableName + "_1", label); if (d_curve){ c->setCurveType(d_curve->curveType()); c->setAxis(d_curve->xAxis(), d_curve->yAxis()); } if (d_peaks > 1) c->setPen(QPen(d_curveColor, 2)); else c->setPen(QPen(d_curveColor, 1)); if (c->curveType() == QwtPlotCurve::Xfy) c->setData(Y, X, d_points); else c->setData(X, Y, d_points); d_output_graph->insertPlotItem(c, Graph::Line); d_output_graph->addFitCurve(c); if (generate_peak_curves){ for (i=0; i<peaks_aux; i++){//add the peak curves for (j=0; j<d_points; j++) Y[j] = gsl_matrix_get (m, j, i); label = tableName + "_" + tr("peak") + QString::number(i+1); c = new DataCurve(d_result_table, tableName + "_1", label); c->setPen(QPen(d_peaks_color, 1)); if (d_curve){ c->setCurveType(d_curve->curveType()); c->setAxis(d_curve->xAxis(), d_curve->yAxis()); } if (c->curveType() == QwtPlotCurve::Xfy) c->setData(Y, X, d_points); else c->setData(X, Y, d_points); d_output_graph->insertPlotItem(c, Graph::Line); d_output_graph->addFitCurve(c); } } d_output_graph->replot(); } gsl_matrix_free(m); free(X); free(Y); } }
bool CanvasPicker::eventFilter(QObject *object, QEvent *e) { if (object != (QObject *)plot()->canvas()) return false; Graph *g = plot(); QList<QwtPlotMarker *> lines = g->linesList(); switch(e->type()) { case QEvent::MouseButtonPress: { const QMouseEvent *me = (const QMouseEvent *)e; if (!(me->modifiers() & Qt::ShiftModifier)) g->deselect(); emit selectPlot(); bool allAxisDisabled = true; for (int i=0; i < QwtPlot::axisCnt; i++){ if (g->axisEnabled(i)){ allAxisDisabled = false; break; } } int dist, point; g->closestCurve(me->pos().x(), me->pos().y(), dist, point); if (me->button() == Qt::LeftButton && (g->drawLineActive())){ startLinePoint = me->pos(); return true; } if (!g->zoomOn() && selectMarker(me)){ if (me->button() == Qt::RightButton) emit showMarkerPopupMenu(); return true; } return !(me->modifiers() & Qt::ShiftModifier); } break; case QEvent::MouseButtonDblClick: { if (d_editing_marker) { return d_editing_marker->eventFilter(g->canvas(), e); } else if (g->selectedMarker()) { if (lines.contains(g->selectedMarker())){ emit viewLineDialog(); return true; } } else if (g->isPiePlot()){ emit showPlotDialog(0); return true; } else { const QMouseEvent *me = (const QMouseEvent *)e; int dist, point; QwtPlotCurve *c = g->closestCurve(me->pos().x(), me->pos().y(), dist, point); if (c && dist < 10) emit showPlotDialog(g->curveIndex(c)); else emit showPlotDialog(-1); return true; } } break; case QEvent::MouseMove: { const QMouseEvent *me = (const QMouseEvent *)e; if (me->state() != Qt::LeftButton) return true; QPoint pos = me->pos(); DataCurve *c = g->selectedCurveLabels(); if (c){ c->moveLabels(pos); return true; } if (plot()->drawLineActive()) { drawLineMarker(pos, g->drawArrow()); return true; } return false; } break; case QEvent::MouseButtonRelease: { const QMouseEvent *me = (const QMouseEvent *)e; if (g->drawLineActive()) { ApplicationWindow *app = g->multiLayer()->applicationWindow(); if (!app) return true; ArrowMarker mrk; mrk.attach(g); mrk.setStartPoint(startLinePoint); mrk.setEndPoint(QPoint(me->x(), me->y())); mrk.setColor(app->defaultArrowColor); mrk.setWidth(app->defaultArrowLineWidth); mrk.setStyle(app->defaultArrowLineStyle); mrk.setHeadLength(app->defaultArrowHeadLength); mrk.setHeadAngle(app->defaultArrowHeadAngle); mrk.fillArrowHead(app->defaultArrowHeadFill); mrk.drawEndArrow(g->drawArrow()); mrk.drawStartArrow(false); g->addArrow(&mrk); g->drawLine(false); mrk.detach(); g->replot(); return true; } return false; } break; case QEvent::KeyPress: { int key = ((const QKeyEvent *)e)->key(); QwtPlotMarker *selectedMarker = g->selectedMarker(); if (lines.contains(selectedMarker) && (key == Qt::Key_Enter || key == Qt::Key_Return)){ emit viewLineDialog(); return true; } } break; default: break; } return QObject::eventFilter(object, e); }
void DataCurve::loadData() { Graph *g = (Graph *)plot(); if (!g) return; int xcol = d_x_table->colIndex(d_x_column); int ycol = d_table->colIndex(title().text()); if (xcol < 0 || ycol < 0) { remove(); return; } int rows = d_table->numRows(); if (d_end_row < 0 || d_end_row >= rows) d_end_row = rows - 1; int xColType = d_x_table->columnType(xcol); int yColType = d_table->columnType(ycol); int r = abs(d_end_row - d_start_row) + 1; QPolygonF data; data.reserve(r); QStringList xLabels, yLabels;// store text labels int xAxis = QwtPlot::xBottom; if (d_type == Graph::HorizontalBars) xAxis = QwtPlot::yLeft; QString date_time_fmt = d_table->columnFormat(xcol); int size = 0, from = 0; d_data_ranges.clear(); for (int i = d_start_row; i <= d_end_row; i++ ) { QString xval = d_x_table->text(i, xcol); QString yval = d_table->text(i, ycol); if (!xval.isEmpty() && !yval.isEmpty()) { bool valid_data = true; QPointF p; if (xColType == Table::Text) { xLabels << xval; p.setX((double)(size + 1)); } else if (xColType == Table::Time) p.setX(Table::fromTime(QTime::fromString(xval.trimmed(), date_time_fmt))); else if (xColType == Table::Date) p.setX(Table::fromDateTime(QDateTime::fromString(xval.trimmed(), date_time_fmt))); else p.setX(g->locale().toDouble(xval, &valid_data)); if (yColType == Table::Text) { yLabels << yval; p.setY((double)(size + 1)); } else p.setY(g->locale().toDouble(yval, &valid_data)); if (valid_data) { data << p; size++; } } else if (from < size) { DataRange range; range.from = from; range.to = size - 1; d_data_ranges.push_back(range); from = size; } } if (d_data_ranges.size() && from < size) { DataRange range; range.from = from; range.to = size - 1; d_data_ranges.push_back(range); } if (!size) { remove(); return; } data.resize(size); if (g->isWaterfallPlot()) { int index = g->curveIndex(this); int curves = g->curveCount(); DataCurve *c = g->dataCurve(0); if (index > 0 && c) { double xmin = c->minXValue(); double dx = index*g->waterfallXOffset()*0.01*g->canvas()->width()/(double)(curves - 1); d_x_offset = g->invTransform(xAxis, g->transform(xAxis, xmin) + dx) - xmin; double ymin = c->minYValue(); double dy = index*g->waterfallYOffset()*0.01*g->canvas()->height()/(double)(curves - 1); d_y_offset = ymin - g->invTransform(yAxis(), g->transform(yAxis(), ymin) + dy); setZ(-index); setBaseline(d_y_offset); data.translate(d_x_offset, d_y_offset); } else { setZ(0); setBaseline(0.0); } if (g->grid()) g->grid()->setZ(-g->curveCount() - 1); } double speedTol = g->getDouglasPeukerTolerance(); if (speedTol != 0.0 && size >= g->speedModeMaxPoints()) { QwtWeedingCurveFitter *fitter = new QwtWeedingCurveFitter(speedTol); data = fitter->fitCurve(data); delete fitter; } if (d_type == Graph::HorizontalBars) { size = data.size(); for (int i = 0; i < size; i++) { QPointF p = data.at(i); data[i] = QPointF(p.y(), p.x()); } } setData(data); foreach(ErrorBarsCurve *c, d_error_bars) c->setData(data); if (xColType == Table::Text) g->setLabelsTextFormat(xAxis, ScaleDraw::Text, d_x_column, xLabels); if (yColType == Table::Text) g->setLabelsTextFormat(QwtPlot::yLeft, ScaleDraw::Text, title().text(), yLabels); setRenderHint(QwtPlotItem::RenderAntialiased, g->isCurveAntialiasingEnabled(this)); if (!d_labels_list.isEmpty()) { ((Graph*)plot())->updatePlot(); loadLabels(); } }
bool CanvasPicker::eventFilter(QObject *object, QEvent *e) { QVector<int> images = plot()->imageMarkerKeys(); QVector<int> lines = plot()->lineMarkerKeys(); if (object != (QObject *)plot()->plotWidget()->canvas()) return false; switch(e->type()) { case QEvent::MouseButtonPress: { plot()->deselect(); emit selectPlot(); const QMouseEvent *me = (const QMouseEvent *)e; bool allAxisDisabled = true; for (int i=0; i < QwtPlot::axisCnt; i++){ if (plotWidget->axisEnabled(i)){ allAxisDisabled = false; break; } } int dist, point; plotWidget->closestCurve(me->pos().x(), me->pos().y(), dist, point); if (me->button() == Qt::LeftButton && (plot()->drawLineActive())){ startLinePoint = me->pos(); return true; } if (me->button() == Qt::LeftButton && plot()->drawTextActive()){ drawTextMarker(me->pos()); return true; } if (!plot()->zoomOn() && selectMarker(me)){ if (me->button() == Qt::RightButton) emit showMarkerPopupMenu(); return true; } return !(me->modifiers() & Qt::ShiftModifier); } break; case QEvent::MouseButtonDblClick: { if (d_editing_marker) { return d_editing_marker->eventFilter(plotWidget->canvas(), e); } else if (plot()->selectedMarkerKey() >= 0) { if (lines.contains(plot()->selectedMarkerKey())){ emit viewLineDialog(); return true; } else if (images.contains(plot()->selectedMarkerKey())){ emit viewImageDialog(); return true; } } else if (plot()->isPiePlot()){ emit showPlotDialog(plot()->curveKey(0)); return true; } else { const QMouseEvent *me = (const QMouseEvent *)e; int dist, point; int curveKey = plotWidget->closestCurve(me->pos().x(), me->pos().y(), dist, point); if (dist < 10) emit showPlotDialog(curveKey); else emit showPlotDialog(-1); return true; } } break; case QEvent::MouseMove: { const QMouseEvent *me = (const QMouseEvent *)e; if (me->state() != Qt::LeftButton) return true; QPoint pos = me->pos(); DataCurve *c = plot()->selectedCurveLabels(); if (c){ c->moveLabels(pos); return true; } if (plot()->drawLineActive()) { drawLineMarker(pos, plot()->drawArrow()); return true; } return false; } break; case QEvent::MouseButtonRelease: { const QMouseEvent *me = (const QMouseEvent *)e; Graph *g = plot(); if (g->drawLineActive()) { ApplicationWindow *app = g->multiLayer()->applicationWindow(); if (!app) return true; ArrowMarker mrk; mrk.attach(g->plotWidget()); mrk.setStartPoint(startLinePoint); mrk.setEndPoint(QPoint(me->x(), me->y())); mrk.setColor(app->defaultArrowColor); mrk.setWidth(app->defaultArrowLineWidth); mrk.setStyle(app->defaultArrowLineStyle); mrk.setHeadLength(app->defaultArrowHeadLength); mrk.setHeadAngle(app->defaultArrowHeadAngle); mrk.fillArrowHead(app->defaultArrowHeadFill); mrk.drawEndArrow(g->drawArrow()); mrk.drawStartArrow(false); g->addArrow(&mrk); g->drawLine(false); mrk.detach(); plotWidget->replot(); return true; } /*if(plot()->zoomOn()) { //fix for colormap changing on releasing the mouse button after zooming Graph * gr=plot(); if(!gr) return NULL; Spectrogram * spectrogram=gr->getSpectrogram(); if(spectrogram) spectrogram->setupColorBarScaling(); }*/ return false; } break; case QEvent::KeyPress: { int key=((const QKeyEvent *)e)->key(); int selectedMarker = plot()->selectedMarkerKey(); if (lines.contains(selectedMarker) && (key==Qt::Key_Enter || key==Qt::Key_Return)) { emit viewLineDialog(); return true; } if (images.contains(selectedMarker) && (key==Qt::Key_Enter || key==Qt::Key_Return)) { emit viewImageDialog(); return true; } } break; default: break; } return QObject::eventFilter(object, e); }
void ErrDialog::add() { ApplicationWindow *app = qobject_cast<ApplicationWindow *>(parent()); if (!app) return; MultiLayer *plot = (MultiLayer *)app->activeWindow(ApplicationWindow::MultiLayerWindow); if (!plot) return; Graph* g = plot->activeLayer(); if (!g) return; QString name = nameLabel->currentText(); DataCurve *curve = g->dataCurve(name); if (!curve){ QMessageBox::critical(app, tr("QtiPlot - Error"), tr("This feature is not available for user defined function curves!")); return; } int direction = xErrBox->isChecked() ? 0 : 1; ErrorBarsCurve *er = NULL; if (columnBox->isChecked()){ QString errColumnName = tableNamesBox->currentText() + "_" + colNamesBox->currentText(); Table *errTable = app->table(errColumnName); if (!errTable) return; /*if (w->numRows() != errTable->numRows()){ QMessageBox::critical(app, tr("QtiPlot - Error"), tr("The selected columns have different numbers of rows!")); return; }*/ if (errTable->isEmptyColumn(errTable->colIndex(errColumnName))){ QMessageBox::critical(app, tr("QtiPlot - Error"), tr("The selected error column is empty!")); return; } er = g->addErrorBars(curve, errTable, errColumnName, direction); } else { Table *t = curve->table(); if (!t) return; if (direction == ErrorBarsCurve::Horizontal) t->addCol(Table::xErr); else t->addCol(Table::yErr); int r = curve->dataSize(); int rows = t->numRows(); int col = t->numCols() - 1; int ycol = t->colIndex(curve->title().text()); if (!direction) ycol = t->colIndex(curve->xColumnName()); QVarLengthArray<double> Y(r); if (direction == ErrorBarsCurve::Horizontal){ for (int i = 0; i < r; i++) Y[i] = curve->x(i); } else { for (int i = 0; i < r; i++) Y[i] = curve->y(i); } if (percentBox->isChecked()){ double prc = 0.01*valueBox->value(); int aux = 0; for (int i = curve->startRow(); i <= curve->endRow(); i++){ if (!t->text(i, ycol).isEmpty() && aux < r){ t->setCell(i, col, Y[aux]*prc); aux++; } } } else if (standardBox->isChecked() || standardErrorBox->isChecked()){ double sd = gsl_stats_sd(Y.data(), 1, r); if (standardErrorBox->isChecked()) sd /= sqrt(r); for (int i = 0; i < rows; i++){ if (!t->text(i, ycol).isEmpty()) t->setCell(i, col, sd); } } er = g->addErrorBars(curve, t, t->colName(col), direction); } if (er){ er->setColor(curve->pen().color()); g->replot(); plot->notifyChanges(); } }
void MultiPeakFit::generateFitCurve() { ApplicationWindow *app = (ApplicationWindow *)parent(); if (!d_gen_function) d_points = d_n; gsl_matrix * m = gsl_matrix_alloc (d_points, d_peaks); if (!m){ QMessageBox::warning(app, tr("MantidPlot - Fit Error"), tr("Could not allocate enough memory for the fit curves!")); return; } QVarLengthArray<double> X(d_points), Y(d_points);//double X[d_points], Y[d_points]; int i, j; int peaks_aux = d_peaks; if (d_peaks == 1) peaks_aux--; if (d_gen_function){ double step = (d_x[d_n-1] - d_x[0])/(d_points-1); for (i = 0; i<d_points; i++){ double x = d_x[0] + i*step; X[i] = x; double yi = 0; for (j=0; j<d_peaks; j++){ double y = evalPeak(d_results, x, j); gsl_matrix_set(m, i, j, y + d_results[d_p - 1]); yi += y; } Y[i] = yi + d_results[d_p - 1];//add offset } customizeFitResults(); if (d_graphics_display){ if (!d_output_graph) d_output_graph = createOutputGraph()->activeGraph(); if (d_peaks > 1) insertFitFunctionCurve(QString(objectName()) + tr("Fit"), X.data(), Y.data(), 2);//insertFitFunctionCurve(QString(objectName()) + tr("Fit"), X, Y, 2); else insertFitFunctionCurve(QString(objectName()) + tr("Fit"), X.data(), Y.data());//insertFitFunctionCurve(QString(objectName()) + tr("Fit"), X, Y); if (generate_peak_curves){ for (i=0; i<peaks_aux; i++){//add the peak curves for (j=0; j<d_points; j++) Y[j] = gsl_matrix_get (m, j, i); insertPeakFunctionCurve(X.data(), Y.data(), i);//insertPeakFunctionCurve(X, Y, i); } } d_output_graph->replot(); } } else { QString tableName = app->generateUniqueName(tr("Fit")); QString dataSet; if (d_curve) dataSet = d_curve->title().text(); else dataSet = d_y_col_name; QString label = d_explanation + " " + tr("fit of") + " " + dataSet; d_result_table = app->newHiddenTable(tableName, label, d_points, peaks_aux + 2); QStringList header = QStringList() << "1"; for (i = 0; i<peaks_aux; i++) header << tr("peak") + QString::number(i+1); header << "2"; d_result_table->setHeader(header); QLocale locale = app->locale(); for (i = 0; i<d_points; i++){ X[i] = d_x[i]; d_result_table->setText(i, 0, locale.toString(X[i], 'e', d_prec)); double yi=0; for (j=0; j<d_peaks; j++){ double diff = X[i] - d_results[3*j + 1]; double w = d_results[3*j + 2]; double y_aux = 0; if (d_profile == Gauss) y_aux += sqrt(M_2_PI)*d_results[3*j]/w*exp(-2*diff*diff/(w*w)); else y_aux += M_2_PI*d_results[3*j]*w/(4*diff*diff+w*w); yi += y_aux; y_aux += d_results[d_p - 1]; d_result_table->setText(i, j+1, locale.toString(y_aux, 'e', d_prec)); gsl_matrix_set(m, i, j, y_aux); } Y[i] = yi + d_results[d_p - 1];//add offset if (d_peaks > 1) d_result_table->setText(i, d_peaks+1, locale.toString(Y[i], 'e', d_prec)); } customizeFitResults(); if (d_graphics_display){ if (!d_output_graph) d_output_graph = createOutputGraph()->activeGraph(); label = tableName + "_2"; DataCurve *c = new DataCurve(d_result_table, tableName + "_1", label); if (d_peaks > 1) c->setPen(QPen(ColorBox::color(d_curveColorIndex), 2)); else c->setPen(QPen(ColorBox::color(d_curveColorIndex), 1)); c->setData(X.data(), Y.data(), d_points);//c->setData(X, Y, d_points); d_output_graph->insertPlotItem(c, Graph::Line); d_output_graph->addFitCurve(c); if (generate_peak_curves){ for (i=0; i<peaks_aux; i++){//add the peak curves for (j=0; j<d_points; j++) Y[j] = gsl_matrix_get (m, j, i); label = tableName + "_" + tr("peak") + QString::number(i+1); c = new DataCurve(d_result_table, tableName + "_1", label); c->setPen(QPen(ColorBox::color(d_peaks_color), 1)); c->setData(X.data(), Y.data(), d_points);//c->setData(X, Y, d_points); d_output_graph->insertPlotItem(c, Graph::Line); d_output_graph->addFitCurve(c); } } d_output_graph->replot(); } } gsl_matrix_free(m); }
void BaselineDialog::subtractBaseline(bool add) { if (!d_baseline) return; disableBaselineTool(); if (!graph) return; int index = graph->curveIndex(boxInputName->currentText()); DataCurve *c = graph->dataCurve(index); if (!c) return; ApplicationWindow *app = (ApplicationWindow *)parent(); if (!app) return; Table *inputTable = c->table(); if (!inputTable) return; int inputPoints = inputTable->numRows(); QString xColName = c->xColumnName(); int xCol = inputTable->colIndex(xColName); int yCol = inputTable->colIndex(c->title().text()); int refPoints = d_baseline->dataSize(); double *x = (double *)malloc(refPoints*sizeof(double)); if (!x) return; double *y = (double *)malloc(refPoints*sizeof(double)); if (!y){ free (x); return; } for (int i = 0; i < refPoints; i++){ x[i] = d_baseline->x(i); y[i] = d_baseline->y(i); } //sort data with respect to x value size_t *p = (size_t *)malloc(refPoints*sizeof(size_t)); if (!p){ free(x); free(y); return; } gsl_sort_index(p, x, 1, refPoints); double *xtemp = (double *)malloc(refPoints*sizeof(double)); if (!xtemp){ free(x); free(y); free(p); return; } double *ytemp = (double *)malloc(refPoints*sizeof(double)); if (!ytemp){ free(x); free(y); free(p); free(xtemp); return; } for (int i = 0; i < refPoints; i++){ xtemp[i] = x[p[i]]; ytemp[i] = y[p[i]]; } free(x); free(y); free(p); //make linear interpolation on sorted data gsl_interp_accel *acc = gsl_interp_accel_alloc(); gsl_spline *interp = gsl_spline_alloc(gsl_interp_linear, refPoints); gsl_spline_init (interp, xtemp, ytemp, refPoints); for (int i = 0; i < inputPoints; i++){ if (!inputTable->text(i, yCol).isEmpty() && !inputTable->text(i, xCol).isEmpty()) inputTable->setCell(i, yCol, combineValues(inputTable->cell(i, yCol), gsl_spline_eval(interp, inputTable->cell(i, xCol), acc), add)); } inputTable->notifyChanges(c->title().text()); gsl_spline_free (interp); gsl_interp_accel_free (acc); free(xtemp); free(ytemp); }