找回密码
 立即注册
收起左侧

Qt编写自定义控件25-自定义QCustomPlot

0
回复
5162
查看
[复制链接]
累计签到:7 天
连续签到:1 天
来源: 2019-6-8 21:34:35 显示全部楼层 |阅读模式

马上注册,查看详细内容!注册请先查看:注册须知

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
一、前言
上次在写大屏数据可视化电子看板系统时候,提到过改造QCustomPlot来实现柱状分组图、横向柱状图、横向分组图、鼠标悬停提示等。这次单独列出来描述,有很多人疑问为啥不用QChart,或者echart等形式,其实这两种方式我都尝试过,比如Qt5.7以后新增的QChart模块,曲线这块,支持数据量很小,而且用法极其不适应,非常别扭,尤其是10W以上数据量的支持,简直是渣渣,优点也是有很多的,比如动画效果,我看过他的完整源码,动画这块处理的非常好,连坐标轴都可以有动画效果,而且支持很多种效果,而且内置了很多套theme皮肤,省去了很多渣渣审美的程序员自己来配色,这个倒是挺方便的。而对于echart,必须依赖浏览器控件,资源占用比较高,后面决定采用改造QCustomPlot来实现用户需要的各种图表效果。
在整个改造的过程中,全部封装成易用的函数,传入参数即可,同时还支持全局样式更改,支持样式表控制整体颜色更改,考虑了很多细节,比如弹出悬停信息的位置等,都自动计算显示在最佳最合理位置。考虑到很多人用的QCustomPlot1.0,特意还做了QCustomPlot1.0和2.0的完全兼容。

二、实现的功能
* 1:可设置X轴Y轴范围值
* 2:可设置背景颜色+文本颜色+网格颜色
* 3:可设置三条曲线颜色+颜色集合
* 4:可设置是否显示定位十字线,可分别设置横向和纵向
* 5:可设置十字线的宽度和颜色
* 6:可设置是否显示数据点以及数据点的大小
* 7:可设置是否填充背景形成面积图
* 8:可设置模式-拖动+缩放等
* 9:可设置坐标轴间距+第二坐标系可见
* 10:提供接口setDataLine直接设置曲线,支持多条
* 11:提供接口setDataBar直接设置柱状图,支持多条形成堆积图
* 12:提供接口setLabs设置文本标签替代key
* 13:提供清空+重绘接口+外部获取QCustomPlot对象
* 14:提供函数start+stop来模拟正弦曲线
* 15:可设置柱状图的值的位置+精确度+颜色
* 16:支持鼠标移动到数据点高亮显示数据点以及显示数据提示信息
* 17:可设置提示信息位置 自动处理+顶部+右上角+右侧+右下角+底部+左下角+左侧+左上角
* 18:可设置是否校验数据产生不同的背景颜色,比如柱状图的每根柱子都可以根据数据生成不同背景颜色
* 19:可设置是否显示图例+图例位置+图例行数
* 20:支持多条曲线+柱状图+柱状分组图+横向柱状图+横向柱状分组图+柱状堆积图
* 21:内置15套精美颜色,自动取颜色集合的颜色,省去配色的烦恼
* 22:同时支持 QCustomPlot 1.0 和 QCustomPlot 2.0

三、效果图



