CustomHistogramBars.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "CustomHistogramBars.h"
  2. CustomHistogramBars::CustomHistogramBars(QCPAxis *keyAxis, QCPAxis *valueAxis)
  3. : QCPBars(keyAxis, valueAxis),
  4. m_textAlignment(Qt::AlignCenter),
  5. m_spacing(5),
  6. m_font(QFont(QLatin1String("sans serif"), 12))
  7. {
  8. }
  9. void CustomHistogramBars::setTextAlignment(Qt::Alignment alignment)
  10. {
  11. m_textAlignment = alignment;
  12. }
  13. void CustomHistogramBars::setSpacing(double spacing)
  14. {
  15. m_spacing = spacing;
  16. }
  17. void CustomHistogramBars::setFont(const QFont &font)
  18. {
  19. m_font = font;
  20. }
  21. void CustomHistogramBars::draw(QCPPainter *painter)
  22. {
  23. if (!mKeyAxis || !mValueAxis) {
  24. qDebug() << Q_FUNC_INFO << "invalid key or value axis";
  25. return;
  26. }
  27. if (mDataContainer->isEmpty())
  28. return;
  29. QCPBarsDataContainer::const_iterator visibleBegin, visibleEnd;
  30. getVisibleDataBounds(visibleBegin, visibleEnd);
  31. // loop over and draw segments of unselected/selected data:
  32. QList<QCPDataRange> selectedSegments, unselectedSegments, allSegments;
  33. getDataSegments(selectedSegments, unselectedSegments);
  34. allSegments << unselectedSegments << selectedSegments;
  35. for (int i = 0; i < allSegments.size(); ++i) {
  36. bool isSelectedSegment = i >= unselectedSegments.size();
  37. QCPBarsDataContainer::const_iterator begin = visibleBegin;
  38. QCPBarsDataContainer::const_iterator end = visibleEnd;
  39. mDataContainer->limitIteratorsToDataRange(begin, end, allSegments.at(i));
  40. if (begin == end)
  41. continue;
  42. for (QCPBarsDataContainer::const_iterator it = begin; it != end; ++it) {
  43. // check data validity if flag set:
  44. #ifdef QCUSTOMPLOT_CHECK_DATA
  45. if (QCP::isInvalidData(it->key, it->value))
  46. qDebug() << Q_FUNC_INFO << "Data point at" << it->key << "of drawn range invalid."
  47. << "Plottable name:" << name();
  48. #endif
  49. // draw bar:
  50. if (isSelectedSegment && mSelectionDecorator) {
  51. mSelectionDecorator->applyBrush(painter);
  52. mSelectionDecorator->applyPen(painter);
  53. } else {
  54. painter->setBrush(mBrush);
  55. painter->setPen(mPen);
  56. }
  57. applyDefaultAntialiasingHint(painter);
  58. // 修改一下
  59. QRectF barRect = getBarRect(it->key, it->value);
  60. painter->drawPolygon(barRect);
  61. // 计算文字的位置
  62. painter->setFont(m_font); // 设置字体
  63. QString text = QString::number(it->value, 'g', 2); // 取得当前value轴的值,保留两位精度
  64. QRectF textRect = painter->fontMetrics().boundingRect(0, 0, 0, 0, Qt::TextDontClip | m_textAlignment,
  65. text); // 计算文字所占用的大小
  66. if (mKeyAxis.data()->orientation() == Qt::Horizontal) { // 当key轴为水平轴的时候
  67. if (mKeyAxis.data()->axisType() == QCPAxis::atTop) // 上轴,移动文字到柱状图下面
  68. textRect.moveTopLeft(barRect.bottomLeft() + QPointF(0, m_spacing));
  69. else // 下轴,移动文字到柱状图上面
  70. textRect.moveBottomLeft(barRect.topLeft() - QPointF(0, m_spacing));
  71. textRect.setWidth(barRect.width());
  72. painter->drawText(textRect, Qt::TextDontClip | m_textAlignment, text);
  73. } else { // 当key轴为竖直轴的时候
  74. if (mKeyAxis.data()->axisType() == QCPAxis::atLeft) // 左轴,移动文字到柱状图右边
  75. textRect.moveTopLeft(barRect.topRight() + QPointF(m_spacing, 0));
  76. else // 右轴,移动文字到柱状图左边
  77. textRect.moveTopRight(barRect.topLeft() - QPointF(m_spacing, 0));
  78. textRect.setHeight(barRect.height());
  79. painter->drawText(textRect, Qt::TextDontClip | m_textAlignment, text);
  80. }
  81. }
  82. }
  83. // draw other selection decoration that isn't just line/scatter pens and brushes:
  84. if (mSelectionDecorator)
  85. mSelectionDecorator->drawDecoration(painter, selection());
  86. }