更新代码

This commit is contained in:
feiyangqingyun
2022-05-18 17:39:04 +08:00
parent 96938b94f9
commit a155ad45d3
32 changed files with 145 additions and 322 deletions

View File

@@ -15,17 +15,19 @@ frmAdvancedAxes::~frmAdvancedAxes()
void frmAdvancedAxes::initForm()
{
#ifndef qcustomplot_v1_3
// configure axis rect:
ui->customPlot->plotLayout()->clear(); // clear default axis rect so we can start from scratch
QCPAxisRect *wideAxisRect = new QCPAxisRect(ui->customPlot);
wideAxisRect->setupFullAxesBox(true);
wideAxisRect->axis(QCPAxis::atRight, 0)->setTickLabels(true);
wideAxisRect->addAxis(QCPAxis::atLeft)->setTickLabelColor(QColor("#6050F8")); // add an extra axis on the left and color its numbers
QCPLayoutGrid *subLayout = new QCPLayoutGrid;
ui->customPlot->plotLayout()->addElement(0, 0, wideAxisRect); // insert axis rect in first row
ui->customPlot->plotLayout()->addElement(1, 0, subLayout); // sub layout in second row (grid layout will grow accordingly)
//ui->customPlot->plotLayout()->setRowStretchFactor(1, 2);
// prepare axis rects that will be placed in the sublayout:
QCPAxisRect *subRectLeft = new QCPAxisRect(ui->customPlot, false); // false means to not setup default axes
QCPAxisRect *subRectRight = new QCPAxisRect(ui->customPlot, false);
@@ -33,6 +35,7 @@ void frmAdvancedAxes::initForm()
subLayout->addElement(0, 1, subRectRight);
subRectRight->setMaximumSize(100, 100); // make bottom right axis rect size fixed 100x100
subRectRight->setMinimumSize(100, 100); // make bottom right axis rect size fixed 100x100
// setup axes in sub layout axis rects:
subRectLeft->addAxes(QCPAxis::atBottom | QCPAxis::atLeft);
subRectRight->addAxes(QCPAxis::atBottom | QCPAxis::atRight);
@@ -40,11 +43,13 @@ void frmAdvancedAxes::initForm()
subRectRight->axis(QCPAxis::atRight)->ticker()->setTickCount(2);
subRectRight->axis(QCPAxis::atBottom)->ticker()->setTickCount(2);
subRectLeft->axis(QCPAxis::atBottom)->grid()->setVisible(true);
// synchronize the left and right margins of the top and bottom axis rects:
QCPMarginGroup *marginGroup = new QCPMarginGroup(ui->customPlot);
subRectLeft->setMarginGroup(QCP::msLeft, marginGroup);
subRectRight->setMarginGroup(QCP::msRight, marginGroup);
wideAxisRect->setMarginGroup(QCP::msLeft | QCP::msRight, marginGroup);
// move newly created axes on "axes" layer and grids on "grid" layer:
foreach (QCPAxisRect *rect, ui->customPlot->axisRects()) {
foreach (QCPAxis *axis, rect->axes()) {
@@ -79,6 +84,7 @@ void frmAdvancedAxes::initForm()
mainGraphCos->rescaleKeyAxis();
mainGraphCos->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, QPen(Qt::black), QBrush(Qt::white), 6));
mainGraphCos->setPen(QPen(QColor(120, 120, 120), 2));
QCPGraph *mainGraphGauss = ui->customPlot->addGraph(wideAxisRect->axis(QCPAxis::atBottom), wideAxisRect->axis(QCPAxis::atLeft, 1));
mainGraphGauss->data()->set(dataGauss);
mainGraphGauss->setPen(QPen(QColor("#8070B8"), 2));
@@ -103,12 +109,11 @@ void frmAdvancedAxes::initForm()
subBars->setBrush(QColor("#705BE8"));
subBars->keyAxis()->setSubTicks(false);
subBars->rescaleAxes();
// setup a ticker for subBars key axis that only gives integer ticks:
QSharedPointer<QCPAxisTickerFixed> intTicker(new QCPAxisTickerFixed);
intTicker->setTickStep(1.0);
intTicker->setScaleStrategy(QCPAxisTickerFixed::ssMultiples);
subBars->keyAxis()->setTicker(intTicker);
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
#endif
}

