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

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