1.概述
QwtPlotMultiBarChart 是 Qwt 绘图库中的一个类,用于绘制多列柱状图。它可以显示多个柱状条并将它们按照不同的类别分组显示。每个类别下的柱状条可以有不同的颜色和宽度。
以下是类继承关系图:
2.常用方法
设置数据:
- void setSamples (const QVector< QwtSetSample > &)
- void setSamples (const QVector< QVector< double > > &)
- void setSamples (QwtSeriesData< QwtSetSample > *)
设置风格:
- void setStyle (ChartStyle style)
设置间距:
- void setSpacing (int) 设置间距
- void setMargin (int) 设置外边距
设置bar title
- void setBarTitles (const QList< QwtText > &)
设置图例:
- setLegendMode( QwtPlotBarChart::LegendBarTitles );设置图例显示模式
- setLegendIconSize( QSize( 10, 14 ) ); 设置图例图标大小
设置符号:
- void setSymbol (int valueIndex, QwtColumnSymbol *)
3.示例
统计各个年代,各个学校的人数。
#ifndef MULTIBARCHARTWIDGET_H
#define MULTIBARCHARTWIDGET_H
#include <QWidget>
namespace Ui {
class MultiBarChartWidget;
}
class MultiBarChartWidget : public QWidget
{
Q_OBJECT
public:
explicit MultiBarChartWidget(QWidget *parent = 0);
~MultiBarChartWidget();
private:
Ui::MultiBarChartWidget *ui;
};
#endif // MULTIBARCHARTWIDGET_H
#include "MultiBarChartWidget.h"
#include "ui_MultiBarChartWidget.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"
#include "qwt_plot_multi_barchart.h"
//自定义坐标轴
class ScaleDraw : public QwtScaleDraw
{
public:
ScaleDraw( Qt::Orientation orientation, const QStringList& labels )
: m_labels( labels )
{
//设置tick长度
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 );
}
//设置label对齐方式
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;
};
static QwtPlot *g_plot = nullptr;
static QwtPlotMultiBarChart *g_multiBarChart = nullptr;
MultiBarChartWidget::MultiBarChartWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MultiBarChartWidget)
{
ui->setupUi(this);
g_plot = new QwtPlot(QwtText("XX学校学生"),this);
ui->verticalLayout->addWidget(g_plot);
//设置画布
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 );
//设置坐标轴标题
g_plot->setAxisTitle( QwtAxis::YLeft, "人数" );
g_plot->setAxisTitle( QwtAxis::XBottom, "学校" );
g_multiBarChart = new QwtPlotMultiBarChart( "Bar Chart " );
g_multiBarChart->setLayoutPolicy( QwtPlotMultiBarChart::AutoAdjustSamples );
g_multiBarChart->setSpacing( 10 );
g_multiBarChart->setMargin( 3 );
//bar的颜色
static const char* colors[] = { "DarkOrchid", "SteelBlue", "Gold" };
const int numSamples = 5;
const int numBars = sizeof( colors ) / sizeof( colors[0] );
QList< QwtText > titles;
for ( int i = 0; i < numBars; i++ )
{
QString title("202%1");
titles += title.arg( i );
}
//设置titles
g_multiBarChart->setBarTitles( titles );
//设置图标大小
g_multiBarChart->setLegendIconSize( QSize( 10, 14 ) );
for ( int i = 0; i < numBars; i++ )
{
QwtColumnSymbol* symbol = new QwtColumnSymbol( QwtColumnSymbol::Box );
symbol->setLineWidth( 2 );
symbol->setFrameStyle( QwtColumnSymbol::Raised );
symbol->setPalette( QPalette( colors[i] ) );
g_multiBarChart->setSymbol( i, symbol );
}
//设置数据
QVector< QVector< double > > series;
for ( int i = 0; i < numSamples; i++ )
{
QVector< double > values;
for ( int j = 0; j < numBars; j++ )
values += ( 2 + qwtRand() % 2000 );
series += values;
}
//设置数据
g_multiBarChart->setSamples( series );
g_multiBarChart->attach( g_plot );
//插入图例
g_plot->insertLegend( new QwtLegend() );
//设置x范围
g_plot->setAxisScale( QwtAxis::XBottom, 0, g_multiBarChart->dataSize() - 1, 1.0 );
//设置y轴自动缩放
g_plot->setAxisAutoScale( QwtAxis::YLeft );
QStringList distros;
distros<<"成都一中"<<"成都二中"<<"成都三中"<<"成都四中"<<"成都五中";
g_plot->setAxisScaleDraw( QwtAxis::XBottom, new ScaleDraw( Qt::Vertical, distros ) );
QwtScaleDraw* scaleDraw1 = g_plot->axisScaleDraw( QwtAxis::XBottom );
scaleDraw1->enableComponent( QwtScaleDraw::Backbone, false );//设置 不显示水平线
scaleDraw1->enableComponent( QwtScaleDraw::Ticks, false );//设置不显示刻度线
QwtScaleDraw* scaleDraw2 = g_plot->axisScaleDraw( QwtAxis::YLeft );
scaleDraw2->enableComponent( QwtScaleDraw::Backbone, true );
scaleDraw2->enableComponent( QwtScaleDraw::Ticks, true );
g_plot->plotLayout()->setAlignCanvasToScale( QwtAxis::XBottom, true );
g_plot->plotLayout()->setAlignCanvasToScale( QwtAxis::YLeft, false );
g_plot->plotLayout()->setCanvasMargin( 0 );
g_plot->updateCanvasMargins();
g_plot->replot();
g_plot->setAutoReplot( true );
}
MultiBarChartWidget::~MultiBarChartWidget()
{
delete ui;
}
4.相关推荐
Qwt QwtPlotBarChart自定义条形统计图-CSDN博客