Qt 的 QChartView 实现了放大和缩小功能
Qt的常用图表方式Qwt、QChart、QCustomPlot等。QCharts是官方的,功能强大些。QCustomPlot是一个小型的Qt画图标类,支持绘制静态曲线、动态曲线、多重坐标曲线,柱状图,蜡烛图等。QCustomPlot比Qchat简单好用些。
这里介绍下QChartView缩放和放大功能的实现。
这里介绍下QChartView缩放和放大功能的实现。
QChartView开启鼠标拖动放大功能:
ui->wdChart->setRubberBand(QChartView::RectangleRubberBand);
开启后,拖动鼠标区域自动放大,鼠标右键点击自动缩小。
恢复的话重新设置下轴的最大最小范围setRange即可。这里介绍下鼠标左键单击实现恢复的办法:
自定以一个MyChartView,继承自QChartView。增加一个信号:
signals:
void sgl_recoverRange(MyChartView *p);
需要在自定义的MyChartView中区分出来是否是鼠标左键的单击事件还是鼠标左键的拖动。这里巧妙实现了下。原理很简单,如果是鼠标拖动的话mouseMoveEvent中把is_Pressed_清掉。
#include "mychartview.h"
MyChartView::MyChartView(QWidget *parent):QChartView(parent)
{
is_Pressed_ = false;
}
void MyChartView::mouseMoveEvent(QMouseEvent *event)
{
is_Pressed_ = false;
QChartView::mouseMoveEvent(event);
}
void MyChartView::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton){
if(is_Pressed_){
is_Pressed_ = false;
// 单击鼠标恢复缩放
emit sgl_recoverRange(this);
}
}
QChartView::mouseReleaseEvent(event);
}
void MyChartView::mousePressEvent(QMouseEvent *event)
{
is_Pressed_ = true;
QChartView::mousePressEvent(event);
}
绑定信号和槽函数:
connect(ui->wdChart,&MyChartView::sgl_recoverRange,
this,&MainWindow::slot_recoverChartRange);
m_tooltip = new Callout(myChart);
在槽函数中对缩放和放大功能进行恢复处理,重新设置range.
void MainWindow::slot_recoverChartRange()
{
qDebug() << "slot_recoverChartRange";
int maxVal = 0;
if(mTbData.recList.size() == 0){
mAxisY->setRange(0,12);
mAxisY1->setRange(0,12);
mAxisX->setRange(0,10);
return;
}
}
更好用的QCustomPlot
QCustomPlot介绍
QCustomPlot 是一个基于Qt的画图和数据可视化C++控件。QCustomPlot 致力于提供美观的界面,高质量的2D画图、图画和图表,同时为实时数据可视化应用提供良好的解决方案。 该绘图库专注于制作美观、出版物质量高的2D绘图、图形和图表,并为实时可视化应用程序提供高性能。
QCustomPlot的下载与安装
QCustomPlot官网链接:Qt Plotting Widget QCustomPlot - Introduction 下载链接:Qt Plotting Widget QCustomPlot - Download
QCustomPlot的使用
QCustomPlot 是一个超强超小巧的qt绘图类,非常漂亮,非常易用。只需要把下载下来的qcustomplot.h和qcustomplot.cpp文件加入项目工程即可使用,远比qwt方便和漂亮,可以自己使用两个源文件也可以自己编译成库文件,非常方便。
把qcustomplot.cpp和qcustomplot.h拷贝到工程目录下,在项目中点右键添加现有文件,把两个文件加入工程。
这时pro文件会添加上qcustomplot.cpp和qcustomplot.h,如果Qt版本在5.0以上,需要在.pro文件中的QT变量加上printsupport,QT += printsupport。
界面上拖上去一个widget控件,然后使一个widget提升为QCustomPlot类,即可使用。
使用示例
void OneGraph::OneGraph_Drawing(QCustomPlot *customPlot)
{
// 将数据用曲线实时显示
QVector<double> x(101),y(101);// x轴数据,y轴数据
for(int i = 0; i < 101;i++)
{
x[i] = i / 50.0 - 1;// x轴数据范围:-1 ~ 1
y[i] = x[i] * x[i];// y轴数据范围:0 ~ 1
}
// 添加一个曲线图QGraph,
customPlot->addGraph();
customPlot->graph(0)->setData(x,y);//为坐标轴添加数据
customPlot->graph(0)->setName("示例1:绘制一个曲线");// 设置曲线图的名字
// 如果需要添加多个曲线,就需要多次调用addGraph()函数
// customPlot->addGraph();
// customPlot->graph(1)->setData("x轴数据","y轴数据");
// customPlot->graph(1)->setName("示例1:绘制第二个一个曲线");
// 设置图表标题
QCPTextElement *title = new QCPTextElement(customPlot,"标题:绘制一个曲线",QFont("sans",10,QFont::Bold));
title->setTextColor(Qt::green);
title->setMargins(QMargins(0,6,0,10));
// 在第一行第一列添加标题
customPlot->plotLayout()->insertRow(0);// 插入一行
customPlot->plotLayout()->addElement(0, 0, title);
//为图例添加标题
QCPTextElement *legend_title = new QCPTextElement(customPlot,"这是图例的标题",QFont("sans",10,QFont::Bold));
legend_title->setTextColor(Qt::red);
legend_title->setMargins(QMargins(0,6,0,10));// 为了效果更好,添加一些边距
legend_title->setLayer("legend");// 一定要把标题的层设置为legend层
customPlot->legend->insertRow(0);// 插入一行
customPlot->legend->addElement(0,0,legend_title);// 在第一行第一列添加标题
// x轴设置属性
customPlot->xAxis->setLabel("x轴数据");// 设置x轴的标签
customPlot->xAxis->setRange(-1,1);// 设置x轴的范围为(-1,1)
customPlot->xAxis->setPadding(30);//设置外边距,数值可以改大或者改小来观察效果
customPlot->xAxis->setLabelPadding(20);//设置标签内边距
customPlot->xAxis->setTickLabelPadding(10);
// y轴设置属性
customPlot->yAxis->setLabel("y轴数据");
customPlot->yAxis->setRange(-1,1);
customPlot->yAxis->setPadding(10);
//设置QCustomPlot的背景颜色
QLinearGradient plotGradient;
plotGradient.setStart(0,0);//背景颜色起始点,从图左上角开始,y方向0~400之间为红色渐变,开始位置为红色
plotGradient.setFinalStop(0,400);//y方向 >400 为绿色渐变,结束位置为绿色
plotGradient.setColorAt(0,QColor(200,200,200));//黑色,透明度从 0 ~ 1,
plotGradient.setColorAt(1,QColor(120,120,120));
customPlot->setBackground(plotGradient);
//设置QCPAxisRect轴矩形的背景颜色
QLinearGradient axisRectGradient;
axisRectGradient.setStart(0,0);
axisRectGradient.setFinalStop(0,350);
axisRectGradient.setColorAt(0,QColor("#87CEFA"));//亮天蓝色
axisRectGradient.setColorAt(1,QColor("#FFB6C1"));//浅粉红
customPlot->axisRect()->setBackground(axisRectGradient);
//设置QCPAxis轴的风格
customPlot->xAxis->setBasePen(QPen(Qt::white,2));// x轴线的画笔颜色和粗细
customPlot->xAxis->setTickPen(QPen(Qt::white,3));// x轴线上的主刻度线(有数字的刻度线)的画笔颜色和粗细
customPlot->xAxis->setTickLabelColor(Qt::green);// x轴线上的主刻度线下的文字颜色
customPlot->xAxis->setTickLengthIn(6);// 轴线内主刻度线的长度
customPlot->xAxis->setTickLengthOut(15);// 轴线外主刻度线的长度
customPlot->xAxis->setSubTickPen(QPen(QColor(220,20,60),1));//粉红色,x轴线上的子刻度线(有数字的刻度线)的画笔颜色和粗细
customPlot->xAxis->setLabelColor(Qt::red);// 只有设置了标签,轴标签的颜色才会显示
customPlot->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);// 设置轴线结束时的风格为 实角三角形但内部有凹陷的形状
customPlot->xAxis->setLowerEnding(QCPLineEnding::esDisc);//setLowerEnding设置轴线开始时的风格
//设置QCPGrid网格的风格,每条网格对应一个刻度
customPlot->xAxis->grid()->setPen(QPen(QColor(255,0,255),1,Qt::SolidLine));//实线
customPlot->yAxis->grid()->setPen(QPen(QColor(255,0,255),2,Qt::DotLine));//点线
customPlot->xAxis->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine)); // 子网格线(对应子刻度)画笔
customPlot->yAxis->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::SolidLine));//设置颜色后,需要显示子网格线,才能看到效果
customPlot->xAxis->grid()->setSubGridVisible(true);// 显示子网格线
customPlot->yAxis->grid()->setSubGridVisible(true);
customPlot->xAxis->grid()->setZeroLinePen(QPen(Qt::black,3));// 设置刻度为0时的网格线的画笔
customPlot->yAxis->grid()->setZeroLinePen(QPen(Qt::black,3));
customPlot->legend->setVisible(true);// 显示图例:曲线颜色和曲线名,一般在右上角,可以设置
// customPlot->replot();重绘
}
引用
学习QT之QCustomPlot介绍_贝勒里恩的博客-****博客_qcustomplot
QCustomPlot基础教程(一)——QCustomPlot的安装及基础实例_wendy_ya的博客-****博客_qcustomplot
qt超强精美绘图控件 - QCustomPlot一览 及 安装使用教程_尘中远的博客-****博客_qt绘图控件
qt超强绘图控件qwt - 安装及配置_尘中远的博客-****博客
Qt 下快速读写Excel指南_尘中远的博客-****博客_qt读写excel
Qt使用多线程的一些心得——2.继承QObject的多线程使用方法_尘中远的博客-****博客
QCustomplot - 6.QCustomplot详解_Mr.codeee的博客-****博客_qcustomplot
Qwt、QChart、QCustomPlot使用_mahuifa的博客-****博客_qcustomplot和qchart对比
Qt-QCustomplot画静态、动态曲线教程图解_52_赫兹的鲸的博客-****博客_qcustomplot曲线
QT图表:QChartQCustomplot_rainbow_lucky0106的博客-****博客_qcustomplot和qchart对比
Qt-Qcustomplot设置并使用双重坐标轴(刻度不同的X、Y轴)_52_赫兹的鲸的博客-****博客
QCustomPlot使用_pzs0221的博客-****博客_qcustomplot
使用QCustomPlot在数据曲线上设置可随鼠标移动的游标_weixin_45875835的博客-****博客
推荐阅读
-
Qt 的 QChartView 实现了放大和缩小功能
-
Grid++Report 锐浪报表开发常见问题解答集锦-报表设计 问:怎样在设计时打印预览报表? 答:为了及时查看报表的设计效果,Grid++Report 报表设计应用程序提供了四种查看视图:普通视图、页面视图、预览视图与查询视图。通过窗口下边的 Tab 按钮可以在四种视图中任意切换。在预览视图中查看报表的打印预览效果,在查询视图中查看报表的查询显示效果。如果在报表的记录集提供了数据源连接串与查询 SQL,在进入预览视图与查询视图时会利用数据源连接串与查询 SQL 从数据源中自动取数,否则 Grid++Report 将自动生成模拟数据进行模拟打印预览与查询显示。注意:在预览视图与查询视图中看到的报表运行结果有可能与在你程序中的最终运行结果有差异,因为在报表的生成过程中我们可以在程序中对报表的生成行为进行一定的控制。 问:怎样用 Grid++Report 设计交叉表? 答:Grid++Report 没有提供专门实现交叉表的功能,其它的报表构件提供的交叉表功能一般也比较死板和功能有限。利用 Grid++Report 的编程接口可以做出灵活多变,功能丰富的交叉表。示例程序 CrossTab 就是一个实现交叉表的例子程序,认真领会此例子程序,你就可以做出自己想要各种交叉表,并能提取一些共用代码,便于重复使用。 问:怎样设置整个报表的缺省字体? 答:设置报表主对象的字体属性,也就是设置了整个报表的缺省字体。如果改变报表主对象的字体属性,则没有专门的设置字体属性的子对象的字体属性也跟随改变。同样每个报表节与明细网格也有字体属性,他们的字体属性也就是其拥有的子对象的缺省字体。 问:怎样在打印时限制一页的输出行数? 答:设定明细网格的内容行的‘每页行数(RowsPerPage)’属性即可。另外要注意‘调节行高(AdjustRowHeight)’属性值:为真时根据页面的输出高度自动调整行的高度,使整个页面的输出区域充满。为假时按设计时的高度输出行。 问:怎样显示中文大写金额? 答:将对象的“格式(Format)”属性设为 “$$” 及可,可以设置格式的对象有:字段(IGRField)、参数(IGRParameter)、系统变量(IGRSystemVarBox)与综合文字框(IGRMemoBox),其中综合文字框是在报表式上设格式。 问:能否实现自定义纸张与票据打印? 答:Grid++Report 完全支持自定义纸张的打印,只要在报表设定时在页面设置中选定自定义纸张,并指定准确的纸张尺寸。当然要在最终输出时得道合适的打印结果,输出打印机必须支持自定义纸张打印。Windows2000/XP/2003 操作系统上可以在打印机上定义自定义纸张,也可以采用这种方式实现自定义纸张打印。 问:怎样实现 0 值不打印? 答:直接设置格式串就可以,在“数字格式”设置对话框中选定“0 不显示”,就会得到合适的格式串。也可以通过直接录入格式串来指定 0 不显示,但格式串必须符合 Grid++Report 的规定格式。另一种实现办法是在报表获取明细记录数据时,在 BeforePostRecord 事件中将值为零的字段设为空,调用字段的 Clear 方法将字段置为空。 问:怎样实现多栏报表? 答:在明细网格上设‘页栏数(PageColumnCount)’属性值大于 1 即可。通过 Grid++Report 的“页栏输出顺序”还可以指定多栏报表的输出顺序是“先从上到下”还是“先从左到右”。 问:如何实现票据套打? 答:Grid++Report 为实现票据套打做了很多专门的安排:报表设计器提供了页面设计模式,按照设定的纸张尺寸显示设计面板,如果将空白票据的扫描图设为设计背景图,在定位报表内容的输出位置会非常方便。报表部件可以设定打印类别,非套打输出的内容在套打打印模式下就不会输出。 问:Grid++Report 有没有横向分页功能? 答:回答是肯定的,在列的总宽度超过打印页面的输出宽度时,Grid++Report 可以另起新页输出剩余的列,如果左边存在锁定列,锁定列可以在后面的新页中重复输出,这样可以保证关键数据列在每一页都有输出。仔细体会 Grid++Report 提供的多种打印适应策略,选用最合适的方式。Grid++Report 的多种打印适应策略为开发动态报表提供了很好的支持。 问:怎样实现报表本页小计功能? 答:定义一个报表分组,将本分组定义为页分组,在本分组的分组头与分组尾上定义统计。页分组就是在每页产生一个分组项,在每页的上端与下端都会分别显示页分组的分组头与分组尾,页分组不用定义分组依据字段。 报表运行 问:怎样与数据库建立连接? 答:如果在设计报表时指定了数据集的数据源连接串与查询 SQL 语句,Grid++Report 采用拉模式直接从数据源取得报表数据,Grid++Report 利用 OLE DB 从数据源取数,OLE DB 提供了广泛的数据源操作能力。如果 Grid++Report 的数据来源采用推模式,即 Grid++Report 不直接与数据库建立连接,各种编程语言/平台都提供了很好的数据库连接方式,并且易于操作,应用程序在报表主对象(IGridppReport)的 FetchRecord 事件中将数据传入,例子程序提供了各种编程语言填入数据的通用方法,对C++Builder 和 Delphi 还进行了专门的包装,直接关联 TDataSet 对象也可以将 TDataSet 对象中的数据传给报表。 问:打印时能否对打印纸张进行自适应?支持表格的折行打印吗? 答:Grid++Report 在打印时采用多种适应策略,通过设置明细网格(IGRDetailGrid)的‘打印策略(PrintAdaptMethod)’属性指定打印策略。(1)丢弃:按设计时列的宽度输出,超出范围的内容不显示。(2)绕行:按设计时列的宽度输出,如果在当前行不能完整输出,则另起新行进行输出。(3)缩放适应:对所有列的输出宽度进行按比例地缩放,使总宽度等于页面的输出宽度。(4)缩小适应:如果列的总宽度小于页面的输出宽度,对所有列的输出宽度进行按比例地缩小,使总宽度等于页面的输出宽度。(5)横向分页:超范围的列在新页中输出。(6)横向分页并重复锁定列。 问:如何改变缺省打印预览窗口的窗口标题? 答:改变报表主对象的‘标题(Title)’属性即可。 问:利用集合对象的编程接口取子对象的接口引用,但不是自己期望的结果。 答:Grid++Report中所有集合对象的下标索引都是从 1 开始,另按对象的名称查找对象的接口引用时,名称字符是不区分大小写的。 问:怎样在运行时控制报表中各个对象的可见性?即怎样在运行时显示或隐藏对象? 答:在报表主对象(GridppReport)的 SectionFormat 事件中设定相应报表子对象的可见(Visible)属性即可。 问:报表主对象重新载入数据,设计器中为什么没有反映新载入的数据? 答:应调用 IGRDesigner 的 Reload 方法。 问:怎样实现不进入打印预览界面,直接将报表打印出来?