1. Overview
QwtScaleDraw is a class in the Qt drawing library Qwt, used to draw axis ticks and tick labels. It provides methods and properties to set the style, layout, and alignment of tick marks and labels.
The following is the class inheritance relationship:
2. Commonly used methods
Tag related methods:
- setLabelRotation(double angle): Set the label rotation angle.
- setLabelAlignment(Alignment alignment): Set label alignment.
Tick mark related settings:
- void setTickLength (QwtScaleDiv::TickType, double length): Set the tick length
Custom label, override label method
- virtual QwtText label (double) const
3.Example
Customize the coordinate axis of the x-axis.
#ifndef BARCHARTSINGLEWIDGET_H #define BARCHARTSINGLEWIDGET_H #include <QWidget> namespace Ui { class BarChartSingleWidget; } class BarChartSingleWidget : public QWidget { Q_OBJECT public: explicit BarChartSingleWidget(QWidget *parent = 0); ~BarChartSingleWidget(); private: Ui::BarChartSingleWidget *ui; QStringList m_distros; }; #endif // BARCHARTSINGLEWIDGET_H #include "BarChartSingleWidget.h" #include "ui_BarChartSingleWidget.h" #include "qwt_plot.h" #include "qwt_plot_curve.h" #include "qwt_text.h" #include "qwt_legend.h" #include "qwt_symbol.h" #include "qwt_plot_marker.h" #include "qwt_plot_grid.h" #include "qwt_scale_div.h" #include "qwt_plot_canvas.h" #include "qwt_plot_legenditem.h" #include "qwt_math.h" #include "qwt_plot_layout.h" #include "qwt_plot_barchart.h" #include "qwt_scale_draw.h" #include "qwt_column_symbol.h" #include "qwt_plot_renderer.h" //custom axis class ScaleDraw : public QwtScaleDraw { public: ScaleDraw( Qt::Orientation orientation, const QStringList & labels ) : m_labels( labels ) { //Set tick length setTickLength( QwtScaleDiv::MinorTick, 0 ); setTickLength( QwtScaleDiv::MediumTick, 0 ); setTickLength( QwtScaleDiv::MajorTick, 2 ); enableComponent( QwtScaleDraw::Backbone, false ); //Set direction if ( orientation == Qt::Vertical ) { setLabelRotation( -60.0 ); } else { setLabelRotation( -20.0 ); } //Set label alignment setLabelAlignment( Qt::AlignLeft | Qt::AlignVCenter ); } //Rewrite label method virtual QwtText label(double value) const QWT_OVERRIDE { QwtText lbl; const int index = qRound( value ); if ( index >= 0 & amp; & amp; index < m_labels.size() ) { lbl = m_labels[index]; } return lbl; } private: const QStringList m_labels; }; //Customize the ChartItem class and implement the specialSymbol and barTitle methods class ChartItem : public QwtPlotBarChart { public: ChartItem() : QwtPlotBarChart( "Page Hits" ) { setLegendMode( QwtPlotBarChart::LegendBarTitles ); setLegendIconSize( QSize( 10, 14 ) ); setLayoutPolicy( AutoAdjustSamples ); setLayoutHint( 4.0 ); // minimum width for a single bar setSpacing( 10 ); // spacing between bars } void addDistro( const QString & amp; distro, const QColor & amp; color ) { m_colors + = color; m_distros + = distro; itemChanged(); } virtual QwtColumnSymbol* specialSymbol( int index, const QPointF & amp; ) const QWT_OVERRIDE { // we want to have individual colors for each bar //Create a new mark QwtColumnSymbol* symbol = new QwtColumnSymbol( QwtColumnSymbol::Box ); symbol->setLineWidth(2); //Set line width symbol->setFrameStyle( QwtColumnSymbol::Raised );//Set the border style QColor c( Qt::white ); if ( index >= 0 & amp; & amp; index < m_colors.size() ) c = m_colors[index]; //Set color symbol->setPalette( c ); return symbol; } //Set the title of bar virtual QwtText barTitle( int sampleIndex ) const QWT_OVERRIDE { QwtText title; if ( sampleIndex >= 0 & amp; & amp; sampleIndex < m_distros.size() ) title = m_distros[ sampleIndex ]; return title; } private: QList<QColor> m_colors; //The color of each bar QList<QString> m_distros; //The title of each bar }; static QwtPlot *g_plot = nullptr; BarChartSingleWidget::BarChartSingleWidget(QWidget *parent): QWidget(parent), ui(new Ui::BarChartSingleWidget) { ui->setupUi(this); const struct { const char* distro; const int hits; QColor color; } pageHits[] = { { "First Grade", 1114, QColor( "DodgerBlue" ) }, { "Grade 2", 1373, QColor( "#d70751" ) }, { "Grade 3", 1638, QColor( "SteelBlue" ) }, { "Grade 4", 1395, QColor( "Indigo" ) }, { "Fifth Grade", 3874, QColor( 183, 255, 183 ) }, { "Sixth grade", 1532, QColor( 115, 186, 37 ) }, { "Grade 7", 1059, QColor( "LightSkyBlue" ) }, { "Eighth Grade", 2391, QColor( "FireBrick" ) } }; //Set plot background color g_plot = new QwtPlot(QwtText("XX school student population statistics"), this); g_plot->setAutoFillBackground( true ); g_plot->setPalette( QColor( "Linen" ) ); //Set the canvas QwtPlotCanvas* canvas = new QwtPlotCanvas(); canvas->setLineWidth(2); canvas->setFrameStyle( QFrame::Box | QFrame::Sunken ); canvas->setBorderRadius(10); //Set the background color of the canvas QPalette canvasPalette( QColor( "Plum" ) ); canvasPalette.setColor( QPalette::WindowText, QColor( "Indigo" ) ); canvas->setPalette(canvasPalette); g_plot->setCanvas(canvas); //Create a histogram ChartItem* chartItem = new ChartItem(); //Set bar chart data QVector< double > samples; for ( uint i = 0; i < sizeof( pageHits ) / sizeof( pageHits[ 0 ] ); i + + ) { m_distros + = pageHits[ i ].distro; samples + = pageHits[ i ].hits; chartItem->addDistro( pageHits[ i ].distro, pageHits[ i ].color ); } chartItem->setSamples( samples ); chartItem->attach( g_plot ); //Set the coordinate axis //Set custom axis g_plot->setAxisTitle( QwtAxis::XBottom, "grade" ); g_plot->setAxisMaxMinor( QwtAxis::XBottom, 3 ); g_plot->setAxisScaleDraw( QwtAxis::XBottom, new ScaleDraw( Qt::Vertical, m_distros ) ); g_plot->setAxisTitle( QwtAxis::YLeft, "number of people" ); g_plot->setAxisMaxMinor( QwtAxis::YLeft, 3 ); //Set custom axis QwtScaleDraw* scaleDraw = new QwtScaleDraw(); scaleDraw->setTickLength( QwtScaleDiv::MediumTick, 4 ); g_plot->setAxisScaleDraw( QwtAxis::YLeft, scaleDraw ); g_plot->plotLayout()->setCanvasMargin( 0 ); //Insert legend g_plot->insertLegend( new QwtLegend() ); g_plot->replot(); //Display drawing object ui->verticalLayout->addWidget(g_plot); } BarChartSingleWidget::~BarChartSingleWidget() { delete ui; }
4. Related references
Detailed explanation of Qwt QwtLegend and QwtPlotLegendItem legend classes-CSDN Blog
Detailed explanation of Qwt QwtPlot class-CSDN Blog
Qwt QwtPlotBarChart custom bar chart-CSDN Blog