Qt编写可视化大屏电子看板系统18-柱状分组图
生活随笔
收集整理的這篇文章主要介紹了
Qt编写可视化大屏电子看板系统18-柱状分组图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、前言
柱狀分組圖是柱狀堆積圖的衍生或者另外一種展示效果,設置的數據值數據源集合完全一樣,只不過就是把柱子給拿下來了放在旁邊,然后一個分組多個柱子橫向排列,不同分組之間有一定的空隙隔開,默認QCustomPlot是不提供分組圖的,怎么辦呢,于是搞了一個很巧妙的算法,在標注柱狀圖的基礎上,通過不同分組設置不同的x坐標也就是key值來形成分組的效果,key相隔的差值越大則距離越大,同一分組之間的柱子是緊挨著的沒有差值,只有不同分組的第一個柱子需要有差值,于是搞了一個小小的計算公式來計算這個差值。
沒有內置分組圖的功能也不能怪QCustomPlot,畢竟他核心處理是曲線圖,在曲線圖展示處理這塊牛逼的沒得話說,這是他的核心,拋開這個核心再三心二意搞點周邊的功能,搞得好還好說,搞不好就一堆罵,其實這正是我想強調的一點,做軟件開發這點尤為重要,千萬不能三心二意東搞搞西搞搞,一定要先把核心的功能處理好穩定好,再慢慢拓展周邊的功能,如果是上級領導要求遍地開花的搞一下這個有搞一下那個,大可以頂回去,讓他自己寫代碼去。
二、功能特點
三、體驗地址
四、效果圖
五、核心代碼
#include "customplottracer.h"CustomPlotTracer::CustomPlotTracer(QCustomPlot *parentPlot) : QCPLayerable(parentPlot) {showLineh = false;showLinev = false;lineWidth = 3;lineColor = QColor(255, 0, 0);movePos = QPoint(100, 50);customPlot = parentPlot;connect(parentPlot, SIGNAL(mouseMove(QMouseEvent *)), this, SLOT(mouseMove(QMouseEvent *))); }void CustomPlotTracer::applyDefaultAntialiasingHint(QCPPainter *painter) const {Q_UNUSED(painter); }void CustomPlotTracer::draw(QCPPainter *painter) {QPen pen;pen.setWidth(lineWidth);pen.setColor(lineColor);painter->setPen(pen);if (showLinev) {painter->drawLine(QPointF(x, dHeight), QPointF(x, dY));pen.setColor(Qt::white);painter->setPen(pen);QFontMetrics fm = painter->fontMetrics();QString year = QString::number(QDate::currentDate().year()); #if (QT_VERSION >= QT_VERSION_CHECK(5,11,0))int width = fm.horizontalAdvance(year); #elseint width = fm.width(year); #endifpainter->drawText(QPointF(x - width / 2, dHeight - 3), year);}if (showLineh) {pen.setColor(lineColor);painter->setPen(pen);painter->drawLine(QPointF(dX, y), QPointF(dWidth, y));} }bool CustomPlotTracer::getShowLineh() const {return this->showLineh; }bool CustomPlotTracer::getShowLinev() const {return this->showLinev; }int CustomPlotTracer::getLineWidth() const {return this->lineWidth; }QColor CustomPlotTracer::getLineColor() const {return this->lineColor; }void CustomPlotTracer::mouseMove(QMouseEvent *event) {Q_UNUSED(event);movePos = event->pos();drawLine(); }void CustomPlotTracer::setShowLineh(bool showLineh) {if (this->showLineh != showLineh) {this->showLineh = showLineh;customPlot->replot();} }void CustomPlotTracer::setShowLinev(bool showLinev) {if (this->showLinev != showLinev) {this->showLinev = showLinev;customPlot->replot();} }void CustomPlotTracer::setLineWidth(int lineWidth) {if (this->lineWidth != lineWidth) {this->lineWidth = lineWidth;customPlot->replot();} }void CustomPlotTracer::setLineColor(const QColor &lineColor) {if (this->lineColor != lineColor) {this->lineColor = lineColor;customPlot->replot();} }void CustomPlotTracer::drawLine() {x = movePos.x();y = movePos.y();//取出當前XY軸的范圍值QCPRange rangex = customPlot->xAxis->range();QCPRange rangey = customPlot->yAxis->range();//qDebug() << rangex.lower << rangex.upper << rangey.lower << rangey.upper;//取出左下角坐標軸原點(0,0)在整個畫布的屏幕坐標位置dX = customPlot->xAxis->coordToPixel(rangex.lower);dY = customPlot->yAxis->coordToPixel(rangey.lower);dWidth = customPlot->xAxis->coordToPixel(rangex.upper);dHeight = customPlot->yAxis->coordToPixel(rangey.upper);//必須在曲線區域內if (x >= dX && x <= dWidth && y <= dY && y >= dHeight) {customPlot->replot();} }總結
以上是生活随笔為你收集整理的Qt编写可视化大屏电子看板系统18-柱状分组图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql时间戳转换日期
- 下一篇: 哲学笔记:之:天道性命