Qt编写可视化大屏电子看板系统16-标准柱状图
一、前言
標準柱狀圖是大屏系統中最常用的一種展示數據效果圖,提供不同的柱子顯示數據值,在QCustomPlot的基礎上拓展了頂部顯示對應的值,不同的柱子不同的顏色,同時還可以調用內置的觸發報警顏色的機制,比如超過90%就自動紅色顯示,這樣用戶使用的時候只要傳入值就行,默認的機制一般是三種顏色,正常顏色、警告顏色、報警顏色,三種顏色都對應觸發值,超過對應的值就顯示對應的顏色。一般的規則都是大于設定的警戒值就觸發,其實真實世界是簡單的也是復雜的,還有不少的場景是低于一定的值才報警,倒過來的規則,可能會有三種規則,規則1大于報警值報警(大部分的規則都是這個),規則2小于報警值報警(比如氣體濃度),規則3在范圍值內則報警。
在柱狀圖上面顯示對應的值這個需求非常多,不清楚為何很多曲線圖控件不默認就支持這個特性,而是需要額外的寫代碼繪制處理,比如Qt界著名的曲線圖三劍客QCustomPlot、Qwt、QChart都默認沒有這個效果,好在開源的世界是豐富多彩的,這三劍客本身都是開源的,源代碼量也不是很多,稍微花點時間看看學習下也是不錯的選擇,而且柱狀圖上顯示對應的值的處理和代碼網上也是一大堆,面向百度和搜索編程相信是眾多程序員的選擇和喜愛。
開源庫QCustomPlot有多個版本,基本上可以歸納為v1和v2,在部分代碼處理上有區別,所以為了支持v1和v2兩個版本,在做二次開發的時候都做了兼容,其實必須性不大,畢竟v2也從Qt4.6支持到了Qt6,蠻好的,做支持的時候純粹為了鍛煉技術,看下兩者到底區別在哪,處理方式有何區別,還有個稍微微小的考慮就是可能有些用戶還在用v1為了兼容這批用戶,省得換成v2整個項目曲線的地方改動不小。
二、功能特點
三、體驗地址
四、效果圖
五、核心代碼
#include "customplotbarv.h"CustomPlotBarv::CustomPlotBarv(QCPAxis *keyAxis, QCPAxis *valueAxis) : QCPBars(keyAxis, valueAxis) {valuePosition = 1;valuePrecision = 0;valueColor = Qt::white;checkData = false; }void CustomPlotBarv::draw(QCPPainter *painter) {//必須先繼續繪制父類,不然父類的所有東西沒有繪制//順序不能反,先繪制完父類再繪制自定義的數據,不然會覆蓋QCPBars::draw(painter);//迭代拿到每個區域的坐標和寬高int index = -1; #ifdef qcustomplot_v1QCPBarDataMap::const_iterator visibleBegin, visibleEnd;getVisibleDataBounds(visibleBegin, visibleEnd);for (QCPBarDataMap::const_iterator it = visibleBegin; it != visibleEnd; ++it) { #elseQCPBarsDataContainer::const_iterator visibleBegin, visibleEnd;getVisibleDataBounds(visibleBegin, visibleEnd);for (QCPBarsDataContainer::const_iterator it = visibleBegin; it != visibleEnd; ++it) { #endif//獲取柱狀圖區域 #ifdef qcustomplot_v1QPolygonF barPolygon = getBarPolygon(it.key(), it.value().value);QRectF rect;int x1 = barPolygon.at(1).x();int y1 = barPolygon.at(1).y();int x2 = barPolygon.at(3).x();int y2 = barPolygon.at(3).y();rect.setX(x1);rect.setY(y1);rect.setWidth(x2 - x1);rect.setHeight(y2 - y1); #elseQRectF rect = getBarRect(it->key, it->value); #endif//先處理校驗數據index++;if (checkData) {if (it->value >= 80) {setBrush(QColor(0, 176, 180));} else if (it->value >= 60) {setBrush(QColor(255, 192, 0));} else {setBrush(QColor(214, 77, 84));}} else {//如果存在顏色集合則取顏色集合if (index < barColors.count()) {setBrush(barColors.at(index));}}//設置畫筆和畫刷,繪制矩形區域形成柱狀圖if (this->pen() != Qt::NoPen) {painter->setPen(this->pen().color());}painter->setBrush(this->brush());painter->drawRect(rect);//設置文本的顏色,還可以設置字體painter->setPen(valueColor);//這里可以設置小數點精確度QString strValue = QString::number(it->value, 'f', valuePrecision);//計算字體的高度QFontMetrics fm = painter->fontMetrics(); #if (QT_VERSION >= QT_VERSION_CHECK(5,11,0))int textWidth = fm.horizontalAdvance(strValue); #elseint textWidth = fm.width(strValue); #endifint textHeight = fm.ascent() + fm.descent();//如果矩形寬度小于文字寬度則不繪制if (rect.width() < textWidth) {continue;}//如果不在頂部,矩形高度小于文字高度則不繪制if (valuePosition > 1) {if (rect.height() < textHeight) {continue;}}//0-不繪制 1-頂部上面 2-頂部居中 3-中間居中 4-底部居中//設置區域一點點偏差,看起來不那么擁擠int offset = 3;if (valuePosition == 1) {rect.setY(rect.y() - textHeight - offset);painter->drawText(rect, Qt::AlignTop | Qt::AlignHCenter, strValue);} else if (valuePosition == 2) {rect.setY(rect.y() + offset);painter->drawText(rect, Qt::AlignTop | Qt::AlignHCenter, strValue);} else if (valuePosition == 3) {painter->drawText(rect, Qt::AlignVCenter | Qt::AlignHCenter, strValue);} else if (valuePosition == 4) {rect.setHeight(rect.height() - offset);painter->drawText(rect, Qt::AlignBottom | Qt::AlignHCenter, strValue);}} }int CustomPlotBarv::getValuePosition() const {return this->valuePosition; }int CustomPlotBarv::getValuePrecision() const {return this->valuePrecision; }QColor CustomPlotBarv::getValueColor() const {return this->valueColor; }bool CustomPlotBarv::getCheckData() const {return this->checkData; }void CustomPlotBarv::setValuePostion(int valuePosition) {if (this->valuePosition != valuePosition) {this->valuePosition = valuePosition;} }void CustomPlotBarv::setValuePrecision(int valuePrecision) {if (this->valuePrecision != valuePrecision) {this->valuePrecision = valuePrecision;} }void CustomPlotBarv::setValueColor(const QColor &valueColor) {if (this->valueColor != valueColor) {this->valueColor = valueColor;} }void CustomPlotBarv::setCheckData(bool checkData) {if (this->checkData != checkData) {this->checkData = checkData;} }void CustomPlotBarv::setBarColors(const lcolor &barColors) {this->barColors = barColors; }總結
以上是生活随笔為你收集整理的Qt编写可视化大屏电子看板系统16-标准柱状图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IDG刘雨坤:关于SaaS业务七点经验
- 下一篇: Oracle Livelabs实验: S