QCustomPlot Discussion and Comments

different scaleReturn to overview

Hello, I have code

   ui->widget->plotLayout()->clear();
    // создаем оси и присвоим цвета
        QCPAxisRect *wideAxisRect = new QCPAxisRect(ui->widget); //
           wideAxisRect->setupFullAxesBox(true);
           wideAxisRect->axis(QCPAxis::atRight, 0)->setTickLabels(true);
                                                                                                                    //  Axis  |
           wideAxisRect->addAxis(QCPAxis::atLeft)->setTickLabelColor  (QColor (  0,255,  0));                       //    Y1  |
           wideAxisRect->addAxis(QCPAxis::atLeft)->setTickLabelColor  (QColor (255,  0,  0));                       //    Y2  |
           //wideAxisRect->addAxis(QCPAxis::atRight)->setTickLabelColor (QColor (  0,255,  0));                       //    Y3  |
           //wideAxisRect->addAxis(QCPAxis::atRight)->setTickLabelColor (QColor (  0,  0,255));
    //--------------------------------------
    // грузим все в оси
    ui->widget->plotLayout()->addElement  (0, 0, wideAxisRect);

      QCPGraph *mainGraph1 = ui->widget->addGraph(wideAxisRect->axis(QCPAxis::atBottom), wideAxisRect->axis(QCPAxis::atLeft));
      mainGraph1->setData(x1, y1);
      mainGraph1->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, QPen(Qt::red), QBrush(Qt::white), 1));
      mainGraph1->setPen(QPen(QColor( 120,  00,  00), 1));
      mainGraph1->rescaleAxes();

      QCPGraph *mainGraph2 = ui->widget->addGraph(wideAxisRect->axis(QCPAxis::atBottom), wideAxisRect->axis(QCPAxis::atLeft));
      mainGraph2->setData(x2, y2);
      mainGraph2->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, QPen(Qt::green), QBrush(Qt::white), 1));
      mainGraph2->setPen(QPen(QColor(   0, 120,  00), 1));
      mainGraph2->rescaleAxes();

      QCPGraph *mainGraph3 = ui->widget->addGraph(wideAxisRect->axis(QCPAxis::atBottom), wideAxisRect->axis(QCPAxis::atLeft));
      mainGraph3->setData(x3, y3);
      mainGraph3->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, QPen(Qt::blue), QBrush(Qt::white), 1));
      mainGraph3->setPen(QPen(QColor(   0,   0, 120), 1));
      mainGraph3->rescaleAxes();
    //--------------------------------------
     wideAxisRect->axis(QCPAxis::atLeft, 1)->setRangeLower(0);
    ui->widget->replot();

the Y1 Y2 Y3 axes are different in scale, and it scales only one by one
mainGraph1->rescaleAxes();
// or
mainGraph2->rescaleAxes();
//or
mainGraph3->rescaleAxes();

How to display graphs with different scales y1,y2,y3?

Hi, I understood that you need different Y-axis for each graph. This can be achieved as following:

// Remove default Y-axes from wideAxisRect
wideAxisRect->removeAxis(this->plot->yAxis);
wideAxisRect->removeAxis(this->plot->yAxis2);


//Graph1
QCPAxis *Yaxis1 = ui->widget->wideAxisRect->addAxis(QCPAxis::atLeft, 0);
QCPGraph *mainGraph1 = ui->widget->addGraph(wideAxisRect->axis(QCPAxis::atBottom), Yaxis1);
//do stuff
mainGraph1->rescaleAxes();


//Graph2 with Right Y-axis and common X-axis
QCPAxis *Yaxis2 = ui->widget->wideAxisRect->addAxis(QCPAxis::atRight, 0);
QCPGraph *mainGraph2 = ui->widget->addGraph(wideAxisRect->axis(QCPAxis::atBottom), Yaxis2);
//do stuff
mainGraph2->rescaleAxes();


