void ICResultChart::setOrientation(int orientation) { QwtPlot::Axis axis1, axis2; if (orientation == 0) { axis1 = QwtPlot::xBottom; axis2 = QwtPlot::yLeft; d_barChartItem->setOrientation(Qt::Vertical); } else { axis1 = QwtPlot::yLeft; axis2 = QwtPlot::xBottom; d_barChartItem->setOrientation(Qt::Horizontal); } setAxisTitle(axis2, tr("Number")); setAxisTitle(axis1, tr("Answers")); setAxisScaleDraw(axis1, new ChoicesScaleDraw(result.keys())); setAxisScaleDraw(axis2, new QwtScaleDraw); int size = result.size()-1 <= 0 ? 0: result.size()-1; if (size == 0) { setAxisAutoScale(axis1); } else { setAxisScale(axis1, 0, size, 1.0); } int maxsize = 0; foreach (int size, result.values()) { if (maxsize < size) maxsize = size; } setAxisScale(axis2, 0, maxsize * 1.3); QwtScaleDraw *scaleDraw1 = axisScaleDraw(axis1); scaleDraw1->enableComponent(QwtScaleDraw::Backbone, false); scaleDraw1->enableComponent(QwtScaleDraw::Ticks, false); QwtScaleDraw *scaleDraw2 = axisScaleDraw(axis2); scaleDraw2->enableComponent(QwtScaleDraw::Backbone, false); scaleDraw2->enableComponent(QwtScaleDraw::Ticks, true); //plotLayout()->setAlignCanvasToScales( true ); plotLayout()->setAlignCanvasToScale(axis1, true); plotLayout()->setAlignCanvasToScale(axis2, false); plotLayout()->setCanvasMargin(0); updateCanvasMargins(); replot(); }
/*! \brief Event filter The plot handles the following events for the canvas: - QEvent::Resize The canvas margins might depend on its size - QEvent::ContentsRectChange The layout needs to be recalculated \param object Object to be filtered \param event Event \return See QFrame::eventFilter() \sa updateCanvasMargins(), updateLayout() */ bool QwtPlot::eventFilter( QObject *object, QEvent *event ) { if ( object == d_data->canvas ) { if ( event->type() == QEvent::Resize ) { updateCanvasMargins(); } else if ( event->type() == QEvent::ContentsRectChange ) { updateLayout(); } } return QFrame::eventFilter( object, event ); }
void BarChart::setOrientation( int orientation ) { QwtPlot::Axis axis1, axis2; if ( orientation == 0 ) { axis1 = QwtPlot::xBottom; axis2 = QwtPlot::yLeft; d_barChartItem->setOrientation( Qt::Vertical ); } else { axis1 = QwtPlot::yLeft; axis2 = QwtPlot::xBottom; d_barChartItem->setOrientation( Qt::Horizontal ); } //设置坐标轴的min max和step setAxisScale( axis1, 0, d_barChartItem->dataSize() - 1, 1.0 ); setAxisAutoScale( axis2 ); QwtScaleDraw *scaleDraw1 = axisScaleDraw( axis1 ); scaleDraw1->enableComponent( QwtScaleDraw::Backbone, false ); scaleDraw1->enableComponent( QwtScaleDraw::Ticks, false ); QwtScaleDraw *scaleDraw2 = axisScaleDraw( axis2 ); scaleDraw2->enableComponent( QwtScaleDraw::Backbone, true ); scaleDraw2->enableComponent( QwtScaleDraw::Ticks, true ); plotLayout()->setAlignCanvasToScale( axis1, true ); plotLayout()->setAlignCanvasToScale( axis2, false ); plotLayout()->setCanvasMargin( 0 ); updateCanvasMargins(); replot(); }
void BarChart::setOrientation(const QStringList &NameList) { QwtPlot::Axis axis1, axis2; axis1 = QwtPlot::xBottom; axis2 = QwtPlot::yLeft; d_barChartItem->setOrientation(Qt::Vertical); setAxisScale( axis1, 0, d_barChartItem->dataSize() - 1, 1.0 ); setAxisAutoScale( axis2 ); //QwtScaleDraw *scaleDraw1 = axisScaleDraw( axis1 ); //scaleDraw1->enableComponent( QwtScaleDraw::Backbone, false ); //scaleDraw1->enableComponent( QwtScaleDraw::Ticks, false ); //QwtScaleDraw *scaleDraw2 = axisScaleDraw( axis2 ); //scaleDraw2->enableComponent( QwtScaleDraw::Backbone, true ); //scaleDraw2->enableComponent( QwtScaleDraw::Ticks, true ); //plotLayout()->setAlignCanvasToScale( axis1, true ); //plotLayout()->setAlignCanvasToScale( axis2, false ); //const struct //{ // const char *distro; // const int hits; // QColor color; //} pageHits[] = //{ // { "Arch", 1114, QColor("DodgerBlue") }, // { "Debian", 1373, QColor("#d70751") }, // { "Fedora", 1638, QColor("SteelBlue") }, // { "Mageia", 1395, QColor("Indigo") }, // { "Mint", 3874, QColor(183, 255, 183) }, // { "openSuSE", 1532, QColor(115, 186, 37) }, // { "Puppy", 1059, QColor("LightSkyBlue") }, // { "Ubuntu", 2391, QColor("FireBrick") }, // { "Arch", 1114, QColor("DodgerBlue") }, // { "Debian", 1373, QColor("#d70751") }, // { "Fedora", 1638, QColor("SteelBlue") }, // { "Mageia", 1395, QColor("Indigo") }, // { "Mint", 3874, QColor(183, 255, 183) }, // { "openSuSE", 1532, QColor(115, 186, 37) }, // { "Puppy", 1059, QColor("LightSkyBlue") }, // { "Ubuntu", 2391, QColor("FireBrick") } //}; //for (uint i = 0; i < sizeof(pageHits) / sizeof(pageHits[0]); i++) //{ // d_distros += pageHits[i].distro; // //d_barChartItem->addDistro( // // pageHits[i].distro, pageHits[i].color); //} //setAxisTitle(axis1, "Distros"); //setAxisMaxMinor(axis1, 3); setAxisScaleDraw(axis1, new DistroScaleDraw(NameList)); plotLayout()->setCanvasMargin( 0 ); updateCanvasMargins(); }
/*! Paint the contents of a QwtPlot instance into a given rectangle. \param plot Plot to be rendered \param painter Painter \param plotRect Bounding rectangle \sa renderDocument(), renderTo(), QwtPainter::setRoundingAlignment() */ void QwtPlotRenderer::render( QwtPlot *plot, QPainter *painter, const QRectF &plotRect ) const { if ( painter == 0 || !painter->isActive() || !plotRect.isValid() || plot->size().isNull() ) { return; } if ( !( d_data->discardFlags & DiscardBackground ) ) QwtPainter::drawBackgound( painter, plotRect, plot ); /* The layout engine uses the same methods as they are used by the Qt layout system. Therefore we need to calculate the layout in screen coordinates and paint with a scaled painter. */ QTransform transform; transform.scale( double( painter->device()->logicalDpiX() ) / plot->logicalDpiX(), double( painter->device()->logicalDpiY() ) / plot->logicalDpiY() ); QRectF layoutRect = transform.inverted().mapRect( plotRect ); if ( !( d_data->discardFlags & DiscardBackground ) ) { // subtract the contents margins int left, top, right, bottom; plot->getContentsMargins( &left, &top, &right, &bottom ); layoutRect.adjust( left, top, -right, -bottom ); } QwtPlotLayout *layout = plot->plotLayout(); int baseLineDists[QwtAxis::PosCount]; int canvasMargins[QwtAxis::PosCount]; for ( int axisPos = 0; axisPos < QwtAxis::PosCount; axisPos++ ) { canvasMargins[ axisPos ] = layout->canvasMargin( axisPos ); if ( d_data->layoutFlags & FrameWithScales ) { const QwtAxisId axisId( axisPos, QWT_DUMMY_ID ); QwtScaleWidget *scaleWidget = plot->axisWidget( axisId ); if ( scaleWidget ) { baseLineDists[ axisPos ] = scaleWidget->margin(); scaleWidget->setMargin( 0 ); } if ( !plot->isAxisVisible( axisId ) ) { int left = 0; int right = 0; int top = 0; int bottom = 0; // When we have a scale the frame is painted on // the position of the backbone - otherwise we // need to introduce a margin around the canvas switch( axisPos ) { case QwtAxis::yLeft: layoutRect.adjust( 1, 0, 0, 0 ); break; case QwtAxis::yRight: layoutRect.adjust( 0, 0, -1, 0 ); break; case QwtAxis::xTop: layoutRect.adjust( 0, 1, 0, 0 ); break; case QwtAxis::xBottom: layoutRect.adjust( 0, 0, 0, -1 ); break; } layoutRect.adjust( left, top, right, bottom ); } } } // Calculate the layout for the document. QwtPlotLayout::Options layoutOptions = QwtPlotLayout::IgnoreScrollbars; if ( ( d_data->layoutFlags & FrameWithScales ) || ( d_data->discardFlags & DiscardCanvasFrame ) ) { layoutOptions |= QwtPlotLayout::IgnoreFrames; } if ( d_data->discardFlags & DiscardLegend ) layoutOptions |= QwtPlotLayout::IgnoreLegend; if ( d_data->discardFlags & DiscardTitle ) layoutOptions |= QwtPlotLayout::IgnoreTitle; if ( d_data->discardFlags & DiscardFooter ) layoutOptions |= QwtPlotLayout::IgnoreFooter; layout->update( plot, layoutRect, layoutOptions ); // canvas QwtScaleMapTable mapTable = buildCanvasMaps( plot, layout->canvasRect() ); if ( updateCanvasMargins( plot, layout->canvasRect(), mapTable ) ) { // recalculate maps and layout, when the margins // have been changed layout->update( plot, layoutRect, layoutOptions ); mapTable = buildCanvasMaps( plot, layout->canvasRect() ); } // now start painting painter->save(); painter->setWorldTransform( transform, true ); renderCanvas( plot, painter, layout->canvasRect(), mapTable ); if ( !( d_data->discardFlags & DiscardTitle ) && ( !plot->titleLabel()->text().isEmpty() ) ) { renderTitle( plot, painter, layout->titleRect() ); } if ( !( d_data->discardFlags & DiscardFooter ) && ( !plot->footerLabel()->text().isEmpty() ) ) { renderFooter( plot, painter, layout->footerRect() ); } if ( !( d_data->discardFlags & DiscardLegend ) && plot->legend() && !plot->legend()->isEmpty() ) { renderLegend( plot, painter, layout->legendRect() ); } for ( int axisPos = 0; axisPos < QwtAxis::PosCount; axisPos++ ) { for ( int i = 0; i < plot->axesCount( i ); i++ ) { const QwtAxisId axisId( axisPos, i ); QwtScaleWidget *scaleWidget = plot->axisWidget( axisId ); if ( scaleWidget ) { int baseDist = scaleWidget->margin(); int startDist, endDist; scaleWidget->getBorderDistHint( startDist, endDist ); renderScale( plot, painter, axisId, startDist, endDist, baseDist, layout->scaleRect( axisId ) ); } } } painter->restore(); // restore all setting to their original attributes. for ( int axisPos = 0; axisPos < QwtAxis::PosCount; axisPos++ ) { if ( d_data->layoutFlags & FrameWithScales ) { const QwtAxisId axisId( axisPos, QWT_DUMMY_ID ); QwtScaleWidget *scaleWidget = plot->axisWidget( axisId ); if ( scaleWidget ) scaleWidget->setMargin( baseLineDists[axisPos] ); } layout->setCanvasMargin( canvasMargins[axisPos] ); } layout->invalidate(); }