Results 1 to 3 of 3

Thread: Getting consistency of QwtPlotMultiBarChart item value from QwtPicker trackerText

  1. #1
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Getting consistency of QwtPlotMultiBarChart item value from QwtPicker trackerText

    How to make QwtPicker to work with QwtPlotMultiBarChart, is it possible?
    My current code is like these one:

    Qt Code:
    1. #include "barchart.h"
    2. #include <qwt_plot_renderer.h>
    3. #include <qwt_plot_canvas.h>
    4. #include <qwt_plot_multi_barchart.h>
    5. #include <qwt_column_symbol.h>
    6. #include <qwt_plot_layout.h>
    7. #include <qwt_legend.h>
    8. #include <qwt_scale_draw.h>
    9. #include <qwt_picker.h>
    10. #include <QDebug>
    11.  
    12. class Picker:public QwtPicker
    13. {
    14. public:
    15. Picker(QWidget *canvas):QwtPicker(canvas){
    16. setRubberBandPen( QColor( Qt::darkGreen ) );
    17. setTrackerMode( QwtPicker::AlwaysOn );
    18. }
    19. ~Picker(){}
    20.  
    21. QwtText trackerText(const QPoint & pos ) const
    22. {
    23. s+=QString("x: %1, y:%2").arg(pos.x()).arg(pos.y());
    24. qDebug()<<s;
    25. QwtText text;
    26. text.setColor( Qt::white );
    27. QColor c = rubberBandPen().color();
    28. text.setBorderPen( QPen( c ) );
    29. text.setBorderRadius( 6 );
    30. c.setAlpha( 170 );
    31. text.setBackgroundBrush( c );
    32. return text;
    33. }
    34.  
    35. private:
    36.  
    37.  
    38. };
    39.  
    40. BarChart::BarChart( QWidget *parent ):
    41. QwtPlot( parent )
    42. {
    43. setAutoFillBackground( true );
    44.  
    45. setPalette( Qt::white );
    46. canvas()->setPalette( QColor( "LemonChiffon" ) );
    47. Picker *pick=new Picker(canvas());
    48.  
    49. setTitle( "Bar Chart" );
    50.  
    51. setAxisTitle( QwtPlot::yLeft, "Whatever" );
    52. setAxisTitle( QwtPlot::xBottom, "Whatever" );
    53.  
    54. d_barChartItem = new QwtPlotMultiBarChart( "Bar Chart " );
    55. d_barChartItem->setLayoutPolicy( QwtPlotMultiBarChart::AutoAdjustSamples );
    56. d_barChartItem->setSpacing( 20 );
    57. d_barChartItem->setMargin( 3 );
    58.  
    59. d_barChartItem->attach( this );
    60.  
    61. insertLegend( new QwtLegend() );
    62.  
    63. populate();
    64. setOrientation( 0 );
    65.  
    66. setAutoReplot( true );
    67. }
    68.  
    69. void BarChart::populate()
    70. {
    71. static const char *colors[] = { "DarkOrchid", "SteelBlue", "Gold" };
    72.  
    73. const int numSamples = 5;
    74. const int numBars = sizeof( colors ) / sizeof( colors[0] );
    75.  
    76. QList<QwtText> titles;
    77. for ( int i = 0; i < numBars; i++ )
    78. {
    79. QString title("Bar %1");
    80. titles += title.arg( i );
    81. }
    82. d_barChartItem->setBarTitles( titles );
    83. d_barChartItem->setLegendIconSize( QSize( 10, 14 ) );
    84.  
    85. for ( int i = 0; i < numBars; i++ )
    86. {
    87. QwtColumnSymbol *symbol = new QwtColumnSymbol( QwtColumnSymbol::Box );
    88. symbol->setLineWidth( 2 );
    89. symbol->setFrameStyle( QwtColumnSymbol::Raised );
    90. symbol->setPalette( QPalette( colors[i] ) );
    91.  
    92. d_barChartItem->setSymbol( i, symbol );
    93. }
    94.  
    95. QVector< QVector<double> > series;
    96. for ( int i = 0; i < numSamples; i++ )
    97. {
    98. QVector<double> values;
    99. for ( int j = 0; j < numBars; j++ )
    100. values += ( 2 + qrand() % 8 );
    101.  
    102. series += values;
    103. }
    104.  
    105. d_barChartItem->setSamples( series );
    106. }
    107.  
    108. void BarChart::setMode( int mode )
    109. {
    110. if ( mode == 0 )
    111. {
    112. d_barChartItem->setStyle( QwtPlotMultiBarChart::Grouped );
    113. }
    114. else
    115. {
    116. d_barChartItem->setStyle( QwtPlotMultiBarChart::Stacked );
    117. }
    118. }
    119.  
    120. void BarChart::setOrientation( int orientation )
    121. {
    122. QwtPlot::Axis axis1, axis2;
    123.  
    124. if ( orientation == 0 )
    125. {
    126. axis1 = QwtPlot::xBottom;
    127. axis2 = QwtPlot::yLeft;
    128.  
    129. d_barChartItem->setOrientation( Qt::Vertical );
    130. }
    131. else
    132. {
    133. axis1 = QwtPlot::yLeft;
    134. axis2 = QwtPlot::xBottom;
    135.  
    136. d_barChartItem->setOrientation( Qt::Horizontal );
    137. }
    138.  
    139. setAxisScale( axis1, 0, d_barChartItem->dataSize() - 1, 1.0 );
    140. setAxisAutoScale( axis2 );
    141.  
    142. QwtScaleDraw *scaleDraw1 = axisScaleDraw( axis1 );
    143. scaleDraw1->enableComponent( QwtScaleDraw::Backbone, false );
    144. scaleDraw1->enableComponent( QwtScaleDraw::Ticks, false );
    145.  
    146. QwtScaleDraw *scaleDraw2 = axisScaleDraw( axis2 );
    147. scaleDraw2->enableComponent( QwtScaleDraw::Backbone, true );
    148. scaleDraw2->enableComponent( QwtScaleDraw::Ticks, true );
    149.  
    150. plotLayout()->setAlignCanvasToScale( axis1, true );
    151. plotLayout()->setAlignCanvasToScale( axis2, false );
    152.  
    153. plotLayout()->setCanvasMargin( 0 );
    154. updateCanvasMargins();
    155.  
    156. replot();
    157. }
    158.  
    159. void BarChart::exportChart()
    160. {
    161. QwtPlotRenderer renderer;
    162. renderer.exportTo( this, "barchart.pdf" );
    163. }
    To copy to clipboard, switch view to plain text mode 

    Above code is taken from barchart example with addition of QwtPicker for use.

    Note that what I want is to retrieve current hovered barchart item vertical y values from trackerText overloaded function.

  2. #2
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Getting consistency of QwtPlotMultiBarChart item value from QwtPicker trackerText

    Also the tooltip textmarker is not showing as is stockchart demo doing. How to do that ?

  3. #3
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Getting consistency of QwtPlotMultiBarChart item value from QwtPicker trackerText

    Maybe the easiest solution might be to overload QwtPlotMultiBarChart::drawBar, where you can store the geometries of each bar. Then you can use QRectF::contains() to identify, where the mouse is.

    Uwe

  4. The following user says thank you to Uwe for this useful post:

    kangjoni (19th May 2017)

Similar Threads

  1. QwtPlotMultiBarChart with raw data
    By Momergil in forum Qwt
    Replies: 2
    Last Post: 14th May 2014, 18:27
  2. Stacked Curves - QwtPlotMultiBarChart
    By GG2013 in forum Qwt
    Replies: 2
    Last Post: 13th August 2013, 03:58
  3. QwtPicker RubberBand Performance
    By xerionn in forum Qwt
    Replies: 6
    Last Post: 29th April 2011, 10:36
  4. PlotPicker: TrackerText Position
    By FelixB in forum Qwt
    Replies: 2
    Last Post: 14th December 2010, 09:27
  5. QwtPicker with QwtHistogram
    By cptpingu in forum Qwt
    Replies: 6
    Last Post: 22nd November 2010, 18:05

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.