TVPlot::TVPlot( QWidget *parent ): QwtPlot( parent ) { setTitle( "Watching TV during a weekend" ); QwtPlotCanvas *canvas = new QwtPlotCanvas(); canvas->setPalette( Qt::gray ); canvas->setBorderRadius( 10 ); setCanvas( canvas ); plotLayout()->setAlignCanvasToScales( true ); setAxisTitle( QwtPlot::yLeft, "Number of People" ); setAxisTitle( QwtPlot::xBottom, "Number of Hours" ); QwtLegend *legend = new QwtLegend; legend->setDefaultItemMode( QwtLegendData::Checkable ); insertLegend( legend, QwtPlot::RightLegend ); populate(); connect( legend, SIGNAL( checked( const QVariant &, bool, int ) ), SLOT( showItem( const QVariant &, bool ) ) ); replot(); // creating the legend items QwtPlotItemList items = itemList( QwtPlotItem::Rtti_PlotHistogram ); for ( int i = 0; i < items.size(); i++ ) { if ( i == 0 ) { const QVariant itemInfo = itemToInfo( items[i] ); QwtLegendLabel *legendLabel = qobject_cast<QwtLegendLabel *>( legend->legendWidget( itemInfo ) ); if ( legendLabel ) legendLabel->setChecked( true ); items[i]->setVisible( true ); } else { items[i]->setVisible( false ); } } setAutoReplot( true ); }
void Graph::initLegend() { QwtLegend *legend = new QwtLegend; legend->setDefaultItemMode(QwtLegendData::Checkable); insertLegend(legend, QwtPlot::BottomLegend); connect(legend, SIGNAL(checked(QVariant,bool,int)), SLOT(showCurve(QVariant,bool,int))); QwtPlotItemList l = this->itemList(QwtPlotItem::Rtti_PlotCurve); for(int i = 0; i < l.size(); ++i) { QwtPlotItem *curve = l[i]; QVariant info = itemToInfo(curve); QWidget *w = legend->legendWidget(info); if(w && w->inherits("QwtLegendLabel")) ((QwtLegendLabel*)w)->setChecked(curve->isVisible()); } }