Qt World Summit: Submit your Presentation

Label data points with vertical text (QChart)

  • Hi

    I writing a code that plots two spline graphs using QChartView and QChart. Inside that chart I wanted to add some rectangles without succees. In fact, I managed to add such rectangles with QGraphicsRectItem but they won't rescale when I resize the main window. Overriding with resizeEvent makes it all too slow.
    Therefore, I used QScatterSeries to create a data series in the chart that look like rectangles. Now I nedd to label them with some text.
    I activated the labels with setPointLabelVisible(true) and gave them a text using setPointLabelsFormat("text").
    My question now is, is there a way to make the text apear in vertical direction? Otherwise the text would overlap and therefore not be readable anymore.
    I attached a picture to visualize my problem.

    Best regards!

  • @delos

    I guess you have to write your own vertical Label-class and paint it above your data point manually.


    From there:

    void LabelWidget::paintEvent(QPaintEvent*)
        QPainter painter(this);
        //... Need an appropriate call to painter.translate() for this to work properly
        painter.drawText(QPoint(0,0), _text);

    Is it necessary to show the text at all time? Something like a ToolTip ( triggered by mouseHover over data point) might solve your problem?!

  • @Pl45m4

    Thanks for your answer. I tried things like using my own text objects but they won't scale with the data points. Mayby I didn't assign the to the data correctly. Actually I don't know how to d this ;)

    The tool tips are in fact a great idea. Could you let me know how to do this?


  • @delos

    There is this signal... (I've never used this before, but it should work)

    You connect your funtion to it and display your custom tooltip bubble with your information.


    @delos said in Label data points with vertical text (QChart):

    they won't scale with the data points

    If you resize the whole window or if you add more data points to your ScatterSeries?

  • @Pl45m4

    Both, whe I rescale and when I add more data points out of range.

  • @delos

    Then using tooltips should fix it, because the QXYSeries::hovered signal will send the point and the state (true / false = mouse over point / mouse left data point).
    You "only" need to map your tooltip data (label text) to your data points. Implementing the tooltip functionality might be not that easy but I dont see any other way, if using the default labels doesn't work for you.

  • Thanks @Pl45m4

    I'll try but it might take a while since I am just starting.
    When I'am done, I will report on my success or failure.


Log in to reply