由于 qcustomplot 有的版本不支持饼状图,所以这部分一般有个人绘画,用得较多的还是painter:
void CPieWidget::drawDefaultPie( QPainter *painter ) { qreal sum = getSumValue(); int w = width(); int h = height(); int radius = qMin(w,h)*0.8; //直径 QRect rect(w/2-radius/2,h/2-radius/2,radius,radius); painter->save(); painter->setPen(Qt::NoPen); QHashIterator<QString, float> i(m_datamap); qreal index = 30; //启始位置 int colorindex=0; QStringList keylist = m_datamap.keys(); for (int i = 0; i < keylist.count(); ++i) { qreal v = m_datamap.value(keylist.at(i)); v =v/sum*(360); QRect newrect = rect; if (m_explodedindex == i || m_isexploded) { QPoint newcenter = newrect.center(); int midangel = index+v/2; QPoint tp = getMovePoint(midangel); newcenter += tp; newrect.moveCenter(newcenter); } QPoint centerPoint = newrect.center(); QColor firstColor = m_colorlist.at(colorindex); QRadialGradient firstGradient(centerPoint, radius/2); firstGradient.setColorAt(0, firstColor.lighter(80)); firstGradient.setColorAt(1.0, firstColor.darker(80)); painter->setBrush(firstGradient); painter->drawPie(newrect, index * 16, v * 16); painter->setPen(firstColor); QRect textrect = newrect; textrect.setX( newrect.x()-30); textrect.setY( newrect.y()-30+24*i); painter->drawText(textrect,keylist.at(i)+” “+QString::number(m_datamap.value(keylist.at(i)))); index+=v; colorindex++; if (colorindex==m_colorlist.count()) { colorindex = 0; } } painter->restore(); }<
显示效果图:
免责声明:文章内容来自互联网,本站不对其真实性负责,也不承担任何法律责任,如有侵权等情况,请与本站联系删除。
转载请注明出处:qt 饼状图绘画 https://www.yhzz.com.cn/a/15974.html