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