File indexing completed on 2024-11-24 03:57:46
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 <KChartDataValueAttributes> 0015 0016 using namespace KChart; 0017 0018 class ChartWidget : public QWidget 0019 { 0020 Q_OBJECT 0021 public: 0022 explicit ChartWidget( QWidget *parent = nullptr ); 0023 0024 private: 0025 Chart m_chart; 0026 QStandardItemModel m_model; 0027 }; 0028 0029 ChartWidget::ChartWidget( QWidget *parent ) 0030 : QWidget( parent ) 0031 { 0032 m_model.insertRows( 0, 2, QModelIndex() ); 0033 m_model.insertColumns( 0, 3, QModelIndex() ); 0034 for ( int row = 0; row < 3; ++row ) { 0035 for ( int column = 0; column < 3; ++column ) { 0036 QModelIndex index = m_model.index( row, column, QModelIndex() ); 0037 m_model.setData(index, row * 3 + column ); 0038 } 0039 } 0040 0041 BarDiagram* diagram = new KChart::BarDiagram; 0042 diagram->setModel( &m_model ); 0043 0044 BarAttributes ba( diagram->barAttributes() ); 0045 //set the bar width and enable it 0046 ba.setFixedBarWidth( 140 ); 0047 ba.setUseFixedBarWidth( true ); 0048 //configure gap between values 0049 //and blocks 0050 ba.setGroupGapFactor( 0.50 ); 0051 ba.setBarGapFactor( 0.125 ); 0052 0053 //assign to the diagram 0054 diagram->setBarAttributes( ba ); 0055 0056 // display the values 0057 DataValueAttributes dva( diagram->dataValueAttributes() ); 0058 TextAttributes ta( dva.textAttributes() ); 0059 //rotate if you wish 0060 //ta.setRotation( 0 ); 0061 ta.setFont( QFont( "Comic", 9 ) ); 0062 ta .setPen( QPen( QColor( Qt::darkGreen ) ) ); 0063 ta.setVisible( true ); 0064 dva.setTextAttributes( ta ); 0065 dva.setVisible( true ); 0066 diagram->setDataValueAttributes( dva ); 0067 0068 //draw a surrounding line around bars 0069 QPen linePen; 0070 linePen.setColor( Qt::magenta ); 0071 linePen.setWidth( 4 ); 0072 linePen.setStyle( Qt::DotLine ); 0073 //draw only around a dataset 0074 //to draw around all the bars 0075 // call setPen( myPen ); 0076 diagram->setPen( 1, linePen ); 0077 0078 m_chart.coordinatePlane()->replaceDiagram( diagram ); 0079 m_chart.setGlobalLeadingTop( 40 ); 0080 0081 QVBoxLayout *l = new QVBoxLayout( this ); 0082 l->addWidget( &m_chart ); 0083 setLayout( l ); 0084 } 0085 0086 0087 int main( int argc, char** argv ) 0088 { 0089 QApplication app( argc, argv ); 0090 0091 ChartWidget w; 0092 w.show(); 0093 0094 return app.exec(); 0095 } 0096 0097 #include "main.moc"