Loading...
qt 饼状图绘画
big_anana 发布于 05/19 16:57 浏览 77

由于 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();
}

显示效果图:

*本文仅代表作者观点,不代表易百纳技术社区立场。系作者授权易百纳技术社区发表,未经许可不得转载。

点赞0
收藏0

精彩评论

内容存在敏感词

自媒体


海思讨论群

Opencv双目视觉

联系我们
客服QQ: 1289459644
客服手机: 17714209983
确定要删除此文章、专栏、评论吗?
确定
取消

详细的问题描述有助于平台快速解决问题

屏幕截图
问题描述