//Graph3 with another Left Y-axis and common X-axis
QCPAxis *Yaxis3 = ui->widget->wideAxisRect->addAxis(QCPAxis::atLeft, 0);
QCPGraph *mainGraph3 = ui->widget->addGraph(wideAxisRect->axis(QCPAxis::atBottom), Yaxis3);
//do stuff
mainGraph3->rescaleAxes();


//Replot
ui->widget->replot();

Thank you very much. Here is the resulting code.

  ui->widget->clearGraphs();
  ui->widget->plotLayout()->clear();
  QCPAxisRect *wideAxisRect = new QCPAxisRect(ui->widget);
  wideAxisRect->removeAxis(this->ui->widget->yAxis);
  wideAxisRect->removeAxis(this->ui->widget->yAxis2);
  ui->widget->plotLayout()->addElement(0,0,wideAxisRect); 
  //*************************************
  QCPAxis *Yaxis1 = wideAxisRect->addAxis(QCPAxis::atLeft, 0);
  Yaxis1->setRange(mos_Ngtd[0],mos_Ngtd[4]);

  Yaxis1->setLabel("Ngtd=Ngtd(N_kvd_pr)");
  Yaxis1->setBasePen(QPen(Qt::red));
  Yaxis1->setLabelColor(Qt::red);
  Yaxis1->setTickLabelColor(Qt::red);

  QCPGraph *mainGraph1 = ui->widget->addGraph(wideAxisRect->axis(QCPAxis::atBottom),Yaxis1);
  mainGraph1->setData(x1,y1);
  mainGraph1->setPen(QPen(Qt::red));

  mainGraph1->rescaleAxes();
  //*************************************
  QCPAxis *Yaxis2 = wideAxisRect->addAxis(QCPAxis::atRight, 0);
  Yaxis2->setRange(mos_T04_pr[0],mos_T04_pr[3]);

  Yaxis2->setLabel("T04=T04(N_kvd_pr)");
  Yaxis2->setBasePen(QPen(Qt::blue));
  Yaxis2->setLabelColor(Qt::blue);
  Yaxis2->setTickLabelColor(Qt::blue);

  QCPGraph *mainGraph2 = ui->widget->addGraph(wideAxisRect->axis(QCPAxis::atBottom),Yaxis2);
  mainGraph2->setData(x2,y2);
  mainGraph2->setPen(QPen(Qt::blue));

  mainGraph2->rescaleAxes();
  //*************************************
  QCPAxis *Yaxis3 = wideAxisRect->addAxis(QCPAxis::atLeft, 0);
  Yaxis3->setRange(mos_Cn[0],mos_Cn[3]);

  Yaxis3->setLabel("Сn=Сn(N_kvd_pr)");
  Yaxis3->setBasePen(QPen(Qt::black));
  Yaxis3->setLabelColor(Qt::black);
  Yaxis3->setTickLabelColor(Qt::black);

  QCPGraph *mainGraph3 = ui->widget->addGraph(wideAxisRect->axis(QCPAxis::atBottom),Yaxis3);
  mainGraph3->setData(x3,y3);
  mainGraph3->setPen(QPen(Qt::black));

  mainGraph3->rescaleAxes();
  //*************************************
  ui->widget->replot();

There is a problem, the old axis is displayed Yaxis, how to hide or paint over it?
http://vfl.ru/fotos/dd0baca027452182.html
There was also a chart tracking function:
connect(ui->widget, &QCustomPlot::mouseMove, this, &MainWindow::slotMouseMove);

void MainWindow::slotMouseMove(QMouseEvent *event)
{
  double coordX = ui->widget->xAxis->pixelToCoord(event->pos().x());
  double coordY = ui->widget->yAxis->pixelToCoord(event->pos().y());
  double coordY2 = ui->widget->yAxis2->pixelToCoord(event->pos().y());
   ui->textEdit->setText("x: " + QString::number(coordX) +
                        " y1: " + QString::number(coordY)+
                        " y2:" + QString::number(coordY2));
}

how to connect now?