Qwt QwtScaleDraw自定义坐标轴

1.概述

QwtScaleDraw 是 Qt 绘图库 Qwt 中的一个类,用于绘制坐标轴刻度线和刻度标签。它提供了一些方法和属性来设置刻度线和标签的样式、布局和对齐方式。

以下是类继承关系:

2.常用方法

标签相关方法:

  • setLabelRotation(double angle):设置标签旋转角度。
  • setLabelAlignment(Alignment alignment):设置标签对齐方式。

刻度线相关设置:

  • void setTickLength (QwtScaleDiv::TickType, double length):设置刻度线长度

自定义标签,重写label方法

  • virtual QwtText label (double) const

3.示例

自定义下x轴的坐标轴。

#ifndef BARCHARTSINGLEWIDGET_H
#define BARCHARTSINGLEWIDGET_H#include <QWidget>namespace Ui {
class BarChartSingleWidget;
}class BarChartSingleWidget : public QWidget
{Q_OBJECTpublic: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 ){//设置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;
};//自定义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 barsetSpacing( 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->insertLegend( new QwtLegend() );g_plot->replot();// 显示绘图对象ui->verticalLayout->addWidget(g_plot);
}BarChartSingleWidget::~BarChartSingleWidget()
{delete ui;
}

4.相关参考

Qwt QwtLegend和QwtPlotLegendItem图例类详解-CSDN博客

Qwt QwtPlot类详解-CSDN博客

Qwt QwtPlotBarChart自定义条形统计图-CSDN博客 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/154614.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

开源库存管理系统InvenTree的安装

本文是应网友 shijie880500 要求折腾的&#xff1b; 什么是 InvenTree &#xff1f; InvenTree 是一个开源的库存管理系统&#xff0c;提供强大的低级别库存控制和零件跟踪。InvenTree 系统的核心是 Python/Django 数据库后端&#xff0c;它提供了一个管理界面&#xff08;基于…

将字符串中符合规则的元素替换为指定元素 re.sub()

【小白从小学Python、C、Java】 【计算机等级考试500强双证书】 【Python-数据分析】 将字符串中符合规则的元素 替换为指定元素 re.sub() 选择题 请问re.sub(r[0-9],*,s)的结果是&#xff1a; import re s "hello123" print("【显示】s ",s) print(&quo…

前端包管理工具详解

一、npm包管理工具 1.代码共享方案 我们已经学习了在JavaScript中可以通过模块化的方式将代码划分成一个个小的结构&#xff1a; 在以后的开发中我们就可以通过模块化的方式来封装自己的代码&#xff0c;并且封装成一个工具&#xff1b;这个工具我们可以让同事通过导入的方式…

软考系统架构师知识点集锦九:数据库系统

一、考情分析 二、考点精讲 2.1数据库概述 2.1.1数据库模式 (1)三级模式:外模式对应视图&#xff0c;模式(也称为概念模式)对应数据库表&#xff0c;内模式对应物理文件。(2)两层映像:外模式-模式映像&#xff0c;模式-内模式映像;两层映像可以保证数据库中的数据具有较高的…

如何在Android设备上检查应用程序使用情况,包括使用时间

你可能不知道自己花了多少时间在手机上。很可能你一天中有一半的时间都在盯着手机屏幕。如果你怀疑这一事实,你会很快核实的。在这篇文章中,我们将向你介绍如何在Android设备上检查应用程序的使用情况。 如何在Android上检查应用程序电池使用情况 你使用时间最长的应用程序…

delphi 11.3 FastReport 多设备跨平台 打印之解决方法

以下能WINDOWS10 DELPHI 11.3 FastReport6.0上顺利通过 FastReport6.2对Multi-Device Application应用的支持不够友好&#xff0c;如下图&#xff1b;在palette FastReport6.0才出现几个制件。 非Multi-Device Application应用时是一大堆&#xff1b; 非Multi-Device Appl…

C++笔记-RTTR编译安装简单使用

这里以Linux为例&#xff0c;我使用的机器的gcc版本是4.9.2 使用的RTTR的版本是0.9.5 编译&安装 首先在官网将rttr的0.9.5版本下载下来。 Home |RTTR 按照官方的安装流程&#xff1a; 但这里可能会出现一个问题&#xff1a; 按照解答&#xff0c;切换成root用户&#x…

[java/力扣110]平衡二叉树——优化前后的两种方法

分析 根据平衡二叉树的定义&#xff0c;只需要满足&#xff1a;1、根节点两个子树的高度差不超过1&#xff1b;2、左右子树都为平衡二叉树 代码 public class BalancedBinaryTree {public class TreeNode{int val;TreeNode left;TreeNode right;TreeNode(){}TreeNode(int va…

8+双疾病+WGCNA+多机器学习筛选疾病的共同靶点并验证表达

今天给同学们分享一篇双疾病WGCNA多机器学习的生信文章“Shared diagnostic genes and potential mechanism between PCOS and recurrent implantation failure revealed by integrated transcriptomic analysis and machine learning”&#xff0c;这篇文章于2023年5月16日发表…

生产级 React 框架介绍

文章目录 生产级 React 框架生产级 React 框架Next.jsRemixGatsbyExpo 如何选择生产级 React 框架 生产级 React 框架 React 是一个流行的 JavaScript 框架&#xff0c;用于构建用户界面。React 框架可以帮助你快速构建高质量的 React 应用&#xff0c;但并不是所有的 React 框…

MySQL(2):环境搭建

1.软件下载 软装去官网下载&#xff08;社区版&#xff09;&#xff1a;https://downloads.mysql.com/archives/installer/&#xff08;历史版本可选&#xff09; 选择下面的&#xff0c;一步到位 2.软件安装 双击 .msi 文件 选完 Custom 自定义后点 next 按 1&#xff0c…

新恶意软件使用 MSIX 软件包来感染 Windows

人们发现&#xff0c;一种新的网络攻击活动正在使用 MSIX&#xff08;一种 Windows 应用程序打包格式&#xff09;来感染 Windows PC&#xff0c;并通过将隐秘的恶意软件加载程序放入受害者的 PC 中来逃避检测。 Elastic Security Labs 的研究人员发现&#xff0c;开发人员通常…