コード例 #1
0
ファイル: PlotCurve.cpp プロジェクト: mantidproject/mantid
void DataCurve::loadData() {
  Plot *plot = static_cast<Plot *>(this->plot());
  Graph *g = static_cast<Graph *>(plot->parent());
  if (!g)
    return;

  int xcol = d_table->colIndex(d_x_column);
  int ycol = d_table->colIndex(title().text());

  if (xcol < 0 || ycol < 0) {
    remove();
    return;
  }

  int r = abs(d_end_row - d_start_row) + 1;
  QVarLengthArray<double> X(r), Y(r);
  int xColType = d_table->columnType(xcol);
  int yColType = d_table->columnType(ycol);

  QStringList xLabels, yLabels; // store text labels

  //  int xAxis = QwtPlot::xBottom;
  //  if (d_type == GraphOptions::HorizontalBars)
  //    xAxis = QwtPlot::yLeft;

  QTime time0;
  QDateTime date0;
  QString date_time_fmt = d_table->columnFormat(xcol);
  if (xColType == Table::Time) {
    for (int i = d_start_row; i <= d_end_row; i++) {
      QString xval = d_table->text(i, xcol);
      if (!xval.isEmpty()) {
        time0 = QTime::fromString(xval, date_time_fmt);
        if (time0.isValid())
          break;
      }
    }
  } else if (xColType == Table::Date) {
    for (int i = d_start_row; i <= d_end_row; i++) {
      QString xval = d_table->text(i, xcol);
      if (!xval.isEmpty()) {
        date0 = QDateTime::fromString(xval, date_time_fmt);
        if (date0.isValid())
          break;
      }
    }
  }

  int size = 0;
  for (int i = d_start_row; i <= d_end_row; i++) {
    QString xval = d_table->text(i, xcol);
    QString yval = d_table->text(i, ycol);
    if (!xval.isEmpty() && !yval.isEmpty()) {
      bool valid_data = true;
      if (xColType == Table::Text) {
        xLabels << xval;
        X[size] = (double)(size + 1);
      } else if (xColType == Table::Time) {
        QTime time = QTime::fromString(xval, date_time_fmt);
        if (time.isValid())
          X[size] = time0.msecsTo(time);
      } else if (xColType == Table::Date) {
        QDateTime d = QDateTime::fromString(xval, date_time_fmt);
        if (d.isValid())
          X[size] = (double)date0.secsTo(d);
      } else
        X[size] = plot->locale().toDouble(xval, &valid_data);

      if (yColType == Table::Text) {
        yLabels << yval;
        Y[size] = (double)(size + 1);
      } else
        Y[size] = plot->locale().toDouble(yval, &valid_data);

      if (valid_data)
        size++;
    }
  }

  X.resize(size);
  Y.resize(size);

  // The code for calculating the waterfall offsets, that is here in QtiPlot,
  // has been moved up to
  // PlotCurve so that MantidCurve can access it as well.
  if (g->isWaterfallPlot()) {
    // Calculate the offsets
    double a, b;
    computeWaterfallOffsets(a, b);
  }
  // End re-jigged waterfall offset code

  if (!size) {
    remove();
    return;
  } else {
    if (d_type == GraphOptions::HorizontalBars) {
      setData(Y.data(), X.data(), size);
      foreach (DataCurve *c, d_error_bars)
        c->setData(Y.data(), X.data(), size);
    } else {
      setData(X.data(), Y.data(), size);
      foreach (DataCurve *c, d_error_bars)
        c->setData(X.data(), Y.data(), size);
    }

    if (xColType == Table::Text) {
      if (d_type == GraphOptions::HorizontalBars)
        g->setLabelsTextFormat(QwtPlot::yLeft, ScaleDraw::Text, d_x_column,
                               xLabels);
      else
        g->setLabelsTextFormat(QwtPlot::xBottom, ScaleDraw::Text, d_x_column,
                               xLabels);
    } else if (xColType == Table::Time || xColType == Table::Date) {
      int axis = QwtPlot::xBottom;
      if (d_type == GraphOptions::HorizontalBars)
        axis = QwtPlot::yLeft;
      ScaleDraw *old_sd = static_cast<ScaleDraw *>(plot->axisScaleDraw(axis));
      ScaleDraw *sd = new ScaleDraw(plot, old_sd);
      if (xColType == Table::Date)
        sd->setDateTimeOrigin(date0);
      else
        sd->setDateTimeOrigin(QDateTime(QDate::currentDate(), time0));
      plot->setAxisScaleDraw(axis, sd);
    }

    if (yColType == Table::Text)
      g->setLabelsTextFormat(QwtPlot::yLeft, ScaleDraw::Text, title().text(),
                             yLabels);
  }

  if (!d_labels_list.isEmpty()) {
    (static_cast<Graph *>(plot->parent()))->updatePlot();
    loadLabels();
  }
}
コード例 #2
0
void DataCurve::loadData()
{
	Graph *g = (Graph *)plot();
	if (!g)
		return;

	int xcol = d_table->colIndex(d_x_column);
	int ycol = d_table->colIndex(title().text());

	if (xcol < 0 || ycol < 0){
		remove();
		return;
	}

	int r = abs(d_end_row - d_start_row) + 1;
    QVarLengthArray<double> X(r), Y(r);
	int xColType = d_table->columnType(xcol);
	int yColType = d_table->columnType(ycol);

	QStringList xLabels, yLabels;// store text labels

	QTime time0;
	QDateTime date0;
	QString date_time_fmt = d_table->columnFormat(xcol);
	if (xColType == Table::Time){
		for (int i = d_start_row; i <= d_end_row; i++ ){
			QString xval=d_table->text(i,xcol);
			if (!xval.isEmpty()){
				time0 = QTime::fromString (xval, date_time_fmt);
				if (time0.isValid())
					break;
			}
		}
	} else if (xColType == Table::Date){
		for (int i = d_start_row; i <= d_end_row; i++ ){
			QString xval=d_table->text(i,xcol);
			if (!xval.isEmpty()){
				date0 = QDateTime::fromString (xval, date_time_fmt);
				if (date0.isValid())
					break;
			}
		}
	}

	int size = 0;
	for (int i = d_start_row; i <= d_end_row; i++ ){
		QString xval = d_table->text(i,xcol);
		QString yval = d_table->text(i,ycol);
		if (!xval.isEmpty() && !yval.isEmpty()){
		    bool valid_data = true;
			if (xColType == Table::Text){
				xLabels << xval;
				X[size] = (double)(size + 1);
			} else if (xColType == Table::Time){
				QTime time = QTime::fromString (xval, date_time_fmt);
				if (time.isValid())
					X[size]= time0.msecsTo (time);
			} else if (xColType == Table::Date){
				QDateTime d = QDateTime::fromString (xval, date_time_fmt);
				if (d.isValid())
					X[size] = (double) date0.secsTo(d);
			} else
				X[size] = g->locale().toDouble(xval, &valid_data);

			if (yColType == Table::Text){
				yLabels << yval;
				Y[size] = (double)(size + 1);
			} else
				Y[size] = g->locale().toDouble(yval, &valid_data);

            if (valid_data)
                size++;
		}
	}

    X.resize(size);
    Y.resize(size);

	if (!size){
		remove();
		return;
	} else {
		if (d_type == Graph::HorizontalBars){
			setData(Y.data(), X.data(), size);
			foreach(DataCurve *c, d_error_bars)
                c->setData(Y.data(), X.data(), size);
		} else {
			setData(X.data(), Y.data(), size);
			foreach(DataCurve *c, d_error_bars)
                c->setData(X.data(), Y.data(), size);
		}

		if (xColType == Table::Text){
			if (d_type == Graph::HorizontalBars)
				g->setLabelsTextFormat(QwtPlot::yLeft, ScaleDraw::Text, d_x_column, xLabels);
			else
                g->setLabelsTextFormat(QwtPlot::xBottom, ScaleDraw::Text, d_x_column, xLabels);
		} else if (xColType == Table::Time || xColType == Table::Date){
			int axis = QwtPlot::xBottom;
			if (d_type == Graph::HorizontalBars)
                axis = QwtPlot::yLeft;
            ScaleDraw *old_sd = (ScaleDraw *)g->axisScaleDraw(axis);
            ScaleDraw *sd = new ScaleDraw(g, old_sd);
            if (xColType == Table::Date)
                sd->setDateTimeOrigin(date0);
            else
                sd->setDateTimeOrigin(QDateTime(QDate::currentDate(), time0));
            g->setAxisScaleDraw(axis, sd);
		}

		if (yColType == Table::Text)
            g->setLabelsTextFormat(QwtPlot::yLeft, ScaleDraw::Text, title().text(), yLabels);
	}

    if (!d_labels_list.isEmpty()){
        ((Graph*)plot())->updatePlot();
        loadLabels();
    }
}