View File

@@ -15,7 +15,6 @@ frmBarChart::~frmBarChart()
void frmBarChart::initForm()
{
#ifndef qcustomplot_v1_3
// set dark background gradient:
QLinearGradient gradient(0, 0, 0, 400);
gradient.setColorAt(0, QColor(90, 90, 90));
@@ -27,12 +26,14 @@ void frmBarChart::initForm()
QCPBars *regen = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
QCPBars *nuclear = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
QCPBars *fossil = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
regen->setAntialiased(false); // gives more crisp, pixel aligned bar borders
nuclear->setAntialiased(false);
fossil->setAntialiased(false);
regen->setStackingGap(1);
nuclear->setStackingGap(1);
fossil->setStackingGap(1);
// set names and colors:
fossil->setName("Fossil fuels");
fossil->setPen(QPen(QColor(111, 9, 176).lighter(170)));
@@ -43,6 +44,7 @@ void frmBarChart::initForm()
regen->setName("Regenerative");
regen->setPen(QPen(QColor(0, 168, 140).lighter(130)));
regen->setBrush(QColor(0, 168, 140));
// stack bars on top of each other:
nuclear->moveAbove(fossil);
regen->moveAbove(nuclear);
@@ -52,6 +54,7 @@ void frmBarChart::initForm()
QVector<QString> labels;
ticks << 1 << 2 << 3 << 4 << 5 << 6 << 7;
labels << "USA" << "Japan" << "Germany" << "France" << "UK" << "Italy" << "Canada";
QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
textTicker->addTicks(ticks, labels);
ui->customPlot->xAxis->setTicker(textTicker);
@@ -97,5 +100,4 @@ void frmBarChart::initForm()
legendFont.setPointSize(10);
ui->customPlot->legend->setFont(legendFont);
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
#endif
}

View File

@@ -27,6 +27,7 @@ void frmColorMap::initForm()
int ny = 200;
colorMap->data()->setSize(nx, ny); // we want the color map to have nx * ny data points
colorMap->data()->setRange(QCPRange(-4, 4), QCPRange(-4, 4)); // and span the coordinate range -4..4 in both key (x) and value (y) dimensions
// now we assign some data, by accessing the QCPColorMapData instance of the color map:
double x, y, z;
for (int xIndex = 0; xIndex < nx; ++xIndex) {

View File

@@ -15,9 +15,9 @@ frmDate::~frmDate()
void frmDate::initForm()
{
#ifndef qcustomplot_v1_3
// set locale to english, so we get english month names:
ui->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
@@ -40,21 +40,26 @@ void frmDate::initForm()
}
ui->customPlot->graph()->data()->set(timeData);
}
// configure bottom axis to show date instead of number:
QSharedPointer<QCPAxisTickerDateTime> dateTicker(new QCPAxisTickerDateTime);
dateTicker->setDateTimeFormat("d. MMMM\nyyyy");
ui->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");
ui->customPlot->yAxis->setTicker(textTicker);
// set a more compact font size for bottom and left axis tick labels:
ui->customPlot->xAxis->setTickLabelFont(QFont(QFont().family(), 8));
ui->customPlot->yAxis->setTickLabelFont(QFont(QFont().family(), 8));
// set axis labels:
ui->customPlot->xAxis->setLabel("Date");
ui->customPlot->yAxis->setLabel("Random wobbly lines value");
// make top and right axes visible but without ticks and labels:
ui->customPlot->xAxis2->setVisible(true);
ui->customPlot->yAxis2->setVisible(true);
@@ -62,12 +67,13 @@ void frmDate::initForm()
ui->customPlot->yAxis2->setTicks(false);
ui->customPlot->xAxis2->setTickLabels(false);
ui->customPlot->yAxis2->setTickLabels(false);
// set axis ranges to show all data:
ui->customPlot->xAxis->setRange(now, now + 24 * 3600 * 249);
ui->customPlot->yAxis->setRange(0, 60);
// show legend with slightly transparent background brush:
ui->customPlot->legend->setVisible(true);
ui->customPlot->legend->setBrush(QColor(255, 255, 255, 150));
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
#endif
}