四、核心代码
  1. void CustomPlot::setDataLine(int index, const QString &name, const QVector<double> &key, const QVector<double> &value)
  2. {
  3.     if (customPlot->graphCount() > index) {
  4.         customPlot->graph(index)->setName(name);
  5.         customPlot->graph(index)->setData(key, value);
  6.         customPlot->xAxis->setRange(-offsetX, key.count() + offsetX, Qt::AlignLeft);

  7.         //超过3条线条颜色设置颜色集合的颜色
  8.         if (index >= 3) {
  9.             setColor(index, colors.at(index));
  10.         } else {
  11.             setColor(0, colors.at(0));
  12.             setColor(1, colors.at(1));
  13.             setColor(2, colors.at(2));
  14.         }
  15.     }
  16. }

  17. void CustomPlot::setDataBarv(const QStringList &rowNames,
  18.                              const QStringList &columnNames,
  19.                              const QList<QVector<double> > &values,
  20.                              const QColor &borderColor,
  21.                              int valuePosition,
  22.                              int valuePrecision,
  23.                              const QColor &valueColor,
  24.                              bool checkData)
  25. {
  26.     //只有1列的才能设置
  27.     if (columnNames.count() != 1) {
  28.         return;
  29.     }

  30.     //可以直接用堆积图,因为只有一列的柱状图不会形成堆积
  31.     setDataBars(rowNames, columnNames, values, borderColor, valuePosition, valuePrecision, valueColor, checkData);
  32. }

  33. void CustomPlot::setDataBarvs(const QStringList &rowNames,
  34.                               const QStringList &columnNames,
  35.                               const QList<QVector<double> > &values,
  36.                               const QColor &borderColor,
  37.                               int valuePosition,
  38.                               int valuePrecision,
  39.                               const QColor &valueColor,
  40.                               bool checkData)
  41. {
  42.     //过滤个数不一致数据,防止索引越界
  43.     int rowCount = rowNames.count();
  44.     int columnCount = columnNames.count();
  45.     int valueCount = values.count();
  46.     if (columnCount == 0 || valueCount == 0 || columnCount != valueCount) {
  47.         return;
  48.     }

  49.     //设置网格线不显示,会更好看
  50.     customPlot->xAxis->grid()->setVisible(false);
  51.     //customPlot->yAxis->grid()->setVisible(false);

  52.     //设置横坐标文字描述
  53.     QVector<double> ticks;
  54.     QVector<QString> labels;
  55.     int count = rowCount * columnCount;
  56.     for (int i = 0; i < rowCount; i++) {
  57.         ticks << 1.5 + (i * columnCount);
  58.         labels << rowNames.at(i);
  59.     }

  60.     setLabX(ticks, labels);
  61.     customPlot->xAxis->setRange(0, count + 1);

  62.     for (int i = 0; i < columnCount; i++) {
  63.         //同样也要先过滤个数是否符合要求
  64.         QVector<double> value = values.at(i);
  65.         if (rowCount != value.count()) {
  66.             continue;
  67.         }

  68.         //创建柱状图
  69.         CustomBarv *bar = new CustomBarv(customPlot->xAxis, customPlot->yAxis);
  70.         bar->setCheckData(checkData);

  71.         //设置宽度比例
  72.         bar->setWidth(0.9);

  73.         //设置显示值的位置 0-不绘制 1-顶部上面 2-顶部居中 3-中间居中 4-底部居中
  74.         bar->setValuePostion(valuePosition);
  75.         bar->setValuePrecision(valuePrecision);
  76.         bar->setValueColor(valueColor);

  77.         //设置名称
  78.         bar->setName(columnNames.at(i));

  79.         //设置颜色,取颜色集合
  80.         QColor color = QColor(51, 204, 255);
  81.         if (i < colors.count()) {
  82.             color = colors.at(i);
  83.         }

  84.         //边缘高亮,如果传入了边框颜色则取边框颜色
  85.         bar->setPen(QPen(borderColor == Qt::transparent ? color.light(150) : borderColor));
  86.         bar->setBrush(color);

  87.         //这个算法很巧妙,想了很久
  88.         QVector<double> ticks;
  89.         double offset = i * 0.9;
  90.         for (int j = 0; j < rowCount; j++) {
  91.             ticks << 1.0 + (j * columnCount) + offset;
  92.         }

  93.         //设置数据
  94.         bar->setData(ticks, value);
  95.     }
  96. }

  97. void CustomPlot::setDataBarh(const QStringList &rowNames,
  98.                              const QStringList &columnNames,
  99.                              const QList<QVector<double> > &values,
  100.                              const QColor &borderColor,
  101.                              int valuePosition,
  102.                              int valuePrecision,
  103.                              const QColor &valueColor,
  104.                              bool checkData)
  105. {
  106.     //只有1列的才能设置
  107.     if (columnNames.count() != 1) {
  108.         return;
  109.     }

  110.     //过滤个数不一致数据,防止索引越界
  111.     int rowCount = rowNames.count();
  112.     int columnCount = columnNames.count();
  113.     int valueCount = values.count();
  114.     if (columnCount == 0 || valueCount == 0 || columnCount != valueCount) {
  115.         return;
  116.     }

  117.     //设置网格线不显示,会更好看
  118.     customPlot->xAxis->grid()->setVisible(false);
  119.     customPlot->yAxis->grid()->setVisible(false);
  120.     customPlot->yAxis->setTickLength(0, 0);

  121.     //设置横坐标文字描述
  122.     QVector<double> ticks;
  123.     QVector<QString> labels;
  124.     int count = rowCount * columnCount;
  125.     double padding = 1;
  126.     for (int i = 0; i < rowCount; i++) {
  127.         ticks << padding + (i * columnCount);
  128.         labels << rowNames.at(i);
  129.     }

  130.     setLabY(ticks, labels);
  131.     customPlot->yAxis->setRange(0, count + 1);

  132.     //先计算出每个柱子占用的高度
  133.     double barHeight = 0.7;
  134.     for (int i = 0; i < columnCount; i++) {
  135.         //同样也要先过滤个数是否符合要求
  136.         QVector<double> value = values.at(i);
  137.         if (rowCount != value.count()) {
  138.             continue;
  139.         }

  140.         //先绘制系列1的数据,再绘制系列2,依次类推
  141.         for (int j = 0; j < rowCount; j++) {
  142.             //创建横向柱状图
  143.             double y = (0.67 + (j * columnCount));
  144.             CustomBarh *bar = new CustomBarh(customPlot);
  145.             bar->setCheckData(checkData);
  146.             bar->setRect(QPointF(0, y), QPointF(value.at(j), y + barHeight));
  147.             bar->setValue(value.at(j));

  148.             //设置显示值的位置 0-不绘制 1-顶部上面 2-顶部居中 3-中间居中 4-底部居中
  149.             bar->setValuePostion(valuePosition);
  150.             bar->setValuePrecision(valuePrecision);
  151.             bar->setValueColor(valueColor);

  152.             //设置颜色,取颜色集合
  153.             QColor color = QColor(51, 204, 255);
  154.             if (i < colors.count()) {
  155.                 color = colors.at(i);
  156.             }

  157.             //边缘高亮,如果传入了边框颜色则取边框颜色
  158.             bar->setPen(QPen(borderColor == Qt::transparent ? color.light(150) : borderColor));
  159.             bar->setBrush(color);
  160.         }
  161.     }
  162. }

  163. void CustomPlot::setDataBarhs(const QStringList &rowNames,
  164.                               const QStringList &columnNames,
  165.                               const QList<QVector<double> > &values,
  166.                               const QColor &borderColor,
  167.                               int valuePosition,
  168.                               int valuePrecision,
  169.                               const QColor &valueColor,
  170.                               bool checkData)
  171. {
  172.     //过滤个数不一致数据,防止索引越界
  173.     int rowCount = rowNames.count();
  174.     int columnCount = columnNames.count();
  175.     int valueCount = values.count();
  176.     if (columnCount == 0 || valueCount == 0 || columnCount != valueCount) {
  177.         return;
  178.     }

  179.     //设置网格线不显示,会更好看
  180.     customPlot->xAxis->grid()->setVisible(false);
  181.     customPlot->yAxis->grid()->setVisible(false);
  182.     customPlot->yAxis->setTickLength(0, 0);
  183.     customPlot->xAxis->setVisible(false);

  184.     //设置横坐标文字描述
  185.     QVector<double> ticks;
  186.     QVector<QString> labels;
  187.     int count = rowCount * columnCount;
  188.     //这个算法想了很久,很牛逼
  189.     double padding = 1.5 + (columnCount - 2) * 0.4;
  190.     for (int i = 0; i < rowCount; i++) {
  191.         ticks << padding + (i * columnCount);
  192.         labels << rowNames.at(i);
  193.     }

  194.     setLabY(ticks, labels);
  195.     customPlot->yAxis->setRange(0, count + 1);

  196.     //先计算出每个柱子占用的高度
  197.     double barHeight = 0.8;
  198.     for (int i = 0; i < columnCount; i++) {
  199.         //同样也要先过滤个数是否符合要求
  200.         QVector<double> value = values.at(i);
  201.         if (rowCount != value.count()) {
  202.             continue;
  203.         }

  204.         //先绘制系列1的数据,再绘制系列2,依次类推
  205.         for (int j = 0; j < rowCount; j++) {
  206.             //创建横向柱状图
  207.             double y = (0.7 + i * barHeight + (j * columnCount));
  208.             CustomBarh *bar = new CustomBarh(customPlot);
  209.             bar->setCheckData(checkData);
  210.             bar->setRect(QPointF(0, y), QPointF(value.at(j), y + barHeight));
  211.             bar->setValue(value.at(j));

  212.             //设置显示值的位置 0-不绘制 1-顶部上面 2-顶部居中 3-中间居中 4-底部居中
  213.             bar->setValuePostion(valuePosition);
  214.             bar->setValuePrecision(valuePrecision);
  215.             bar->setValueColor(valueColor);

  216.             //设置颜色,取颜色集合
  217.             QColor color = QColor(51, 204, 255);
  218.             if (j < colors.count()) {
  219.                 color = colors.at(j);
  220.             }

  221.             //边缘高亮,如果传入了边框颜色则取边框颜色
  222.             bar->setPen(QPen(borderColor == Qt::transparent ? color.light(150) : borderColor));
  223.             bar->setBrush(color);
  224.         }
  225.     }
  226. }

  227. void CustomPlot::setDataBars(const QStringList &rowNames,
  228.                              const QStringList &columnNames,
  229.                              const QList<QVector<double> > &values,
  230.                              const QColor &borderColor,
  231.                              int valuePosition,
  232.                              int valuePrecision,
  233.                              const QColor &valueColor,
  234.                              bool checkData)
  235. {
  236.     //过滤个数不一致数据,防止索引越界
  237.     int rowCount = rowNames.count();
  238.     int columnCount = columnNames.count();
  239.     int valueCount = values.count();
  240.     if (columnCount == 0 || valueCount == 0 || columnCount != valueCount) {
  241.         return;
  242.     }

  243.     //设置网格线不显示,会更好看
  244.     customPlot->xAxis->grid()->setVisible(false);
  245.     //customPlot->yAxis->grid()->setVisible(false);

  246.     //先清空原有柱状图
  247.     bars.clear();

  248.     //设置横坐标文字描述
  249.     QVector<double> ticks;
  250.     QVector<QString> labels;
  251.     for (int i = 0; i < rowCount; i++) {
  252.         ticks << i + 1;
  253.         labels << rowNames.at(i);
  254.     }

  255.     setLabX(ticks, labels);
  256.     customPlot->xAxis->setRange(0, rowCount + 1);

  257.     for (int i = 0; i < columnCount; i++) {
  258.         //同样也要先过滤个数是否符合要求
  259.         QVector<double> value = values.at(i);
  260.         if (rowCount != value.count()) {
  261.             continue;
  262.         }

  263.         //创建柱状堆积图
  264.         CustomBarv *bar = new CustomBarv(customPlot->xAxis, customPlot->yAxis);
  265.         bar->setCheckData(checkData);

  266.         //设置宽度比例
  267.         bar->setWidth(0.6);

  268.         //设置显示值的位置 0-不绘制 1-顶部上面 2-顶部居中 3-中间居中 4-底部居中
  269.         bar->setValuePostion(valuePosition);
  270.         bar->setValuePrecision(valuePrecision);
  271.         bar->setValueColor(valueColor);

  272. #ifndef old
  273.         //设置堆积间隙
  274.         if (borderColor != Qt::transparent) {
  275.             bar->setStackingGap(1);
  276.         }
  277. #endif
  278.         //设置名称
  279.         bar->setName(columnNames.at(i));

  280.         //设置颜色,取颜色集合
  281.         QColor color = QColor(51, 204, 255);
  282.         if (i < colors.count()) {
  283.             color = colors.at(i);
  284.         }

  285.         //边缘高亮,如果传入了边框颜色则取边框颜色
  286.         if (columnCount > 1 && borderColor == Qt::transparent) {
  287.             bar->setPen(Qt::NoPen);
  288.         } else {
  289.             bar->setPen(QPen(borderColor == Qt::transparent ? color.light(150) : borderColor));
  290.         }

  291.         bar->setBrush(color);

  292.         //设置堆积层叠顺序,后面那个移到前一个上面
  293.         bars << bar;
  294.         if (i > 0) {
  295.             bar->moveAbove(bars.at(i - 1));
  296.         }

  297.         //设置数据
  298.         bar->setData(ticks, value);
  299.     }
  300. }
