当前位置:   article > 正文

QtChart官方自带example(Callout)X轴坐标逆序后遇到的问题,求大神指点_qt callout

qt callout
  1. #include "GraphPage.h"
  2. #include <QtGui/QResizeEvent>
  3. #include <QtWidgets/QGraphicsScene>
  4. #include <QtCharts/QChart>
  5. #include <QtCharts/QLineSeries>
  6. #include <QtCharts/QSplineSeries>
  7. #include <QtWidgets/QGraphicsTextItem>
  8. #include <QtGui/QMouseEvent>
  9. #include <QDebug>
  10. GraphPage::GraphPage(QChartView *parent)
  11. : QChartView(parent),
  12. poChart(0),
  13. poCallout(0)
  14. {
  15. this->setDragMode(QChartView::NoDrag);
  16. this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  17. this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  18. // chart
  19. poChart = new QChart;
  20. poChart->setMinimumSize(640, 480);
  21. poChart->setTitle("Hover the line to show callout. Click the line to make it stay");
  22. poChart->legend()->setAlignment(Qt::AlignRight);
  23. QLineSeries *poSeries = new QLineSeries;
  24. poSeries->setName("L1-100/D88-1(Ex)");
  25. poSeries->setPointsVisible(true);
  26. poSeries->append(1, 3);
  27. poSeries->append(2, 5);
  28. poSeries->append(3, 4.5);
  29. poSeries->append(4, 1);
  30. poSeries->append(5, 2);
  31. poSeries->append(6, 9);
  32. poChart->addSeries(poSeries);
  33. QSplineSeries *series2 = new QSplineSeries;
  34. series2->setPointsVisible(true);
  35. series2->setName("L1-101/D69-2(Ex)");
  36. series2->append(1, 1.4);
  37. series2->append(2, 3.5);
  38. series2->append(3, 2.5);
  39. series2->append(4, 4);
  40. series2->append(5, 2);
  41. series2->append(6, -6);
  42. poChart->addSeries(series2);
  43. QLogValueAxis *poAxisX = new QLogValueAxis;
  44. poAxisX->setLabelFormat("%g");
  45. poAxisX->setTitleText(QObject::tr("频率(Hz)"));
  46. poAxisX->setBase(10);
  47. poAxisX->setLabelsVisible(true);
  48. poAxisX->setLabelsAngle(26);
  49. poAxisX->setReverse(false);
  50. poChart->addAxis(poAxisX, Qt::AlignBottom);
  51. poSeries->attachAxis(poAxisX);
  52. series2->attachAxis(poAxisX);
  53. QValueAxis *poAxisY = new QValueAxis;
  54. poAxisY->setLabelFormat("%g");
  55. poAxisY->setTitleText(QObject::tr("电场值(\u03BCV)"));
  56. poChart->addAxis(poAxisY, Qt::AlignLeft);
  57. poSeries->attachAxis(poAxisY);
  58. series2->attachAxis(poAxisY);
  59. poChart->setAcceptHoverEvents(true);
  60. setRenderHint(QPainter::Antialiasing);
  61. this->setChart(poChart);
  62. connect(poSeries, SIGNAL(hovered(QPointF, bool)), this, SLOT(TooltipShow(QPointF,bool)));
  63. connect(series2, SIGNAL(hovered(QPointF, bool)), this, SLOT(TooltipShow(QPointF,bool)));
  64. this->MarkersConnect(poChart);
  65. this->setMouseTracking(true);
  66. }
  67. void GraphPage::TooltipShow(QPointF oPoint, bool bState)
  68. {
  69. QLineSeries *poSeries = qobject_cast<QLineSeries *>(sender());
  70. QList<QPointF> aoPointF = poSeries->points();
  71. QList<qreal> arDst;
  72. for(int i = 0; i < aoPointF.count(); i++)
  73. {
  74. arDst.append( qAbs(aoPointF[i].x() - oPoint.x()) );
  75. }
  76. qreal rMin = arDst[0];
  77. for(int i = 1 ; i < arDst.count(); i++)
  78. {
  79. if(arDst[i]< rMin)
  80. {
  81. rMin = arDst[i];
  82. }
  83. }
  84. QPointF seriesPoint = aoPointF[arDst.indexOf(rMin)];
  85. if (poCallout == 0)
  86. poCallout = new Callout(poChart);
  87. if (bState)
  88. {
  89. qDebug()<<"rMin value:"<<rMin;
  90. qDebug()<<"index:"<<arDst.indexOf(rMin);
  91. poCallout->setText(QString("%1\n(%2Hz, %3\u03bcV)")
  92. .arg(poSeries->name())
  93. .arg(seriesPoint.x())
  94. .arg(seriesPoint.y()));
  95. poCallout->setAnchor(poChart->mapToPosition(seriesPoint, poSeries));
  96. poCallout->setPos(poChart->mapToPosition(seriesPoint, poSeries) + QPoint(10, -50));
  97. poCallout->setZValue(11);
  98. poCallout->show();
  99. }
  100. else
  101. {
  102. poCallout->hide();
  103. }
  104. }
  105. void GraphPage::resizeEvent(QResizeEvent *event)
  106. {
  107. if (scene())
  108. {
  109. this->scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
  110. poChart->resize(event->size());
  111. }
  112. QGraphicsView::resizeEvent(event);
  113. }
  114. /***************************************************
  115. * Connect legend
  116. */
  117. void GraphPage::MarkersConnect(QChart *poChart)
  118. {
  119. /* Connect all markers to handler */
  120. foreach (QLegendMarker* poLegendMarker, poChart->legend()->markers())
  121. {
  122. /* Disconnect possible existing connection to avoid multiple connections */
  123. disconnect(poLegendMarker, SIGNAL(clicked()), this, SLOT(HandleMarkerClicked()));
  124. connect(poLegendMarker, SIGNAL(clicked()), this, SLOT(HandleMarkerClicked()));
  125. }
  126. }
  127. /*******************************************************************************
  128. * Handle Marker Clicked
  129. */
  130. void GraphPage::HandleMarkerClicked()
  131. {
  132. QLegendMarker* poLegendMarker = qobject_cast<QLegendMarker*> (sender());
  133. Q_ASSERT(poLegendMarker);
  134. switch (poLegendMarker->type())
  135. {
  136. case QLegendMarker::LegendMarkerTypeXY:
  137. {
  138. /* Toggle visibility of Series */
  139. poLegendMarker->series()->setVisible(!poLegendMarker->series()->isVisible());
  140. /* Turn legend marker back to visible,
  141. * since hiding poSeries also hides the marker
  142. * and we don't want it to happen now. */
  143. poLegendMarker->setVisible(true);
  144. /* Dim the marker, if series is not visible */
  145. qreal alpha = 1.0;
  146. if (!poLegendMarker->series()->isVisible())
  147. {
  148. alpha = 0.5;
  149. }
  150. QColor oColor;
  151. QBrush oBrush = poLegendMarker->labelBrush();
  152. oColor = oBrush.color();
  153. oColor.setAlphaF(alpha);
  154. oBrush.setColor(oColor);
  155. poLegendMarker->setLabelBrush(oBrush);
  156. oBrush = poLegendMarker->brush();
  157. oColor = oBrush.color();
  158. oColor.setAlphaF(alpha);
  159. oBrush.setColor(oColor);
  160. poLegendMarker->setBrush(oBrush);
  161. QPen oPen = poLegendMarker->pen();
  162. oColor = oPen.color();
  163. oColor.setAlphaF(alpha);
  164. oPen.setColor(oColor);
  165. poLegendMarker->setPen(oPen);
  166. break;
  167. }
  168. default:
  169. {
  170. qDebug() << "Unknown poLegendMarker type";
  171. break;
  172. }
  173. }
  174. }

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the Qt Charts module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:GPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU
  19. ** General Public License version 3 or (at your option) any later version
  20. ** approved by the KDE Free Qt Foundation. The licenses are as published by
  21. ** the Free Software Foundation and appearing in the file LICENSE.GPL3
  22. ** included in the packaging of this file. Please review the following
  23. ** information to ensure the GNU General Public License requirements will
  24. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  25. **
  26. ** $QT_END_LICENSE$
  27. **
  28. ****************************************************************************/
  29. #include "Callout.h"
  30. #include <QtGui/QPainter>
  31. #include <QtGui/QFontMetrics>
  32. #include <QtWidgets/QGraphicsSceneMouseEvent>
  33. #include <QtGui/QMouseEvent>
  34. Callout::Callout(QGraphicsItem * parent):
  35. QGraphicsItem(parent)
  36. {
  37. }
  38. QRectF Callout::boundingRect() const
  39. {
  40. QPointF anchor = mapFromParent(m_anchor);
  41. QRectF rect;
  42. rect.setLeft(qMin(m_rect.left(), anchor.x()));
  43. rect.setRight(qMax(m_rect.right(), anchor.x()));
  44. rect.setTop(qMin(m_rect.top(), anchor.y()));
  45. rect.setBottom(qMax(m_rect.bottom(), anchor.y()));
  46. return rect;
  47. }
  48. void Callout::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  49. {
  50. Q_UNUSED(option)
  51. Q_UNUSED(widget)
  52. QPainterPath path;
  53. path.addRoundedRect(m_rect, 5, 5);
  54. QPointF anchor = mapFromParent(m_anchor);
  55. if (!m_rect.contains(anchor)) {
  56. QPointF point1, point2;
  57. // establish the position of the anchor point in relation to m_rect
  58. bool above = anchor.y() <= m_rect.top();
  59. bool aboveCenter = anchor.y() > m_rect.top() && anchor.y() <= m_rect.center().y();
  60. bool belowCenter = anchor.y() > m_rect.center().y() && anchor.y() <= m_rect.bottom();
  61. bool below = anchor.y() > m_rect.bottom();
  62. bool onLeft = anchor.x() <= m_rect.left();
  63. bool leftOfCenter = anchor.x() > m_rect.left() && anchor.x() <= m_rect.center().x();
  64. bool rightOfCenter = anchor.x() > m_rect.center().x() && anchor.x() <= m_rect.right();
  65. bool onRight = anchor.x() > m_rect.right();
  66. // get the nearest m_rect corner.
  67. qreal x = (onRight + rightOfCenter) * m_rect.width();
  68. qreal y = (below + belowCenter) * m_rect.height();
  69. bool cornerCase = (above && onLeft) || (above && onRight) || (below && onLeft) || (below && onRight);
  70. bool vertical = qAbs(anchor.x() - x) > qAbs(anchor.y() - y);
  71. qreal x1 = x + leftOfCenter * 10 - rightOfCenter * 20 + cornerCase * !vertical * (onLeft * 10 - onRight * 20);
  72. qreal y1 = y + aboveCenter * 10 - belowCenter * 20 + cornerCase * vertical * (above * 10 - below * 20);;
  73. point1.setX(x1);
  74. point1.setY(y1);
  75. qreal x2 = x + leftOfCenter * 20 - rightOfCenter * 10 + cornerCase * !vertical * (onLeft * 20 - onRight * 10);;
  76. qreal y2 = y + aboveCenter * 20 - belowCenter * 10 + cornerCase * vertical * (above * 20 - below * 10);;
  77. point2.setX(x2);
  78. point2.setY(y2);
  79. path.moveTo(point1);
  80. path.lineTo(mapFromParent(m_anchor));
  81. path.lineTo(point2);
  82. path = path.simplified();
  83. }
  84. painter->setBrush(QColor(255, 255, 255));
  85. painter->drawPath(path);
  86. painter->drawText(m_textRect, m_text);
  87. }
  88. void Callout::mousePressEvent(QGraphicsSceneMouseEvent *event)
  89. {
  90. event->setAccepted(true);
  91. }
  92. void Callout::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
  93. {
  94. if (event->buttons() & Qt::LeftButton)
  95. {
  96. setPos(mapToParent(event->pos() - event->buttonDownPos(Qt::LeftButton)));
  97. event->setAccepted(true);
  98. }
  99. else
  100. {
  101. event->setAccepted(false);
  102. }
  103. }
  104. void Callout::setText(const QString &text)
  105. {
  106. m_text = text;
  107. QFontMetrics metrics(m_font);
  108. m_textRect = metrics.boundingRect(QRect(0, 0, 150, 150), Qt::AlignLeft, m_text);
  109. m_textRect.translate(5, 5);
  110. prepareGeometryChange();
  111. m_rect = m_textRect.adjusted(-5, -5, 5, 5);
  112. }
  113. void Callout::setAnchor(QPointF point)
  114. {
  115. m_anchor = point;
  116. }


  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the Qt Charts module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:GPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU
  19. ** General Public License version 3 or (at your option) any later version
  20. ** approved by the KDE Free Qt Foundation. The licenses are as published by
  21. ** the Free Software Foundation and appearing in the file LICENSE.GPL3
  22. ** included in the packaging of this file. Please review the following
  23. ** information to ensure the GNU General Public License requirements will
  24. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  25. **
  26. ** $QT_END_LICENSE$
  27. **
  28. ****************************************************************************/
  29. #ifndef CALLOUT_H
  30. #define CALLOUT_H
  31. #include <QtWidgets/QGraphicsItem>
  32. #include <QtGui/QFont>
  33. QT_BEGIN_NAMESPACE
  34. class QGraphicsSceneMouseEvent;
  35. QT_END_NAMESPACE
  36. class Callout : public QGraphicsItem
  37. {
  38. public:
  39. Callout(QGraphicsItem * parent = 0);
  40. void setText(const QString &text);
  41. void setAnchor(QPointF point);
  42. QRectF boundingRect() const;
  43. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);
  44. protected:
  45. void mousePressEvent(QGraphicsSceneMouseEvent *event);
  46. void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
  47. private:
  48. QString m_text;
  49. QRectF m_textRect;
  50. QRectF m_rect;
  51. QPointF m_anchor;
  52. QFont m_font;
  53. };
  54. #endif // CALLOUT_H

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the Qt Charts module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:GPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU
  19. ** General Public License version 3 or (at your option) any later version
  20. ** approved by the KDE Free Qt Foundation. The licenses are as published by
  21. ** the Free Software Foundation and appearing in the file LICENSE.GPL3
  22. ** included in the packaging of this file. Please review the following
  23. ** information to ensure the GNU General Public License requirements will
  24. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  25. **
  26. ** $QT_END_LICENSE$
  27. **
  28. ****************************************************************************/
  29. #ifndef VIEW_H
  30. #define VIEW_H
  31. #include <QtWidgets/QGraphicsView>
  32. #include <QtCharts/QChartGlobal>
  33. #include <QChartView>
  34. #include <QValueAxis>
  35. #include <QLogValueAxis>
  36. #include <QLegendMarker>
  37. #include "Callout.h"
  38. QT_BEGIN_NAMESPACE
  39. class QGraphicsScene;
  40. class QMouseEvent;
  41. class QResizeEvent;
  42. QT_END_NAMESPACE
  43. QT_CHARTS_BEGIN_NAMESPACE
  44. class QChart;
  45. QT_CHARTS_END_NAMESPACE
  46. QT_CHARTS_USE_NAMESPACE
  47. class GraphPage: public QChartView
  48. {
  49. Q_OBJECT
  50. public:
  51. GraphPage(QChartView *parent = 0);
  52. public slots:
  53. void TooltipShow(QPointF point, bool state);
  54. protected:
  55. void resizeEvent(QResizeEvent *event);
  56. private:
  57. QChart *poChart;
  58. Callout *poCallout;
  59. void MarkersConnect(QChart *poChart);
  60. private slots:
  61. void HandleMarkerClicked();
  62. };
  63. #endif






本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/805481
推荐阅读
相关标签
  

闽ICP备14008679号