QT chart PlotArea
Solved
General and Desktop
-
void ExChart::InitializeChart() { m_pChartViewer = new QChartView(createLineChart()); m_pChartViewer->setRenderHint(QPainter::Antialiasing); m_pChartViewer->chart()->legend()->hide(); this->ui.verticalLayout->addWidget(m_pChartViewer); UpdateChart(); } QChart* ExChart::createLineChart() { m_pChart = new QChart(); m_pChart->setTitle("Line chart"); QPen pen(QColor(47, 245, 76)); pen.setWidth(3); QString name("Series "); int nameIndex = 0; m_pLineSeries = new QLineSeries(m_pChart); m_pLineSeries->setPen(pen); m_pScatSeries = new QScatterSeries(m_pChart); m_pChart->addSeries(m_pLineSeries); m_pChart->addSeries(m_pScatSeries); m_pChart->createDefaultAxes(); m_pChart->axes(Qt::Horizontal).first()->setRange(0, 100); m_pChart->axes(Qt::Vertical).first()->setRange(0, 100); return m_pChart; } void ExChart::UpdateChart() { QRectF rect = m_pChartViewer->chart()->plotArea(); // only 차트 사이즈 (마진 제외) for (int i = 0; i < 100; i++) { qreal spacing = (((rect.right() - rect.left()) / ((qreal)100 - (qreal)0)) * i) + rect.right(); int y = QRandomGenerator::global()->bounded(0, 101); m_pLineSeries->append(i, y); m_pScatSeries->append(i, y); m_vPoint.push_back(QPointF(i, y)); QString str = QString("X: %1\nY: %2").arg(i).arg(y); m_pTextItem = new QGraphicsTextItem(m_pChart); m_pTextItem->setFont(QFont("Bahnschrift Condensed")); m_pTextItem->setZValue(12); m_pTextItem->setDefaultTextColor(Qt::white); m_pTextItem->setHtml(QString("<div style='background-color: rgba(255,0,0,150);'>") + str + "</div>"); qreal PosX = m_pChart->mapToPosition(QPointF(i, y), m_pLineSeries).x(); qreal PosY = m_pChart->mapToPosition(QPointF(i, y), m_pLineSeries).y(); m_pChart->mapToScene(PosX, PosY); m_pTextItem->setPos(PosX, PosY); } }
What I want to do is to mark the x,y values for each position of the scatter.
I don't think it's working. It's always zero.
qreal PosX = m_pChart->mapToPosition(QPointF(i, y), m_pLineSeries).x();
qreal PosY = m_pChart->mapToPosition(QPointF(i, y), m_pLineSeries).y();