How to have text centered inside each slice of a pie chart?

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How to have text centered inside each slice of a pie chart?



I would like to get the text labels (percentages) centered within each pie slice. It currently works a bit for two of the quadrants:



screenshot



What am I doing wrong?


void PieChartWidget::paintEvent(QPaintEvent *) {
QPainter painter(this);
QRectF size;
painter.setPen(QPen(Qt::black, 2));

if (this->height() > this->width()) {
size = QRectF(5, 5, this->width() - 10, this->width() - 10);
} else {
size = QRectF(5, 5, this->height() - 5, this->height() - 10);
}

double sum = 0.0, startAng = 0.0;
double angle, endAng;
double percent;

for (int i = 0; i < qvValues.size(); i++) {
sum += qvValues[i];
}
for (int i = 0; i < qvValues.size(); i++) {
percent = qvValues[i] / sum;
angle = percent * 360.0;
endAng = startAng + angle;
painter.setBrush(qvColors[i]);
painter.drawPie(size, static_cast<int>(startAng * 16),
static_cast<int>(angle * 16));
startAng = endAng;
if (percent != 0) {
double draw_x = width() / 2 +
cos(PI * (endAng / 180.0 - angle / 360.0)) * this->width() / 4.0;
double draw_y = height() / 2 +
sin(PI * (endAng / 180.0 - angle / 360.0)) * this->width() / 4.0;
painter.drawText(draw_x, draw_y, QString::number(percent * 100) + "%");
}
}
}





The question body should have all of the question. Currently your expected output ("I expect to have percent inside each part of the pie chart") is only put in the title. See meta post.
– user202729
Jul 17 at 15:47







Perhaps try changing QString::number(percent*100) to QString::number(percent*100.0)? Also, what is qvValues in this context?
– renspaceyi
Jul 17 at 15:50


QString::number(percent*100)


QString::number(percent*100.0)





@renspaceyi qvValues are the values taken from a table in excel , the purpose is to draw a piechart from an excel file
– Oumaima Sh
Jul 17 at 15:54




1 Answer
1



On this line:


painter.drawText(this->width()/4,this->height(), QString::number(percent*100)+"%");



You seem to draw the percentage in the same place every time. You do successfully draw the percentage for each section, they're just being drawn in the same place every time. Try changing it to this:


painter.drawText(double(i + 1) * this->width()/4,this->height(), QString::number(percent*100)+"%");



And you'll see what I mean. By multiplying the x value by some changing value, the x position of each drawn text will change, and thus you will be able to see the different percentages being drawn.



If you want it to draw in each quadrant, then your code might look something like this:


# define PI 3.14159265358979323846

...

double draw_x = this->width / 2.0 + cos(PI * (end_angle / 180.0 - angle / 360.0)) * this->width / 4.0;
double draw_y = this->height / 2.0 - sin(PI * (end_angle / 180.0 - angle / 360.0)) * this->width / 4.0;
painter.drawText(draw_x, draw_y, QString::number(percent*100)+"%");



Basically, what's happening in the above code is I'm calculating the x and y coords of the middle of each slice. Then, I'm drawing the percentages in those positions.





Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew
Jul 18 at 1:57






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

WumNWK lTCo6fPP0YsMcs Dh T1,B 2bIFIU8 GrBHD PfE
B,6BGU,4qFjapG

Popular posts from this blog

Makefile test if variable is not empty

Visual Studio Code: How to configure includePath for better IntelliSense results

Will Oldham