File indexing completed on 2024-05-12 05:09:21
0001 /*************************************************************************** 0002 Copyright (C) 2021 Robby Stephenson <robby@periapsis.org> 0003 ***************************************************************************/ 0004 0005 /*************************************************************************** 0006 * * 0007 * This program is free software; you can redistribute it and/or * 0008 * modify it under the terms of the GNU General Public License as * 0009 * published by the Free Software Foundation; either version 2 of * 0010 * the License or (at your option) version 3 or any later version * 0011 * accepted by the membership of KDE e.V. (or its successor approved * 0012 * by the membership of KDE e.V.), which shall act as a proxy * 0013 * defined in Section 14 of version 3 of the license. * 0014 * * 0015 * This program is distributed in the hope that it will be useful, * 0016 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0018 * GNU General Public License for more details. * 0019 * * 0020 * You should have received a copy of the GNU General Public License * 0021 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 0022 * * 0023 ***************************************************************************/ 0024 0025 #include "barchart.h" 0026 #include "../tellico_debug.h" 0027 0028 #include <KLocalizedString> 0029 0030 #include <QBarSet> 0031 #include <QHorizontalBarSeries> 0032 #include <QHorizontalStackedBarSeries> 0033 #include <QBarCategoryAxis> 0034 #include <QValueAxis> 0035 #include <QCategoryAxis> 0036 #include <QGraphicsLayout> 0037 0038 using namespace QtCharts; 0039 using Tellico::BarChart; 0040 0041 BarChart::BarChart(const QStringList& names_, const QList<qreal>& values_) : QChart() { 0042 QFont f = titleFont(); 0043 int fontSize = f.pointSize(); 0044 if(fontSize > 0) { 0045 f.setPointSize(qRound(fontSize * 1.3)); 0046 } else { 0047 f.setPixelSize(qRound(f.pixelSize() * 1.3)); 0048 } 0049 f.setWeight(QFont::Bold); 0050 setTitleFont(f); 0051 0052 // horizontal bar charts 0053 auto series = new QHorizontalStackedBarSeries(); 0054 QList<qreal> dummyList; 0055 dummyList.reserve(names_.count()); 0056 for(int i = 0; i < names_.count(); ++i) { 0057 auto barSet = new QBarSet(names_.at(i)); 0058 // to "trick" the bar chart into different colors, each new group must be in a different value position 0059 // so replace the previous value with 0 and insert the next one 0060 if(i > 0) dummyList.replace(i-1, 0); 0061 dummyList << values_.at(i); 0062 barSet->append(dummyList); 0063 // add the bar set and series to the chart 0064 series->append(barSet); 0065 } 0066 series->setLabelsVisible(true); 0067 series->setLabelsFormat(QStringLiteral("(@value) ")); 0068 series->setLabelsPosition(QAbstractBarSeries::LabelsInsideEnd); 0069 series->setBarWidth(0.8); 0070 addSeries(series); 0071 0072 auto axisX = new QValueAxis(); 0073 axisX->setLabelFormat(QStringLiteral("%d")); 0074 addAxis(axisX, Qt::AlignBottom); 0075 series->attachAxis(axisX); 0076 if(axisX->max() < 5) axisX->setMax(5); 0077 axisX->applyNiceNumbers(); 0078 0079 auto axisY = new QBarCategoryAxis(); 0080 axisY->append(names_); 0081 addAxis(axisY, Qt::AlignLeft); 0082 series->attachAxis(axisY); 0083 axisY->setGridLineVisible(false); 0084 0085 layout()->setContentsMargins(0, 20, 0, 20); // default is 20, 20, 20, 20 0086 legend()->hide(); 0087 setBackgroundRoundness(0); 0088 }