示例1
- 包含两个折线图
- 折线图颜色,和X轴的闭合填充颜色
- 坐标轴自动缩放
void MainWindow::setupSimpleDemo(QCustomPlot *customPlot)
{
demoName = "Simple Demo";
// 添加2个折线图
// add two new graphs and set their look:
customPlot->addGraph();
// 设置画笔就是线的颜色
customPlot->graph(0)->setPen(QPen(Qt::blue)); // line color blue for first graph
//设置画刷就会用画刷填充与X的轴闭合区域
customPlot->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20))); // first graph will be filled with translucent blue
customPlot->addGraph();
customPlot->graph(1)->setPen(QPen(Qt::red)); // line color red for second graph
// generate some points of data (y0 for first, y1 for second graph):
QVector<double> x(251), y0(251), y1(251);
for (int i=0; i<251; ++i)
{
x[i] = i;
y0[i] = qExp(-i/150.0)*qCos(i/10.0); // exponentially decaying cosine
y1[i] = qExp(-i/150.0); // exponential envelope
}
// configure right and top axis to show ticks but no labels:
// 一个轴矩形包含4个轴,上下左右,设置可以显示的坐标轴 (see QCPAxisRect::setupFullAxesBox for a quicker method to do this)
customPlot->xAxis2->setVisible(true);
customPlot->xAxis2->setTickLabels(false);
customPlot->yAxis2->setVisible(true);
customPlot->yAxis2->setTickLabels(false);
// 自动跳帧坐标轴 make left and bottom axes always transfer their ranges to right and top axes:
connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));
// 给折线图添加数据 pass data points to graphs:
customPlot->graph(0)->setData(x, y0);
customPlot->graph(1)->setData(x, y1);
// 自适应坐标轴
// 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
// Allow user to drag axis ranges with mouse, zoom with mouse wheel and select graphs by clicking:
//允许缩放、拖动、plot可以选着
customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
}
示例2
-
添加多个曲线
-
使用了误差线
-
坐标州的旋转、刻度显示格式、精度
-
连个折线图的闭合区域填充
void MainWindow::setupSincScatterDemo(QCustomPlot *customPlot)
{
demoName = "Sinc Scatter Demo";
//显示图例
customPlot->legend->setVisible(true);
// 设置图例字体和大小
customPlot->legend->setFont(QFont("Helvetica",9));
// set locale to english, so we get english decimal separator:
customPlot->setLocale(QLocale(QLocale::English, QLocale::UnitedKingdom));
// add confidence band graphs:
// 添加第0个
customPlot->addGraph();
QPen pen;
pen.setStyle(Qt::DotLine);
pen.setWidth(1);
pen.setColor(QColor(180,180,180));
customPlot->graph(0)->setName("Confidence Band 68%");//这个名字也就是图例显示的内容
customPlot->graph(0)->setPen(pen);
customPlot->graph(0)->setBrush(QBrush(QColor(255,50,30,20)));
//添加第1个
customPlot->addGraph();
// 第一个图例不显示
// customPlot->legend->removeItem(customPlot->legend->itemCount()-1); // don't show two confidence band graphs in legend
customPlot->graph(1)->setPen(pen);
// 设置折线图0和折线图1之间的闭合区域填充
customPlot->graph(0)->setChannelFillGraph(customPlot->graph(1));
// 添加第2个
// add theory curve graph:
customPlot->addGraph();
pen.setStyle(Qt::DashLine);
pen.setWidth(2);
pen.setColor(Qt::red);
customPlot->graph(2)->setPen(pen);
customPlot->graph(2)->setName("Theory Curve");
//添加第3个折线图
// add data point graph:
customPlot->addGraph();
customPlot->graph(3)->setPen(QPen(Qt::blue));
customPlot->graph(3)->setName("Measurement");
customPlot->graph(3)->setLineStyle(QCPGraph::lsNone);
customPlot->graph(3)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCross, 4));
// add error bars:
QCPErrorBars *errorBars = new QCPErrorBars(customPlot->xAxis, customPlot->yAxis);
errorBars->setName("error bars");
//不显示图例
errorBars->removeFromLegend();
errorBars->setAntialiased(false);
//绑定 error barba 参数数据就是第3个线图
errorBars->setDataPlottable(customPlot->graph(3));
errorBars->setPen(QPen(QColor(255,180,180)));
// 生成数据generate ideal sinc curve data and some randomly perturbed data for scatter plot:
QVector<double> x0(250), y0(250);
QVector<double> yConfUpper(250), yConfLower(250);
for (int i=0; i<250; ++i)
{
x0[i] = (i/249.0-0.5)*30+0.01; // by adding a small offset we make sure not do divide by zero in next code line
y0[i] = qSin(x0[i])/x0[i]; // sinc function
yConfUpper[i] = y0[i]+0.15;
yConfLower[i] = y0[i]-0.15;
x0[i] *= 1000;
}
QVector<double> x1(50), y1(50), y1err(50);
for (int i=0; i<50; ++i)
{
// generate a gaussian distributed random number:
double tmp1 = rand()/(double)RAND_MAX;
double tmp2 = rand()/(double)RAND_MAX;
double r = qSqrt(-2*qLn(tmp1))*qCos(2*M_PI*tmp2); // box-muller transform for gaussian distribution
// set y1 to value of y0 plus a random gaussian pertubation:
x1[i] = (i/50.0-0.5)*30+0.25;
y1[i] = qSin(x1[i])/x1[i]+r*0.15;
x1[i] *= 1000;
y1err[i] = 0.15;
}
y1[25]=1;
y1err[25]=0.5;
// 设置数据给Plot pass data to graphs and let QCustomPlot determine the axes ranges so the whole thing is visible:
customPlot->graph(0)->setData(x0, yConfUpper);
customPlot->graph(1)->setData(x0, yConfLower);
customPlot->graph(2)->setData(x0, y0);
//给误差bar设置误差值
customPlot->graph(3)->setData(x1, y1);
errorBars->setData(y1err);
customPlot->graph(0)->rescaleAxes(true);
// customPlot->graph(1)->rescaleAxes(true);
// customPlot->graph(2)->rescaleAxes(true);
// customPlot->graph(3)->rescaleAxes(true);
// setup look of bottom tick labels:
// x轴的刻度旋转30°
customPlot->xAxis->setTickLabelRotation(30);
customPlot->xAxis->ticker()->setTickCount(10);
//坐标轴数字显示格式
customPlot->xAxis->setNumberFormat("ebc");
//保留小数位数/精度
customPlot->xAxis->setNumberPrecision(1);
customPlot->yAxis->setRange(-1,3);
//移动坐标轴
customPlot->xAxis->moveRange(-10);
customPlot->yAxis->moveRange(0);
// make top right axes clones of bottom left axes. Looks prettier:
customPlot->axisRect()->setupFullAxesBox();
}
示例3
1.各种图的散点风格示例
void MainWindow::setupScatterStyleDemo(QCustomPlot *customPlot)
{
demoName = "Scatter Style Demo";
customPlot->legend->setVisible(true);
customPlot->legend->setFont(QFont("Helvetica", 9));
customPlot->legend->setRowSpacing(-3);
QVector<QCPScatterStyle::ScatterShape> shapes;
shapes << QCPScatterStyle::ssCross;
shapes << QCPScatterStyle::ssPlus;
shapes << QCPScatterStyle::ssCircle;
shapes << QCPScatterStyle::ssDisc;
shapes << QCPScatterStyle::ssSquare;
shapes << QCPScatterStyle::ssDiamond;
shapes << QCPScatterStyle::ssStar;
shapes << QCPScatterStyle::ssTriangle;
shapes << QCPScatterStyle::ssTriangleInverted;
shapes << QCPScatterStyle::ssCrossSquare;
shapes << QCPScatterStyle::ssPlusSquare;
shapes << QCPScatterStyle::ssCrossCircle;
shapes << QCPScatterStyle::ssPlusCircle;
shapes << QCPScatterStyle::ssPeace;
shapes << QCPScatterStyle::ssCustom;
QPen pen;
// add graphs with different scatter styles:
for (int i=0; i<shapes.size(); ++i)
{
customPlot->addGraph();
pen.setColor(QColor(qSin(i*0.3)*100+100, qSin(i*0.6+0.7)*100+100, qSin(i*0.4+0.6)*100+100));
// generate data:
QVector<double> x(10), y(10);
for (int k=0; k<10; ++k)
{
x[k] = k/10.0 * 4*3.14 + 0.01;
y[k] = 7*qSin(x[k])/x[k] + (shapes.size()-i)*5;
}
customPlot->graph()->setData(x, y);
customPlot->graph()->rescaleAxes(true);
customPlot->graph()->setPen(pen);
customPlot->graph()->setName(QCPScatterStyle::staticMetaObject.enumerator(QCPScatterStyle::staticMetaObject.indexOfEnumerator("ScatterShape")).valueToKey(shapes.at(i)));
customPlot->graph()->setLineStyle(QCPGraph::lsLine);
// set scatter style:
if (shapes.at(i) != QCPScatterStyle::ssCustom)
{
customPlot->graph()->setScatterStyle(QCPScatterStyle(shapes.at(i), 10));
}
else
{
QPainterPath customScatterPath;
for (int i=0; i<3; ++i)
customScatterPath.cubicTo(qCos(2*M_PI*i/3.0)*9, qSin(2*M_PI*i/3.0)*9, qCos(2*M_PI*(i+0.9)/3.0)*9, qSin(2*M_PI*(i+0.9)/3.0)*9, 0, 0);
customPlot->graph()->setScatterStyle(QCPScatterStyle(customScatterPath, QPen(Qt::black, 0), QColor(40, 70, 255, 50), 10));
}
}
// 重新调节坐标轴set blank axis lines:
customPlot->rescaleAxes(true);
customPlot->xAxis->setTicks(false);
customPlot->yAxis->setTicks(false);
customPlot->xAxis->setTickLabels(false);
customPlot->yAxis->setTickLabels(false);
// make top right axes clones of bottom left axes:
customPlot->axisRect()->setupFullAxesBox();
}
折线图风格示例
可选风格
enum LineStyle { lsNone ///< data points are not connected with any lines (e.g. data only represented
///< with symbols according to the scatter style, see \ref setScatterStyle)
,lsLine ///< data points are connected by a straight line
,lsStepLeft ///< line is drawn as steps where the step height is the value of the left data point
,lsStepRight ///< line is drawn as steps where the step height is the value of the right data point
,lsStepCenter ///< line is drawn as steps where the step is in between two data points
,lsImpulse ///< each data point is represented by a line parallel to the value axis, which reaches from the data point to the zero-value-line
};
Q_ENUMS(LineStyle)
示例代码
void MainWindow::setupLineStyleDemo(QCustomPlot *customPlot)
{
demoName = "Line Style Demo";
customPlot->legend->setVisible(true);
customPlot->legend->setFont(QFont("Helvetica", 9));
QPen pen;
QStringList lineNames;
lineNames << "lsNone" << "lsLine" << "lsStepLeft" << "lsStepRight" << "lsStepCenter" << "lsImpulse";
// add graphs with different line styles:
for (int i=QCPGraph::lsNone; i<=QCPGraph::lsImpulse; ++i)
{
customPlot->addGraph();
pen.setColor(QColor(qSin(i*1+1.2)*80+80, qSin(i*0.3+0)*80+80, qSin(i*0.3+1.5)*80+80));
customPlot->graph()->setPen(pen);
customPlot->graph()->setName(lineNames.at(i-QCPGraph::lsNone));
customPlot->graph()->setLineStyle((QCPGraph::LineStyle)i);
customPlot->graph()->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5));
// generate data:
QVector<double> x(15), y(15);
for (int j=0; j<15; ++j)
{
x[j] = j/15.0 * 5*3.14 + 0.01;
y[j] = 7*qSin(x[j])/x[j] - (i-QCPGraph::lsNone)*5 + (QCPGraph::lsImpulse)*5 + 2;
}
customPlot->graph()->setData(x, y);
customPlot->graph()->rescaleAxes(true);
}
// zoom out a bit:
customPlot->yAxis->scaleRange(1.1, customPlot->yAxis->range().center());
customPlot->xAxis->scaleRange(1.1, customPlot->xAxis->range().center());
// set blank axis lines:
customPlot->xAxis->setTicks(false);
customPlot->yAxis->setTicks(true);
customPlot->xAxis->setTickLabels(false);
customPlot->yAxis->setTickLabels(true);
// make top right axes clones of bottom left axes:
customPlot->axisRect()->setupFullAxesBox();
}
给散点添加图片作为散点样式 示例
void MainWindow::setupScatterPixmapDemo(QCustomPlot *customPlot)
{
demoName = "Scatter Pixmap Demo";
//设置背景图
customPlot->axisRect()->setBackground(QPixmap("./solarpanels.jpg"));
customPlot->addGraph();
customPlot->graph()->setLineStyle(QCPGraph::lsLine);
QPen pen;
pen.setColor(QColor(255, 200, 20, 200));
pen.setStyle(Qt::DashLine);
pen.setWidthF(2.5);
customPlot->graph(0)->setPen(pen);
customPlot->graph(0)->setBrush(QBrush(QColor(255,200,20,70)));
//设置散点为一个图片,同时图例也会使用这个图
customPlot->graph(0)->setScatterStyle(QCPScatterStyle(QPixmap("./sun.png")));
// set graph name, will show up in legend next to icon:
customPlot->graph(0)->setName("Data from Photovoltaic\nenergy barometer 2011");
// set data:
QVector<double> year, value;
year << 2005 << 2006 << 2007 << 2008 << 2009 << 2010 << 2011;
value << 2.17 << 3.42 << 4.94 << 10.38 << 15.86 << 29.33 << 52.1;
customPlot->graph(0)->setData(year, value);
// set title of plot:
customPlot->plotLayout()->insertRow(0);
customPlot->plotLayout()->addElement(0, 0, new QCPTextElement(customPlot, "Regenerative Energies", QFont("sans", 12, QFont::Bold)));
// axis configurations:
customPlot->xAxis->setLabel("Year");
customPlot->yAxis->setLabel("Installed Gigawatts of\nphotovoltaic in the European Union");
customPlot->xAxis2->setVisible(true);
customPlot->yAxis2->setVisible(true);
customPlot->xAxis2->setTickLabels(false);
customPlot->yAxis2->setTickLabels(false);
customPlot->xAxis2->setTicks(false);
customPlot->yAxis2->setTicks(false);
customPlot->xAxis2->setSubTicks(false);
customPlot->yAxis2->setSubTicks(false);
customPlot->xAxis->setRange(2004.5, 2011.5);
customPlot->yAxis->setRange(0, 52);
// setup legend:
customPlot->legend->setFont(QFont(font().family(), 7));
// 设置图例中ico的大小
customPlot->legend->setIconSize(50, 20);
customPlot->legend->setVisible(true);
//设置图例的显示位置
customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignLeft | Qt::AlignTop);
}
使用时间刻度类 示例
- 创建1个对象
- 设置时间格式
- 设置刻度对象给指定轴
void MainWindow::setupDateDemo(QCustomPlot *customPlot)
{
demoName = "Date Demo";
// set locale to english, so we get english month names:
customPlot->setLocale(QLocale(QLocale::English, QLocale::UnitedKingdom));
// seconds of current time, we'll use it as starting point in time for data:
double now = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0;
srand(8); // set the random seed, so we always get the same random data
// create multiple graphs:
for (int gi=0; gi<5; ++gi)
{
customPlot->addGraph();
customPlot->graph(gi)->setName(QString("G%1").arg(gi));
QColor color(20+200/4.0*gi,70*(1.6-gi/4.0), 150, 150);
customPlot->graph()->setLineStyle(QCPGraph::lsLine);
customPlot->graph()->setPen(QPen(color.lighter(200)));
customPlot->graph()->setBrush(QBrush(color));
// generate random walk data:
QVector<QCPGraphData> timeData(250);
for (int i=0; i<250; ++i)
{
timeData[i].key = now + 24*3600*i;
if (i == 0)
timeData[i].value = (i/50.0+1)*(rand()/(double)RAND_MAX-0.5);
else
timeData[i].value = qFabs(timeData[i-1].value)*(1+0.02/4.0*(4-gi)) + (i/50.0+1)*(rand()/(double)RAND_MAX-0.5);
}
customPlot->graph()->data()->set(timeData);
}
// 时间刻度类QCPAxisTickerDateTime ; configure bottom axis to show date instead of number:
QSharedPointer<QCPAxisTickerDateTime> dateTicker(new QCPAxisTickerDateTime);
// 时间开个格式
dateTicker->setDateTimeFormat("d. MMMM\nyyyy");
// 给X轴设置时间刻度
customPlot->xAxis->setTicker(dateTicker);
// configure left axis text labels:
QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
textTicker->addTick(10, "a bit\nlow");
textTicker->addTick(50, "quite\nhigh");
customPlot->yAxis->setTicker(textTicker);
// 设置刻度的字体 set a more compact font size for bottom and left axis tick labels:
customPlot->xAxis->setTickLabelFont(QFont(QFont().family(), 8));
customPlot->yAxis->setTickLabelFont(QFont(QFont().family(), 8));
// set axis labels:
customPlot->xAxis->setLabel("Date");
customPlot->yAxis->setLabel("Random wobbly lines value");
// make top and right axes visible but without ticks and labels:
customPlot->xAxis2->setVisible(true);
customPlot->yAxis2->setVisible(true);
customPlot->xAxis2->setTicks(false);
customPlot->yAxis2->setTicks(false);
customPlot->xAxis2->setTickLabels(false);
customPlot->yAxis2->setTickLabels(false);
// set axis ranges to show all data:
customPlot->xAxis->setRange(now, now+24*3600*249);
customPlot->yAxis->setRange(0, 60);
// 显示图例 show legend with slightly transparent background brush:
customPlot->legend->setVisible(true);
customPlot->legend->setBrush(QColor(255, 255, 255, 150));
}
使用图片作为填充物,也就二十设置画刷
void MainWindow::setupTextureBrushDemo(QCustomPlot *customPlot)
{
demoName = "Texture Brush Demo";
// add two graphs with a textured fill:
customPlot->addGraph();
QPen redDotPen;
redDotPen.setStyle(Qt::DotLine);
redDotPen.setColor(QColor(170, 100, 100, 180));
redDotPen.setWidthF(2);
customPlot->graph(0)->setPen(redDotPen);
// 核心:设置画刷就是一个图片,实现图片作为2条线的填充
customPlot->graph(0)->setBrush(QBrush(QPixmap("./balboa.jpg"))); // fill with texture of specified image
customPlot->addGraph();
customPlot->graph(1)->setPen(QPen(Qt::red));
// activate channel fill for graph 0 towards graph 1:
customPlot->graph(0)->setChannelFillGraph(customPlot->graph(1));
// generate data:
QVector<double> x(250);
QVector<double> y0(250), y1(250);
for (int i=0; i<250; ++i)
{
// just playing with numbers, not much to learn here
x[i] = 3*i/250.0;
y0[i] = 1+qExp(-x[i]*x[i]*0.8)*(x[i]*x[i]+x[i]);
y1[i] = 1-qExp(-x[i]*x[i]*0.4)*(x[i]*x[i])*0.1;
}
// pass data points to graphs:
customPlot->graph(0)->setData(x, y0);
customPlot->graph(1)->setData(x, y1);
// activate top and right axes, which are invisible by default:
customPlot->xAxis2->setVisible(true);
customPlot->yAxis2->setVisible(true);
// make tick labels invisible on top and right axis:
customPlot->xAxis2->setTickLabels(false);
customPlot->yAxis2->setTickLabels(false);
// set ranges:
customPlot->xAxis->setRange(0, 2.5);
customPlot->yAxis->setRange(0.9, 1.6);
// assign top/right axes same properties as bottom/left:
customPlot->axisRect()->setupFullAxesBox();
}
显示4个轴,添加标题 ,使用π坐标轴
-
π坐标轴:QCPAxisTickerPi
-
图表题:QCPTextElement
void MainWindow::setupMultiAxisDemo(QCustomPlot *customPlot)
{
customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
demoName = "Multi Axis Demo";
customPlot->setLocale(QLocale(QLocale::English, QLocale::UnitedKingdom)); // period as decimal separator and comma as thousand separator
// 显示图例
customPlot->legend->setVisible(true);
QFont legendFont = font(); // start out with MainWindow's font..
legendFont.setPointSize(9); // and make a bit smaller for legend
customPlot->legend->setFont(legendFont);
customPlot->legend->setBrush(QBrush(QColor(255,255,255,230)));
// by default, the legend is in the inset layout of the main axis rect. So this is how we access it to change legend placement:
customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignBottom|Qt::AlignRight);
// setup for graph 0: key axis left, value axis bottom
// will contain left maxwell-like function
customPlot->addGraph(customPlot->yAxis, customPlot->xAxis);
customPlot->graph(0)->setPen(QPen(QColor(255, 100, 0)));
customPlot->graph(0)->setBrush(QBrush(QPixmap("./balboa.jpg"))); // fill with texture of specified image
customPlot->graph(0)->setLineStyle(QCPGraph::lsLine);
customPlot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 5));
customPlot->graph(0)->setName("Left maxwell function");
// setup for graph 1: key axis bottom, value axis left (those are the default axes)
// will contain bottom maxwell-like function with error bars
customPlot->addGraph();
customPlot->graph(1)->setPen(QPen(Qt::red));
customPlot->graph(1)->setBrush(QBrush(QPixmap("./balboa.jpg"))); // same fill as we used for graph 0
customPlot->graph(1)->setLineStyle(QCPGraph::lsStepCenter);
customPlot->graph(1)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, Qt::red, Qt::white, 7));
customPlot->graph(1)->setName("Bottom maxwell function");
QCPErrorBars *errorBars = new QCPErrorBars(customPlot->xAxis, customPlot->yAxis);
errorBars->removeFromLegend();
errorBars->setDataPlottable(customPlot->graph(1));
// setup for graph 2: key axis top, value axis right
// will contain high frequency sine with low frequency beating:
customPlot->addGraph(customPlot->xAxis2, customPlot->yAxis2);
customPlot->graph(2)->setPen(QPen(Qt::blue));
customPlot->graph(2)->setName("High frequency sine");
// setup for graph 3: same axes as graph 2
// will contain low frequency beating envelope of graph 2
customPlot->addGraph(customPlot->xAxis2, customPlot->yAxis2);
QPen blueDotPen;
blueDotPen.setColor(QColor(30, 40, 255, 150));
blueDotPen.setStyle(Qt::DotLine);
blueDotPen.setWidthF(4);
customPlot->graph(3)->setPen(blueDotPen);
customPlot->graph(3)->setName("Sine envelope");
// setup for graph 4: key axis right, value axis top
// will contain parabolically distributed data points with some random perturbance
customPlot->addGraph(customPlot->yAxis2, customPlot->xAxis2);
customPlot->graph(4)->setPen(QColor(50, 50, 50, 255));
customPlot->graph(4)->setLineStyle(QCPGraph::lsNone);
customPlot->graph(4)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 4));
customPlot->graph(4)->setName("Some random data around\na quadratic function");
// generate data, just playing with numbers, not much to learn here:
QVector<double> x0(25), y0(25);
QVector<double> x1(15), y1(15), y1err(15);
QVector<double> x2(250), y2(250);
QVector<double> x3(250), y3(250);
QVector<double> x4(250), y4(250);
for (int i=0; i<25; ++i) // data for graph 0
{
x0[i] = 3*i/25.0;
y0[i] = qExp(-x0[i]*x0[i]*0.8)*(x0[i]*x0[i]+x0[i]);
}
for (int i=0; i<15; ++i) // data for graph 1
{
x1[i] = 3*i/15.0;;
y1[i] = qExp(-x1[i]*x1[i])*(x1[i]*x1[i])*2.6;
y1err[i] = y1[i]*0.25;
}
for (int i=0; i<250; ++i) // data for graphs 2, 3 and 4
{
x2[i] = i/250.0*3*M_PI;
x3[i] = x2[i];
x4[i] = i/250.0*100-50;
y2[i] = qSin(x2[i]*12)*qCos(x2[i])*10;
y3[i] = qCos(x3[i])*10;
y4[i] = 0.01*x4[i]*x4[i] + 1.5*(rand()/(double)RAND_MAX-0.5) + 1.5*M_PI;
}
// pass data points to graphs:
customPlot->graph(0)->setData(x0, y0);
customPlot->graph(1)->setData(x1, y1);
errorBars->setData(y1err);
customPlot->graph(2)->setData(x2, y2);
customPlot->graph(3)->setData(x3, y3);
customPlot->graph(4)->setData(x4, y4);
// activate top and right axes, which are invisible by default:
// 显示顶部X轴,显示右侧Y轴
customPlot->xAxis2->setVisible(true);
customPlot->yAxis2->setVisible(true);
// set ranges appropriate to show data:
// 设置每个轴的显示范围
customPlot->xAxis->setRange(0, 2.7);
customPlot->yAxis->setRange(0, 2.6);
customPlot->xAxis2->setRange(0, 3.0*M_PI);
customPlot->yAxis2->setRange(-70, 35);
// set pi ticks on top axis:
// 设置顶部X轴为一个π轴
customPlot->xAxis2->setTicker(QSharedPointer<QCPAxisTickerPi>(new QCPAxisTickerPi));
// 给Plot 设置一个标题 使用QCPTextElement add title layout element:
customPlot->plotLayout()->insertRow(0);
customPlot->plotLayout()->addElement(0, 0, new QCPTextElement(customPlot, "Way too many graphs in one plot", QFont("sans", 12, QFont::Bold)));
// set labels:
// 设置每个轴的标签
customPlot->xAxis->setLabel("Bottom axis with outward ticks");
customPlot->yAxis->setLabel("Left axis label");
customPlot->xAxis2->setLabel("Top axis label");
customPlot->yAxis2->setLabel("Right axis label");
// make ticks on bottom axis go outward:
customPlot->xAxis->setTickLength(0, 5);
customPlot->xAxis->setSubTickLength(0, 3);
// make ticks on right axis go inward and outward:
customPlot->yAxis2->setTickLength(3, 3);
customPlot->yAxis2->setSubTickLength(1, 1);
}