View File

@@ -60,10 +60,12 @@ void frmFinancial::initForm()
volumeAxisRect->setMaximumSize(QSize(QWIDGETSIZE_MAX, 100));
volumeAxisRect->axis(QCPAxis::atBottom)->setLayer("axes");
volumeAxisRect->axis(QCPAxis::atBottom)->grid()->setLayer("grid");
// bring bottom and main axis rect closer together:
ui->customPlot->plotLayout()->setRowSpacing(0);
volumeAxisRect->setAutoMargins(QCP::msLeft | QCP::msRight | QCP::msBottom);
volumeAxisRect->setMargins(QMargins(0, 0, 0, 0));
// create two bar plottables, for positive (green) and negative (red) volume bars:
ui->customPlot->setAutoAddPlottableToLegend(false);
QCPBars *volumePos = new QCPBars(volumeAxisRect->axis(QCPAxis::atBottom), volumeAxisRect->axis(QCPAxis::atLeft));
@@ -72,6 +74,7 @@ void frmFinancial::initForm()
int v = std::rand() % 20000 + std::rand() % 20000 + std::rand() % 20000 - 10000 * 3;
(v < 0 ? volumeNeg : volumePos)->addData(startTime + 3600 * 5.0 * i, qAbs(v)); // add data to either volumeNeg or volumePos, depending on sign of v
}
volumePos->setWidth(3600 * 4);
volumePos->setPen(Qt::NoPen);
volumePos->setBrush(QColor(100, 180, 110));
@@ -82,6 +85,7 @@ void frmFinancial::initForm()
// interconnect x axis ranges of main and bottom axis rects:
connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), volumeAxisRect->axis(QCPAxis::atBottom), SLOT(setRange(QCPRange)));
connect(volumeAxisRect->axis(QCPAxis::atBottom), SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis, SLOT(setRange(QCPRange)));
// configure axes of both main and bottom axis rect:
QSharedPointer<QCPAxisTickerDateTime> dateTimeTicker(new QCPAxisTickerDateTime);
dateTimeTicker->setDateTimeSpec(Qt::UTC);
@@ -100,6 +104,5 @@ void frmFinancial::initForm()
QCPMarginGroup *group = new QCPMarginGroup(ui->customPlot);
ui->customPlot->axisRect()->setMarginGroup(QCP::msLeft | QCP::msRight, group);
volumeAxisRect->setMarginGroup(QCP::msLeft | QCP::msRight, group);
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
}

View File

@@ -15,7 +15,7 @@ frmItem::~frmItem()
void frmItem::showEvent(QShowEvent *)
{
dataTimer.start(0); // Interval 0 means to refresh as fast as possible
dataTimer.start(0);
}
void frmItem::hideEvent(QHideEvent *)
@@ -35,6 +35,7 @@ void frmItem::initForm()
x[i] = i / (double)(n - 1) * 34 - 17;
y[i] = qExp(-x[i] * x[i] / 20.0) * qSin(k * x[i] + phase);
}
graph->setData(x, y);
graph->setPen(QPen(Qt::blue));
graph->rescaleKeyAxis();
@@ -150,10 +151,9 @@ void frmItem::bracketDataSlot()
x[i] = i / (double)(n - 1) * 34 - 17;
y[i] = qExp(-x[i] * x[i] / 20.0) * qSin(k * x[i] + phase);
}
ui->customPlot->graph()->setData(x, y);
phaseTracer->setGraphKey((8 * M_PI + fmod(M_PI * 1.5 - phase, 6 * M_PI)) / k);
ui->customPlot->replot();
// calculate frames per second:

