qwt_plot_axis.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. #include "qwt_plot.h"
  10. #include "qwt_math.h"
  11. #include "qwt_scale_widget.h"
  12. #include "qwt_scale_div.h"
  13. #include "qwt_scale_engine.h"
  14. class QwtPlot::AxisData
  15. {
  16. public:
  17. bool isEnabled;
  18. bool doAutoScale;
  19. double minValue;
  20. double maxValue;
  21. double stepSize;
  22. int maxMajor;
  23. int maxMinor;
  24. QwtScaleDiv scaleDiv;
  25. QwtScaleEngine *scaleEngine;
  26. QwtScaleWidget *scaleWidget;
  27. };
  28. //! Initialize axes
  29. void QwtPlot::initAxesData()
  30. {
  31. int axisId;
  32. for ( axisId = 0; axisId < axisCnt; axisId++ )
  33. d_axisData[axisId] = new AxisData;
  34. d_axisData[yLeft]->scaleWidget =
  35. new QwtScaleWidget( QwtScaleDraw::LeftScale, this );
  36. d_axisData[yRight]->scaleWidget =
  37. new QwtScaleWidget( QwtScaleDraw::RightScale, this );
  38. d_axisData[xTop]->scaleWidget =
  39. new QwtScaleWidget( QwtScaleDraw::TopScale, this );
  40. d_axisData[xBottom]->scaleWidget =
  41. new QwtScaleWidget( QwtScaleDraw::BottomScale, this );
  42. QFont fscl( fontInfo().family(), 10 );
  43. QFont fttl( fontInfo().family(), 12, QFont::Bold );
  44. for ( axisId = 0; axisId < axisCnt; axisId++ )
  45. {
  46. AxisData &d = *d_axisData[axisId];
  47. d.scaleWidget->setFont( fscl );
  48. d.scaleWidget->setMargin( 2 );
  49. QwtText text = d.scaleWidget->title();
  50. text.setFont( fttl );
  51. d.scaleWidget->setTitle( text );
  52. d.doAutoScale = true;
  53. d.minValue = 0.0;
  54. d.maxValue = 1000.0;
  55. d.stepSize = 0.0;
  56. d.maxMinor = 5;
  57. d.maxMajor = 8;
  58. d.scaleEngine = new QwtLinearScaleEngine;
  59. d.scaleDiv.invalidate();
  60. }
  61. d_axisData[yLeft]->isEnabled = true;
  62. d_axisData[yRight]->isEnabled = false;
  63. d_axisData[xBottom]->isEnabled = true;
  64. d_axisData[xTop]->isEnabled = false;
  65. }
  66. void QwtPlot::deleteAxesData()
  67. {
  68. for ( int axisId = 0; axisId < axisCnt; axisId++ )
  69. {
  70. delete d_axisData[axisId]->scaleEngine;
  71. delete d_axisData[axisId];
  72. d_axisData[axisId] = NULL;
  73. }
  74. }
  75. /*!
  76. \return specified axis, or NULL if axisId is invalid.
  77. \param axisId axis index
  78. */
  79. const QwtScaleWidget *QwtPlot::axisWidget( int axisId ) const
  80. {
  81. if ( axisValid( axisId ) )
  82. return d_axisData[axisId]->scaleWidget;
  83. return NULL;
  84. }
  85. /*!
  86. \return specified axis, or NULL if axisId is invalid.
  87. \param axisId axis index
  88. */
  89. QwtScaleWidget *QwtPlot::axisWidget( int axisId )
  90. {
  91. if ( axisValid( axisId ) )
  92. return d_axisData[axisId]->scaleWidget;
  93. return NULL;
  94. }
  95. /*!
  96. Change the scale engine for an axis
  97. \param axisId axis index
  98. \param scaleEngine Scale engine
  99. \sa axisScaleEngine()
  100. */
  101. void QwtPlot::setAxisScaleEngine( int axisId, QwtScaleEngine *scaleEngine )
  102. {
  103. if ( axisValid( axisId ) && scaleEngine != NULL )
  104. {
  105. AxisData &d = *d_axisData[axisId];
  106. delete d.scaleEngine;
  107. d.scaleEngine = scaleEngine;
  108. d.scaleDiv.invalidate();
  109. autoRefresh();
  110. }
  111. }
  112. /*!
  113. \param axisId axis index
  114. \return Scale engine for a specific axis
  115. */
  116. QwtScaleEngine *QwtPlot::axisScaleEngine( int axisId )
  117. {
  118. if ( axisValid( axisId ) )
  119. return d_axisData[axisId]->scaleEngine;
  120. else
  121. return NULL;
  122. }
  123. /*!
  124. \param axisId axis index
  125. \return Scale engine for a specific axis
  126. */
  127. const QwtScaleEngine *QwtPlot::axisScaleEngine( int axisId ) const
  128. {
  129. if ( axisValid( axisId ) )
  130. return d_axisData[axisId]->scaleEngine;
  131. else
  132. return NULL;
  133. }
  134. /*!
  135. \return \c true if autoscaling is enabled
  136. \param axisId axis index
  137. */
  138. bool QwtPlot::axisAutoScale( int axisId ) const
  139. {
  140. if ( axisValid( axisId ) )
  141. return d_axisData[axisId]->doAutoScale;
  142. else
  143. return false;
  144. }
  145. /*!
  146. \return \c true if a specified axis is enabled
  147. \param axisId axis index
  148. */
  149. bool QwtPlot::axisEnabled( int axisId ) const
  150. {
  151. if ( axisValid( axisId ) )
  152. return d_axisData[axisId]->isEnabled;
  153. else
  154. return false;
  155. }
  156. /*!
  157. \return the font of the scale labels for a specified axis
  158. \param axisId axis index
  159. */
  160. QFont QwtPlot::axisFont( int axisId ) const
  161. {
  162. if ( axisValid( axisId ) )
  163. return axisWidget( axisId )->font();
  164. else
  165. return QFont();
  166. }
  167. /*!
  168. \return the maximum number of major ticks for a specified axis
  169. \param axisId axis index
  170. sa setAxisMaxMajor()
  171. */
  172. int QwtPlot::axisMaxMajor( int axisId ) const
  173. {
  174. if ( axisValid( axisId ) )
  175. return d_axisData[axisId]->maxMajor;
  176. else
  177. return 0;
  178. }
  179. /*!
  180. \return the maximum number of minor ticks for a specified axis
  181. \param axisId axis index
  182. sa setAxisMaxMinor()
  183. */
  184. int QwtPlot::axisMaxMinor( int axisId ) const
  185. {
  186. if ( axisValid( axisId ) )
  187. return d_axisData[axisId]->maxMinor;
  188. else
  189. return 0;
  190. }
  191. /*!
  192. \brief Return the scale division of a specified axis
  193. axisScaleDiv(axisId)->lowerBound(), axisScaleDiv(axisId)->upperBound()
  194. are the current limits of the axis scale.
  195. \param axisId axis index
  196. \return Scale division
  197. \sa QwtScaleDiv, setAxisScaleDiv()
  198. */
  199. const QwtScaleDiv *QwtPlot::axisScaleDiv( int axisId ) const
  200. {
  201. if ( !axisValid( axisId ) )
  202. return NULL;
  203. return &d_axisData[axisId]->scaleDiv;
  204. }
  205. /*!
  206. \brief Return the scale division of a specified axis
  207. axisScaleDiv(axisId)->lowerBound(), axisScaleDiv(axisId)->upperBound()
  208. are the current limits of the axis scale.
  209. \param axisId axis index
  210. \return Scale division
  211. \sa QwtScaleDiv, setAxisScaleDiv()
  212. */
  213. QwtScaleDiv *QwtPlot::axisScaleDiv( int axisId )
  214. {
  215. if ( !axisValid( axisId ) )
  216. return NULL;
  217. return &d_axisData[axisId]->scaleDiv;
  218. }
  219. /*!
  220. \returns the scale draw of a specified axis
  221. \param axisId axis index
  222. \return specified scaleDraw for axis, or NULL if axis is invalid.
  223. \sa QwtScaleDraw
  224. */
  225. const QwtScaleDraw *QwtPlot::axisScaleDraw( int axisId ) const
  226. {
  227. if ( !axisValid( axisId ) )
  228. return NULL;
  229. return axisWidget( axisId )->scaleDraw();
  230. }
  231. /*!
  232. \returns the scale draw of a specified axis
  233. \param axisId axis index
  234. \return specified scaleDraw for axis, or NULL if axis is invalid.
  235. \sa QwtScaleDraw
  236. */
  237. QwtScaleDraw *QwtPlot::axisScaleDraw( int axisId )
  238. {
  239. if ( !axisValid( axisId ) )
  240. return NULL;
  241. return axisWidget( axisId )->scaleDraw();
  242. }
  243. /*!
  244. Return the step size parameter, that has been set
  245. in setAxisScale. This doesn't need to be the step size
  246. of the current scale.
  247. \param axisId axis index
  248. \return step size parameter value
  249. \sa setAxisScale()
  250. */
  251. double QwtPlot::axisStepSize( int axisId ) const
  252. {
  253. if ( !axisValid( axisId ) )
  254. return 0;
  255. return d_axisData[axisId]->stepSize;
  256. }
  257. /*!
  258. \return the title of a specified axis
  259. \param axisId axis index
  260. */
  261. QwtText QwtPlot::axisTitle( int axisId ) const
  262. {
  263. if ( axisValid( axisId ) )
  264. return axisWidget( axisId )->title();
  265. else
  266. return QwtText();
  267. }
  268. /*!
  269. \brief Enable or disable a specified axis
  270. When an axis is disabled, this only means that it is not
  271. visible on the screen. Curves, markers and can be attached
  272. to disabled axes, and transformation of screen coordinates
  273. into values works as normal.
  274. Only xBottom and yLeft are enabled by default.
  275. \param axisId axis index
  276. \param tf \c true (enabled) or \c false (disabled)
  277. */
  278. void QwtPlot::enableAxis( int axisId, bool tf )
  279. {
  280. if ( axisValid( axisId ) && tf != d_axisData[axisId]->isEnabled )
  281. {
  282. d_axisData[axisId]->isEnabled = tf;
  283. updateLayout();
  284. }
  285. }
  286. /*!
  287. Transform the x or y coordinate of a position in the
  288. drawing region into a value.
  289. \param axisId axis index
  290. \param pos position
  291. \warning The position can be an x or a y coordinate,
  292. depending on the specified axis.
  293. */
  294. double QwtPlot::invTransform( int axisId, int pos ) const
  295. {
  296. if ( axisValid( axisId ) )
  297. return( canvasMap( axisId ).invTransform( pos ) );
  298. else
  299. return 0.0;
  300. }
  301. /*!
  302. \brief Transform a value into a coordinate in the plotting region
  303. \param axisId axis index
  304. \param value value
  305. \return X or y coordinate in the plotting region corresponding
  306. to the value.
  307. */
  308. double QwtPlot::transform( int axisId, double value ) const
  309. {
  310. if ( axisValid( axisId ) )
  311. return( canvasMap( axisId ).transform( value ) );
  312. else
  313. return 0.0;
  314. }
  315. /*!
  316. \brief Change the font of an axis
  317. \param axisId axis index
  318. \param f font
  319. \warning This function changes the font of the tick labels,
  320. not of the axis title.
  321. */
  322. void QwtPlot::setAxisFont( int axisId, const QFont &f )
  323. {
  324. if ( axisValid( axisId ) )
  325. axisWidget( axisId )->setFont( f );
  326. }
  327. /*!
  328. \brief Enable autoscaling for a specified axis
  329. This member function is used to switch back to autoscaling mode
  330. after a fixed scale has been set. Autoscaling is enabled by default.
  331. \param axisId axis index
  332. \sa setAxisScale(), setAxisScaleDiv()
  333. */
  334. void QwtPlot::setAxisAutoScale( int axisId )
  335. {
  336. if ( axisValid( axisId ) && !d_axisData[axisId]->doAutoScale )
  337. {
  338. d_axisData[axisId]->doAutoScale = true;
  339. autoRefresh();
  340. }
  341. }
  342. /*!
  343. \brief Disable autoscaling and specify a fixed scale for a selected axis.
  344. \param axisId axis index
  345. \param min
  346. \param max minimum and maximum of the scale
  347. \param stepSize Major step size. If <code>step == 0</code>, the step size is
  348. calculated automatically using the maxMajor setting.
  349. \sa setAxisMaxMajor(), setAxisAutoScale(), axisStepSize()
  350. */
  351. void QwtPlot::setAxisScale( int axisId, double min, double max, double stepSize )
  352. {
  353. if ( axisValid( axisId ) )
  354. {
  355. AxisData &d = *d_axisData[axisId];
  356. d.doAutoScale = false;
  357. d.scaleDiv.invalidate();
  358. d.minValue = min;
  359. d.maxValue = max;
  360. d.stepSize = stepSize;
  361. autoRefresh();
  362. }
  363. }
  364. /*!
  365. \brief Disable autoscaling and specify a fixed scale for a selected axis.
  366. \param axisId axis index
  367. \param scaleDiv Scale division
  368. \sa setAxisScale(), setAxisAutoScale()
  369. */
  370. void QwtPlot::setAxisScaleDiv( int axisId, const QwtScaleDiv &scaleDiv )
  371. {
  372. if ( axisValid( axisId ) )
  373. {
  374. AxisData &d = *d_axisData[axisId];
  375. d.doAutoScale = false;
  376. d.scaleDiv = scaleDiv;
  377. autoRefresh();
  378. }
  379. }
  380. /*!
  381. \brief Set a scale draw
  382. \param axisId axis index
  383. \param scaleDraw object responsible for drawing scales.
  384. By passing scaleDraw it is possible to extend QwtScaleDraw
  385. functionality and let it take place in QwtPlot. Please note
  386. that scaleDraw has to be created with new and will be deleted
  387. by the corresponding QwtScale member ( like a child object ).
  388. \sa QwtScaleDraw, QwtScaleWidget
  389. \warning The attributes of scaleDraw will be overwritten by those of the
  390. previous QwtScaleDraw.
  391. */
  392. void QwtPlot::setAxisScaleDraw( int axisId, QwtScaleDraw *scaleDraw )
  393. {
  394. if ( axisValid( axisId ) )
  395. {
  396. axisWidget( axisId )->setScaleDraw( scaleDraw );
  397. autoRefresh();
  398. }
  399. }
  400. /*!
  401. Change the alignment of the tick labels
  402. \param axisId axis index
  403. \param alignment Or'd Qt::AlignmentFlags <see qnamespace.h>
  404. \sa QwtScaleDraw::setLabelAlignment()
  405. */
  406. void QwtPlot::setAxisLabelAlignment( int axisId, Qt::Alignment alignment )
  407. {
  408. if ( axisValid( axisId ) )
  409. axisWidget( axisId )->setLabelAlignment( alignment );
  410. }
  411. /*!
  412. Rotate all tick labels
  413. \param axisId axis index
  414. \param rotation Angle in degrees. When changing the label rotation,
  415. the label alignment might be adjusted too.
  416. \sa QwtScaleDraw::setLabelRotation(), setAxisLabelAlignment()
  417. */
  418. void QwtPlot::setAxisLabelRotation( int axisId, double rotation )
  419. {
  420. if ( axisValid( axisId ) )
  421. axisWidget( axisId )->setLabelRotation( rotation );
  422. }
  423. /*!
  424. Set the maximum number of minor scale intervals for a specified axis
  425. \param axisId axis index
  426. \param maxMinor maximum number of minor steps
  427. \sa axisMaxMinor()
  428. */
  429. void QwtPlot::setAxisMaxMinor( int axisId, int maxMinor )
  430. {
  431. if ( axisValid( axisId ) )
  432. {
  433. if ( maxMinor < 0 )
  434. maxMinor = 0;
  435. if ( maxMinor > 100 )
  436. maxMinor = 100;
  437. AxisData &d = *d_axisData[axisId];
  438. if ( maxMinor != d.maxMinor )
  439. {
  440. d.maxMinor = maxMinor;
  441. d.scaleDiv.invalidate();
  442. autoRefresh();
  443. }
  444. }
  445. }
  446. /*!
  447. Set the maximum number of major scale intervals for a specified axis
  448. \param axisId axis index
  449. \param maxMajor maximum number of major steps
  450. \sa axisMaxMajor()
  451. */
  452. void QwtPlot::setAxisMaxMajor( int axisId, int maxMajor )
  453. {
  454. if ( axisValid( axisId ) )
  455. {
  456. if ( maxMajor < 1 )
  457. maxMajor = 1;
  458. if ( maxMajor > 1000 )
  459. maxMajor = 10000;
  460. AxisData &d = *d_axisData[axisId];
  461. if ( maxMajor != d.maxMajor )
  462. {
  463. d.maxMajor = maxMajor;
  464. d.scaleDiv.invalidate();
  465. autoRefresh();
  466. }
  467. }
  468. }
  469. /*!
  470. \brief Change the title of a specified axis
  471. \param axisId axis index
  472. \param title axis title
  473. */
  474. void QwtPlot::setAxisTitle( int axisId, const QString &title )
  475. {
  476. if ( axisValid( axisId ) )
  477. axisWidget( axisId )->setTitle( title );
  478. }
  479. /*!
  480. \brief Change the title of a specified axis
  481. \param axisId axis index
  482. \param title axis title
  483. */
  484. void QwtPlot::setAxisTitle( int axisId, const QwtText &title )
  485. {
  486. if ( axisValid( axisId ) )
  487. axisWidget( axisId )->setTitle( title );
  488. }
  489. //! Rebuild the scales
  490. void QwtPlot::updateAxes()
  491. {
  492. // Find bounding interval of the item data
  493. // for all axes, where autoscaling is enabled
  494. QwtInterval intv[axisCnt];
  495. const QwtPlotItemList& itmList = itemList();
  496. QwtPlotItemIterator it;
  497. for ( it = itmList.begin(); it != itmList.end(); ++it )
  498. {
  499. const QwtPlotItem *item = *it;
  500. if ( !item->testItemAttribute( QwtPlotItem::AutoScale ) )
  501. continue;
  502. if ( axisAutoScale( item->xAxis() ) || axisAutoScale( item->yAxis() ) )
  503. {
  504. const QRectF rect = item->boundingRect();
  505. intv[item->xAxis()] |= QwtInterval( rect.left(), rect.right() );
  506. intv[item->yAxis()] |= QwtInterval( rect.top(), rect.bottom() );
  507. }
  508. }
  509. // Adjust scales
  510. for ( int axisId = 0; axisId < axisCnt; axisId++ )
  511. {
  512. AxisData &d = *d_axisData[axisId];
  513. double minValue = d.minValue;
  514. double maxValue = d.maxValue;
  515. double stepSize = d.stepSize;
  516. if ( d.doAutoScale && intv[axisId].isValid() )
  517. {
  518. d.scaleDiv.invalidate();
  519. minValue = intv[axisId].minValue();
  520. maxValue = intv[axisId].maxValue();
  521. d.scaleEngine->autoScale( d.maxMajor,
  522. minValue, maxValue, stepSize );
  523. }
  524. if ( !d.scaleDiv.isValid() )
  525. {
  526. d.scaleDiv = d.scaleEngine->divideScale(
  527. minValue, maxValue,
  528. d.maxMajor, d.maxMinor, stepSize );
  529. }
  530. QwtScaleWidget *scaleWidget = axisWidget( axisId );
  531. scaleWidget->setScaleDiv(
  532. d.scaleEngine->transformation(), d.scaleDiv );
  533. int startDist, endDist;
  534. scaleWidget->getBorderDistHint( startDist, endDist );
  535. scaleWidget->setBorderDist( startDist, endDist );
  536. }
  537. for ( it = itmList.begin(); it != itmList.end(); ++it )
  538. {
  539. QwtPlotItem *item = *it;
  540. item->updateScaleDiv( *axisScaleDiv( item->xAxis() ),
  541. *axisScaleDiv( item->yAxis() ) );
  542. }
  543. }