Search code examples
qtqtcharts

Extracting XY Coordinates from QTCharts (a line series) on Mouse click


I'm trying to pick the X and Y Coordinates from a 2D Chart in QT when a mouse click is done on the 2D Chart widget.

I tried to use the mapFromScene , mapToValue and other similar functions that are available in the QTChart Library. But I'm unable to get the values.

Sample 2D Chart

Expected Output for XY Co-ords Extraction


Solution

  • I got the expected output for this problem.

    This is my Code which works fine for me.

    // function for mouse press events
    void 2D_Graph::mousePressEvent (QMouseEvent * e) {
        if(chartView->chart()->isEnabled()) {
            auto curPoint = QCursor::pos();
            curPoint = chartView->mapFromGlobal(curPoint);
            auto pickVal = chartView->mapToScene(curPoint);
            pickVal = chartView->chart()->mapFromScene(curPoint);
            pickVal = chartView->chart()->mapToValue(curPoint,chartView->chart()->series().at(0));
            qDebug() << "Diagram Picked Value : " << pickVal;
        }
    }