File indexing completed on 2024-06-16 04:09:00

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 <KChartBarDiagram>
0012 #include <KChartHeaderFooter>
0013 #include <KChartPosition>
0014 #include <KChartBackgroundAttributes>
0015 #include <KChartFrameAttributes>
0016 #include <QApplication>
0017 
0018 using namespace KChart;
0019 
0020 class ChartWidget : public QWidget {
0021   Q_OBJECT
0022 public:
0023   explicit ChartWidget(QWidget* parent = nullptr)
0024     : QWidget(parent)
0025   {
0026 
0027     m_model.insertRows( 0, 2, QModelIndex() );
0028     m_model.insertColumns(  0,  3,  QModelIndex() );
0029     for (int row = 0; row < 3; ++row) {
0030             for (int column = 0; column < 3; ++column) {
0031                 QModelIndex index = m_model.index(row, column, QModelIndex());
0032                 m_model.setData(index, QVariant(row+1 * column) );
0033             }
0034     }
0035 
0036     BarDiagram* diagram = new BarDiagram;
0037     diagram->setModel(&m_model);
0038 
0039     m_chart.coordinatePlane()->replaceDiagram(diagram);
0040 
0041     // Add at one Header and set it up
0042     HeaderFooter* header = new HeaderFooter( &m_chart );
0043     header->setPosition( Position::North );
0044     header->setText( "A Simple Bar Chart" );
0045     m_chart.addHeaderFooter( header );
0046 
0047     // Configure the Header text attributes
0048     TextAttributes hta;
0049     hta.setPen( QPen(  Qt::blue ) );
0050 
0051     // let the header resize itself
0052     // together with the widget.
0053     // so-called relative size
0054     Measure m( 35.0 );
0055     m.setRelativeMode( header->autoReferenceArea(), KChartEnums::MeasureOrientationMinimum );
0056     hta.setFontSize( m );
0057     // min font size
0058     m.setValue( 3.0 );
0059     m.setCalculationMode( KChartEnums::MeasureCalculationModeAbsolute );
0060     hta.setMinimalFontSize( m );
0061     header->setTextAttributes( hta );
0062 
0063     // Configure the header Background attributes
0064     BackgroundAttributes hba;
0065     hba.setBrush(  Qt::white );
0066     hba.setVisible( true );
0067     header->setBackgroundAttributes(  hba );
0068 
0069     // Configure the header Frame attributes
0070     FrameAttributes hfa;
0071     hfa.setPen( QPen ( QBrush( Qt::darkGray ), 2 ) );
0072     hfa.setVisible( true );
0073     header->setFrameAttributes(  hfa );
0074 
0075     QVBoxLayout* l = new QVBoxLayout(this);
0076     l->addWidget(&m_chart);
0077     m_chart.setGlobalLeadingTop( 10 );
0078     setLayout(l);
0079   }
0080 
0081 private:
0082   Chart m_chart;
0083   QStandardItemModel m_model;
0084 };
0085 
0086 int main( int argc, char** argv ) {
0087     QApplication app( argc, argv );
0088 
0089     ChartWidget w;
0090     w.show();
0091 
0092     return app.exec();
0093 }
0094 
0095 #include "main.moc"