XCustomPlot *customPlot = new XCustomPlot(); customPlot->showTracer(true); // add title layout element:
customPlot->plotLayout()->insertRow(0); customPlot->plotLayout()->addElement(0, 0, new QCPTextElement(customPlot, "标题", QFont("黑体", 12, QFont::Bold))); 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::AlignTop|Qt::AlignCenter); // 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))); // Allow user to drag axis ranges with mouse, zoom with mouse wheel and select graphs by clicking:
customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables); // generate some data:
int nCount = 100; QVector<double> x(nCount), y0(nCount), y1(nCount); // initialize with entries 0..100
for (int i = 0; i < nCount; ++i) { x[i] = i/50.0 - 1; // x goes from -1 to 1
y0[i] = x[i]*x[i]; // let's plot a quadratic function
y1[i] = x[i]*x[i] - x[i]; // let's plot a quadratic function
} // create graph and assign data to it:
QPen pen; int i = 1; QCPGraph *pGraph = customPlot->addGraph(); // customPlot->graph(0)->setData(x, y0);
pGraph->setName("曲线"); pGraph->setData(x,y0); 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)); pGraph->setPen(pen); // give the axes some labels:
customPlot->xAxis->setLabel("x"); customPlot->yAxis->setLabel("y"); // set axes ranges, so we see all data:
customPlot->xAxis->setRange(-1, 1); customPlot->yAxis->setRange(0, 1); customPlot->repl