File indexing completed on 2024-05-26 04:23:43
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 "mainwindow.h" 0010 0011 #include <KChartChart> 0012 #include <KChartPlotter> 0013 #include <KChartDataValueAttributes> 0014 #include <KChartTextAttributes> 0015 #include <KChartMarkerAttributes> 0016 0017 #include <QStandardItemModel> 0018 #include <QTimer> 0019 0020 0021 using namespace KChart; 0022 0023 0024 0025 static const int nBubbles = 7; 0026 // we display seven bubbles using the following data structure: 0027 // 0: Y 0028 // 1: X 0029 // 2: size 0030 struct DataType{ 0031 DataType( qreal x_, 0032 qreal y_, 0033 qreal size_ ) 0034 : x(x_) 0035 , y(y_) 0036 , size(size_) 0037 {} 0038 qreal x; 0039 qreal y; 0040 qreal size; 0041 }; 0042 static const DataType bubblesData[nBubbles] = { 0043 DataType(0.5, 1.0, 100), 0044 DataType(1.0, 0.5, 60 ), 0045 DataType(1.6, 2.0, 28 ), 0046 DataType(0.7, 0.3, 55 ), 0047 DataType(1.3, 2.0, 95 ), 0048 DataType(2.0, 1.0, 75 ), 0049 DataType(1.4, 1.1, 85 ) 0050 }; 0051 0052 #define ROLE_SIZE Qt::UserRole + 1 0053 0054 0055 0056 MainWindow::MainWindow( QWidget* parent ) : 0057 QWidget( parent ), 0058 m_model(nullptr) 0059 { 0060 setupUi( this ); 0061 0062 QHBoxLayout* chartLayout = new QHBoxLayout( chartFrame ); 0063 m_chart = new Chart(); 0064 chartLayout->addWidget( m_chart ); 0065 // Set up the data 0066 initializeDataModel(); 0067 // Set up the diagram 0068 m_plotter = new Plotter(); 0069 // Register the data model at the diagram 0070 m_plotter->setModel( m_model ); 0071 // Add axes to the diagram 0072 CartesianAxis *xAxis = new CartesianAxis( m_plotter ); 0073 CartesianAxis *xAxis2 = new CartesianAxis( m_plotter ); 0074 CartesianAxis *yAxis = new CartesianAxis ( m_plotter ); 0075 CartesianAxis *yAxis2 = new CartesianAxis ( m_plotter ); 0076 xAxis->setPosition ( KChart::CartesianAxis::Bottom ); 0077 xAxis2->setPosition( KChart::CartesianAxis::Top ); 0078 yAxis->setPosition ( KChart::CartesianAxis::Left ); 0079 yAxis2->setPosition( KChart::CartesianAxis::Right ); 0080 m_plotter->addAxis( xAxis ); 0081 m_plotter->addAxis( xAxis2 ); 0082 m_plotter->addAxis( yAxis ); 0083 m_plotter->addAxis( yAxis2 ); 0084 connect( threeDEnabled, SIGNAL(toggled(bool)), this, SLOT(setMarkerAttributes()) ); 0085 0086 m_chart->coordinatePlane()->replaceDiagram( m_plotter ); 0087 m_chart->setGlobalLeading( 20, 20, 20, 20 ); 0088 0089 setMarkerAttributes(); 0090 } 0091 0092 0093 void MainWindow::initializeDataModel() 0094 { 0095 m_model = new QStandardItemModel( nBubbles, 2 ); 0096 m_model->setHeaderData(0, Qt::Horizontal, tr("Some Bubbles")); 0097 for ( int i=0; i < nBubbles; ++i ) { 0098 const QModelIndex indexX = m_model->index(i, 0); 0099 const QModelIndex indexY = m_model->index(i, 1); 0100 0101 m_model->setData(indexX, QVariant( bubblesData[i].x ), Qt::DisplayRole); 0102 0103 m_model->setData(indexY, QVariant( bubblesData[i].y ), Qt::DisplayRole); 0104 0105 m_model->setData(indexY, bubblesData[i].size, ROLE_SIZE ); 0106 } 0107 } 0108 0109 0110 void MainWindow::setMarkerAttributes() 0111 { 0112 // disable the connecting line 0113 KChart::LineAttributes la = m_plotter->lineAttributes(); 0114 la.setVisible(false); 0115 m_plotter->setLineAttributes(la); 0116 0117 for ( int iRow = 0; iRow<nBubbles; ++iRow ) { 0118 const QModelIndex indexX = m_plotter->model()->index(iRow, 0); 0119 const QModelIndex indexY = m_plotter->model()->index(iRow, 1); 0120 DataValueAttributes dva( m_plotter->dataValueAttributes( indexX ) ); 0121 dva.setVisible( true ); 0122 0123 TextAttributes ta( dva.textAttributes() ); 0124 ta.setVisible( false ); 0125 0126 MarkerAttributes ma( dva.markerAttributes() ); 0127 ma.setVisible( true ); 0128 ma.setMarkerStyle( MarkerAttributes::MarkerCircle ); 0129 ma.setThreeD( threeDEnabled->isChecked() ); 0130 0131 // set the size 0132 const qreal d = m_model->data( indexY, ROLE_SIZE ).toReal(); 0133 ma.setMarkerSize( QSizeF(d,d) ); 0134 0135 RelativePosition pos( dva.positivePosition() ); 0136 pos.setAlignment( Qt::AlignCenter ); 0137 pos.setHorizontalPadding(0); 0138 0139 dva.setPositivePosition( pos ); 0140 dva.setMarkerAttributes( ma ); 0141 dva.setTextAttributes( ta ); 0142 0143 // note: The KChart::Plotter looks at the X cell 0144 // for data value attributes, 0145 // any attrs set at the Y cell are ignored. 0146 m_plotter->setDataValueAttributes( indexX, dva ); 0147 } 0148 m_chart->update(); 0149 }