#include "CustomHistogramBars.h" CustomHistogramBars::CustomHistogramBars(QCPAxis *keyAxis, QCPAxis *valueAxis) : QCPBars(keyAxis, valueAxis), m_textAlignment(Qt::AlignCenter), m_spacing(5), m_font(QFont(QLatin1String("sans serif"), 12)) { } void CustomHistogramBars::setTextAlignment(Qt::Alignment alignment) { m_textAlignment = alignment; } void CustomHistogramBars::setSpacing(double spacing) { m_spacing = spacing; } void CustomHistogramBars::setFont(const QFont &font) { m_font = font; } void CustomHistogramBars::draw(QCPPainter *painter) { if (!mKeyAxis || !mValueAxis) { qDebug() << Q_FUNC_INFO << "invalid key or value axis"; return; } if (mDataContainer->isEmpty()) return; QCPBarsDataContainer::const_iterator visibleBegin, visibleEnd; getVisibleDataBounds(visibleBegin, visibleEnd); // loop over and draw segments of unselected/selected data: QList selectedSegments, unselectedSegments, allSegments; getDataSegments(selectedSegments, unselectedSegments); allSegments << unselectedSegments << selectedSegments; for (int i = 0; i < allSegments.size(); ++i) { bool isSelectedSegment = i >= unselectedSegments.size(); QCPBarsDataContainer::const_iterator begin = visibleBegin; QCPBarsDataContainer::const_iterator end = visibleEnd; mDataContainer->limitIteratorsToDataRange(begin, end, allSegments.at(i)); if (begin == end) continue; for (QCPBarsDataContainer::const_iterator it = begin; it != end; ++it) { // check data validity if flag set: #ifdef QCUSTOMPLOT_CHECK_DATA if (QCP::isInvalidData(it->key, it->value)) qDebug() << Q_FUNC_INFO << "Data point at" << it->key << "of drawn range invalid." << "Plottable name:" << name(); #endif // draw bar: if (isSelectedSegment && mSelectionDecorator) { mSelectionDecorator->applyBrush(painter); mSelectionDecorator->applyPen(painter); } else { painter->setBrush(mBrush); painter->setPen(mPen); } applyDefaultAntialiasingHint(painter); // 修改一下 QRectF barRect = getBarRect(it->key, it->value); painter->drawPolygon(barRect); // 计算文字的位置 painter->setFont(m_font); // 设置字体 QString text = QString::number(it->value, 'g', 2); // 取得当前value轴的值,保留两位精度 QRectF textRect = painter->fontMetrics().boundingRect(0, 0, 0, 0, Qt::TextDontClip | m_textAlignment, text); // 计算文字所占用的大小 if (mKeyAxis.data()->orientation() == Qt::Horizontal) { // 当key轴为水平轴的时候 if (mKeyAxis.data()->axisType() == QCPAxis::atTop) // 上轴,移动文字到柱状图下面 textRect.moveTopLeft(barRect.bottomLeft() + QPointF(0, m_spacing)); else // 下轴,移动文字到柱状图上面 textRect.moveBottomLeft(barRect.topLeft() - QPointF(0, m_spacing)); textRect.setWidth(barRect.width()); painter->drawText(textRect, Qt::TextDontClip | m_textAlignment, text); } else { // 当key轴为竖直轴的时候 if (mKeyAxis.data()->axisType() == QCPAxis::atLeft) // 左轴,移动文字到柱状图右边 textRect.moveTopLeft(barRect.topRight() + QPointF(m_spacing, 0)); else // 右轴,移动文字到柱状图左边 textRect.moveTopRight(barRect.topLeft() - QPointF(m_spacing, 0)); textRect.setHeight(barRect.height()); painter->drawText(textRect, Qt::TextDontClip | m_textAlignment, text); } } } // draw other selection decoration that isn't just line/scatter pens and brushes: if (mSelectionDecorator) mSelectionDecorator->drawDecoration(painter, selection()); }