File indexing completed on 2024-05-12 15:54:31

0001 /**
0002  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
0003  *
0004  * This file is part of the KD Chart library.
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License as
0008  * published by the Free Software Foundation; either version 2 of
0009  * the License, or (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "mainwindow.h"
0021 
0022 #include <KChartChart>
0023 #include <KChartAbstractCoordinatePlane>
0024 #include <KChartLineDiagram>
0025 #include <KChartBarDiagram>
0026 
0027 #include <QDebug>
0028 #include <QPainter>
0029 
0030 using namespace KChart;
0031 
0032 #define USE_ROOTINDEX 1
0033 
0034 MainWindow::MainWindow( QWidget* parent ) :
0035     QWidget( parent )
0036 {
0037     setupUi( this );
0038 
0039     QHBoxLayout* chartLayout = new QHBoxLayout( chartFrame );
0040     m_chart = new Chart();
0041     chartLayout->addWidget( m_chart );
0042 
0043 #if USE_ROOTINDEX
0044     m_model.insertRows( 0,2, QModelIndex() );
0045     m_model.insertColumns( 0,1, QModelIndex() );
0046     QModelIndex idx1 = m_model.index( 0,0,QModelIndex() );
0047     QModelIndex idx2 = m_model.index( 1,0,QModelIndex() );
0048 
0049     m_model.setData( idx1, 3.14 );
0050     m_model.setData( idx2, 2*3.14 );
0051 
0052     m_model.insertRows( 0,5, idx1 );
0053     m_model.insertColumns( 0,5, idx1 );
0054     
0055     for ( int i = 0; i < 5; ++i ) {
0056       for ( int j = 0; j < 5; ++j ) {
0057     m_model.setData( m_model.index( i,j,idx1), (qreal)i*j );
0058       }
0059     }
0060 
0061     m_model.insertRows( 0,2, idx2 );
0062     m_model.insertColumns( 0,2, idx2 );
0063     
0064     for ( int i = 0; i < 2; ++i ) {
0065       for ( int j = 0; j < 2; ++j ) {
0066     m_model.setData( m_model.index( i,j,idx2), 10.*(i+1.)/(j+1.) );
0067       }
0068     }
0069 #else
0070     QModelIndex idx1 = QModelIndex();
0071     m_model.insertRows( 0,5, idx1 );
0072     m_model.insertColumns( 0,5, idx1 );
0073     
0074     for ( int i = 0; i < 5; ++i ) {
0075       for ( int j = 0; j < 5; ++j ) {
0076     m_model.setData( m_model.index( i,j,idx1), (qreal)i*j );
0077       }
0078     }
0079     QModelIndex idx2 = idx1;
0080 
0081     m_model2.insertRows( 0,2, idx2 );
0082     m_model2.insertColumns( 0,2, idx2 );
0083     
0084     for ( int i = 0; i < 2; ++i ) {
0085       for ( int j = 0; j < 2; ++j ) {
0086     m_model2.setData( m_model2.index( i,j,idx2), 10.*(i+1.)/(j+1.) );
0087       }
0088     }
0089     
0090 #endif
0091 
0092     // Set up the diagram
0093     m_lines = new LineDiagram();
0094     m_lines->setModel( &m_model );
0095 #if USE_ROOTINDEX
0096     m_lines->setRootIndex(idx1);
0097 #endif
0098 
0099     m_bars = new BarDiagram();
0100 #if USE_ROOTINDEX
0101     m_bars->setModel( &m_model );
0102     m_bars->setRootIndex(idx2);
0103 #else
0104     m_bars->setModel( &m_model2 );
0105 #endif
0106 
0107     plane = new CartesianCoordinatePlane( m_chart );
0108 
0109     CartesianAxis *xAxis = new CartesianAxis( m_lines );
0110     CartesianAxis *yAxis = new CartesianAxis ( m_lines );
0111     CartesianAxis *yAxis3 = new CartesianAxis ( m_lines );
0112     xAxis->setPosition ( KChart::CartesianAxis::Bottom );
0113     yAxis->setPosition ( KChart::CartesianAxis::Left );
0114     yAxis3->setPosition ( KChart::CartesianAxis::Left );
0115 
0116     CartesianAxis *yAxis2 = new CartesianAxis ( m_bars );
0117     yAxis2->setPosition ( KChart::CartesianAxis::Right );
0118 
0119     // explicitly add it to the second diagram, we want to share it
0120     m_bars->addAxis( xAxis );
0121 
0122     m_chart->coordinatePlane()->replaceDiagram( m_lines );
0123     plane->replaceDiagram( m_bars );
0124     // We want both planes to use the same space.
0125     plane->setReferenceCoordinatePlane( m_chart->coordinatePlane() );
0126     m_chart->addCoordinatePlane( plane/*, 1*/);
0127 }