File indexing completed on 2025-01-26 04:02:44

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 <QLabel>
0010 #include <QString>
0011 #include <QTextStream>
0012 
0013 #include <KChartChart>
0014 #include <KChartTernaryAxis>
0015 #include <KChartTernaryLineDiagram>
0016 #include <KChartTernaryPointDiagram>
0017 #include <KChartTernaryCoordinatePlane>
0018 
0019 #include "mainwindow.h"
0020 
0021 MainWindow::MainWindow( QWidget* parent )
0022     : QDialog( parent )
0023     , m_chart( nullptr )
0024     , m_diagram( nullptr )
0025     , m_ternaryPlane( nullptr )
0026 {
0027     setupUi( this );
0028     // make a chart
0029     m_chart = new KChart::Chart;
0030     // replace the default (cartesian) coordinate plane with a ternary one
0031     m_ternaryPlane = new KChart::TernaryCoordinatePlane;
0032     m_chart->replaceCoordinatePlane( m_ternaryPlane );
0033     // make a ternary line diagram
0034     m_diagram = new KChart::TernaryPointDiagram;
0035     // and replace the default diagram with it
0036     m_ternaryPlane->replaceDiagram( m_diagram );
0037 
0038     // add the three ternary axes, see the positions
0039     KChart::TernaryAxis* axisA = new KChart::TernaryAxis( m_diagram );
0040     axisA->setPosition( KChartEnums::PositionSouth );
0041     KChart::TernaryAxis* axisB = new KChart::TernaryAxis( m_diagram );
0042     axisB->setPosition( KChartEnums::PositionWest );
0043     KChart::TernaryAxis* axisC = new KChart::TernaryAxis( m_diagram );
0044     axisC->setPosition( KChartEnums::PositionEast );
0045 
0046     QHBoxLayout* chartLayout = new QHBoxLayout( frame );
0047     chartLayout->addWidget( m_chart );
0048 
0049     setupModel();
0050     m_diagram->setModel( &m_model );
0051     connect( m_diagram, SIGNAL(clicked(QModelIndex)),
0052              SLOT(indexClicked(QModelIndex)) );
0053 }
0054 
0055 void MainWindow::setupModel()
0056 {
0057     m_model.insertRows( 0, 5 );
0058     m_model.insertColumns(  0,  3 );
0059 
0060     const float column0Share = 1.0f/3.0f;
0061     const float column1Share = 1.0f/3.0f;
0062     const float column2Share = 1.0f/3.0f;
0063 
0064     for ( int row = 0; row < m_model.rowCount(); ++row ) {
0065         const qreal SkewX = column0Share * ( 1 - 1.0/( 5 * row*row*row + 1 ) );
0066         const qreal SkewY = SkewX;
0067         QModelIndex index;
0068 
0069         index = m_model.index( row, 0 );
0070         m_model.setData( index, QVariant( column0Share - SkewX ) );
0071         index = m_model.index( row, 1 );
0072         m_model.setData( index, QVariant( column1Share + SkewX - SkewY) );
0073         index = m_model.index( row, 2 );
0074         m_model.setData( index, QVariant( column2Share + SkewY ) );
0075     }
0076 }
0077 
0078 void MainWindow::indexClicked( const QModelIndex& index )
0079 {
0080     qDebug() << "MainWindow::indexClicked:" << index;
0081     if ( index.isValid() ) {
0082         QString text;
0083         QTextStream stream( &text );
0084         stream << "Row: " << index.row() << " - Column: " << index.column();
0085         label->setText( text );
0086     }
0087 }
0088