1.概述
QwtScaleDraw 是 Qt 繪圖庫(kù) Qwt 中的一個(gè)類(lèi),用于繪制坐標(biāo)軸刻度線和刻度標(biāo)簽。它提供了一些方法和屬性來(lái)設(shè)置刻度線和標(biāo)簽的樣式、布局和對(duì)齊方式。
以下是類(lèi)繼承關(guān)系:
2.常用方法
標(biāo)簽相關(guān)方法:
- setLabelRotation(double angle):設(shè)置標(biāo)簽旋轉(zhuǎn)角度。
- setLabelAlignment(Alignment alignment):設(shè)置標(biāo)簽對(duì)齊方式。
刻度線相關(guān)設(shè)置:
- void?setTickLength (QwtScaleDiv::TickType, double length):設(shè)置刻度線長(zhǎng)度
自定義標(biāo)簽,重寫(xiě)label方法
- virtual QwtText?label (double) const
3.示例
自定義下x軸的坐標(biāo)軸。
#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"
//自定義坐標(biāo)軸
class ScaleDraw : public QwtScaleDraw
{
public:
ScaleDraw( Qt::Orientation orientation, const QStringList& labels )
: m_labels( labels )
{
//設(shè)置tick長(zhǎng)度
setTickLength( QwtScaleDiv::MinorTick, 0 );
setTickLength( QwtScaleDiv::MediumTick, 0 );
setTickLength( QwtScaleDiv::MajorTick, 2 );
enableComponent( QwtScaleDraw::Backbone, false );
//設(shè)置方向
if ( orientation == Qt::Vertical )
{
setLabelRotation( -60.0 );
}
else
{
setLabelRotation( -20.0 );
}
//設(shè)置label對(duì)齊方式
setLabelAlignment( Qt::AlignLeft | Qt::AlignVCenter );
}
//重寫(xiě)label方法
virtual QwtText label( double value ) const QWT_OVERRIDE
{
QwtText lbl;
const int index = qRound( value );
if ( index >= 0 && index < m_labels.size() )
{
lbl = m_labels[ index ];
}
return lbl;
}
private:
const QStringList m_labels;
};
//自定義ChartItem類(lèi),實(shí)現(xiàn)specialSymbol和barTitle方法
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& distro, const QColor& color )
{
m_colors += color;
m_distros += distro;
itemChanged();
}
virtual QwtColumnSymbol* specialSymbol(
int index, const QPointF& ) const QWT_OVERRIDE
{
// we want to have individual colors for each bar
//新建一個(gè)標(biāo)記
QwtColumnSymbol* symbol = new QwtColumnSymbol( QwtColumnSymbol::Box );
symbol->setLineWidth( 2 ); //設(shè)置線寬
symbol->setFrameStyle( QwtColumnSymbol::Raised );//設(shè)置邊框風(fēng)格
QColor c( Qt::white );
if ( index >= 0 && index < m_colors.size() )
c = m_colors[ index ];
//設(shè)置顏色
symbol->setPalette( c );
return symbol;
}
//設(shè)置bar的標(biāo)題
virtual QwtText barTitle( int sampleIndex ) const QWT_OVERRIDE
{
QwtText title;
if ( sampleIndex >= 0 && sampleIndex < m_distros.size() )
title = m_distros[ sampleIndex ];
return title;
}
private:
QList< QColor > m_colors; //每個(gè)bar的顏色
QList< QString > m_distros; //每個(gè)bar的標(biāo)題
};
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[] =
{
{ "一年級(jí)", 1114, QColor( "DodgerBlue" ) },
{ "二年級(jí)", 1373, QColor( "#d70751" ) },
{ "三年級(jí)", 1638, QColor( "SteelBlue" ) },
{ "四年級(jí)", 1395, QColor( "Indigo" ) },
{ "五年級(jí)", 3874, QColor( 183, 255, 183 ) },
{ "六年級(jí)", 1532, QColor( 115, 186, 37 ) },
{ "七年級(jí)", 1059, QColor( "LightSkyBlue" ) },
{ "八年級(jí)", 2391, QColor( "FireBrick" ) }
};
//設(shè)置plot背景色
g_plot = new QwtPlot(QwtText("XX學(xué)校學(xué)生人數(shù)統(tǒng)計(jì)"),this);
g_plot->setAutoFillBackground( true );
g_plot->setPalette( QColor( "Linen" ) );
//設(shè)置畫(huà)布
QwtPlotCanvas* canvas = new QwtPlotCanvas();
canvas->setLineWidth( 2 );
canvas->setFrameStyle( QFrame::Box | QFrame::Sunken );
canvas->setBorderRadius( 10 );
//設(shè)置畫(huà)布的背景色
QPalette canvasPalette( QColor( "Plum" ) );
canvasPalette.setColor( QPalette::WindowText, QColor( "Indigo" ) );
canvas->setPalette( canvasPalette );
g_plot->setCanvas( canvas );
// 創(chuàng)建柱狀圖
ChartItem* chartItem = new ChartItem();
//設(shè)置條形圖數(shù)據(jù)
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 );
//設(shè)置坐標(biāo)軸
//設(shè)置自定義的坐標(biāo)軸
g_plot->setAxisTitle( QwtAxis::XBottom, "年級(jí)" );
g_plot->setAxisMaxMinor( QwtAxis::XBottom, 3 );
g_plot->setAxisScaleDraw( QwtAxis::XBottom, new ScaleDraw( Qt::Vertical, m_distros ) );
g_plot->setAxisTitle( QwtAxis::YLeft, "人數(shù)" );
g_plot->setAxisMaxMinor( QwtAxis::YLeft, 3 );
//設(shè)置自定義的坐標(biāo)軸
QwtScaleDraw* scaleDraw = new QwtScaleDraw();
scaleDraw->setTickLength( QwtScaleDiv::MediumTick, 4 );
g_plot->setAxisScaleDraw( QwtAxis::YLeft, scaleDraw );
g_plot->plotLayout()->setCanvasMargin( 0 );
//插入圖例
g_plot->insertLegend( new QwtLegend() );
g_plot->replot();
// 顯示繪圖對(duì)象
ui->verticalLayout->addWidget(g_plot);
}
BarChartSingleWidget::~BarChartSingleWidget()
{
delete ui;
}
4.相關(guān)參考
Qwt QwtLegend和QwtPlotLegendItem圖例類(lèi)詳解-CSDN博客
Qwt QwtPlot類(lèi)詳解-CSDN博客文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-727821.html
Qwt QwtPlotBarChart自定義條形統(tǒng)計(jì)圖-CSDN博客?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-727821.html
到了這里,關(guān)于Qwt QwtScaleDraw自定義坐標(biāo)軸的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!