File indexing completed on 2025-02-02 04:12:20

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 <KChartLineDiagram>
0012 #include <KChartHeaderFooter>
0013 #include <KChartPosition>
0014 #include <KChartBackgroundAttributes>
0015 #include <KChartFrameAttributes>
0016 #include <KChartGridAttributes>
0017 #include <KChartAbstractCoordinatePlane>
0018 #include <KChartCartesianCoordinatePlane>
0019 #include <KChartAbstractCartesianDiagram>
0020 #include <KChartDataValueAttributes>
0021 #include <QApplication>
0022 
0023 
0024 using namespace KChart;
0025 
0026 class ChartWidget : public QWidget {
0027   Q_OBJECT
0028 public:
0029   explicit ChartWidget(QWidget* parent = nullptr)
0030     : QWidget(parent)
0031   {
0032 
0033     m_model.insertRows( 0, 6, QModelIndex() );
0034     m_model.insertColumns(  0,  4,  QModelIndex() );
0035     for (int row = 0; row < 6; ++row) {
0036             for (int column = 0; column < 4; ++column) {
0037                 QModelIndex index = m_model.index(row, column, QModelIndex());
0038                 m_model.setData(index, QVariant(row*0.5 + column) );
0039             }
0040     }
0041 
0042     LineDiagram* diagram = new LineDiagram;
0043     diagram->setModel(&m_model);
0044 
0045     CartesianAxis *xAxis = new CartesianAxis( diagram );
0046     CartesianAxis *yAxis = new CartesianAxis ( diagram );
0047     xAxis->setPosition ( KChart::CartesianAxis::Bottom );
0048     yAxis->setPosition ( KChart::CartesianAxis::Left );
0049     diagram->addAxis( xAxis );
0050     diagram->addAxis( yAxis );
0051 
0052     m_chart.coordinatePlane()->replaceDiagram(diagram);
0053 
0054     /* Header */
0055 
0056    // Add at one Header and set it up
0057     HeaderFooter* header = new HeaderFooter( &m_chart );
0058     header->setPosition( Position::North );
0059     header->setText( "A Line Chart with Grid Configured" );
0060     m_chart.addHeaderFooter( header );
0061 
0062     // Configure the Header text attributes
0063     TextAttributes hta;
0064     hta.setPen( QPen(  Qt::red ) );
0065 
0066     // let the header resize itself
0067     // together with the widget.
0068     // so-called relative size
0069     Measure m( 35.0 );
0070     m.setRelativeMode( header->autoReferenceArea(),
0071                        KChartEnums::MeasureOrientationMinimum );
0072     hta.setFontSize( m );
0073     // min font size
0074     m.setValue( 3.0 );
0075     m.setCalculationMode( KChartEnums::MeasureCalculationModeAbsolute );
0076     hta.setMinimalFontSize( m );
0077     header->setTextAttributes( hta );
0078 
0079     // Configure the Header's Background
0080     BackgroundAttributes hba;
0081     hba.setBrush( Qt::white );
0082     hba.setVisible( true );
0083     header->setBackgroundAttributes(  hba );
0084 
0085     // Configure the header Frame attributes
0086     FrameAttributes hfa;
0087     hfa.setPen( QPen ( QBrush( Qt::darkGray ), 2 ) );
0088     hfa.setPadding( 2 );
0089     hfa.setVisible( true );
0090     header->setFrameAttributes(  hfa );
0091 
0092 
0093     // diagram->coordinatePlane returns an abstract plane one.
0094     // if we want to specify the orientation we need to cast
0095     // as follow
0096     CartesianCoordinatePlane* plane = static_cast <CartesianCoordinatePlane*>
0097             ( diagram->coordinatePlane() );
0098 
0099     /* Configure grid steps and pen */
0100 
0101     // Vertical
0102     GridAttributes gv ( plane->gridAttributes( Qt::Vertical ) );
0103 
0104     // Configure a grid pen
0105     // I know it is horrible
0106     // just for demo'ing
0107     QPen gridPen(  Qt::gray );
0108     gridPen.setWidth( 2 );
0109     gv.setGridPen(  gridPen );
0110 
0111     // Configure a sub-grid pen
0112     QPen subGridPen( Qt::darkGray );
0113     subGridPen.setStyle( Qt::DotLine );
0114     gv.setSubGridPen(  subGridPen );
0115 
0116     // Display a blue zero line
0117     gv.setZeroLinePen( QPen( Qt::blue ) );
0118 
0119     // change step and substep width
0120     // or any of those.
0121     gv.setGridStepWidth( 1.0 );
0122     gv.setGridSubStepWidth( 0.5 );
0123     gv.setGridVisible(  true );
0124     gv.setSubGridVisible( true );
0125 
0126     // Horizontal
0127 
0128     GridAttributes gh = plane->gridAttributes( Qt::Horizontal );
0129     gh.setGridPen( gridPen );
0130     gh.setGridStepWidth(  0.5 );
0131     gh.setSubGridPen(  subGridPen );
0132     gh.setGridSubStepWidth( 0.1 );
0133 
0134     plane->setGridAttributes( Qt::Vertical,  gv );
0135     plane->setGridAttributes( Qt::Horizontal,  gh );
0136 
0137     // Data Values Display and position
0138     const int colCount = diagram->model()->columnCount(diagram->rootIndex());
0139     for ( int iColumn = 0; iColumn<colCount; ++iColumn ) {
0140         DataValueAttributes a( diagram->dataValueAttributes( iColumn ) );
0141         RelativePosition pos ( a.position( true ) );
0142         pos.setAlignment( Qt::AlignRight );
0143         a.setPositivePosition( pos );
0144         QBrush brush( diagram->brush( iColumn ) );
0145         TextAttributes ta( a.textAttributes() );
0146         ta.setRotation( 0 );
0147         ta.setFont( QFont( "Comic", 10 ) );
0148         ta.setPen( QPen( brush.color() ) );
0149         ta.setVisible( true );
0150         a.setVisible( true );
0151         a.setTextAttributes( ta );
0152         diagram->setDataValueAttributes( iColumn, a );
0153     }
0154 
0155 
0156     QVBoxLayout* l = new QVBoxLayout(this);
0157     l->addWidget(&m_chart);
0158     m_chart.setGlobalLeadingRight( 20 );
0159     setLayout(l);
0160   }
0161 
0162 private:
0163   Chart m_chart;
0164   QStandardItemModel m_model;
0165     QPixmap pixmap;
0166 };
0167 
0168 int main( int argc, char** argv ) {
0169     QApplication app( argc, argv );
0170 
0171     ChartWidget w;
0172     w.show();
0173 
0174     return app.exec();
0175 }
0176 
0177 #include "main.moc"