复制代码


五、控件介绍
1. 超过146个精美控件,涵盖了各种仪表盘、进度条、进度球、指南针、曲线图、标尺、温度计、导航条、导航栏,flatui、高亮按钮、滑动选择器、农历等。远超qwt集成的控件数量。
2. 每个类都可以独立成一个单独的控件,零耦合,每个控件一个头文件和一个实现文件,不依赖其他文件,方便单个控件以源码形式集成到项目中,较少代码量。qwt的控件类环环相扣,高度耦合,想要使用其中一个控件,必须包含所有的代码。
3. 全部纯Qt编写,QWidget+QPainter绘制,支持Qt4.6到Qt5.12的任何Qt版本,支持mingw、msvc、gcc等编译器,支持任意操作系统比如windows+linux+mac+嵌入式linux等,不乱码,可直接集成到Qt  Creator中,和自带的控件一样使用,大部分效果只要设置几个属性即可,极为方便。
4. 每个控件都有一个对应的单独的包含该控件源码的DEMO,方便参考使用。同时还提供一个所有控件使用的集成的DEMO。
5. 每个控件的源代码都有详细中文注释,都按照统一设计规范编写,方便学习自定义控件的编写。
6. 每个控件默认配色和demo对应的配色都非常精美。
7. 超过130个可见控件,6个不可见控件。
8. 部分控件提供多种样式风格选择,多种指示器样式选择。
9. 所有控件自适应窗体拉伸变化。
10.  集成自定义控件属性设计器,支持拖曳设计,所见即所得,支持导入导出xml格式。
11. 自带activex控件demo,所有控件可以直接运行在ie浏览器中。
12. 集成fontawesome图形字体+阿里巴巴iconfont收藏的几百个图形字体,享受图形字体带来的乐趣。
13. 所有控件最后生成一个dll动态库文件,可以直接集成到qtcreator中拖曳设计使用。
14. 目前已经有qml版本,后期会考虑出pyqt版本,如果用户需求量很大的话。

六、SDK下载
- SDK下载链接:https://pan.baidu.com/s/1tD9v1YPfE2fgYoK6lqUr1Q 提取码:lyhk
- 自定义控件+属性设计器欣赏:https://pan.baidu.com/s/1l6L3rKSiLu_uYi7lnL3ibQ 提取码:tmvl
- 下载链接中包含了各个版本的动态库文件,所有控件的头文件,使用demo。
- 自定义控件插件开放动态库dll使用(永久免费),无任何后门和限制,请放心使用。
- 目前已提供26个版本的dll,其中包括了qt5.12.3 msvc2017 32+64 mingw 32+64 的。
- 不定期增加控件和完善控件,不定期更新SDK,欢迎各位提出建议,谢谢!
- widget版本(QQ:517216493)qml版本(QQ:373955953)三峰驼(QQ:278969898)。
- 涛哥的知乎专栏 Qt进阶之路 https://zhuanlan.zhihu.com/TaoQt
- 欢迎关注微信公众号【高效程序员】,C++/Python、学习方法、写作技巧、热门技术、职场发展等内容,干货多多,福利多多!




回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了