View File

@@ -17,9 +17,11 @@ void frmLineStyle::initForm()
{
ui->customPlot->legend->setVisible(true);
ui->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) {
ui->customPlot->addGraph();
@@ -28,23 +30,28 @@ void frmLineStyle::initForm()
ui->customPlot->graph()->setName(lineNames.at(i - QCPGraph::lsNone));
ui->customPlot->graph()->setLineStyle((QCPGraph::LineStyle)i);
ui->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;
}
ui->customPlot->graph()->setData(x, y);
ui->customPlot->graph()->rescaleAxes(true);
}
// zoom out a bit:
ui->customPlot->yAxis->scaleRange(1.1, ui->customPlot->yAxis->range().center());
ui->customPlot->xAxis->scaleRange(1.1, ui->customPlot->xAxis->range().center());
// set blank axis lines:
ui->customPlot->xAxis->setTicks(false);
ui->customPlot->yAxis->setTicks(true);
ui->customPlot->xAxis->setTickLabels(false);
ui->customPlot->yAxis->setTickLabels(true);
// make top right axes clones of bottom left axes:
ui->customPlot->axisRect()->setupFullAxesBox();
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);

View File

@@ -17,6 +17,7 @@ void frmLogarithmic::initForm()
{
ui->customPlot->setNoAntialiasingOnDrag(true); // more performance/responsiveness during dragging
ui->customPlot->addGraph();
QPen pen;
pen.setColor(QColor(255, 170, 100));
pen.setWidth(2);
@@ -54,6 +55,7 @@ void frmLogarithmic::initForm()
dataPlusSinExp[i].key = i / 10.0;
dataPlusSinExp[i].value = qSin(dataPlusSinExp[i].key) * qExp(dataPlusSinExp[i].key);
}
for (int i = 0; i < dataFactorialCount; ++i) {
dataFactorial[i].key = i;
dataFactorial[i].value = 1.0;
@@ -61,6 +63,7 @@ void frmLogarithmic::initForm()
dataFactorial[i].value *= k; // factorial
}
}
ui->customPlot->graph(0)->data()->set(dataLinear);
ui->customPlot->graph(1)->data()->set(dataMinusSinExp);
ui->customPlot->graph(2)->data()->set(dataPlusSinExp);
@@ -70,6 +73,7 @@ void frmLogarithmic::initForm()
ui->customPlot->xAxis->grid()->setSubGridVisible(true);
ui->customPlot->yAxis->setScaleType(QCPAxis::stLogarithmic);
ui->customPlot->yAxis2->setScaleType(QCPAxis::stLogarithmic);
QSharedPointer<QCPAxisTickerLog> logTicker(new QCPAxisTickerLog);
ui->customPlot->yAxis->setTicker(logTicker);
ui->customPlot->yAxis2->setTicker(logTicker);
@@ -87,7 +91,5 @@ void frmLogarithmic::initForm()
ui->customPlot->legend->setVisible(true);
ui->customPlot->legend->setBrush(QBrush(QColor(255, 255, 255, 150)));
ui->customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignLeft | Qt::AlignTop); // make legend align in top left corner or axis rect
// make range draggable and zoomable:
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
}

View File

