目录
1.简单例子
2. 稍微复杂点
QPieSeries Class:饼状图数据
QChart | 管理图表系列、图例和轴的图形表示 |
QChartView | 可以显示图表的独立小部件 |
QPieSeries | 在饼图中显示数据 |
QPieSlice | 表示饼图系列中的单个切片(其实就是标签) |
1.简单例子
//![1]
QPieSeries *series = new QPieSeries();
series->append("Jane", 1);
series->append("Joe", 2);
series->append("Andy", 3);
series->append("Barbara", 4);
series->append("Axel", 5);
//![1]
//![2]
QPieSlice *slice = series->slices().at(1);
slice->setExploded();
slice->setLabelVisible();
slice->setPen(QPen(Qt::darkGreen, 2));
slice->setBrush(Qt::green);
//![2]
//![3]
QChart *chart = new QChart();
chart->addSeries(series);
chart->setTitle("Simple piechart example");
chart->legend()->hide();
//![3]
//![4]
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
//![4]
2. 稍微复杂点
tip:这里面有两个地方没看懂(我标红),有看懂的的留言解释一下
drilldownchart.h/.cpp
//.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 DRILLDOWNCHART_H
#define DRILLDOWNCHART_H
#include <QtCharts/QChart>
QT_CHARTS_BEGIN_NAMESPACE
class QAbstractSeries;
class QPieSlice;
QT_CHARTS_END_NAMESPACE
QT_CHARTS_USE_NAMESPACE
class DrilldownChart : public QChart
{
Q_OBJECT
public:
explicit DrilldownChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
~DrilldownChart();
void changeSeries(QAbstractSeries *series);
public Q_SLOTS:
void handleSliceClicked(QPieSlice *slice);
private:
QAbstractSeries *m_currentSeries;
};
#endif // DRILLDOWNCHART_H
// .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 "drilldownchart.h"
#include "drilldownslice.h"
QT_CHARTS_USE_NAMESPACE
DrilldownChart::DrilldownChart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
: QChart(QChart::ChartTypeCartesian, parent, wFlags),
m_currentSeries(0)
{
}
DrilldownChart::~DrilldownChart()
{
}
void DrilldownChart::changeSeries(QAbstractSeries *series)
{
// NOTE: if the series is owned by the chart it will be deleted
// here the "window" owns the series...
if (m_currentSeries)
removeSeries(m_currentSeries);
m_currentSeries = series;
addSeries(series);
setTitle(series->name());
}
void DrilldownChart::handleSliceClicked(QPieSlice *slice)
{
DrilldownSlice *drilldownSlice = static_cast<DrilldownSlice *>(slice);
changeSeries(drilldownSlice->drilldownSeries());
}
#include "moc_drilldownchart.cpp"
drilldownslice.h/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$
**
****************************************************************************/
#ifndef DRILLDOWNSLICE_H
#define DRILLDOWNSLICE_H
#include <QtCharts/QPieSlice>
QT_CHARTS_BEGIN_NAMESPACE
class QAbstractSeries;
QT_CHARTS_END_NAMESPACE
QT_CHARTS_USE_NAMESPACE
class DrilldownSlice : public QPieSlice
{
Q_OBJECT
public:
DrilldownSlice(qreal value, QString prefix, QAbstractSeries *drilldownSeries);
virtual ~DrilldownSlice();
QAbstractSeries *drilldownSeries() const;
public Q_SLOTS:
void updateLabel();
void showHighlight(bool show);
private:
QAbstractSeries *m_drilldownSeries;
QString m_prefix;
};
#endif // DRILLDOWNSLICE_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$
**
****************************************************************************/
#include "drilldownslice.h"
QT_CHARTS_USE_NAMESPACE
DrilldownSlice::DrilldownSlice(qreal value, QString prefix, QAbstractSeries *drilldownSeries)
: m_drilldownSeries(drilldownSeries),
m_prefix(prefix)
{
setValue(value);
updateLabel();
setLabelFont(QFont("Arial", 8));
connect(this, &DrilldownSlice::percentageChanged, this, &DrilldownSlice::updateLabel);
connect(this, &DrilldownSlice::hovered, this, &DrilldownSlice::showHighlight);
}
DrilldownSlice::~DrilldownSlice()
{
}
QAbstractSeries *DrilldownSlice::drilldownSeries() const
{
return m_drilldownSeries;
}
void DrilldownSlice::updateLabel()
{
QString label = m_prefix;
label += " $";
label += QString::number(this->value());
label += ", ";
label += QString::number(this->percentage() * 100, 'f', 1);
label += "%";
setLabel(label);
}
void DrilldownSlice::showHighlight(bool show)
{
setLabelVisible(show);
setExploded(show);
}
#include "moc_drilldownslice.cpp"
main.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 "drilldownchart.h"
#include "drilldownslice.h"
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCore/QRandomGenerator>
#include <QtCharts/QChartView>
#include <QtCharts/QLegend>
#include <QtCharts/QPieSeries>
QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow window;
DrilldownChart *chart = new DrilldownChart();
chart->setTheme(QChart::ChartThemeLight);
chart->setAnimationOptions(QChart::AllAnimations);
chart->legend()->setVisible(true);
chart->legend()->setAlignment(Qt::AlignRight);
QPieSeries *yearSeries = new QPieSeries(&window);
yearSeries->setName("Sales by year - All");
const QStringList months = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
const QStringList names = {
"Jane", "John", "Axel", "Mary", "Susan", "Bob"
};
for (const QString &name : names) {
QPieSeries *series = new QPieSeries(&window);
series->setName("Sales by month - " + name);
//QRandomGenerator::global()->bounded(2)
for (const QString &month : months)
*series << new DrilldownSlice(QRandomGenerator::global()->bounded(1000), month, yearSeries);
QObject::connect(series, &QPieSeries::clicked, chart, &DrilldownChart::handleSliceClicked);
*yearSeries << new DrilldownSlice(series->sum(), name, series);
}
QObject::connect(yearSeries, &QPieSeries::clicked, chart, &DrilldownChart::handleSliceClicked);
chart->changeSeries(yearSeries);
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
window.setCentralWidget(chartView);
window.resize(800, 500);
window.show();
return a.exec();
}
tip:上面代码QObject::connect按道理不应该这样写,是按照下面这样去弄,没看懂上面这样写是什么意思
for (const QString &name : names) {
QPieSeries *series = new QPieSeries(&window);
series->setName("Sales by month - " + name);
//QRandomGenerator::global()->bounded(2)
for (const QString &month : months)
{
*series << new DrilldownSlice(QRandomGenerator::global()->bounded(1000), month, yearSeries);
QObject::connect(series, &QPieSeries::clicked, chart, &DrilldownChart::handleSliceClicked);
}
*yearSeries << new DrilldownSlice(series->sum(), name, series);
QObject::connect(yearSeries, &QPieSeries::clicked, chart, &DrilldownChart::handleSliceClicked);
}