void MainWindow::initPlotter() { QCustomPlot *cp = this->ui->plotter; cp->addGraph(); cp->addGraph(); cp->addGraph(); cp->xAxis->setLabel("Epoch"); cp->yAxis->setLabel("fitness"); cp->graph(0)->setPen(QPen(Qt::blue)); cp->graph(1)->setPen(QPen(Qt::red)); cp->graph(2)->setPen(QPen(Qt::green)); cp->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables); cp->setLocale(QLocale(QLocale::Portuguese, QLocale::Brazil)); cp->legend->setVisible(true); }
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QCustomPlot *customPlot = (QCustomPlot*) (ui->graph); // 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().toTime_t(); 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(); QPen pen; pen.setColor(QColor(0, 0, 255, 200)); customPlot->graph()->setLineStyle(QCPGraph::lsLine); customPlot->graph()->setPen(pen); customPlot->graph()->setBrush(QBrush(QColor(255/4.0*gi,160,50,150))); // generate random walk data: QVector<double> time(250), value(250); for (int i=0; i<250; ++i) { time[i] = now + 24*3600*i; if (i == 0) value[i] = (i/50.0+1)*(rand()/(double)RAND_MAX-0.5); else value[i] = fabs(value[i-1])*(1+0.02/4.0*(4-gi)) + (i/50.0+1)*(rand()/(double)RAND_MAX-0.5); } customPlot->graph()->setData(time, value); } // configure bottom axis to show date and time instead of number: customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime); customPlot->xAxis->setDateTimeFormat("MMMM\nyyyy"); // 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 a fixed tick-step to one tick per month: customPlot->xAxis->setAutoTickStep(false); customPlot->xAxis->setTickStep(2628000); // one month in seconds customPlot->xAxis->setSubTickCount(3); // apply manual tick and tick label for left axis: customPlot->yAxis->setAutoTicks(false); customPlot->yAxis->setAutoTickLabels(false); customPlot->yAxis->setTickVector(QVector<double>() << 5 << 55); customPlot->yAxis->setTickVectorLabels(QVector<QString>() << "Not so\nhigh" << "Very\nhigh"); // 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: customPlot->legend->setVisible(true); }
void ChartPage::setupChart() { this->buildingChart=true; QCustomPlot *customPlot = ui->chartBlock; customPlot->setInteraction(QCP::iRangeDrag, true); customPlot->setInteraction(QCP::iSelectPlottables, true); customPlot->setInteraction(QCP::iSelectItems, true); customPlot->setInteraction(QCP::iRangeZoom, true); 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)); // create graph and assign data to it: customPlot->addGraph(); customPlot->graph(0)->setName("Difficulty"); // customPlot->graph(0)->setData(x, y); // give the axes some labels: customPlot->xAxis->setLabel("Date"); customPlot->yAxis->setLabel("Difficulty"); customPlot->legend->setSelectableParts(QCPLegend::spItems); customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes | QCP::iSelectLegend | QCP::iSelectPlottables); customPlot->axisRect()->setupFullAxesBox(); customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime); customPlot->xAxis->setDateTimeFormat("dd/MM/yyyy"); customPlot->xAxis->setAutoTickCount(5); //customPlot->yAxis->setAutoTickCount(5); // set axes ranges, so we see all data: customPlot->yAxis->setRange(-0.1, 3); ClientModel *model = this->clientModel; // generate some data: QVector<double> x(1000), y(1000); //, y1(50000); int maxBlocks; maxBlocks = model->getNumBlocks(); if (maxBlocks > 1000) { maxBlocks=1000; } int i = 0; if (maxBlocks > 0) { for (i = 0; i < maxBlocks; i++) { CBlock blk = model->getBlock(i); CBlockIndex* cIndex = model->getBlockIndex(i); x[i]=blk.GetBlockTime(); y[i]=model->getDiff(cIndex); } customPlot->graph(0)->setData(x, y); customPlot->xAxis->setRange(x[i-1]-(60*60*24), x[i-1]); this->lastBlockHeight = i; } customPlot->replot(); this->buildingChart=false; }