File indexing completed on 2024-05-12 15:54:30
0001 /** 0002 * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved. 0003 * 0004 * This file is part of the KD Chart library. 0005 * 0006 * This program is free software; you can redistribute it and/or 0007 * modify it under the terms of the GNU General Public License as 0008 * published by the Free Software Foundation; either version 2 of 0009 * the License, or (at your option) any later version. 0010 * 0011 * This program is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 * GNU General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU General Public License 0017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 0018 */ 0019 0020 #include <QStandardItemModel> 0021 #include <QPushButton> 0022 #include <QApplication> 0023 #include <KChartChart> 0024 #include <KChartBarDiagram> 0025 #include <KChartCartesianAxis> 0026 #include <KChartCartesianCoordinatePlane> 0027 #include <KChartLegend> 0028 0029 class ChartWidget : public QWidget { 0030 Q_OBJECT 0031 public: 0032 explicit ChartWidget(QWidget* parent = nullptr) 0033 : QWidget(parent) 0034 { 0035 m_model.insertRows( 0, 0, QModelIndex() ); 0036 KChart::BarDiagram* diagram = new KChart::BarDiagram; 0037 diagram->setModel(&m_model); 0038 0039 KChart::Legend* legend = new KChart::Legend(diagram,diagram); 0040 m_chart.addLegend(legend); 0041 0042 // The code below doesn't work, but it would 0043 // be nice if it did: 0044 #if 0 0045 KChart::Legend* legend = new KChart::Legend; 0046 legend->addDiagram(diagram1); 0047 legend->addDiagram(diagram2); 0048 ... 0049 m_chart.addLegend(legend); 0050 #endif 0051 0052 KChart::CartesianAxis* abcissa = new KChart::CartesianAxis(diagram); 0053 abcissa->setPosition( KChart::CartesianAxis::Bottom ); 0054 KChart::CartesianAxis* ordinate = new KChart::CartesianAxis(diagram); 0055 ordinate->setPosition( KChart::CartesianAxis::Left ); 0056 diagram->addAxis(abcissa); 0057 diagram->addAxis(ordinate); 0058 0059 // NOTE: If this is done before adding axes, 0060 // the axes don't show up at all 0061 m_chart.coordinatePlane()->replaceDiagram(diagram); 0062 0063 m_rowbutton.setText( tr("Add rows") ); 0064 m_colbutton.setText( tr("Add columns") ); 0065 connect( &m_rowbutton, SIGNAL(clicked()), 0066 this, SLOT(addRows())); 0067 connect( &m_colbutton, SIGNAL(clicked()), 0068 this, SLOT(addCols())); 0069 0070 QVBoxLayout* l = new QVBoxLayout(this); 0071 l->addWidget(&m_chart); 0072 l->addWidget(&m_rowbutton); 0073 l->addWidget(&m_colbutton); 0074 0075 setLayout(l); 0076 } 0077 0078 private Q_SLOTS: 0079 0080 void addRows() 0081 { 0082 m_model.insertRows(m_model.rowCount(),1); 0083 for ( int i = 0; i < m_model.columnCount(); ++i ) { 0084 m_model.setData( m_model.index(m_model.rowCount()-1,i), i ); 0085 } 0086 } 0087 void addCols() 0088 { 0089 m_model.insertColumns(m_model.columnCount(),1); 0090 for ( int i = 0; i < m_model.rowCount(); ++i ) { 0091 m_model.setData( m_model.index(i,m_model.columnCount()-1), i ); 0092 } 0093 } 0094 0095 private: 0096 KChart::Chart m_chart; 0097 QPushButton m_rowbutton; 0098 QPushButton m_colbutton; 0099 QStandardItemModel m_model; 0100 }; 0101 0102 int main( int argc, char** argv ) { 0103 QApplication app( argc, argv ); 0104 0105 ChartWidget w; 0106 w.show(); 0107 0108 return app.exec(); 0109 } 0110 0111 #include "main.moc"