0

How can I get the coordinates of a QPieSlice (center)?

Background: I want to add a "Callout" see here to my QChart which displays a QPieSeries. I have connected the hovered signal to a custom slot. In this slot, I have access to the QPieSlice. To display my "Callout" I need the x and y coordinates in my QChart coordinate frame. The anchor of the "Callout" should be the center of my currently hovered QPieSlice.

How can I get the coordinates of the QPieSlice center?

I tried something like:

double angle = pieSlice->startAngle()+pieSlice->angleSpan()/2;
double x = 1*cos(angle*deg2rad);
double y = 1*sin(angle*deg2rad);
callout->setAnchor(QPointF(x,y));
user7431005
  • 3,899
  • 4
  • 22
  • 49
  • Can you show a picture of your Pie chart with sample data? Computing the center of the pieces becomes more comlex if you change hole sizes or show exploded pie pieces. – Mailerdaimon Aug 06 '18 at 09:42

1 Answers1

1

You need to take the radius into acount as well.

If you want to get the center you take half the radius of your plot as input for the formular to get the point on a circle given below:

x = cx + r * cos(a)
y = cy + r * sin(a)

so, combined with your code this gives:

double angle = pieSlice->startAngle()+pieSlice->angleSpan()/2;
double calloutRadius= plotRadius*0.5;
double x = centerPlotX + calloutRadius*cos(angle*deg2rad);
double y = centerPlotY + calloutRadius*sin(angle*deg2rad);
callout->setAnchor(QPointF(x,y));

where

plotRadius = Radius of your pie plot
centerPlotX = the X center of your pie plot
centerPlotY = the Y center of your pie plot
Mailerdaimon
  • 6,003
  • 3
  • 35
  • 46
  • 1
    Thank you for your help! I also thought about that, but I do not know how to get the centerPlotX and centerPlotY coordinates. – user7431005 Aug 06 '18 at 08:59
  • I think the radius can be set by pieSeries->setPieSize() – user7431005 Aug 06 '18 at 09:01
  • Get the Rect of your chart and compute the center position from it. The center of the Pie is the center of your Chart (as default, otherwise use horizontalPosition and verticalPosition of the PieSeries. You can get the Chart object from your PieSeries. – Mailerdaimon Aug 06 '18 at 09:07