File indexing completed on 2025-02-16 04:03:07

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 <KChartPolarDiagram>
0012 #include <KChartDataValueAttributes>
0013 #include <KChartBackgroundAttributes>
0014 
0015 #include <QApplication>
0016 
0017 using namespace KChart;
0018 
0019 class ChartWidget : public QWidget {
0020     Q_OBJECT
0021 public:
0022     explicit ChartWidget(QWidget* parent = nullptr)
0023         : QWidget(parent)
0024     {
0025 
0026         // initialize the ItemModel and fill in some data
0027         m_model.insertRows( 0, 10 );
0028         m_model.insertColumns(  0,  5 );
0029         int value = 0;
0030         for ( int column = 0; column < m_model.columnCount(); ++column ) {
0031             for ( int row = 0; row < m_model.rowCount(); ++row ) {
0032                 QModelIndex index = m_model.index( row, column );
0033                 m_model.setData( index, QVariant( value++  ) );
0034             }
0035         }
0036         // We need a Polar plane for the Polar type
0037         PolarCoordinatePlane* polarPlane = new PolarCoordinatePlane( &m_chart );
0038         // replace the default Cartesian plane with
0039         // our Polar plane
0040         m_chart.replaceCoordinatePlane( polarPlane );
0041 
0042         // assign the model to our polar diagram
0043         PolarDiagram* diagram = new PolarDiagram;
0044         diagram->setModel(&m_model);
0045 
0046         // Configure the plane's Background
0047         BackgroundAttributes pba;
0048         pba.setBrush( QBrush(QColor(0x20,0x20,0x60)) );
0049         pba.setVisible( true );
0050         polarPlane->setBackgroundAttributes(  pba );
0051 
0052 
0053         // Configure some global / dataset / cell specific attributes:
0054 
0055         DataValueAttributes dva( diagram->dataValueAttributes() );
0056         MarkerAttributes ma( dva.markerAttributes() );
0057         ma.setVisible( true );
0058         ma.setMarkerStyle( MarkerAttributes::MarkerSquare );
0059         ma.setMarkerSize( QSize( 6,6 ) );
0060         dva.setMarkerAttributes( ma );
0061         // find a nicer place for the data value texts:
0062         // We want them to be centered on top of their respective markers.
0063         RelativePosition relativePosition( dva.positivePosition() );
0064         relativePosition.setReferencePosition( Position::Center );
0065         relativePosition.setAlignment( Qt::AlignBottom | Qt::AlignHCenter );
0066         relativePosition.setHorizontalPadding( KChart::Measure( 0.0, KChartEnums::MeasureCalculationModeAbsolute ) );
0067         relativePosition.setVerticalPadding(   KChart::Measure( 0.0, KChartEnums::MeasureCalculationModeAbsolute ) );
0068         dva.setPositivePosition( relativePosition );
0069         diagram->setDataValueAttributes( dva );
0070 
0071         // Display data values
0072         const QFont font(QFont( "Comic", 10 ));
0073         const int colCount = diagram->model()->columnCount();
0074         for ( int iColumn = 0; iColumn<colCount; ++iColumn ) {
0075             DataValueAttributes dva( diagram->dataValueAttributes( iColumn ) );
0076             TextAttributes ta( dva.textAttributes() );
0077             ta.setRotation( 0 );
0078             ta.setFont( font );
0079             ta .setPen( QPen( Qt::gray ) );
0080             ta.setVisible( true );
0081             dva.setTextAttributes( ta );
0082             dva.setVisible( true );
0083             diagram->setDataValueAttributes( iColumn, dva);
0084         }
0085 
0086 
0087         // Set the marker of one single cell differently to show
0088         // how per-cell marker attributes can be used:
0089         const QModelIndex index = diagram->model()->index( 1, 2, QModelIndex() );
0090         dva = diagram->dataValueAttributes( index );
0091         ma = dva.markerAttributes();
0092         ma.setMarkerStyle( MarkerAttributes::MarkerCircle );
0093         ma.setMarkerSize( QSize( 40,40 ) );
0094 
0095         // This is the canonical way to adjust a marker's color:
0096         // By default the color is invalid so we use an explicit fallback
0097         // here to make sure we are getting the right color, as it would
0098         // be used by KD Chart's built-in logic too:
0099         QColor semiTrans( ma.markerColor() );
0100         if ( ! semiTrans.isValid() )
0101             semiTrans = diagram->brush( index ).color();
0102 
0103         semiTrans.setAlpha(164);
0104         ma.setMarkerColor( semiTrans.darker() );
0105 
0106         dva.setMarkerAttributes( ma );
0107 
0108         // While we are at it we also set the text alignment to centered
0109         // for this special point:
0110         relativePosition = dva.positivePosition();
0111         relativePosition.setAlignment( Qt::AlignCenter );
0112         dva.setPositivePosition( relativePosition );
0113         diagram->setDataValueAttributes( index, dva);
0114 
0115 
0116 
0117         // Assign our diagram to the Chart
0118         m_chart.coordinatePlane()->replaceDiagram(diagram);
0119 
0120         // We want to have a nice gap around the polar diagram,
0121         // but we also want to have the coord. plane's background cover that area,
0122         // so we just use some zooming.
0123         // NOTE: Setting a zoom factor must not be done before
0124         //       a diagram has been specified and assigned to the coordinate plane!
0125         polarPlane->setZoomFactorX(0.9);
0126         polarPlane->setZoomFactorY(0.9);
0127 
0128         QVBoxLayout* l = new QVBoxLayout(this);
0129         l->addWidget(&m_chart);
0130         m_chart.setGlobalLeadingTop( 5 );
0131         m_chart.setGlobalLeadingBottom( 5 );
0132         setLayout(l);
0133     }
0134 
0135 private:
0136     Chart m_chart;
0137     QStandardItemModel m_model;
0138 };
0139 
0140 int main( int argc, char** argv ) {
0141     QApplication app( argc, argv );
0142 
0143     ChartWidget w;
0144     w.show();
0145 
0146     return app.exec();
0147 }
0148 
0149 #include "main.moc"