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.
https://stackoverflow.com/questions/9183050/vertical-qlabel-or-the-equivalent
From there:
void LabelWidget::paintEvent(QPaintEvent*) { QPainter painter(this); painter.setPen(Qt::black); //... Need an appropriate call to painter.translate() for this to work properly painter.rotate(90); 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?!
-
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?
Cheers
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.
https://stackoverflow.com/questions/51085719/showing-tooltip-in-a-qt-chart-with-multiple-y-axes
@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?
-
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.