qwt_plot.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
  2. * Qwt Widget Library
  3. * Copyright (C) 1997 Josef Wilgen
  4. * Copyright (C) 2002 Uwe Rathmann
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Qwt License, Version 1.0
  8. *****************************************************************************/
  9. #ifndef QWT_PLOT_H
  10. #define QWT_PLOT_H
  11. #include "qwt_global.h"
  12. #include "qwt_text.h"
  13. #include "qwt_plot_dict.h"
  14. #include "qwt_scale_map.h"
  15. #include "qwt_interval.h"
  16. #include <qframe.h>
  17. #include <qlist.h>
  18. #include <qvariant.h>
  19. class QwtPlotLayout;
  20. class QwtAbstractLegend;
  21. class QwtScaleWidget;
  22. class QwtScaleEngine;
  23. class QwtScaleDiv;
  24. class QwtScaleDraw;
  25. class QwtTextLabel;
  26. /*!
  27. \brief A 2-D plotting widget
  28. QwtPlot is a widget for plotting two-dimensional graphs.
  29. An unlimited number of plot items can be displayed on
  30. its canvas. Plot items might be curves (QwtPlotCurve), markers
  31. (QwtPlotMarker), the grid (QwtPlotGrid), or anything else derived
  32. from QwtPlotItem.
  33. A plot can have up to four axes, with each plot item attached to an x- and
  34. a y axis. The scales at the axes can be explicitly set (QwtScaleDiv), or
  35. are calculated from the plot items, using algorithms (QwtScaleEngine) which
  36. can be configured separately for each axis.
  37. The simpleplot example is a good starting point to see how to set up a
  38. plot widget.
  39. \image html plot.png
  40. \par Example
  41. The following example shows (schematically) the most simple
  42. way to use QwtPlot. By default, only the left and bottom axes are
  43. visible and their scales are computed automatically.
  44. \code
  45. #include <qwt_plot.h>
  46. #include <qwt_plot_curve.h>
  47. QwtPlot *myPlot = new QwtPlot( "Two Curves", parent );
  48. // add curves
  49. QwtPlotCurve *curve1 = new QwtPlotCurve( "Curve 1" );
  50. QwtPlotCurve *curve2 = new QwtPlotCurve( "Curve 2" );
  51. // connect or copy the data to the curves
  52. curve1->setData( ... );
  53. curve2->setData( ... );
  54. curve1->attach( myPlot );
  55. curve2->attach( myPlot );
  56. // finally, refresh the plot
  57. myPlot->replot();
  58. \endcode
  59. \endpar
  60. */
  61. class QWT_EXPORT QwtPlot: public QFrame, public QwtPlotDict
  62. {
  63. Q_OBJECT
  64. Q_PROPERTY( QBrush canvasBackground
  65. READ canvasBackground WRITE setCanvasBackground )
  66. Q_PROPERTY( bool autoReplot READ autoReplot WRITE setAutoReplot )
  67. #if 0
  68. // This property is intended to configure the plot
  69. // widget from a special dialog in the deigner plugin.
  70. // Disabled until such a dialog has been implemented.
  71. Q_PROPERTY( QString propertiesDocument
  72. READ grabProperties WRITE applyProperties )
  73. #endif
  74. public:
  75. //! \brief Axis index
  76. enum Axis
  77. {
  78. //! Y axis left of the canvas
  79. yLeft,
  80. //! Y axis right of the canvas
  81. yRight,
  82. //! X axis below the canvas
  83. xBottom,
  84. //! X axis above the canvas
  85. xTop,
  86. //! Number of axes
  87. axisCnt
  88. };
  89. /*!
  90. Position of the legend, relative to the canvas.
  91. \sa insertLegend()
  92. */
  93. enum LegendPosition
  94. {
  95. //! The legend will be left from the QwtPlot::yLeft axis.
  96. LeftLegend,
  97. //! The legend will be right from the QwtPlot::yRight axis.
  98. RightLegend,
  99. //! The legend will be below the footer
  100. BottomLegend,
  101. //! The legend will be above the title
  102. TopLegend
  103. };
  104. explicit QwtPlot( QWidget * = NULL );
  105. explicit QwtPlot( const QwtText &title, QWidget * = NULL );
  106. virtual ~QwtPlot();
  107. void applyProperties( const QString & );
  108. QString grabProperties() const;
  109. void setAutoReplot( bool = true );
  110. bool autoReplot() const;
  111. // Layout
  112. void setPlotLayout( QwtPlotLayout * );
  113. QwtPlotLayout *plotLayout();
  114. const QwtPlotLayout *plotLayout() const;
  115. // Title
  116. void setTitle( const QString & );
  117. void setTitle( const QwtText & );
  118. QwtText title() const;
  119. QwtTextLabel *titleLabel();
  120. const QwtTextLabel *titleLabel() const;
  121. // Footer
  122. void setFooter( const QString & );
  123. void setFooter( const QwtText & );
  124. QwtText footer() const;
  125. QwtTextLabel *footerLabel();
  126. const QwtTextLabel *footerLabel() const;
  127. // Canvas
  128. void setCanvas( QWidget * );
  129. QWidget *canvas();
  130. const QWidget *canvas() const;
  131. void setCanvasBackground( const QBrush & );
  132. QBrush canvasBackground() const;
  133. virtual QwtScaleMap canvasMap( int axisId ) const;
  134. double invTransform( int axisId, int pos ) const;
  135. double transform( int axisId, double value ) const;
  136. // Axes
  137. QwtScaleEngine *axisScaleEngine( int axisId );
  138. const QwtScaleEngine *axisScaleEngine( int axisId ) const;
  139. void setAxisScaleEngine( int axisId, QwtScaleEngine * );
  140. void setAxisAutoScale( int axisId, bool on = true );
  141. bool axisAutoScale( int axisId ) const;
  142. void enableAxis( int axisId, bool tf = true );
  143. bool axisEnabled( int axisId ) const;
  144. void setAxisFont( int axisId, const QFont & );
  145. QFont axisFont( int axisId ) const;
  146. void setAxisScale( int axisId, double min, double max, double stepSize = 0 );
  147. void setAxisScaleDiv( int axisId, const QwtScaleDiv & );
  148. void setAxisScaleDraw( int axisId, QwtScaleDraw * );
  149. double axisStepSize( int axisId ) const;
  150. QwtInterval axisInterval( int axisId ) const;
  151. const QwtScaleDiv &axisScaleDiv( int axisId ) const;
  152. const QwtScaleDraw *axisScaleDraw( int axisId ) const;
  153. QwtScaleDraw *axisScaleDraw( int axisId );
  154. const QwtScaleWidget *axisWidget( int axisId ) const;
  155. QwtScaleWidget *axisWidget( int axisId );
  156. void setAxisLabelAlignment( int axisId, Qt::Alignment );
  157. void setAxisLabelRotation( int axisId, double rotation );
  158. void setAxisTitle( int axisId, const QString & );
  159. void setAxisTitle( int axisId, const QwtText & );
  160. QwtText axisTitle( int axisId ) const;
  161. void setAxisMaxMinor( int axisId, int maxMinor );
  162. int axisMaxMinor( int axisId ) const;
  163. void setAxisMaxMajor( int axisId, int maxMajor );
  164. int axisMaxMajor( int axisId ) const;
  165. // Legend
  166. void insertLegend( QwtAbstractLegend *,
  167. LegendPosition = QwtPlot::RightLegend, double ratio = -1.0 );
  168. QwtAbstractLegend *legend();
  169. const QwtAbstractLegend *legend() const;
  170. void updateLegend();
  171. void updateLegend( const QwtPlotItem * );
  172. // Misc
  173. virtual QSize sizeHint() const;
  174. virtual QSize minimumSizeHint() const;
  175. virtual void updateLayout();
  176. virtual void drawCanvas( QPainter * );
  177. void updateAxes();
  178. void updateCanvasMargins();
  179. virtual void getCanvasMarginsHint(
  180. const QwtScaleMap maps[], const QRectF &canvasRect,
  181. double &left, double &top, double &right, double &bottom) const;
  182. virtual bool event( QEvent * );
  183. virtual bool eventFilter( QObject *, QEvent * );
  184. virtual void drawItems( QPainter *, const QRectF &,
  185. const QwtScaleMap maps[axisCnt] ) const;
  186. virtual QVariant itemToInfo( QwtPlotItem * ) const;
  187. virtual QwtPlotItem *infoToItem( const QVariant & ) const;
  188. Q_SIGNALS:
  189. /*!
  190. A signal indicating, that an item has been attached/detached
  191. \param plotItem Plot item
  192. \param on Attached/Detached
  193. */
  194. void itemAttached( QwtPlotItem *plotItem, bool on );
  195. /*!
  196. A signal with the attributes how to update
  197. the legend entries for a plot item.
  198. \param itemInfo Info about a plot item, build from itemToInfo()
  199. \param data Attributes of the entries ( usually <= 1 ) for
  200. the plot item.
  201. \sa itemToInfo(), infoToItem(), QwtAbstractLegend::updateLegend()
  202. */
  203. void legendDataChanged( const QVariant &itemInfo,
  204. const QList<QwtLegendData> &data );
  205. public Q_SLOTS:
  206. virtual void replot();
  207. void autoRefresh();
  208. protected:
  209. static bool axisValid( int axisId );
  210. virtual void resizeEvent( QResizeEvent *e );
  211. private Q_SLOTS:
  212. void updateLegendItems( const QVariant &itemInfo,
  213. const QList<QwtLegendData> &legendData );
  214. private:
  215. friend class QwtPlotItem;
  216. void attachItem( QwtPlotItem *, bool );
  217. void initAxesData();
  218. void deleteAxesData();
  219. void updateScaleDiv();
  220. void initPlot( const QwtText &title );
  221. class AxisData;
  222. AxisData *d_axisData[axisCnt];
  223. class PrivateData;
  224. PrivateData *d_data;
  225. };
  226. #endif