Results 1 to 2 of 2

Thread: drawing seperate custom QwtColumnSymbol for bar and legend icon

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

    Wink drawing seperate custom QwtColumnSymbol for bar and legend icon

    I want to draw seperate custom symbol bar with some text number value inside the bar, but the legend icon seems following the same custom symbol bar representation.
    I want that the text number is hidden in the legendlabel icon bar. And only showing on the center bar. here is my code so far based on the modification of barchat from the
    example provided.
    barchat.h
    Qt Code:
    1. #ifndef _BAR_CHART_H_
    2.  
    3. #include <qwt_plot.h>
    4.  
    5. class QwtPlotMultiBarChart;
    6.  
    7. class BarChart: public QwtPlot
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. BarChart( QWidget * = NULL );
    13.  
    14. public Q_SLOTS:
    15. void setMode( int );
    16. void setOrientation( int );
    17. void exportChart();
    18.  
    19. private:
    20. void populate();
    21. void fixlegend();
    22. QVariantList mcolor;
    23. QwtPlotMultiBarChart *d_barChartItem;
    24. };
    25.  
    26. #endif
    To copy to clipboard, switch view to plain text mode 

    and barchat.cpp
    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_painter.h>
    8. #include <qwt_legend.h>
    9. #include <qwt_scale_draw.h>
    10. #include <qwt_legend_label.h>
    11.  
    12. class ColumnSymbolBox:public QwtColumnSymbol
    13. {
    14. public:
    15. ColumnSymbolBox():QwtColumnSymbol(QwtColumnSymbol::Box),num(0){}
    16. virtual void draw ( QPainter * painter,
    17. const QwtColumnRect & rect ) const
    18. {
    19.  
    20. QRectF r = rect.toRect();
    21. if ( QwtPainter::roundingAlignment( painter ) )
    22. {
    23. r.setLeft( qRound( r.left() ) );
    24. r.setRight( qRound( r.right() ) );
    25. r.setTop( qRound( r.top() ) );
    26. r.setBottom( qRound( r.bottom() ) );
    27. }
    28. qwtDrawPanel(painter,r,palette(),lineWidth());
    29. painter->drawText(r, Qt::AlignTop| Qt::AlignHCenter,QString::number(num));
    30.  
    31.  
    32. }
    33. static void qwtDrawPanel( QPainter *painter, const QRectF &rect,
    34. const QPalette &pal, double lw )
    35. {
    36. if ( lw > 0.0 )
    37. {
    38. if ( rect.width() == 0.0 )
    39. {
    40. painter->setPen( pal.window().color() );
    41. painter->drawLine( rect.topLeft(), rect.bottomLeft() );
    42. return;
    43. }
    44.  
    45. if ( rect.height() == 0.0 )
    46. {
    47. painter->setPen( pal.window().color() );
    48. painter->drawLine( rect.topLeft(), rect.topRight() );
    49. return;
    50. }
    51.  
    52. lw = qMin( lw, rect.height() / 2.0 - 1.0 );
    53. lw = qMin( lw, rect.width() / 2.0 - 1.0 );
    54.  
    55. const QRectF outerRect = rect.adjusted( 0, 0, 1, 1 );
    56. const QRectF innerRect = outerRect.adjusted( lw, lw, -lw, -lw );
    57.  
    58. QPolygonF lines[2];
    59.  
    60. lines[0] += outerRect.bottomLeft();
    61. lines[0] += outerRect.topLeft();
    62. lines[0] += outerRect.topRight();
    63. lines[0] += innerRect.topRight();
    64. lines[0] += innerRect.topLeft();
    65. lines[0] += innerRect.bottomLeft();
    66.  
    67. lines[1] += outerRect.topRight();
    68. lines[1] += outerRect.bottomRight();
    69. lines[1] += outerRect.bottomLeft();
    70. lines[1] += innerRect.bottomLeft();
    71. lines[1] += innerRect.bottomRight();
    72. lines[1] += innerRect.topRight();
    73.  
    74. painter->setPen( Qt::NoPen );
    75.  
    76. painter->setBrush( pal.light() );
    77. painter->drawPolygon( lines[0] );
    78. painter->setBrush( pal.dark() );
    79. painter->drawPolygon( lines[1] );
    80. //painter->save();
    81. }
    82.  
    83. painter->drawRect( rect.adjusted( lw, lw, -lw + 1, -lw + 1 ));
    84. painter->setBrush(pal.window() );
    85. painter->setPen(Qt::white);
    86. }
    87. double num;
    88. };
    89.  
    90. BarChart::BarChart( QWidget *parent ):
    91. QwtPlot( parent )
    92. {
    93. setAutoFillBackground( true );
    94.  
    95. setPalette( Qt::white );
    96. canvas()->setPalette( QColor( "LemonChiffon" ) );
    97.  
    98. setTitle( "Bar Chart" );
    99.  
    100. setAxisTitle( QwtPlot::yLeft, "Whatever" );
    101. setAxisTitle( QwtPlot::xBottom, "Whatever" );
    102.  
    103. d_barChartItem = new QwtPlotMultiBarChart( "Bar Chart " );
    104. d_barChartItem->setLayoutPolicy( QwtPlotMultiBarChart::AutoAdjustSamples );
    105. d_barChartItem->setSpacing( 20 );
    106. d_barChartItem->setMargin( 3 );
    107.  
    108. d_barChartItem->attach( this );
    109. QwtLegend *legend = new QwtLegend();
    110. legend->setDefaultItemMode( QwtLegendData::Clickable );
    111. insertLegend( legend );
    112.  
    113. populate();
    114. setOrientation( 0 );
    115.  
    116. setAutoReplot( true );
    117. }
    118.  
    119. void BarChart::populate()
    120. {
    121. mcolor << QColor(Qt::darkRed) << QColor(Qt::darkGreen)<<QColor(Qt::darkCyan) <<QColor(Qt::darkYellow);
    122.  
    123. const int numSamples = 5;
    124. QList<QwtText> titles;
    125. for ( int i = 0; i < mcolor.size(); i++ )
    126. {
    127. QString title("Bar %1");
    128. titles += title.arg( i );
    129. }
    130. d_barChartItem->setBarTitles( titles );
    131. d_barChartItem->setLegendIconSize( QSize( 10, 14 ) );
    132. QVector<double> values;
    133. for ( int j = 0; j < mcolor.size(); j++ ) values += ( 2 + qrand() % 8 );
    134.  
    135. for ( int i = 0; i < mcolor.size(); i++ )
    136. {
    137. ColumnSymbolBox *symbol = new ColumnSymbolBox();
    138. symbol->setLineWidth( 2 );
    139. symbol->setPalette( QPalette( mcolor[i].value<QColor>() ) );
    140. symbol->num=values.at(i);
    141. d_barChartItem->setSymbol( i, symbol );
    142. }
    143.  
    144. QVector< QVector<double> > series;
    145. for ( int i = 0; i < numSamples; i++ )
    146. {
    147.  
    148. series += values;
    149. }
    150. fixlegend();
    151. d_barChartItem->setSamples( series );
    152. }
    153. void BarChart::fixlegend()
    154. {
    155. auto m = d_barChartItem->plot()->legend()->findChildren<QwtLegendLabel*>();
    156. if (!m.isEmpty()){
    157. //
    158. qDebug() << "m.size() == mcolor.size()"<<m.size()<<":"<<mcolor.size();
    159. if (m.size() !=mcolor.size()) {
    160. qDebug() << "m.size() == mcolor.size()"<<m.size()<<":"<<mcolor.size()<<"failed";
    161. return;
    162. }
    163. for (int x=0;x<m.size();x++){
    164. QImage pic;
    165. pic.fill(mcolor.at(x).value<QColor>());
    166. QPixmap pmap,mpmap;
    167. pmap.convertFromImage(pic);
    168. // m[x]->setIcon(pmap); //here problematic QPixmap, I dont understand why this was null
    169. // the text symbol from the center bar was also being drawn here, so how to remove the text symbol and draw only intended color one based on the center bar?
    170. }
    171. }
    172. }
    173. void BarChart::setMode( int mode )
    174. {
    175. if ( mode == 0 )
    176. {
    177. d_barChartItem->setStyle( QwtPlotMultiBarChart::Grouped );
    178. }
    179. else
    180. {
    181. d_barChartItem->setStyle( QwtPlotMultiBarChart::Stacked );
    182. }
    183. fixlegend();
    184. }
    185.  
    186. void BarChart::setOrientation( int orientation )
    187. {
    188. QwtPlot::Axis axis1, axis2;
    189.  
    190. if ( orientation == 0 )
    191. {
    192. axis1 = QwtPlot::xBottom;
    193. axis2 = QwtPlot::yLeft;
    194.  
    195. d_barChartItem->setOrientation( Qt::Vertical );
    196. }
    197. else
    198. {
    199. axis1 = QwtPlot::yLeft;
    200. axis2 = QwtPlot::xBottom;
    201.  
    202. d_barChartItem->setOrientation( Qt::Horizontal );
    203. }
    204.  
    205. setAxisScale( axis1, 0, d_barChartItem->dataSize() - 1, 1.0 );
    206. setAxisAutoScale( axis2 );
    207.  
    208. QwtScaleDraw *scaleDraw1 = axisScaleDraw( axis1 );
    209. scaleDraw1->enableComponent( QwtScaleDraw::Backbone, false );
    210. scaleDraw1->enableComponent( QwtScaleDraw::Ticks, false );
    211.  
    212. QwtScaleDraw *scaleDraw2 = axisScaleDraw( axis2 );
    213. scaleDraw2->enableComponent( QwtScaleDraw::Backbone, true );
    214. scaleDraw2->enableComponent( QwtScaleDraw::Ticks, true );
    215.  
    216. plotLayout()->setAlignCanvasToScale( axis1, true );
    217. plotLayout()->setAlignCanvasToScale( axis2, false );
    218.  
    219. plotLayout()->setCanvasMargin( 0 );
    220. updateCanvasMargins();
    221. fixlegend();
    222. replot();
    223. }
    224.  
    225. void BarChart::exportChart()
    226. {
    227. QwtPlotRenderer renderer;
    228. renderer.exportTo( this, "barchart.pdf" );
    229. }
    To copy to clipboard, switch view to plain text mode 

    So, what is your good way recommendation for fixlegend function in barchat.cpp . thanks

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

    Default Re: drawing seperate custom QwtColumnSymbol for bar and legend icon

    Quote Originally Posted by kangjoni View Post
    I want to draw seperate custom symbol bar with some text number value inside the bar, but the legend icon seems following the same custom symbol bar representation.
    I want that the text number is hidden in the legendlabel icon bar.
    Overload QwtPlotMultiBarChart::legendIcon(), drawing your customized bar without the text.

    Uwe

Similar Threads

  1. Drawing a custom QGraphicsItem
    By saifulkhan in forum Newbie
    Replies: 2
    Last Post: 12th March 2013, 20:09
  2. Replies: 7
    Last Post: 5th June 2011, 20:12
  3. Drawing standard widgets using a custom paint engine
    By Waywocket in forum Qt Programming
    Replies: 26
    Last Post: 21st October 2010, 20:40
  4. Drawing curve legend
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2010, 03:42
  5. Replies: 1
    Last Post: 3rd November 2009, 22:26

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.