Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to display rectangle on a graph at a specific location.



  • I need to create a graph that displays rectangles that can be changed depending upon the user input values. And the rectangle should contain text.

    I used QAreaSeries to display a rectangle but there's no function to type text over it. I need to add a rectangle to the same graph every time that function is called.

    The first image is how I want to display.

    The second image is how my code displays the rectangle.

    QChart *qChart = new QChart();
    QChartView *myChartView = new QChartView(qChart);
    QValueAxis *qAxisX = new QValueAxis;
    QValueAxis qAxisY = new QValueAxis;
    qAxisX->setRange(0, 30);
    qAxisY->setRange(0, ending_height_of_rect
    2);

    QLineSeries *series0 = new QLineSeries();
    QLineSeries *series1 = new QLineSeries();

    *series0 << QPointF(1, starting_point_of_rectangle) << QPointF(1, ending_height_of_rect);
    *series1 << QPointF(4, starting_point_of_rectangle) << QPointF(4, ending_height_of_rect);

    QAreaSeries *series = new QAreaSeries(series0, series1);
    myChartView->chart()->addSeries(series);
    qChart->setAxisX(qAxisX, series);
    qChart->setAxisY(qAxisY, series);

    ui->verticalLayout->addWidget(myChartView);

    Annotation1.png

    Annotation 2.png

    Please let me know if I can use any other method instead of QAreaSeries which helps me get this work done a lot more easily.

    Any type of help is appreciated.

    Thank you.


  • Lifetime Qt Champion

    Hi
    I think you can reuse this example
    https://doc.qt.io/qt-5/qtcharts-callout-example.html


Log in to reply