@@ -41,6 +41,7 @@ void frmMultiAxis::initForm()
ui->customPlot->graph(1)->setLineStyle(QCPGraph::lsStepCenter);
ui->customPlot->graph(1)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, Qt::red, Qt::white, 7));
ui->customPlot->graph(1)->setName("Bottom maxwell function");
QCPErrorBars *errorBars = new QCPErrorBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
errorBars->removeFromLegend();
errorBars->setDataPlottable(ui->customPlot->graph(1));
@@ -75,15 +76,18 @@ void frmMultiAxis::initForm()
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];
@@ -100,19 +104,23 @@ void frmMultiAxis::initForm()
ui->customPlot->graph(2)->setData(x2, y2);
ui->customPlot->graph(3)->setData(x3, y3);
ui->customPlot->graph(4)->setData(x4, y4);
// activate top and right axes, which are invisible by default:
ui->customPlot->xAxis2->setVisible(true);
ui->customPlot->yAxis2->setVisible(true);
// set ranges appropriate to show data:
ui->customPlot->xAxis->setRange(0, 2.7);
ui->customPlot->yAxis->setRange(0, 2.6);
ui->customPlot->xAxis2->setRange(0, 3.0 * M_PI);
ui->customPlot->yAxis2->setRange(-70, 35);
// set pi ticks on top axis:
ui->customPlot->xAxis2->setTicker(QSharedPointer<QCPAxisTickerPi>(new QCPAxisTickerPi));
// add title layout element:
ui->customPlot->plotLayout()->insertRow(0);
ui->customPlot->plotLayout()->addElement(0, 0, new QCPTextElement(ui->customPlot, "Way too many graphs in one plot", QFont("sans", 12, QFont::Bold)));
// set labels:
ui->customPlot->xAxis->setLabel("Bottom axis with outward ticks");
ui->customPlot->yAxis->setLabel("Left axis label");

View File

@@ -19,6 +19,7 @@ void frmParametricCurve::initForm()
QCPCurve *fermatSpiral1 = new QCPCurve(ui->customPlot->xAxis, ui->customPlot->yAxis);
QCPCurve *fermatSpiral2 = new QCPCurve(ui->customPlot->xAxis, ui->customPlot->yAxis);
QCPCurve *deltoidRadial = new QCPCurve(ui->customPlot->xAxis, ui->customPlot->yAxis);
// generate the curve data points:
const int pointCount = 500;
QVector<QCPCurveData> dataSpiral1(pointCount), dataSpiral2(pointCount), dataDeltoid(pointCount);
@@ -29,21 +30,25 @@ void frmParametricCurve::initForm()
dataSpiral2[i] = QCPCurveData(i, -dataSpiral1[i].key, -dataSpiral1[i].value);
dataDeltoid[i] = QCPCurveData(i, 2 * qCos(2 * theta) + qCos(1 * theta) + 2 * qSin(theta), 2 * qSin(2 * theta) - qSin(1 * theta));
}
// pass the data to the curves; we know t (i in loop above) is ascending, so set alreadySorted=true (saves an extra internal sort):
fermatSpiral1->data()->set(dataSpiral1, true);
fermatSpiral2->data()->set(dataSpiral2, true);
deltoidRadial->data()->set(dataDeltoid, true);
// color the curves:
fermatSpiral1->setPen(QPen(Qt::blue));
fermatSpiral1->setBrush(QBrush(QColor(0, 0, 255, 20)));
fermatSpiral2->setPen(QPen(QColor(255, 120, 0)));
fermatSpiral2->setBrush(QBrush(QColor(255, 120, 0, 30)));
QRadialGradient radialGrad(QPointF(310, 180), 200);
radialGrad.setColorAt(0, QColor(170, 20, 240, 100));
radialGrad.setColorAt(0.5, QColor(20, 10, 255, 40));
radialGrad.setColorAt(1, QColor(120, 20, 240, 10));
deltoidRadial->setPen(QPen(QColor(170, 20, 240)));
deltoidRadial->setBrush(QBrush(radialGrad));
// set some basic ui->customPlot config:
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
ui->customPlot->axisRect()->setupFullAxesBox();

View File

@@ -28,6 +28,7 @@ void frmPolarPlot::initForm()
ticker->setTickCount(8);
polarAxis->setTicker(ticker);
*/
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
angularAxis->setRangeDrag(false);
angularAxis->setTickLabelMode(QCPPolarAxisAngular::lmUpright);

View File

@@ -21,6 +21,7 @@ void frmQuadratic::initForm()
x[i] = i / 50.0 - 1; // x goes from -1 to 1
y[i] = x[i] * x[i]; // let's plot a quadratic function
}
// create graph and assign data to it:
ui->customPlot->addGraph();
ui->customPlot->graph(0)->setData(x, y);

