ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
ui->customPlot->axisRect()->setRangeDrag(Qt::Vertical);
ui->customPlot->axisRect()->setRangeZoom(Qt::Vertical);
QCPCurve *gr1 = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
QCPCurve *gr2 = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
customPlot->addPlottable(gr1);
customPlot->addPlottable(gr2);
ui->customPlot->addGraph(ui->customPlot->xAxis, ui->customPlot->yAxis);
ui->customPlot->graph(0)->setPen(QPen(Qt::green));
ui->customPlot->graph(0)->setLineStyle(QCPGraph::lsLine);
ui->customPlot->graph(0)->setName("first graph");
ui->customPlot->addGraph(ui->customPlot->xAxis2, ui->customPlot->yAxis2);
ui->customPlot->graph(1)->setPen(QPen(Qt::red));
ui->customPlot->graph(1)->setLineStyle(QCPGraph::lsLine);
ui->customPlot->graph(1)->setName("second graph");
// generate data, just playing with numbers, not much to learn here:
QVector<double> x(250), y1(250), y2(250);
for (int i=0; i<250; ++i)
{
x[i] = i;
y1[i] = sin(x[i]/10);
y2[i] = cos(x[i]/7) + 2;
}
// pass data points to graphs:
ui->customPlot->graph(0)->setData(x, y1);
ui->customPlot->graph(1)->setData(x, y2);
// activate top and right axes, which are invisible by default:
ui->customPlot->xAxis->setVisible(true);
ui->customPlot->yAxis->setVisible(true);
ui->customPlot->xAxis2->setVisible(true);
ui->customPlot->yAxis2->setVisible(true);
ui->customPlot->graph(0)->rescaleAxes(true);
ui->customPlot->graph(1)->rescaleAxes(true);
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
ui->customPlot->axisRect()->setRangeDrag(Qt::Vertical);
ui->customPlot->axisRect()->setRangeZoom(Qt::Vertical);
ui->customPlot->replot();
customPlot->graph(0)->rescaleAxes(true);
customPlot->graph(1)->rescaleAxes(true);
надо писать так: // let the ranges scale themselves so graph 0 fits perfectly in the visible area:
customPlot->graph(0)->rescaleAxes();
// same thing for graph 1, but only enlarge ranges (in case graph 1 is smaller than graph 0):
customPlot->graph(1)->rescaleAxes(true);
// Note: we could have also just called customPlot->rescaleAxes(); instead
void MainWindow::graphics()
{
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
ui->customPlot->axisRect()->setRangeDrag(Qt::Vertical);
ui->customPlot->axisRect()->setRangeZoom(Qt::Vertical);
ui->customPlot->addGraph(ui->customPlot->xAxis, ui->customPlot->yAxis);
ui->customPlot->addGraph(ui->customPlot->xAxis, ui->customPlot->yAxis);
ui->customPlot->addGraph(ui->customPlot->xAxis, ui->customPlot->yAxis);
ui->customPlot->graph(0)->setLineStyle(QCPGraph::lsLine);
ui->customPlot->graph(1)->setLineStyle(QCPGraph::lsLine);
ui->customPlot->graph(2)->setLineStyle(QCPGraph::lsLine);
ui->customPlot->graph(0)->setPen(QPen(Qt::red, 5));
ui->customPlot->graph(1)->setPen(QPen(Qt::yellow, 5));
ui->customPlot->graph(2)->setPen(QPen(Qt::green, 5));
ui->customPlot->xAxis->setVisible(true);
ui->customPlot->yAxis->setVisible(true);
ui->customPlot->xAxis2->setVisible(true);
ui->customPlot->yAxis2->setVisible(true);
ui->customPlot->xAxis->setBasePen(QPen(Qt::black));
ui->customPlot->yAxis->setBasePen(QPen(Qt::black));
ui->customPlot->xAxis2->setBasePen(QPen(Qt::black));
ui->customPlot->yAxis2->setBasePen(QPen(Qt::black));
QVector<double> x(250), y1(250), y2(250), y3(250);
for (int i=0; i<250; ++i)
{
x[i] = i;
y1[i] = sin(x[i]/10);
y2[i] = sin(x[i]/10) + 1.0;
y3[i] = cos(x[i]/10) + 2.0;
}
connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange)));
connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange)));
ui->customPlot->graph(0)->setData(x, y1);
ui->customPlot->graph(1)->setData(x, y2);
ui->customPlot->graph(2)->setData(x, y3);
ui->customPlot->rescaleAxes();
ui->customPlot->replot();
}
void MainWindow::on_btn_green_check_clicked()
{
disconnect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange)));
disconnect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange)));
//все графики кроме зеленого переключаем на 2-ю неподвижную оцифровку
ui->customPlot->graph(0)->setKeyAxis(ui->customPlot->xAxis2);
ui->customPlot->graph(0)->setValueAxis(ui->customPlot->yAxis2);
ui->customPlot->graph(1)->setKeyAxis(ui->customPlot->xAxis2);
ui->customPlot->graph(1)->setValueAxis(ui->customPlot->yAxis2);
ui->customPlot->xAxis->setBasePen(QPen(Qt::green, 2));
ui->customPlot->yAxis->setBasePen(QPen(Qt::green, 2));
ui->customPlot->replot();
}
void MainWindow::on_btn_green_uncheck_clicked()
{
ui->customPlot->yAxis->setRange(ui->customPlot->yAxis2->range()); //можно это не делать (или делать, если нужно)
connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange)));
connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange)));
//все графики кроме зеленого возвращаем на 1-ю подвижную оцифровку
ui->customPlot->graph(0)->setKeyAxis(ui->customPlot->xAxis);
ui->customPlot->graph(0)->setValueAxis(ui->customPlot->yAxis);
ui->customPlot->graph(1)->setKeyAxis(ui->customPlot->xAxis);
ui->customPlot->graph(1)->setValueAxis(ui->customPlot->yAxis);
ui->customPlot->xAxis->setBasePen(QPen(Qt::black));
ui->customPlot->yAxis->setBasePen(QPen(Qt::black));
ui->customPlot->replot();
}
void MainWindow::on_btn_green_check_clicked()
{
disconnect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange)));
disconnect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange)));
//все графики кроме зеленого переключаем на 2-ю неподвижную оцифровку
ui->customPlot->graph(0)->setKeyAxis(ui->customPlot->xAxis2);
ui->customPlot->graph(0)->setValueAxis(ui->customPlot->yAxis2);
ui->customPlot->graph(1)->setKeyAxis(ui->customPlot->xAxis2);
ui->customPlot->graph(1)->setValueAxis(ui->customPlot->yAxis2);
ui->customPlot->xAxis->setBasePen(QPen(Qt::green, 2));
ui->customPlot->yAxis->setBasePen(QPen(Qt::green, 2));
//dop
// ui->customPlot->yAxis->setVisible(true);
ui->customPlot->yAxis->setAutoTicks(false);
ui->customPlot->yAxis->setAutoTickLabels(false);
ui->customPlot->yAxis->setAutoSubTicks(false);
ui->customPlot->yAxis->setTickVector(QVector<double>()
<< 1.0 << 2.0 << 3.0);
ui->customPlot->yAxis->setTickVectorLabels(QVector<QString>()
<< "1,0" << "2,0" << "3,0");
//dop
ui->customPlot->replot();
}
void MainWindow::on_btn_green_uncheck_clicked()
{
ui->customPlot->yAxis->setRange(ui->customPlot->yAxis2->range()); //можно это не делать (или делать, если нужно)
connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange)));
connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange)));
//все графики кроме зеленого возвращаем на 1-ю подвижную оцифровку
ui->customPlot->graph(0)->setKeyAxis(ui->customPlot->xAxis);
ui->customPlot->graph(0)->setValueAxis(ui->customPlot->yAxis);
ui->customPlot->graph(1)->setKeyAxis(ui->customPlot->xAxis);
ui->customPlot->graph(1)->setValueAxis(ui->customPlot->yAxis);
ui->customPlot->xAxis->setBasePen(QPen(Qt::black));
ui->customPlot->yAxis->setBasePen(QPen(Qt::black));
//dop
// ui->customPlot->yAxis->setVisible(false);
ui->customPlot->yAxis->setAutoTicks(true);
ui->customPlot->yAxis->setAutoTickLabels(true);
ui->customPlot->yAxis->setAutoSubTicks(true);
//dop
ui->customPlot->replot();
}
on_btn_green_check_clicked()
кнопок вроде как нет. connect(ui->customPlot, SIGNAL(plottableClick(QCPAbstractPlottable*,QMouseEvent*)),
this, SLOT(graphClicked(QCPAbstractPlottable*)));
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
graphics();
connect(ui->customPlot, SIGNAL(plottableClick(QCPAbstractPlottable*,QMouseEvent*)),
this, SLOT(graphClicked(QCPAbstractPlottable*)));
}
void MainWindow::graphics()
{
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
ui->customPlot->axisRect()->setRangeDrag(Qt::Vertical); // peremeshenie po OY
ui->customPlot->axisRect()->setRangeZoom(Qt::Vertical); // mashtab po OX
ui->customPlot->addGraph(ui->customPlot->xAxis, ui->customPlot->yAxis);
ui->customPlot->addGraph(ui->customPlot->xAxis, ui->customPlot->yAxis);
ui->customPlot->addGraph(ui->customPlot->xAxis, ui->customPlot->yAxis);
ui->customPlot->graph(0)->setLineStyle(QCPGraph::lsLine);
ui->customPlot->graph(1)->setLineStyle(QCPGraph::lsLine);
ui->customPlot->graph(2)->setLineStyle(QCPGraph::lsLine);
ui->customPlot->graph(0)->setPen(QPen(Qt::red, 3));
ui->customPlot->graph(1)->setPen(QPen(Qt::yellow, 3));
ui->customPlot->graph(2)->setPen(QPen(Qt::green, 3));
ui->customPlot->xAxis->setVisible(true);
ui->customPlot->yAxis->setVisible(false);
ui->customPlot->xAxis2->setVisible(false);
ui->customPlot->yAxis2->setVisible(false);
ui->customPlot->xAxis->setBasePen(QPen(Qt::black));
ui->customPlot->yAxis->setBasePen(QPen(Qt::black));
ui->customPlot->xAxis2->setBasePen(QPen(Qt::black));
ui->customPlot->yAxis2->setBasePen(QPen(Qt::black));
QVector<double> x(250), y1(250), y2(250), y3(250);
for (int i=0; i<250; ++i)
{
x[i] = i;
y1[i] = sin(x[i]/10);
y2[i] = sin(x[i]/7) + 1.0;
y3[i] = cos(x[i]/5) + 2.0;
}
// connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange)));
// connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange)));
ui->customPlot->graph(0)->setData(x, y1);
ui->customPlot->graph(1)->setData(x, y2);
ui->customPlot->graph(2)->setData(x, y3);
ui->customPlot->graph(0)->setName("graph 0");
ui->customPlot->graph(1)->setName("graph 1");
ui->customPlot->graph(2)->setName("graph 2");
ui->customPlot->rescaleAxes();
ui->customPlot->replot();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::graphClicked(QCPAbstractPlottable *plottable)
{
ui->statusBar->showMessage(QString("Clicked on graph '%1'.").arg(plottable->name()), 1000);
qDebug() << plottable->name();
if (plottable->name() == "graph 0")
{
ui->customPlot->yAxis->setVisible(true);
ui->customPlot->graph(0)->setKeyAxis(ui->customPlot->xAxis);
ui->customPlot->graph(0)->setValueAxis(ui->customPlot->yAxis);
ui->customPlot->graph(1)->setKeyAxis(ui->customPlot->xAxis2);
ui->customPlot->graph(1)->setValueAxis(ui->customPlot->yAxis2);
ui->customPlot->graph(2)->setKeyAxis(ui->customPlot->xAxis2);
ui->customPlot->graph(2)->setValueAxis(ui->customPlot->yAxis2);
ui->customPlot->yAxis->setAutoTicks(false);
ui->customPlot->yAxis->setAutoTickLabels(false);
ui->customPlot->yAxis->setAutoSubTicks(false);
ui->customPlot->yAxis->setTickVector(QVector<double>()
<< 1.0 << 2.0 << 3.0);
ui->customPlot->yAxis->setTickVectorLabels(QVector<QString>()
<< "1,0" << "2,0" << "3,0");
ui->customPlot->rescaleAxes();
ui->customPlot->replot();
}
if (plottable->name() == "graph 1")
{
ui->customPlot->graph(1)->setKeyAxis(ui->customPlot->xAxis);
ui->customPlot->graph(1)->setValueAxis(ui->customPlot->yAxis);
ui->customPlot->graph(0)->setKeyAxis(ui->customPlot->xAxis2);
ui->customPlot->graph(0)->setValueAxis(ui->customPlot->yAxis2);
ui->customPlot->graph(2)->setKeyAxis(ui->customPlot->xAxis2);
ui->customPlot->graph(2)->setValueAxis(ui->customPlot->yAxis2);
}
}
enum PositionType { ptAbsolute ///< Static positioning in pixels, starting from the top left corner of the viewport/widget.
,ptViewportRatio ///< Static positioning given by a fraction of the viewport size.
,ptAxisRectRatio ///< Static positioning given by a fraction of the axis rect size (see \ref setAxisRect).
,ptPlotCoords ///< Dynamic positioning at a plot coordinate defined by two axes (see \ref setAxes).
};
и все же задала вопрос на родном форуме qcustomplot. Успехов. QCPCurve *gr1 = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
QCPCurve *gr2 = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
customPlot->addPlottable(gr1);
customPlot->addPlottable(gr2);
gr1->setPen(QPen(Qt::darkGreen, 50, Qt::SolidLine));
gr2->setPen(QPen(Qt::darkGreen, 50, Qt::SolidLine));
int pointCount = 2;
QVector<double> x(pointCount), y(pointCount);
x[0] = 0.0; x[1] = 120.0; y[0] = 20.0; y[1] = y[0];
gr1->setData(x, y);
x[0] = 0.0; x[1] = 150.0; y[0] = 50.0; y[1] = y[0];
gr2->setData(x, y);
customPlot->axisRect()->setupFullAxesBox();
customPlot->xAxis->setRange(0.0, 200.0);
customPlot->yAxis->setRange(0.0, 100.0);
QCPCurve *gr1 = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
QCPCurve *gr2 = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
customPlot->addPlottable(gr1);
customPlot->addPlottable(gr2);
gr1->setPen(QPen(Qt::darkGreen, 50, Qt::SolidLine));
gr2->setPen(QPen(Qt::darkGreen, 50, Qt::SolidLine));
int pointCount = 2;
QVector<double> x(pointCount), y(pointCount);
x[0] = 0.0; x[1] = 120.0; y[0] = 20.0; y[1] = y[0];
gr1->setData(x, y);
x[0] = 0.0; x[1] = 150.0; y[0] = 50.0; y[1] = y[0];
gr2->setData(x, y);
customPlot->axisRect()->setupFullAxesBox();
customPlot->xAxis->setRange(0.0, 200.0);
customPlot->yAxis->setRange(0.0, 100.0);