QWT读取MySQL数据并绘制图形_QwtPlot之绘制统计图(三)

(double amount, double planAmount, double purchaseAmount, EnAmount enAmount)

: QwtColumnSymbol(),

m_amount(amount),

m_planAmount(planAmount),

m_purchaseAmount(purchaseAmount),

m_enAmount(enAmount),

m_textHeight(18),

m_textSpacing(10),

m_width(30)

{

//数据四舍五入之后再比较,如果使用真实值比较,则可能存在显示相同,但高度、颜色不一致

m_amount = fRound(m_amount, 0);

m_planAmount = fRound(m_planAmount, 0);

m_purchaseAmount = fRound(m_purchaseAmount, 0);

m_color = QColor(0, 160, 230);

if (m_enAmount == PurchaseAmount)

{

if (sameValue(m_planAmount, 0))

{

if (sameValue(m_purchaseAmount, 0))

{

m_color = Qt::yellow;

}

else if (m_purchaseAmount > 0)

{

m_color = Qt::red;

}

}

else

{

if (sameValue(m_planAmount, m_purchaseAmount))

{

m_color = Qt::yellow;

}

else if (qAbs(m_planAmount - m_purchaseAmount) / m_planAmount <= 0.05)

{

m_color = Qt::yellow;

}

else

{

if (m_planAmount > m_purchaseAmount)

{

m_color = Qt::green;

}

else

{

m_color = Qt::red;

}

}

}

}

m_color.setAlpha(180);

}

//绘制柱形图

void GJHOverdraftColumnSymbol::draw(QPainter *painter, const QwtColumnRect &rect) const

{

QRectF rectF = rect.toRect();

if (!rectF.isValid())

{

return;

}

Qt::Alignment alignment = Qt::AlignTop | Qt::AlignHCenter;

int nHeight = rectF.height();

//设置绘制区域,宽固定为30

if (rectF.width() != m_width)

{

int nLeft = (rectF.width() - m_width) / 2;

rectF = QRect(rectF.left() + nLeft, rectF.top(), m_width, rectF.height());

}

if (m_enAmount == PurchaseAmount)

{

int nPurchaseHeight = nHeight;

if (sameValue(m_purchaseAmount, 0))

{

nPurchaseHeight = 0;

}

else if (m_purchaseAmount < m_amount * 1.2) //小于120%时,120所对应的重量

{

nPurchaseHeight = m_purchaseAmount / (m_amount * 1.2 / nHeight);

}

int nSpacing = nHeight - nPurchaseHeight;

QRectF planRect = QRect(rectF.left(), rectF.top()+ nSpacing, rectF.width(), nPurchaseHeight);

//+5为了显示全数字

QRectF planTextRect = QRect(rectF.left() - m_textSpacing, rectF.top()+ nSpacing - m_textHeight, rectF.width() + 2 * m_textSpacing, nPurchaseHeight + m_textHeight);

painter->setPen(m_color);

painter->setBrush(m_color);

painter->drawRect(planRect);

painter->setPen(Qt::black);

painter->drawText(planTextRect, alignment, QString("%1").arg(m_purchaseAmount));

}

else

{

painter->setBrush(Qt::NoBrush);

painter->setPen(m_color);

painter->drawRect(rectF);

int nPlanHeight = 0;

if (!sameValue(m_amount, 0))

{

nPlanHeight = m_planAmount / (m_amount / nHeight);

}

int nSpacing = nHeight - nPlanHeight;

QRect amountTextRect(rectF.left() - m_textSpacing, rectF.top() - m_textHeight, rectF.width() + 2 * m_textSpacing, nHeight + m_textHeight);

QRect planRect = QRect(rectF.left(), rectF.top()+ nSpacing, rectF.width(), nHeight - nSpacing);

QRect planTextRect = QRect(rectF.left() - m_textSpacing, rectF.top()+ nSpacing - m_textHeight, rectF.width() + 2 * m_textSpacing, nPlanHeight + m_textHeight);

painter->setBrush(m_color);

painter->drawRect(planRect);

painter->setPen(Qt::black);

painter->drawText(amountTextRect, alignment, QString("%1").arg(m_amount));

//当超过50%时候文字显示在区域内,否则显示在区域上

if (m_planAmount > m_amount / 2)

{

planTextRect = planRect;

}

painter->drawText(planTextRect, alignment, QString("%1").arg(m_planAmount));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值