File indexing completed on 2024-06-23 04:17:56

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 <KChartChart>
0011 #include <KChartPieDiagram>
0012 #include <KChartDataValueAttributes>
0013 #include <KChartBackgroundAttributes>
0014 #include <KChartPieAttributes>
0015 #include <KChartPosition>
0016 
0017 #include <QApplication>
0018 
0019 using namespace KChart;
0020 
0021 class ChartWidget : public QWidget {
0022     Q_OBJECT
0023 public:
0024     explicit ChartWidget(QWidget* parent = nullptr)
0025         : QWidget(parent)
0026     {
0027 
0028         m_model.insertRows( 0, 1, QModelIndex() );
0029         m_model.insertColumns(  0,  6,  QModelIndex() );
0030         for (int row = 0; row < 1; ++row) {
0031             for (int column = 0; column < 6; ++column) {
0032                 QModelIndex index = m_model.index(row, column, QModelIndex());
0033                 m_model.setData(index, QVariant(row+1 * column+1) );
0034 
0035                 // this shows the index as static comments:
0036                 // m_model.setData(index, QString("row: %1,  column: %2").arg(row).arg(column), KChart::CommentRole);
0037 
0038                 // this shows the index as volatile tooltips:
0039                 m_model.setData(index, QString("row: %1,  column: %2").arg(row).arg(column), Qt::ToolTipRole);
0040             }
0041         }
0042         // We need a Polar plane for the Pie type
0043         PolarCoordinatePlane* polarPlane = new PolarCoordinatePlane( &m_chart );
0044         // replace the default Cartesian plane with
0045         // our Polar plane
0046         m_chart.replaceCoordinatePlane( polarPlane );
0047 
0048         // assign the model to our pie diagram
0049         PieDiagram* diagram = new PieDiagram;
0050         diagram->setModel(&m_model);
0051 
0052         // Configure some Pie specifical attributes
0053 
0054         // explode a section
0055         PieAttributes pa(  diagram->pieAttributes() );
0056         pa.setExplodeFactor( 0.1 );
0057 
0058         // Assign the attributes
0059         // to the diagram
0060         diagram->setPieAttributes( 1,  pa );
0061 
0062         // Configure a generic attribute
0063         // available to all chart types
0064         QPen sectionPen;
0065         sectionPen.setWidth( 5 );
0066         sectionPen.setStyle( Qt::DashLine );
0067         sectionPen.setColor( Qt::magenta );
0068 
0069         diagram->setPen( 1, sectionPen );
0070 
0071         // Display data values
0072         // not implemented yet - disable for now
0073         const QFont font(QFont( "Comic", 10 ));
0074         const int colCount = diagram->model()->columnCount();
0075         for ( int iColumn = 0; iColumn<colCount; ++iColumn ) {
0076             DataValueAttributes dva( diagram->dataValueAttributes( iColumn ) );
0077             TextAttributes ta( dva.textAttributes() );
0078             ta.setRotation( 0 );
0079             ta.setFont( font );
0080             ta .setPen( QPen( Qt::darkBlue ) );
0081             ta.setVisible( true );
0082             dva.setTextAttributes( ta );
0083 
0084             BackgroundAttributes back( dva.backgroundAttributes() );
0085             back.setBrush( QBrush( QColor(255,0,0) ) );
0086             back.setVisible( true );
0087             dva.setBackgroundAttributes( back );
0088 
0089             RelativePosition posPos( dva.positivePosition() );
0090             posPos.setReferencePosition( KChart::Position::North );
0091             posPos.setAlignment( Qt::AlignCenter );
0092             posPos.setHorizontalPadding( KChart::Measure(0.0) );
0093             posPos.setVerticalPadding( KChart::Measure(-1000.0) );
0094             dva.setPositivePosition( posPos );
0095             dva.setVisible( true );
0096             diagram->setDataValueAttributes( iColumn, dva);
0097         }
0098 
0099         // Assign our diagram to the Chart
0100         m_chart.coordinatePlane()->replaceDiagram(diagram);
0101 
0102         QVBoxLayout* l = new QVBoxLayout(this);
0103         l->addWidget(&m_chart);
0104         setLayout(l);
0105     }
0106 
0107 private:
0108     Chart m_chart;
0109     QStandardItemModel m_model;
0110 };
0111 
0112 int main( int argc, char** argv ) {
0113     QApplication app( argc, argv );
0114 
0115     ChartWidget w;
0116     w.show();
0117 
0118     return app.exec();
0119 }
0120 
0121 #include "main.moc"