void LinesVisualisation::performStepComputation() { // do not perform painting if not visible if(!isVisible()) return; clearGraphs(); QVector<double> x, y; // Paint the best for (int i = 0; i < swarm()->size(); i++) { const Particle *particle = (*swarm())[i]; x.clear(); y.clear(); y = particle->position; for (int j = 0; j < swarm()->dimension(); j++) { x.append(j + 1); } addGraph(); graph(i)->setData(x, y); graph(i)->setPen(VisualisationHelper::rainbowCoding(i, swarm()->size())); } xAxis->setRange(0, swarm()->dimension()); if (swarm()->dimension() < 20) { xAxis->setAutoTickStep(false); xAxis->setTickStep(1); } rescaleAxes(true); setInteraction(QCP::iRangeDrag, true); setInteraction(QCP::iRangeZoom, true); replot(); }
void viewGVpropertieslayout::contextMenuRequest(QPoint pos) { // first get a pointer to the current plot! QCustomPlot * currPlot = (QCustomPlot *) currentSubWindow->widget(); QMenu *menu = new QMenu(this); menu->setAttribute(Qt::WA_DeleteOnClose); if (currPlot->legend->selectTest(pos, false) >= 0) // context menu on legend requested { /*menu->addAction("Move to top left", this, SLOT(moveLegend()))->setData((int)(Qt::AlignTop|Qt::AlignLeft)); menu->addAction("Move to top center", this, SLOT(moveLegend()))->setData((int)(Qt::AlignTop|Qt::AlignHCenter)); menu->addAction("Move to top right", this, SLOT(moveLegend()))->setData((int)(Qt::AlignTop|Qt::AlignRight)); menu->addAction("Move to bottom right", this, SLOT(moveLegend()))->setData((int)(Qt::AlignBottom|Qt::AlignRight)); menu->addAction("Move to bottom left", this, SLOT(moveLegend()))->setData((int)(Qt::AlignBottom|Qt::AlignLeft));*/ } else if (currPlot->xAxis->selectTest(pos, false) >= 0 || \ currPlot->xAxis2->selectTest(pos, false) >= 0) { // enable / disable zoom if (currPlot->axisRect()->rangeZoom() & Qt::Horizontal) menu->addAction("Disable zoom on axis", this, SLOT(toggleHorizontalZoom())); else menu->addAction("Enable zoom on axis", this, SLOT(toggleHorizontalZoom())); // enable / diable drag if (currPlot->axisRect()->rangeDrag() & Qt::Horizontal) menu->addAction("Disable drag on axis", this, SLOT(toggleHorizontalDrag())); else menu->addAction("Enable drag on axis", this, SLOT(toggleHorizontalDrag())); } else if (currPlot->yAxis->selectTest(pos, false) >= 0 || \ currPlot->yAxis2->selectTest(pos, false) >= 0) { // enable / disable zoom if (currPlot->axisRect()->rangeZoom() & Qt::Vertical) menu->addAction("Disable zoom on axis", this, SLOT(toggleVerticalZoom())); else menu->addAction("Enable zoom on axis", this, SLOT(toggleVerticalZoom())); // enable / diable drag if (currPlot->axisRect()->rangeDrag() & Qt::Vertical) menu->addAction("Disable drag on axis", this, SLOT(toggleVerticalDrag())); else menu->addAction("Enable drag on axis", this, SLOT(toggleVerticalDrag())); } else { if (currPlot->graphCount() > 0) menu->addAction("Scale axes to fit", this, SLOT(rescaleAxes())); if (currPlot->selectedGraphs().size() > 0) menu->addAction("Remove selected graph", this, SLOT(removeSelectedGraph())); if (currPlot->graphCount() > 0) menu->addAction("Remove all graphs", this, SLOT(removeAllGraphs())); } menu->popup(currPlot->mapToGlobal(pos)); }
void AdvancedQCustomPlot::mousePressEvent(QMouseEvent *event) { if(event->buttons() & Qt::RightButton) { isZoomed_ = false; rescaleAxes(true); replot(); } QCustomPlot::mousePressEvent(event); }
void qmlPlotPaintedItem::addData(int index, QVariantList x, QVariantList y) { auto g = m_CustomPlot.graph(index); QVector<qreal> xx, yy; for (auto i = 0; i < x.size(); ++i) { xx.push_back(x[i].toReal()); yy.push_back(y[i].toReal()); } g->addData(xx, yy); g->rescaleAxes(); m_CustomPlot.replot(); }
DrawFinancialChart::DrawFinancialChart(QWidget *parent) : QCustomPlot(parent) { resize(600,400); legend->setVisible(true); // generate two sets of random walk data (one for candlestick and one for ohlc chart): int n = 500; QVector<double> time(n), value1(n), value2(n); QDateTime start = QDateTime(QDate(2014, 6, 11)); start.setTimeSpec(Qt::UTC); double startTime = start.toTime_t(); double binSize = 3600*24; // bin data in 1 day intervals time[0] = startTime; value1[0] = 60; value2[0] = 20; qsrand(9); for (int i=1; i<n; ++i) { time[i] = startTime + 3600*i; value1[i] = value1[i-1] + (qrand()/(double)RAND_MAX-0.5)*10; value2[i] = value2[i-1] + (qrand()/(double)RAND_MAX-0.5)*3; } // create candlestick chart: QCPFinancial *candlesticks = new QCPFinancial(xAxis, yAxis); addPlottable(candlesticks); QCPFinancialDataMap data1 = QCPFinancial::timeSeriesToOhlc(time, value1, binSize, startTime); candlesticks->setName("Candlestick"); candlesticks->setChartStyle(QCPFinancial::csCandlestick); candlesticks->setData(&data1, true); candlesticks->setWidth(binSize*0.9); candlesticks->setTwoColored(true); candlesticks->setBrushPositive(QColor(245, 245, 245)); candlesticks->setBrushNegative(QColor(0, 0, 0)); candlesticks->setPenPositive(QPen(QColor(0, 0, 0))); candlesticks->setPenNegative(QPen(QColor(0, 0, 0))); // create ohlc chart: QCPFinancial *ohlc = new QCPFinancial(xAxis, yAxis); addPlottable(ohlc); QCPFinancialDataMap data2 = QCPFinancial::timeSeriesToOhlc(time, value2, binSize/3.0, startTime); // divide binSize by 3 just to make the ohlc bars a bit denser ohlc->setName("OHLC"); ohlc->setChartStyle(QCPFinancial::csOhlc); ohlc->setData(&data2, true); ohlc->setWidth(binSize*0.2); ohlc->setTwoColored(true); // create bottom axis rect for volume bar chart: QCPAxisRect *volumeAxisRect = new QCPAxisRect(this); plotLayout()->addElement(1, 0, volumeAxisRect); 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: 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: QCPBars *volumePos = new QCPBars(volumeAxisRect->axis(QCPAxis::atBottom), volumeAxisRect->axis(QCPAxis::atLeft)); QCPBars *volumeNeg = new QCPBars(volumeAxisRect->axis(QCPAxis::atBottom), volumeAxisRect->axis(QCPAxis::atLeft)); for (int i=0; i<n/5; ++i) { int v = qrand()%20000+qrand()%20000+qrand()%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 } setAutoAddPlottableToLegend(false); addPlottable(volumePos); addPlottable(volumeNeg); volumePos->setWidth(3600*4); volumePos->setPen(Qt::NoPen); volumePos->setBrush(QColor(100, 180, 110)); volumeNeg->setWidth(3600*4); volumeNeg->setPen(Qt::NoPen); volumeNeg->setBrush(QColor(180, 90, 90)); // interconnect x axis ranges of main and bottom axis rects: connect(xAxis, SIGNAL(rangeChanged(QCPRange)), volumeAxisRect->axis(QCPAxis::atBottom), SLOT(setRange(QCPRange))); connect(volumeAxisRect->axis(QCPAxis::atBottom), SIGNAL(rangeChanged(QCPRange)), xAxis, SLOT(setRange(QCPRange))); // configure axes of both main and bottom axis rect: volumeAxisRect->axis(QCPAxis::atBottom)->setAutoTickStep(false); volumeAxisRect->axis(QCPAxis::atBottom)->setTickStep(3600*24*4); // 4 day tickstep volumeAxisRect->axis(QCPAxis::atBottom)->setTickLabelType(QCPAxis::ltDateTime); volumeAxisRect->axis(QCPAxis::atBottom)->setDateTimeSpec(Qt::UTC); volumeAxisRect->axis(QCPAxis::atBottom)->setDateTimeFormat("dd. MMM"); volumeAxisRect->axis(QCPAxis::atBottom)->setTickLabelRotation(15); volumeAxisRect->axis(QCPAxis::atLeft)->setAutoTickCount(3); xAxis->setBasePen(Qt::NoPen); xAxis->setTickLabels(false); xAxis->setTicks(false); // only want vertical grid in main axis rect, so hide xAxis backbone, ticks, and labels xAxis->setAutoTickStep(false); xAxis->setTickStep(3600*24*4); // 4 day tickstep rescaleAxes(); xAxis->scaleRange(1.025, xAxis->range().center()); yAxis->scaleRange(1.1, yAxis->range().center()); // make axis rects' left side line up: QCPMarginGroup *group = new QCPMarginGroup(this); axisRect()->setMarginGroup(QCP::msLeft|QCP::msRight, group); volumeAxisRect->setMarginGroup(QCP::msLeft|QCP::msRight, group); }