1.概述
QwtPlotBarChart 是 Qt 绘图库 Qwt 中的一个类,用于绘制柱状图。它可以将数据通过柱形的高度来展示,方便比较不同数据量的大小差别。QwtPlotBarChart 类提供了一些方法和属性来设置柱形的样式、颜色、边界等。
以下是类的继承关系图:
2. 常用方法
设置数据:
- void setSamples (const QVector< QPointF > &)
- void setSamples (const QVector< double > &)
- void setSamples (QwtSeriesData< QPointF > *)
自定义条形统计图,需重写函数:
- virtual QwtColumnSymbol * specialSymbol (int sampleIndex, const QPointF &) const
- virtual QwtText barTitle (int sampleIndex) const
设置间距:
- void setSpacing (int) 设置间距
- void setMargin (int) 设置外边距
设置方向:
- void setOrientation (Qt::Orientation)
设置基线:
- void setBaseline (double)
设置图例:
- setLegendMode( QwtPlotBarChart::LegendBarTitles );设置图例显示模式
- setLegendIconSize( QSize( 10, 14 ) ); 设置图例图标大小
3.示例
示例1:一个简单的条形统计图
可以明显看见效果很差,坐标轴不美观,条形图没有颜色外观。
#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"
static QwtPlot *g_plot = nullptr;
BarChartSingleWidget::BarChartSingleWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::BarChartSingleWidget)
{
ui->setupUi(this);
//设置plot背景色
g_plot = new QwtPlot(QwtText("XX学校学生人数统计"),this);
g_plot->setAutoFillBackground( true );
g_plot->setPalette( QColor( "Linen" ) );
//设置画布
QwtPlotCanvas* canvas = new QwtPlotCanvas();
canvas->setLineWidth( 2 );
canvas->setFrameStyle( QFrame::Box | QFrame::Sunken );
canvas->setBorderRadius( 10 );
//设置画布的背景色
QPalette canvasPalette( QColor( "Plum" ) );
canvasPalette.setColor( QPalette::WindowText, QColor( "Indigo" ) );
canvas->setPalette( canvasPalette );
g_plot->setCanvas( canvas );
// 创建数据
QVector<double> data;
data << 10 << 20 << 30 << 40;
// 创建柱状图
QwtPlotBarChart *barChart = new QwtPlotBarChart();
// 将数据设置到柱状图上
barChart->setSamples(data);
// 将柱状图添加到绘图对象中
barChart->attach(g_plot);
// 显示绘图对象
ui->verticalLayout->addWidget(g_plot);
}
BarChartSingleWidget::~BarChartSingleWidget()
{
delete ui;
}
示例2:自定义条形统计图
增加bar颜色,自定义坐标轴。
首先自定义bar继承QwtPlotBarChart类,重写specialSymbol和barTitle方法。
然后自定义坐标轴继承QwtScaleDraw类,重写label方法。
#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"
//自定义坐标轴
class ScaleDraw : public QwtScaleDraw
{
public:
ScaleDraw( Qt::Orientation orientation, const QStringList& labels )
: m_labels( labels )
{
setTickLength( QwtScaleDiv::MinorTick, 0 );
setTickLength( QwtScaleDiv::MediumTick, 0 );
setTickLength( QwtScaleDiv::MajorTick, 2 );
enableComponent( QwtScaleDraw::Backbone, false );
if ( orientation == Qt::Vertical )
{
setLabelRotation( -60.0 );
}
else
{
setLabelRotation( -20.0 );
}
setLabelAlignment( Qt::AlignLeft | Qt::AlignVCenter );
}
//重写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类,实现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
//新建一个标记
QwtColumnSymbol* symbol = new QwtColumnSymbol( QwtColumnSymbol::Box );
symbol->setLineWidth( 2 ); //设置线宽
symbol->setFrameStyle( QwtColumnSymbol::Raised );//设置边框风格
QColor c( Qt::white );
if ( index >= 0 && index < m_colors.size() )
c = m_colors[ index ];
//设置颜色
symbol->setPalette( c );
return symbol;
}
//设置bar的标题
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; //每个bar的颜色
QList< QString > m_distros; //每个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[] =
{
{ "一年级", 1114, QColor( "DodgerBlue" ) },
{ "二年级", 1373, QColor( "#d70751" ) },
{ "三年级", 1638, QColor( "SteelBlue" ) },
{ "四年级", 1395, QColor( "Indigo" ) },
{ "五年级", 3874, QColor( 183, 255, 183 ) },
{ "六年级", 1532, QColor( 115, 186, 37 ) },
{ "七年级", 1059, QColor( "LightSkyBlue" ) },
{ "八年级", 2391, QColor( "FireBrick" ) }
};
//设置plot背景色
g_plot = new QwtPlot(QwtText("XX学校学生人数统计"),this);
g_plot->setAutoFillBackground( true );
g_plot->setPalette( QColor( "Linen" ) );
//设置画布
QwtPlotCanvas* canvas = new QwtPlotCanvas();
canvas->setLineWidth( 2 );
canvas->setFrameStyle( QFrame::Box | QFrame::Sunken );
canvas->setBorderRadius( 10 );
//设置画布的背景色
QPalette canvasPalette( QColor( "Plum" ) );
canvasPalette.setColor( QPalette::WindowText, QColor( "Indigo" ) );
canvas->setPalette( canvasPalette );
g_plot->setCanvas( canvas );
// 创建柱状图
ChartItem* chartItem = new ChartItem();
//设置条形图数据
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 );
//设置坐标轴
//设置自定义的坐标轴
g_plot->setAxisTitle( QwtAxis::XBottom, "年级" );
g_plot->setAxisMaxMinor( QwtAxis::XBottom, 3 );
g_plot->setAxisScaleDraw( QwtAxis::XBottom, new ScaleDraw( Qt::Vertical, m_distros ) );
g_plot->setAxisTitle( QwtAxis::YLeft, "人数" );
g_plot->setAxisMaxMinor( QwtAxis::YLeft, 3 );
//设置自定义的坐标轴
QwtScaleDraw* scaleDraw = new QwtScaleDraw();
scaleDraw->setTickLength( QwtScaleDiv::MediumTick, 4 );
g_plot->setAxisScaleDraw( QwtAxis::YLeft, scaleDraw );
g_plot->plotLayout()->setCanvasMargin( 0 );
g_plot->replot();
// 显示绘图对象
ui->verticalLayout->addWidget(g_plot);
}
BarChartSingleWidget::~BarChartSingleWidget()
{
delete ui;
}
4.相关参考
Qwt QwtLegend和QwtPlotLegendItem图例类详解-CSDN博客
Qwt QwtPlot类详解-CSDN博客
Qwt QwtPlotMultiBarChart绘制多列柱状图-CSDN博客