精华内容
下载资源
问答
  • 自定义封装Qt柱状图类,提供简单数据更新等接口实现柱状图对象的创建、销毁、数据更新和清空等功能,实现数据动态更新和柱状图上鼠标悬停数值显示
  • QT 用qcustomplot画图(柱状图为例)实现柱状图显示value 我在网上找了好多demo但是在customplot中是没有直接的方法显示value。 如果想要在柱状图显示每个value则需要自己在draw()中自己添加 painter-&...

    QT 用qcustomplot画图(柱状图为例)实现柱状图上显示value
    在这里插入图片描述
    我在网上找了好多demo但是在customplot中是没有直接的方法显示value。
    如果想要在柱状图上显示每个value则需要自己在draw()中自己添加
    painter->drawText(x, y);x柱状图的横坐标,y是柱状图的纵坐标(value)。有需要的可以参考下。

    展开全文
  • QtChart是qt自带绘制图表的模块。其用法的一些关键点...官方文档只有数据写死的demo,这里记录一下柱状图加载动态数据的实现方式。 先上代码 import QtQuick 2.0 import QtCharts 2.3 Item { ChartView { ...

    QtChart是qt自带绘制图表的模块。其用法的一些关键点比如安装的时候需要选择QtChart的模块,main函数需要使用QApplication等,很多博客都有说明。官方文档只有数据写死的demo,这里记录一下柱状图加载动态数据的实现方式。

    先上代码

    import QtQuick 2.0
    import QtCharts 2.3
    
    Item {
    
        ChartView {
            id: chart
            antialiasing: true
            anchors.fill: parent
    
            //坐标轴
            ValueAxis
            {
                id: axisX
                titleText: "Led [num]"
            }
    
            ValueAxis
            {
                id: axisY
                min: 0
                max: 3.3
                tickCount: 5
            }
    
            //柱状图
            BarSeries
            {
                id: barSeriesX
                axisX: axisX    //x轴
                axisY: axisY    //y轴
    
                //数据展示
                BarSet
                {
                   id: barSetIdX
                }
            }
        }
    
        //设定数据的标签
        function setLabelText (labelText) {
            barSetIdX.label = labelText
        }
    
        //设定y轴的标题
        function setYTitle (text) {
            axisY.titleText = text
        }
    
        //数据更新
        function updateData (data) {
            barSetIdX.values = data
            axisX.max = data.length
        }
    
    
    
    }
    

    主要是使用函数给外部提供接口进行动态更新坐标轴名称、标签、数据等,数据更新就是给BarSet的values传一个数组即可,使用很方便。

    下面看调用的地方:

     Histogram {
            id: histogram
            width: 400
            height: 300
            x: 500
            y: 50
        }
    
    
        Connections {
            target: DataManager
            onSigStart: {
                var data = DataManager.getData();
                histogram.updateData(data);
                histogram.setLabelText("ADC");
                histogram.setYTitle("Y")
            }
    
        }

    这里的DataManager是c++创建的类,数据是c++那边传过来的,然后执行相关的函数即可。

    看运行的效果:

    这里提一下QtChart的打包,如果用的是qml,用windeployqt.exe工具打包出来的时候,QtChart相关的库并没有,需要手动添加,并且需要添加两部分的库,一个是QML的

    还有一个是在bin目录下的

    整个demo的下载地址为点击这里

    展开全文
  • 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绘制柱状图

    2020-09-28 04:00:15
    最近复习已经学习的Qt知识,制作了... 这个柱状图支持任意多的项目(柱子),只需要在代码中添加了相关数据后,使用Paint()函数就可以将其渲染成一个Pixmap,然后作为中央控件(centeral widget)的一张图片显示出来。
  • 这个程序包实现了使用QT、Qwt进行柱状图的呈现,同时可以使用鼠标拖动和显示柱状图的纵坐标
  • 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三维条形图显示数据源码,三D柱状图
  • QT窗口内显示柱状图

    2020-04-14 11:38:11
    .pro .cpp #include "homewindow.h" #include "ui_homewindow.h" #include<QTimer> #include <QtWidgets/QApplication> #include <QtWidgets/QMainWindow>...QtCharts/QCha...
  • QCustomplot绘制柱状图显示数据 结果图: 如图所示,当鼠标移动到柱形长方块,会显示对应的纵坐标值。 具体实现代码: //柱状图绘制(时间对比柱状图) void Timetable::timeHistogram_make() { ui->plot_time-...
  • } 下面是创建柱状图的类. QChart* className::createBarChart() const { QChart *chart = new QChart(); chart->setTitle("XXXX分析图"); chart->layout()->setContentsMargins(0, 0, 0, 0);//设置外边界全部为0 //...
  • 使用Qt中Charts模块显示柱状图

    千次阅读 2019-12-18 22:54:21
    对于数据可视化模块QT有自己的一套模块名字叫做Qtcharts,在项目模块中添加charts模块即可,QTcreator和VS中均可适用,如果你的模块中找不到这个charts模块的话,很有可能是在安装QT的时候没有勾选charts模块,现在的...
  • Qt编写曲线图柱状图源码 1:可设置X轴Y轴范围值 2:可设置背景颜色+文本颜色+网格颜色 3:可设置三条曲线颜色+颜色集合 4:可设置是否显示定位十字线,可分别设置横向和纵向 5:可设置十字线的宽度和颜色 6:可设置是否...
  • 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 charts 动态柱状图

    2017-11-14 08:47:16
    给定数据,但是数据是在由少变多的,比如从1个到40个,需要画出柱状图,当只有一个 或者不足40个时,也会画出图形,但是要求图形有一个从右到左的过程,就是有一个数据 时,最右边画一个,有两个是,最开始的一个...
  • 前言 未经允许,禁止转载 项目源码: 传送门 1.介绍 Qt图标(Qt Charts)出现晚于QWT、...柱状图:可设置标题栏,X轴标题, Y轴标题,柱形颜色,轴刻度,是否显示提示信息(柱形颜色含义)等 饼状图:可设置空
  • 【Y忍冬草】Qt学习之柱状图

    千次阅读 2018-01-03 21:36:34
    柱状图(bar chart),是一种以长方形的长度为变量的表达图形的统计报告图,由一系列高度不等的纵向条纹表示数据分布的情况,用来比较两个或以上的价值(不同时间或者不同条件),只有一个变量,通常利用于较小的数据...
  • echarts 柱状图 x轴多维度显示不同数据源 echarts 柱状图 x轴数据某个值为空时就会出现柱状图显示并且占位现象,如上图,那怎么解决呢? 想了两天终于想到了办法,那就是添加多个X轴的数据就可以,在这记录一下,...
  • 【android studio的一个module】这两个统计的入参只需要ArrayList就行了(自定义Pillar类中只有String date用于显示时间;double value用于显示数值)
  • Qt—QChart动态绘图(折现图、曲线图、柱状图、饼图)工厂 V 1.0前提目的类图Demo基类说明成员参数成员函数基类的实现MyChart.hMyChart.cpp派生类柱状图MyQChartBar.hMyQChartBar.cpp饼状图MyQChartPie.hMyQChartPie...
  • count.plot(kind='bar') plt.title('Category Count') plt.tight_layout() plt.savefig(os.path.join(output_path,'category_count.png')) plt.show() #以直方方式显示 categories_in_sum.plot(kind='bar') plt....

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 926
精华内容 370
关键字:

qt柱状图显示数据