Qwt QwtScaleDraw自定义坐标轴

news2024/9/23 23:33:43

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_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 )
    {
        //设置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 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->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.coloradmin.cn/o/1152441.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Hudi系列文章7-RFC24 Flink 写入流程优化

文章目录 前言问题背景瓶颈与解决方案瓶颈一解决方法工作流程&#xff1a;精准一次语义容灾CoorinatorCheckpoint如何配合使用StreamWriteOperatorCoordinator CheckpointedFunctionStreamWriteFunctionInstant 提前生成问题 瓶颈二问题解决方案BucketAssignerBucketWriter 重点…

将字符串中符合规则的元素替换为指定元素 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…

采购申请单明细账/汇总账页面编写

业务需求和功能 1、功能&#xff1a;编写采购申请页面和采购申请管理页面。在申请单界面添加常用的查询条件&#xff0c;如单品、申请单等。在采购申请管理页面以单品维度去展示采购申请单的汇总信息&#xff0c;添加一个默认查询时间为7天&#xff0c;并对查询出来的不同状态…

【设计模式三原则】

设计模式三原则 单一职责原则开放封闭原则依赖倒转原则里氏代换原则 我们在进行程序设计的时候&#xff0c;要尽可能地保证程序的可扩展性、可维护性和可读性&#xff0c;所以需要使用一些设计模式&#xff0c;这些设计模式都遵循了以下三个原则&#xff0c;下面来依次为大家介…

目标检测算法-SSD

1. SSD介绍 计算机确定图像中一个物体的位置需要四个参数&#xff1a;中心点的x轴、y轴坐标、框的高和宽。 当一张图片被传入SSD的网络中时&#xff0c;图片首先会被调整为300*300的大小。为了防止失真&#xff0c;其会在图片的边缘加上灰条。 之后SSD会将这种图片分为六种不…

IR2104/IR2184电机方案选择

供电越大Rdson越小 D3要用快恢复或者超快恢复不要用肖特基 上图有自举电容的取值公式&#xff0c;自举电容不能用电解电容&#xff0c;最好使用C0G因为它在不停的充放电 C31必须大于10倍C28

基于探路者算法的无人机航迹规划-附代码

基于探路者算法的无人机航迹规划 文章目录 基于探路者算法的无人机航迹规划1.探路者搜索算法2.无人机飞行环境建模3.无人机航迹规划建模4.实验结果4.1地图创建4.2 航迹规划 5.参考文献6.Matlab代码 摘要&#xff1a;本文主要介绍利用探路者算法来优化无人机航迹规划。 1.探路者…

LeetCode 2742.给墙壁刷油漆

思路 dp(u,count)为当前再考虑下标为1-u的墙面&#xff0c;并且还有count免费工次的最小代价 主要是递归边界的选择&#xff1a; u1<count return 0; if(u-1&&count<0)return 0x3f3f3f3f; if(u-1&&count0)retrun 0; 这三个可以合并成 if(u<count) …

k8s基本操作命令

目录 1、//查看资源对象简写 2、//查看集群信息 3、//配置kubectl自动补全 4、//node节点查看日志 5、//查看 master 节点状态 6、//查看命令空间 7、//查看default命名空间的所有资源 8、//创建命名空间app 9、//删除命名空间app 10、//在命名空间kube-public 创建…

量子计算与量子密码(入门级-少图版)

量子计算与量子密码 写在最前面一些可能带来的有趣的知识和潜在的收获 1、Introduction导言四个特性不确定性&#xff08;自由意志论&#xff09;Indeterminism不确定性Uncertainty叠加原理(线性)superposition (linearity)纠缠entanglement 虚数的常见基本运算欧拉公式&#x…

指针运算笔试题解析(2)

指针运算笔试题解析 题目一解析 题目二解析 压轴题&#xff08;困难&#xff09;解析 题目一 #include <stdio.h> int main() {int aa[2][5] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };int *ptr1 (int *)(&aa 1);int *ptr2 (int *)(*(aa 1));printf( "%d&#x…

学习笔记二十五:持久化存储

这里写目录标题 在k8s中为什么要做持久化存储查看k8s支持哪些存储常用的如下&#xff1a;使用存储卷&#xff0c;需要经历如下步骤 k8s持久化存储&#xff1a;emptyDirk8s持久化存储&#xff1a;hostPathhostpath存储卷缺点 k8s持久化存储&#xff1a;nfs搭建nfs服务k8snode2和…

Vue+Echarts 图表 x轴y轴添加单位字段

需求 代码 两个选择其中一个即可&#xff0c;Y轴也是如此设置 xAxis:{name: 月,// 这个设置只在末尾添加单位axisLabel: {formatter: {value}月 // 在每个x轴坐标都添加了单位} }yAxis:{name: 月,// 这个设置只在末尾添加单位axisLabel: {formatter: {value}月 // 在每个x轴坐…

这个提示词,别人不说,自己绝对想不到

AGI火了一年了&#xff0c;不知道大家的热情还在不在&#xff0c;还有没有在跟进相关的知识&#xff0c;技术&#xff0c;工具与最佳实践。对于我&#xff0c;Claude已经是常驻电脑右下角。 很多朋友问我&#xff0c;说哪些岗位会被颠覆掉&#xff0c;哪些人会被淘汰&#xff0…

【Redis】高并发分布式结构服务器

文章目录 服务端高并发分布式结构名词基本概念评价指标1.单机架构缺点 2.应用数据分离架构应用服务集群架构读写分离/主从分离架构引入缓存-冷热分离架构分库分表&#xff08;垂直分库&#xff09;业务拆分⸺微服务 总结 服务端高并发分布式结构 名词基本概念 应⽤&#xff0…

Mac电脑Android Studio和VS Code配置Flutter开发环境(图文超详细)

一、安装Android Studio 官网地址&#xff1a; https://developer.android.google.cn/ 历史版本下载地址&#xff1a; https://developer.android.com/studio/archive?hlzh-cn 二、安装Xcode 到App Store下载安装最新版本&#xff0c;如果MacOS更新不到13.0以上就无法安装…

Ajax学习笔记第5天

无论做什么&#xff0c;都请记得那是为自己而做&#xff0c;那就毫无怨言&#xff01; 【1. 跨域】 1.什么是跨域 跨域是指浏览器不能执行其他网站的脚本。它是浏览器同源策略造成的&#xff0c;是浏览器对JS实施的安全限制。 2.常见的跨域场景 3.什么事同源策略 &#xff…

python + requests接口自动化测试详解

框架详细教程前段时间由于公司测试方向的转型&#xff0c;由原来的web页面功能测试转变成接口测试&#xff0c;之前大多都是手工进行&#xff0c;利用postman和jmeter进行的接口测试&#xff0c;后来&#xff0c;组内有人讲原先web自动化的测试框架移驾成接口的自动化框架&…

电子学会C/C++编程等级考试2023年05月(六级)真题解析

C/C++等级考试(1~8级)全部真题・点这里 第1题:字符串插入 有两个字符串str和substr,str的字符个数不超过10,substr的字符个数为3。(字符个数不包括字符串结尾处的’\0’。)将substr插入到str中ASCII码最大的那个字符后面,若有多个最大则只考虑第一个。 时间限制:1000 …

分类预测 | Matlab实现KOA-CNN-LSTM-selfAttention多特征分类预测(自注意力机制)

分类预测 | Matlab实现KOA-CNN-LSTM-selfAttention多特征分类预测&#xff08;自注意力机制&#xff09; 目录 分类预测 | Matlab实现KOA-CNN-LSTM-selfAttention多特征分类预测&#xff08;自注意力机制&#xff09;分类效果基本描述程序设计参考资料 分类效果 基本描述 1.Mat…