QtCharts第一章:绘制、修饰曲线

QtCharts第一章:绘制曲线

摘要:Qt开发应用程序时,常用到曲线绘制,以往主要用第三方Qwt库,Qt5.7以上版本已经自带了QtCharts用于曲线绘制。本文主要讲解利用QtCharts绘制曲线的基本步骤,同时讲解一些修饰曲线,使其更加美观的骚操作。

绘图三要素

绘制一个曲线,要有三个要素(类)组成,其一是画布QChartView,其二是图形QChart,其三是曲线QSplineSeries(或其他)。

类名描述
QChartView用于显示曲线
QChart相当于QwtPlot,图表框架
QSplineSeries相当于QwtPlotCurve,曲线,有多种类型

QChartView

QChart

坐标轴操作

设置坐标轴范围

函数名称:void QAbstractAxis::setRange(const QVariant &min, const QVariant &max)
函数描述:Sets the range shown on the axis. Depending on the actual axis type the min and max parameters are converted to appropriate types. If the conversion is impossible then the function call does nothing.
调用方法:
    chart->axisX()->setRange(0,2000);
    chart->axisY()->setRange(0,1000);

设置坐标轴刻度

函数名称:void QAbstractAxis::setRange(const QVariant &min, const QVariant &max)
函数描述:Sets the range shown on the axis. Depending on the actual axis type the min and max parameters are converted to appropriate types. If the conversion is impossible then the function call does nothing.

坐标轴综合代码和效果

	QValueAxis *axisX = new QValueAxis;
	axisX->setRange(0, 50);   //设置坐标轴范围
	axisX->setGridLineVisible(true);   //网格线可见
	axisX->setTickCount(20);  //设置坐标轴显示数字数目
	axisX->setMinorTickCount(3);   //设置每格小刻度线的数目
	axisX->setLabelFormat("%d");  //设置坐标轴显示数字,整数、N位小数等
	chartView->chart()->setAxisX(axisX, series);

坐标轴操作效果图

QSplineSeries

该控件继承QWidget, 实现了左右上下四种形式的坐标轴控件。 可以设置固定间隔或自动选择间隔 可以设置最小间隔 开放一个槽来动态调整坐标轴的范围 处理了边缘刻度的显示 /************************************************************************ * 版权所有 (C) 2012-2015, liang1057@yahoo.com.cn 类声明: 坐标轴控件 ************************************************************************/ /** @brief 坐标轴控件 * * @details 坐标轴控件 只有刻度和数字,数字可以隐藏(用来显示其他需要显示的刻度值) */ class uiAxis : public QWidget { Q_OBJECT public: /** @brief 坐标轴类型 * * @details 坐标轴类型 */ enum AXISTYPE{ LEFT_AXIS = 0, TOP_AXIS, RIGHT_AXIS, BOTTOM_AXIS }; /** @brief 构造函数 */ uiAxis(AXISTYPE type = BOTTOM_AXIS, QWidget *parent = 0); /** @brief 析构函数 */ ~uiAxis(void); /** @brief 设置坐标轴的范围 */ void setScop(double minValue, double maxValue); /** @brief 获取坐标轴的范围 */ void getScop(double& minValue,double& maxValue); /** @brief 获取坐标轴的范围 */ double getMinValue(); double getMaxValue(); /** @brief 设置坐标轴的类型 */ void setAxisType(AXISTYPE type); /** @brief 坐标轴的类型 */ AXISTYPE getAxisType(); /** @brief 设置最小刻度(小刻度的最小间隔) */ void setMinInterval(double value); /** @brief 设置自动间隔 */ void setAutoScale(bool val=true); /** @brief 设置固定间隔 */ void setSettedScale(bool val=true); /** @brief 设置坐标轴的绘制范围, 像素值 */ void setBoundary(int left, int right, int top, int bottom); void getBoundary(int& left, int& right, int& top, int& bottom);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值