View File

@@ -16,7 +16,7 @@ frmRealtimeData::~frmRealtimeData()
void frmRealtimeData::showEvent(QShowEvent *)
{
timeStart = QTime::currentTime();
dataTimer.start(0); // Interval 0 means to refresh as fast as possible
dataTimer.start(0);
}
void frmRealtimeData::hideEvent(QHideEvent *)
@@ -35,6 +35,7 @@ void frmRealtimeData::initForm()
ui->customPlot->yAxis->setTickLabelFont(font);
ui->customPlot->legend->setFont(font);
*/
ui->customPlot->addGraph(); // blue line
ui->customPlot->graph(0)->setPen(QPen(QColor(40, 110, 255)));
ui->customPlot->addGraph(); // red line
@@ -51,7 +52,7 @@ void frmRealtimeData::initForm()
connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange)));
// setup a timer that repeatedly calls MainWindow::realtimeDataSlot:
connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));
connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));
}
void frmRealtimeData::realtimeDataSlot()
@@ -68,6 +69,7 @@ void frmRealtimeData::realtimeDataSlot()
//ui->customPlot->graph(1)->rescaleValueAxis(true);
lastPointKey = key;
}
// make key axis range scroll with the data (at a constant range size of 8):
ui->customPlot->xAxis->setRange(key, 8, Qt::AlignRight);
ui->customPlot->replot();

View File

@@ -18,6 +18,7 @@ void frmScatterPixmap::initForm()
ui->customPlot->axisRect()->setBackground(QPixmap(":/image/bg1.jpg"));
ui->customPlot->addGraph();
ui->customPlot->graph()->setLineStyle(QCPGraph::lsLine);
QPen pen;
pen.setColor(QColor(255, 200, 20, 200));
pen.setStyle(Qt::DashLine);
@@ -27,6 +28,7 @@ void frmScatterPixmap::initForm()
ui->customPlot->graph()->setScatterStyle(QCPScatterStyle(QPixmap(":/image/data.png")));
// set graph name, will show up in legend next to icon:
ui->customPlot->graph()->setName("Data from Photovoltaic\nenergy barometer 2011");
// set data:
QVector<double> year, value;
year << 2005 << 2006 << 2007 << 2008 << 2009 << 2010 << 2011;
@@ -49,6 +51,7 @@ void frmScatterPixmap::initForm()
ui->customPlot->yAxis2->setSubTicks(false);
ui->customPlot->xAxis->setRange(2004.5, 2011.5);
ui->customPlot->yAxis->setRange(0, 52);
// setup legend:
ui->customPlot->legend->setFont(QFont(font().family(), 7));
ui->customPlot->legend->setIconSize(50, 20);

View File

@@ -70,6 +70,7 @@ void frmScatterStyle::initForm()
ui->customPlot->graph()->setScatterStyle(QCPScatterStyle(customScatterPath, QPen(Qt::black, 0), QColor(40, 70, 255, 50), 10));
}
}
// set blank axis lines:
ui->customPlot->rescaleAxes();
ui->customPlot->xAxis->setTicks(false);

View File

@@ -21,6 +21,7 @@ void frmSimple::initForm()
ui->customPlot->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20))); // first graph will be filled with translucent blue
ui->customPlot->addGraph();
ui->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) {
@@ -28,15 +29,18 @@ void frmSimple::initForm()
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:
// (see QCPAxisRect::setupFullAxesBox for a quicker method to do this)
ui->customPlot->xAxis2->setVisible(true);
ui->customPlot->xAxis2->setTickLabels(false);
ui->customPlot->yAxis2->setVisible(true);
ui->customPlot->yAxis2->setTickLabels(false);
// make left and bottom axes always transfer their ranges to right and top axes:
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)));
// pass data points to graphs:
ui->customPlot->graph(0)->setData(x, y0);
ui->customPlot->graph(1)->setData(x, y1);

View File

