File indexing completed on 2024-05-12 04:20:45

0001 /**
0002  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
0003  *
0004  * This file is part of the KD Chart library.
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include <QStandardItemModel>
0010 #include <QPushButton>
0011 #include <QApplication>
0012 #include <KChartChart>
0013 #include <KChartBarDiagram>
0014 #include <KChartCartesianAxis>
0015 #include <KChartCartesianCoordinatePlane>
0016 #include <KChartLegend>
0017 
0018 class ChartWidget : public QWidget {
0019   Q_OBJECT
0020 public:
0021   explicit ChartWidget(QWidget* parent = nullptr)
0022     : QWidget(parent)
0023   {
0024     m_model.insertRows( 0, 0, QModelIndex() );
0025     KChart::BarDiagram* diagram = new KChart::BarDiagram;
0026     diagram->setModel(&m_model);    
0027 
0028     KChart::Legend* legend = new KChart::Legend(diagram,diagram);
0029     m_chart.addLegend(legend);
0030 
0031     // The code below doesn't work, but it would 
0032     // be nice if it did:
0033 #if 0
0034     KChart::Legend* legend = new KChart::Legend;
0035     legend->addDiagram(diagram1);
0036     legend->addDiagram(diagram2);
0037     ...
0038     m_chart.addLegend(legend);
0039 #endif
0040 
0041     KChart::CartesianAxis* abcissa = new KChart::CartesianAxis(diagram);
0042     abcissa->setPosition( KChart::CartesianAxis::Bottom );
0043     KChart::CartesianAxis* ordinate = new KChart::CartesianAxis(diagram);
0044     ordinate->setPosition( KChart::CartesianAxis::Left );
0045     diagram->addAxis(abcissa);
0046     diagram->addAxis(ordinate);
0047 
0048     // NOTE: If this is done before adding axes,
0049     // the axes don't show up at all
0050     m_chart.coordinatePlane()->replaceDiagram(diagram);
0051 
0052     m_rowbutton.setText( tr("Add rows") );
0053     m_colbutton.setText( tr("Add columns") );
0054     connect( &m_rowbutton, SIGNAL(clicked()),
0055          this, SLOT(addRows()));
0056     connect( &m_colbutton, SIGNAL(clicked()),
0057          this, SLOT(addCols()));
0058 
0059     QVBoxLayout* l = new QVBoxLayout(this);
0060     l->addWidget(&m_chart);
0061     l->addWidget(&m_rowbutton);
0062     l->addWidget(&m_colbutton);
0063 
0064     setLayout(l);
0065   }
0066 
0067 private Q_SLOTS:
0068 
0069   void addRows()
0070   {
0071     m_model.insertRows(m_model.rowCount(),1);
0072     for ( int i = 0; i < m_model.columnCount(); ++i ) {
0073       m_model.setData( m_model.index(m_model.rowCount()-1,i), i );
0074     }
0075   }
0076   void addCols()
0077   {
0078     m_model.insertColumns(m_model.columnCount(),1);
0079     for ( int i = 0; i < m_model.rowCount(); ++i ) {
0080       m_model.setData( m_model.index(i,m_model.columnCount()-1), i );
0081     }
0082   }
0083 
0084 private:
0085   KChart::Chart m_chart;
0086   QPushButton m_rowbutton;
0087   QPushButton m_colbutton;
0088   QStandardItemModel m_model;
0089 };
0090 
0091 int main( int argc, char** argv ) {
0092     QApplication app( argc, argv );
0093     
0094     ChartWidget w;
0095     w.show();
0096 
0097     return app.exec();
0098 }
0099 
0100 #include "main.moc"