Reputation: 4539
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));
Upvotes: 0
Views: 432
Reputation: 6080
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
Upvotes: 1