@@ -21,6 +21,7 @@ void frmSincScatter::initForm()
ui->customPlot->setLocale(QLocale(QLocale::English, QLocale::UnitedKingdom));
// add confidence band graphs:
ui->customPlot->addGraph();
QPen pen;
pen.setStyle(Qt::DotLine);
pen.setWidth(1);
@@ -32,6 +33,7 @@ void frmSincScatter::initForm()
ui->customPlot->legend->removeItem(ui->customPlot->legend->itemCount() - 1); // don't show two confidence band graphs in legend
ui->customPlot->graph(1)->setPen(pen);
ui->customPlot->graph(0)->setChannelFillGraph(ui->customPlot->graph(1));
// add theory curve graph:
ui->customPlot->addGraph();
pen.setStyle(Qt::DashLine);
@@ -39,12 +41,14 @@ void frmSincScatter::initForm()
pen.setColor(Qt::red);
ui->customPlot->graph(2)->setPen(pen);
ui->customPlot->graph(2)->setName("Theory Curve");
// add data point graph:
ui->customPlot->addGraph();
ui->customPlot->graph(3)->setPen(QPen(Qt::blue));
ui->customPlot->graph(3)->setName("Measurement");
ui->customPlot->graph(3)->setLineStyle(QCPGraph::lsNone);
ui->customPlot->graph(3)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCross, 4));
// add error bars:
QCPErrorBars *errorBars = new QCPErrorBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
errorBars->removeFromLegend();
@@ -62,6 +66,7 @@ void frmSincScatter::initForm()
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:
@@ -74,6 +79,7 @@ void frmSincScatter::initForm()
x1[i] *= 1000;
y1err[i] = 0.15;
}
// pass data to graphs and let Qui->customPlot determine the axes ranges so the whole thing is visible:
ui->customPlot->graph(0)->setData(x0, yConfUpper);
ui->customPlot->graph(1)->setData(x0, yConfLower);
@@ -82,12 +88,14 @@ void frmSincScatter::initForm()
errorBars->setData(y1err);
ui->customPlot->graph(2)->rescaleAxes();
ui->customPlot->graph(3)->rescaleAxes(true);
// setup look of bottom tick labels:
ui->customPlot->xAxis->setTickLabelRotation(30);
ui->customPlot->xAxis->ticker()->setTickCount(9);
ui->customPlot->xAxis->setNumberFormat("ebc");
ui->customPlot->xAxis->setNumberPrecision(1);
ui->customPlot->xAxis->moveRange(-10);
// make top right axes clones of bottom left axes. Looks prettier:
ui->customPlot->axisRect()->setupFullAxesBox();
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);

View File

@@ -20,18 +20,22 @@ void frmStyled::initForm()
QVector<double> x2(100), y2(100);
QVector<double> x3(20), y3(20);
QVector<double> x4(20), y4(20);
for (int i = 0; i < x1.size(); ++i) {
x1[i] = i / (double)(x1.size() - 1) * 10;
y1[i] = qCos(x1[i] * 0.8 + qSin(x1[i] * 0.16 + 1.0)) * qSin(x1[i] * 0.54) + 1.4;
}
for (int i = 0; i < x2.size(); ++i) {
x2[i] = i / (double)(x2.size() - 1) * 10;
y2[i] = qCos(x2[i] * 0.85 + qSin(x2[i] * 0.165 + 1.1)) * qSin(x2[i] * 0.50) + 1.7;
}
for (int i = 0; i < x3.size(); ++i) {
x3[i] = i / (double)(x3.size() - 1) * 10;
y3[i] = 0.05 + 3 * (0.5 + qCos(x3[i] * x3[i] * 0.2 + 2) * 0.5) / (double)(x3[i] + 0.7) + std::rand() / (double)RAND_MAX * 0.01;
}
for (int i = 0; i < x4.size(); ++i) {
x4[i] = x3[i];
y4[i] = (0.5 - y3[i]) + ((x4[i] - 2) * (x4[i] - 2) * 0.02);