精华内容
下载资源
问答
  • Qt 柱状图

    千次阅读 2018-03-22 10:37:48
    1.官网下载qcustomplot .cpp,qcustomplot.h文件 2.将上述两个文件放在自己的工作目录下 ...5.在main.cpp中创建QCustomPlot类,然后调用帮助类中的以上函数(要设置QCustomPlot对象的宽和高)即可完成简单的柱状图

    1.官网下载qcustomplot .cpp,qcustomplot.h文件

    2.将上述两个文件放在自己的工作目录下

    3.将上述两个文件引入CMakeLists.txt中如(qcustomplot .cpp,qcustomplot.h放置的位置和main.cpp在同一目录下):

    set(SOURCE_FILES
          main.cpp
          qcustomplot.h
          qcustomplot.cpp
          )
    4.创建一个帮助类,其中包含以下方法
    
    void CMainForm::setupBarChartDemo(QCustomPlot *customPlot)
    {
    
         //this->setFixedSize(1920,1080);
         std::string demoName = "Bar Chart Demo";
         QLinearGradient gradient(0, 0, 0, 400);
         gradient.setColorAt(0, QColor(90, 90, 90));
         gradient.setColorAt(0.38, QColor(105, 105, 105));
         gradient.setColorAt(1, QColor(70, 70, 70));
         customPlot->setBackground(QBrush(gradient));
    
         // create empty bar chart objects:
         QCPBars *regen = new QCPBars(customPlot->xAxis, customPlot->yAxis);
         QCPBars *nuclear = new QCPBars(customPlot->xAxis, customPlot->yAxis);
         QCPBars *fossil = new QCPBars(customPlot->xAxis, customPlot->yAxis);
         regen->setAntialiased(false); // gives more crisp, pixel aligned bar borders
         nuclear->setAntialiased(false);
         fossil->setAntialiased(false);
         regen->setStackingGap(1);
         nuclear->setStackingGap(1);
         fossil->setStackingGap(1);
         fossil->setName("Fossil fuels");
         fossil->setPen(QPen(QColor(111, 9, 176).lighter(170)));
         fossil->setBrush(QColor(111, 9, 176));
         nuclear->setName("Nuclear");
         nuclear->setPen(QPen(QColor(250, 170, 20).lighter(150)));
         nuclear->setBrush(QColor(250, 170, 20));
         regen->setName("Regenerative");
         regen->setPen(QPen(QColor(0, 168, 140).lighter(130)));
         regen->setBrush(QColor(0, 168, 140));
         // stack bars on top of each other:
         nuclear->moveAbove(fossil);
         regen->moveAbove(nuclear);
    
         // prepare x axis with country labels:
         QVector<double> ticks;
         QVector<QString> labels;
         ticks << 1 << 2 << 3 << 4 << 5 << 6 << 7;
         labels << "USA" << "Japan" << "Germany" << "France" << "UK" << "Italy" << "Canada";
         QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
         textTicker->addTicks(ticks, labels);
         customPlot->xAxis->setTicker(textTicker);
         customPlot->xAxis->setTickLabelRotation(60);
         customPlot->xAxis->setSubTicks(false);
         customPlot->xAxis->setTickLength(0, 4);
         customPlot->xAxis->setRange(0, 8);
         customPlot->xAxis->setBasePen(QPen(Qt::white));
         customPlot->xAxis->setTickPen(QPen(Qt::white));
         customPlot->xAxis->grid()->setVisible(true);
         customPlot->xAxis->grid()->setPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine));
         customPlot->xAxis->setTickLabelColor(Qt::white);
         customPlot->xAxis->setLabelColor(Qt::white);
    
         // prepare y axis:
         customPlot->yAxis->setRange(0, 12.1);//set y
         customPlot->yAxis->setPadding(5); // a bit more space to the left border
         customPlot->yAxis->setLabel("Power Consumption in\nKilowatts per Capita (2007)");
         customPlot->yAxis->setBasePen(QPen(Qt::white));
         customPlot->yAxis->setTickPen(QPen(Qt::white));
         customPlot->yAxis->setSubTickPen(QPen(Qt::white));
         customPlot->yAxis->grid()->setSubGridVisible(true);
         customPlot->yAxis->setTickLabelColor(Qt::white);
         customPlot->yAxis->setLabelColor(Qt::white);
         customPlot->yAxis->grid()->setPen(QPen(QColor(130, 130, 130), 0, Qt::SolidLine));
         customPlot->yAxis->grid()->setSubGridPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine));
    
         // Add data:
         QVector<double> fossilData, nuclearData, regenData;
         fossilData  << 0.86*10.5 << 0.83*5.5 << 0.84*5.5 << 0.52*5.8 << 0.89*5.2 << 0.90*4.2 << 0.67*11.2;
         nuclearData << 0.08*10.5 << 0.12*5.5 << 0.12*5.5 << 0.40*5.8 << 0.09*5.2 << 0.00*4.2 << 0.07*11.2;
         regenData   << 0.06*10.5 << 0.05*5.5 << 0.04*5.5 << 0.06*5.8 << 0.02*5.2 << 0.07*4.2 << 0.25*11.2;
         fossil->setData(ticks, fossilData);
         nuclear->setData(ticks, nuclearData);
         regen->setData(ticks, regenData);
    
         // setup legend:
         customPlot->legend->setVisible(true);
         customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop|Qt::AlignHCenter);
         customPlot->legend->setBrush(QColor(255, 255, 255, 100));
         customPlot->legend->setBorderPen(Qt::NoPen);
        QFont legendFont = font();
        legendFont.setPointSize(10);
        customPlot->legend->setFont(legendFont);
        customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
    }
    
    5.在main.cpp中创建QCustomPlot类,然后调用帮助类中的以上函数(要设置QCustomPlot对象的宽和高)即可完成简单的柱状图
    




    展开全文
  • Qt 柱状图绘制

    2018-03-21 20:11:34
    柱状图的绘制进行封装,通过类函数setValue进行传值,根据所传的值的大小绘制不同高度的柱状图,并在柱状图顶部显示具体数值,纵坐标标有刻度,功能实现简单明了,工程文件下载即可运行。
  • 自定义封装Qt柱状图类,提供简单数据更新等接口实现柱状图对象的创建、销毁、数据更新和清空等功能,实现数据动态更新和柱状图上鼠标悬停数值显示。
  • qt柱状图_饼状图

    2021-05-14 16:15:01
    tip:鼠标悬浮提示...QtCharts/QChartView> #include <QtCharts/QBarSeries> #include <QtCharts/QBarSet> #include <QtCharts/QLegend> #include <QtCharts/QBarCategoryAxis> #incl.

    在这里插入图片描述
    tip:鼠标悬浮提示自定义信息
    barchart.h

    #ifndef BARCHART_H
    #define BARCHART_H
    #include <QtCharts/QChartView>
    #include <QtCharts/QBarSeries>
    #include <QtCharts/QBarSet>
    #include <QtCharts/QLegend>
    #include <QtCharts/QBarCategoryAxis>
    #include "callout.h"
    #include <QObject>
    #include <QLabel>
    
    QT_CHARTS_USE_NAMESPACE
    
    class barChart:public QGraphicsView
    {
        Q_OBJECT
    public:
    
    
        explicit barChart(QWidget *parent = nullptr);
        QChartView *charView(QString title, QList<qreal> list, QStringList toolTipList, QString chartTip);
        QVector<QBarSet*> m_barSetList;
        QStringList categories;
        QStringList topTipList;
        Callout* m_tooltip;
        QChart *chart;
    public  slots:
        void sltTooltip(bool status, int index, QBarSet *barset);
    
    //protected:
    //    bool eventFilter(QObject *watched, QEvent *event);
    };
    
    #endif // BARCHART_H
    
    

    barchart.h

    #include "barchart.h"
    #include <QDebug>
    
    
    barChart::barChart(QWidget *parent):QGraphicsView(new QGraphicsScene, parent)
    {
        chart = new QChart();
    }
    
    QChartView *barChart::charView(QString title,QList<qreal> list,QStringList toolTipList,QString interpretationOfData)
    {
        m_tooltip = new Callout(chart);
        m_tooltip->hide();
        topTipList=toolTipList;
        //![1]
            QBarSet *set0 = new QBarSet(interpretationOfData);
    //        QBarSet *set1 = new QBarSet("John");
                set0->append(list);
    //        *set1 << 5 << 0 << 0 << 4 << 0 << 7;
    
    //    m_barSetList<<set0<<set1<<set2<<set3<<set4;
         m_barSetList<<set0;
            QBarSeries *series = new QBarSeries();
            series->setLabelsPosition(QAbstractBarSeries::LabelsInsideEnd); // 设置数据系列标签的位置于数据柱内测上方
            series->setLabelsVisible(true); // 设置显示数据系列标签
            installEventFilter(series);
            connect(series, SIGNAL(hovered(bool, int, QBarSet*)), this, SLOT(sltTooltip(bool, int, QBarSet*)));
            series->append(set0);
    //        series->append(set1);
    
            chart->addSeries(series);
            chart->setTitle(title);
            chart->setTitleFont(QFont("song", 12, QFont::Bold));
            chart->setAnimationOptions(QChart::SeriesAnimations);
            for(int i=0;i<list.size();i++)
            {
                categories<<QString("检查项%1").arg(QString::number(i+1));
            }
            QBarCategoryAxis *axis = new QBarCategoryAxis();
            axis->append(categories);
            chart->createDefaultAxes();
            chart->setAxisX(axis, series);
    
            chart->legend()->setVisible(true);
            chart->legend()->setAlignment(Qt::AlignBottom);
    
            QChartView *chartView = new QChartView(chart);
            chartView->setRenderHint(QPainter::Antialiasing);
            return chartView;
    }
    
    void barChart::sltTooltip(bool status, int index, QBarSet *barset)
    {
        if (m_tooltip == 0)
            m_tooltip = new Callout(chart);
    
        if (status) {
            double indexbarset=m_barSetList.indexOf(barset)-1;//m_categories是x轴坐标
            indexbarset=indexbarset/6;
            m_tooltip->setText(topTipList.at(index));
            QPointF point(index+indexbarset, barset->at(index));
            m_tooltip->setAnchor(point);
            m_tooltip->setZValue(11);
            m_tooltip->updateGeometry();
            m_tooltip->show();
        }
        else {
            m_tooltip->hide();
    
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    //bool barChart::eventFilter(QObject *watched, QEvent *event)
    //{
    //    if(watched == this)
    //    {
    //        if(event->type() == QEvent::Enter)
    //        {
    //            qDebug()<<tr("监控到按钮事件,鼠标进入按钮事件");
    //            return true;
    //        }
    //        else if(event->type() == QEvent::Leave)
    //        {
    //            qDebug()<<tr("监控到按钮事件,鼠标离开按钮事件");
    //            return true;
    //        }
    //        else if(event->type() == QEvent::MouseButtonPress)
    //        {
    //            qDebug()<<tr("监控到按钮事件,鼠标摁下按钮事件");
    //            return true;
    //        }
    //        else if(event->type() == QEvent::MouseButtonRelease)
    //        {
    //            qDebug()<<tr("监控到按钮事件,鼠标释放按钮事件");
    //            return true;
    //        }
    //    }
    //    return QWidget::eventFilter(watched, event);
    //}
    
    

    callout.h

    /****************************************************************************
    **
    ** Copyright (C) 2016 The Qt Company Ltd.
    ** Contact: https://www.qt.io/licensing/
    **
    ** This file is part of the Qt Charts module of the Qt Toolkit.
    **
    ** $QT_BEGIN_LICENSE:GPL$
    ** Commercial License Usage
    ** Licensees holding valid commercial Qt licenses may use this file in
    ** accordance with the commercial license agreement provided with the
    ** Software or, alternatively, in accordance with the terms contained in
    ** a written agreement between you and The Qt Company. For licensing terms
    ** and conditions see https://www.qt.io/terms-conditions. For further
    ** information use the contact form at https://www.qt.io/contact-us.
    **
    ** GNU General Public License Usage
    ** Alternatively, this file may be used under the terms of the GNU
    ** General Public License version 3 or (at your option) any later version
    ** approved by the KDE Free Qt Foundation. The licenses are as published by
    ** the Free Software Foundation and appearing in the file LICENSE.GPL3
    ** included in the packaging of this file. Please review the following
    ** information to ensure the GNU General Public License requirements will
    ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
    **
    ** $QT_END_LICENSE$
    **
    ****************************************************************************/
    
    #ifndef CALLOUT_H
    #define CALLOUT_H
    
    #include <QtCharts/QChartGlobal>
    #include <QtWidgets/QGraphicsItem>
    #include <QtGui/QFont>
    
    QT_BEGIN_NAMESPACE
    class QGraphicsSceneMouseEvent;
    QT_END_NAMESPACE
    
    QT_CHARTS_BEGIN_NAMESPACE
    class QChart;
    QT_CHARTS_END_NAMESPACE
    
    QT_CHARTS_USE_NAMESPACE
    
    class Callout : public QGraphicsItem
    {
    public:
        Callout(QChart *parent);
    
        void setText(const QString &text);
        void setAnchor(QPointF point);
        void updateGeometry();
    
        QRectF boundingRect() const;
        void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);
    
    protected:
        void mousePressEvent(QGraphicsSceneMouseEvent *event);
        void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    
    private:
        QString m_text;
        QRectF m_textRect;
        QRectF m_rect;
        QPointF m_anchor;
        QFont m_font;
        QChart *m_chart;
    };
    
    #endif // CALLOUT_H
    

    callout.cpp

    
    /****************************************************************************
    **
    ** Copyright (C) 2016 The Qt Company Ltd.
    ** Contact: https://www.qt.io/licensing/
    **
    ** This file is part of the Qt Charts module of the Qt Toolkit.
    **
    ** $QT_BEGIN_LICENSE:GPL$
    ** Commercial License Usage
    ** Licensees holding valid commercial Qt licenses may use this file in
    ** accordance with the commercial license agreement provided with the
    ** Software or, alternatively, in accordance with the terms contained in
    ** a written agreement between you and The Qt Company. For licensing terms
    ** and conditions see https://www.qt.io/terms-conditions. For further
    ** information use the contact form at https://www.qt.io/contact-us.
    **
    ** GNU General Public License Usage
    ** Alternatively, this file may be used under the terms of the GNU
    ** General Public License version 3 or (at your option) any later version
    ** approved by the KDE Free Qt Foundation. The licenses are as published by
    ** the Free Software Foundation and appearing in the file LICENSE.GPL3
    ** included in the packaging of this file. Please review the following
    ** information to ensure the GNU General Public License requirements will
    ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
    **
    ** $QT_END_LICENSE$
    **
    ****************************************************************************/
    
    #include "callout.h"
    #include <QtGui/QPainter>
    #include <QtGui/QFontMetrics>
    #include <QtWidgets/QGraphicsSceneMouseEvent>
    #include <QtGui/QMouseEvent>
    #include <QtCharts/QChart>
    #include <QDebug>
    #include <QGraphicsItem>
    Callout::Callout(QChart *chart):
        QGraphicsItem(chart),
        m_chart(chart)
    {
    }
    
    QRectF Callout::boundingRect() const
    {
        QPointF anchor = mapFromParent(m_chart->mapToPosition(m_anchor));
        QRectF rect;
        rect.setLeft(qMin(m_rect.left(), anchor.x()));
        rect.setRight(qMax(m_rect.right(), anchor.x()));
        rect.setTop(qMin(m_rect.top(), anchor.y()));
        rect.setBottom(qMax(m_rect.bottom(), anchor.y()));
        return rect;
    }
    
    void Callout::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        Q_UNUSED(option)
        Q_UNUSED(widget)
        QPainterPath path;
        path.addRoundedRect(m_rect, 5, 5);
    
        QPointF anchor = mapFromParent(m_chart->mapToPosition(m_anchor));
        if (!m_rect.contains(anchor)) {
            QPointF point1, point2;
    
            // establish the position of the anchor point in relation to m_rect
            bool above = anchor.y() <= m_rect.top();
            bool aboveCenter = anchor.y() > m_rect.top() && anchor.y() <= m_rect.center().y();
            bool belowCenter = anchor.y() > m_rect.center().y() && anchor.y() <= m_rect.bottom();
            bool below = anchor.y() > m_rect.bottom();
    
            bool onLeft = anchor.x() <= m_rect.left();
            bool leftOfCenter = anchor.x() > m_rect.left() && anchor.x() <= m_rect.center().x();
            bool rightOfCenter = anchor.x() > m_rect.center().x() && anchor.x() <= m_rect.right();
            bool onRight = anchor.x() > m_rect.right();
    
            // get the nearest m_rect corner.
            qreal x = (onRight + rightOfCenter) * m_rect.width();
            qreal y = (below + belowCenter) * m_rect.height();
            bool cornerCase = (above && onLeft) || (above && onRight) || (below && onLeft) || (below && onRight);
            bool vertical = qAbs(anchor.x() - x) > qAbs(anchor.y() - y);
    
            qreal x1 = x + leftOfCenter * 10 - rightOfCenter * 20 + cornerCase * !vertical * (onLeft * 10 - onRight * 20);
            qreal y1 = y + aboveCenter * 10 - belowCenter * 20 + cornerCase * vertical * (above * 10 - below * 20);;
            point1.setX(x1);
            point1.setY(y1);
    
            qreal x2 = x + leftOfCenter * 20 - rightOfCenter * 10 + cornerCase * !vertical * (onLeft * 20 - onRight * 10);;
            qreal y2 = y + aboveCenter * 20 - belowCenter * 10 + cornerCase * vertical * (above * 20 - below * 10);;
            point2.setX(x2);
            point2.setY(y2);
    
            path.moveTo(point1);
            path.lineTo(anchor);
            path.lineTo(point2);
            path = path.simplified();
        }
        painter->setBrush(QColor(255, 255, 255));
        painter->drawPath(path);
        painter->drawText(m_textRect, m_text);
    }
    
    void Callout::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
        event->setAccepted(true);
    }
    
    void Callout::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    {
        if (event->buttons() & Qt::LeftButton){
            setPos(mapToParent(event->pos() - event->buttonDownPos(Qt::LeftButton)));
            event->setAccepted(true);
        } else {
            event->setAccepted(false);
        }
    }
    
    void Callout::setText(const QString &text)
    {
    //    qDebug()<<text;
        m_text = text;
        QFontMetrics metrics(QFont("song",10));
        m_textRect = metrics.boundingRect(QRect(0, 0, 400, text.size()*4), Qt::AlignLeft, QString("tooltip"));
        m_textRect.translate(5, 5);
        m_textRect.setWidth(50);
        m_textRect.setHeight(text.size()*4);
        prepareGeometryChange();
        m_rect = m_textRect.adjusted(-5, -5, 5, 5);
    }
    
    void Callout::setAnchor(QPointF point)
    {
    //    qDebug()<<point;
        m_anchor = point;
    }
    
    void Callout::updateGeometry()
    {
        prepareGeometryChange();
        setPos(m_chart->mapToPosition(m_anchor) + QPoint(10, -50));
    }
    
    展开全文
  • qt柱状图控件设计

    2021-03-23 14:58:28
    https://blog.csdn.net/weixin_42887343/article/details/109303972 qt之led(点阵)控件类设计:https://blog.csdn.net/weixin_42887343/article/details/115348953 1、效果满上 2、设计思路 继承QWidget控件类,重写...

    其他推荐内容:

    1、效果满上
    在这里插入图片描述
    2、设计思路

    继承QWidget控件类,重写paintEvent绘制事件函数,按照设置的数值绘制柱状图。

    3、控件代码

    h文件

    #ifndef BarCharWidget_H
    #define BarCharWidget_H
    
    #include <QWidget>
    #include <QPainter>
    
    class BarCharWidget : public QWidget
    {
        Q_OBJECT
    public:
        explicit BarCharWidget(QWidget *parent = nullptr);
    
        void setValue(float value);
        float getValue();
        void setText(const QString &text);
    signals:
    public slots:
    private:
        float mValue = 0;	
        QString mText;
        void paintEvent(QPaintEvent *paintEvent) override;
    };
    #endif // BarCharWidget_H
    

    cpp文件

    #include "barcharwidget.h"
    
    BarCharWidget::BarCharWidget(QWidget *parent) : QWidget(parent)
    {
    }
    void BarCharWidget::setValue(float value)
    {
        mValue = value;
        this->update();
    }
    
    float BarCharWidget::getValue()
    {
        return mValue;
        this->update();
    }
    
    void BarCharWidget::setText(const QString &text)
    {
        mText = text;
    }
    
    void BarCharWidget::paintEvent(QPaintEvent *paintEvent)
    {
        Q_UNUSED(paintEvent);
        QPainter painter(this);
    
        // 进度条最大高度
        float sliderMaxValue = this->height() * 0.9;
    
        //进度条高度
        float sliderHigh  = -this->height() * 0.8;
    
        //进度条宽度
        const int sliderWidth = 60;
    
        // 将坐标系的该点变成原点坐标
        painter.translate(this->width() * 0.3, sliderMaxValue);
    
        // 画进度值
        painter.setBrush(Qt::black);
        painter.drawText(sliderWidth - 40, sliderHigh - 10, QString::number(mValue,'f',2));
        painter.drawText(5, 30, mText);
    
        // 设置画笔为红色
        QPen pen(Qt::white, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
        painter.setPen(pen);
    
        // 画上面直线
        painter.drawLine(0, sliderHigh, sliderWidth, sliderHigh);
        // 画下面直线
        painter.drawLine(0, 0, sliderWidth, 0);
        // 画左边直线
        painter.drawLine(0, 0, 0, sliderHigh);
        // 画右边直线
        painter.drawLine(sliderWidth, 0, sliderWidth, sliderHigh);
        // 画上面直线线
        painter.drawLine(0, sliderHigh, sliderWidth, sliderHigh);
    
        //将矩形填充为白色
        QPolygon polyGon;
        polyGon<<QPoint(sliderWidth, 0);
        polyGon<<QPoint(sliderWidth, sliderHigh);
        polyGon<<QPoint(0, sliderHigh);
        polyGon<<QPoint(0, 0);
        painter.setBrush(Qt::white);
        painter.drawConvexPolygon(polyGon);
    
        //设置渐变
        QLinearGradient linerGradient = QLinearGradient(0, 0, 0, sliderHigh);
        linerGradient.setColorAt(0.0, Qt::cyan);
        linerGradient.setColorAt(0.3, Qt::green);
        linerGradient.setColorAt(0.7, Qt::yellow);
        linerGradient.setColorAt(1.0, Qt::red);
    
        QBrush brush(linerGradient);
        painter.setBrush(brush);
    
        painter.drawRect(QRectF(0, 0, sliderWidth, sliderHigh * mValue / 100));
        painter.end();
    }
    

    4、使用方法
    使用界面提升的方式添加该柱状图控件。
    demo核心代码如下(mainwindows构造函数中添加):

        ui->widget->setText("柱状图1");
        ui->widget->setValue(0);
        
        ui->widget_2->setText("柱状图2");
        ui->widget_2->setValue(25);
        
        mTime.start(10);
        connect(&mTime,QTimer::timeout,this,[=](){
            if(ui->widget_2->getValue() >= 99.9)
            {
                mTime.stop();
                return 0;
            }
            ui->widget_2->setValue(ui->widget_2->getValue()+0.1);
            ui->widget->setValue(ui->widget->getValue()+0.1);
        });
    
    展开全文
  • Qt绘制柱状图

    2020-09-28 04:00:15
    目前它能够很好地显示柱状图。 这个柱状图支持任意多的项目(柱子),只需要在代码中添加了相关数据后,使用Paint()函数就可以将其渲染成一个Pixmap,然后作为中央控件(centeral widget)的一张图片显示出来。
  • QT的官方柱状图,如下图 QtCharts画图表必须要的两样东西QChartView,QChart相当于画布画笔,就不多说了。无论画什么图主要差别在于画笔里面的东西。 然后需要一根一根的柱子QBarSet,再把一根一根的柱子加到柱子的...
    **
     series->setLabelsVisible(true);//设置数据是否可见
    **
    

    QT的官方柱状图,如下图
    在这里插入图片描述
    QtCharts画图表必须要的两样东西QChartView,QChart相当于画布画笔,就不多说了。无论画什么图主要差别在于画笔里面的东西。
    然后需要一根一根的柱子QBarSet,再把一根一根的柱子加到柱子的列当中QBarSeries,那么柱状图机画好了,可是没有x、y轴啊,哈哈哈,所有还需要为QChart和QBarSeries设置x、y轴。那么就完成了。具体实现如下:

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
    //![1]设置数据,相当于多少根柱子,每根的高度,
        QBarSet *set0 = new QBarSet("Jane");
        QBarSet *set1 = new QBarSet("John");
        QBarSet *set2 = new QBarSet("Axel");
        QBarSet *set3 = new QBarSet("Mary");
        QBarSet *set4 = new QBarSet("Samantha");
    
        *set0 << 1 << 2 << 3 << 4 << 5 << 6;
        *set1 << 5 << 0 << 0 << 4 << 0 << 7;
        *set2 << 3 << 5 << 8 << 13 << 8 << 5;
        *set3 << 5 << 6 << 7 << 3 << 4 << 5;
        *set4 << 9 << 7 << 5 << 3 << 1 << 2;
    //![1]
    
    //![2]把数据加载到数据列表中
        QBarSeries *series = new QBarSeries();
        series->append(set0);
        series->append(set1);
        series->append(set2);
        series->append(set3);
        series->append(set4);
    
    //![2]
    
    //![3]初始化画笔
        QChart *chart = new QChart();
        chart->addSeries(series);
        chart->setTitle("Simple barchart example");
        chart->setAnimationOptions(QChart::SeriesAnimations);
    //![3]
    
    //![4]为QChart和QBarSeries设置坐标轴
        QStringList categories;
        categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
        QBarCategoryAxis *axisX = new QBarCategoryAxis();//初始化X轴
        axisX->append(categories);
        chart->addAxis(axisX, Qt::AlignBottom);
        series->attachAxis(axisX);
    
        QValueAxis *axisY = new QValueAxis();//初始化Y轴
        axisY->setRange(0,15);
        chart->addAxis(axisY, Qt::AlignLeft);
        series->attachAxis(axisY);
    //![4]
    
    //![5]设置图标的标题属性
        chart->legend()->setVisible(true);
        chart->legend()->setAlignment(Qt::AlignBottom);
    //![5]
    
    //![6]设置画布
        QChartView *chartView = new QChartView(chart);
        chartView->setRenderHint(QPainter::Antialiasing);
    //![6]
    
    //![7]
        QMainWindow window;
        window.setCentralWidget(chartView);
        window.resize(420, 300);
        window.show();
    //![7]
    
        return a.exec();
    }
    

    百分比柱状图,如下
    在这里插入图片描述做法跟上面的柱状图差不多,就是把数据序列QBarSeries 换成QPercentBarSeries而已,其它完全一致

    横向百分比柱状图,如下
    在这里插入图片描述做法跟上面的柱状图差不多,就是把数据序列QBarSeries 换成QHorizontalPercentBarSeries而已,其它完全一致

    展开全文
  • qt柱状图

    千次阅读 2019-01-06 14:53:05
    qt画了个很简单的柱状图,横坐标长度不变,个数越多,每个柱状图的宽度越窄。没什么难度,写过的就记录一下,以后可能用到类似的直接拿来用。 头文件 #ifndef MAINWINDOW_H #define MAINWINDOW_H #include &...
  • QT柱状图.rar

    2019-12-20 17:47:29
    使用QT的QPainter画一个柱状图,直观的显示各个比例大小并显示比例大小。另外下边显示各个柱状对应的名称。
  • 这个程序包实现了使用QT、Qwt进行柱状图的呈现,同时可以使用鼠标拖动和显示柱状图的纵坐标
  • Qt4中学习使用QtCharts绘图四:绘制柱状图
  • qt多项柱状图案例代码
  • QtChart柱状图动态加载数据

    千次阅读 2019-11-13 22:03:04
    QtChart是qt自带绘制图表的模块。其用法的一些关键点...官方文档只有数据写死的demo,这里记录一下柱状图加载动态数据的实现方式。 先上代码 import QtQuick 2.0 import QtCharts 2.3 Item { ChartView { ...
  • QT 用qcustomplot画图(柱状图为例)实现柱状图上显示value 我在网上找了好多demo但是在customplot中是没有直接的方法显示value。 如果想要在柱状图上显示每个value则需要自己在draw()中自己添加 painter-&...
  • Qt 饼图和柱状图控件

    热门讨论 2012-06-12 21:11:54
    Qt 饼图和柱状图第三方控件,简单易用。
  • 2.折线图和柱状图共轴 3.柱状图实现每条颜色单独控制 4.柱状图和折线图共x轴不,共y轴 //为防止内存泄露用全局变量 QBarSeries *m_lsHistogram = NULL; QLineSeries *m_lsMainSeries = NULL; QLineSeries *m...
  • qt下用ChartDirector控件开发的饼图、柱状图、折线图实例程序,并可以设置截图目录,进行截图;“ChartDirector”文件夹下为头文件和lib文件
  • qt练手小项目的源码。绘制动态柱状图柱状图能够根据数据变化实时上升或下降。支持设置刻度值范围、背景色和柱状图颜色。qt版本:4.8.6;IDE:vs2008
  • qt例程-折线图-柱状图ChartView.7z
  • Qt实现柱状图、饼状图、折线图、曲线图

    千次阅读 多人点赞 2020-12-14 10:33:51
    前言 未经允许,禁止转载 项目源码: 传送门 1.介绍 Qt图标(Qt Charts)出现晚于QWT、...柱状图:可设置标题栏,X轴标题, Y轴标题,柱形颜色,轴刻度,是否显示提示信息(柱形颜色含义)等 饼状图:可设置空
  • Qt中创建柱状图

    千次阅读 2020-05-22 21:53:47
    Qt中创建柱状图 1.必要设置 .pro文件 QT += charts 头文件 #include <QtCharts> QT_CHARTS_USE_NAMESPACE namespace Ui { class MainWindow; } 2.设置数据 存储数据到QTableView中 void MainWindow::iniData...
  • Qt编写曲线图柱状图源码 1:可设置X轴Y轴范围值 2:可设置背景颜色+文本颜色+网格颜色 3:可设置三条曲线颜色+颜色集合 4:可设置是否显示定位十字线,可分别设置横向和纵向 5:可设置十字线的宽度和颜色 6:可设置是否...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,488
精华内容 595
关键字:

qt柱状图