File indexing completed on 2024-11-24 03:57:51
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 <QStandardItemModel> 0012 #include <QFileDialog> 0013 0014 #include <KChartChart> 0015 #include <KChartDataValueAttributes> 0016 #include <KChartLegend> 0017 #include <KChartLineDiagram> 0018 #include <KChartTextAttributes> 0019 #include <KChartThreeDLineAttributes> 0020 0021 0022 using namespace KChart; 0023 0024 MainWindow::MainWindow( QWidget* parent ) : 0025 QWidget( parent ) 0026 { 0027 setupUi( this ); 0028 0029 QHBoxLayout* chartLayout = new QHBoxLayout( chartFrame ); 0030 m_chart = new Chart(); 0031 m_chart->setGlobalLeading( 20, 20, 20, 20 ); 0032 chartLayout->addWidget( m_chart ); 0033 0034 0035 // Initialize the model, and fill it with data 0036 const int rowCount = 8; 0037 const int columnCount = 3; 0038 m_model = new QStandardItemModel(rowCount, columnCount, this); 0039 m_model->setHeaderData(0, Qt::Horizontal, tr("Product A")); 0040 m_model->setHeaderData(1, Qt::Horizontal, tr("Product B")); 0041 m_model->setHeaderData(2, Qt::Horizontal, tr("Product C")); 0042 openFile(":/Charts/qtdata.cht"); 0043 0044 // Set up the diagram 0045 m_lines = new LineDiagram(); 0046 // Register the data model at the diagram 0047 m_lines->setModel( m_model ); 0048 // Add axes to the diagram 0049 CartesianAxis *xAxis = new CartesianAxis( m_lines ); 0050 CartesianAxis *yAxis = new CartesianAxis ( m_lines ); 0051 xAxis->setPosition ( KChart::CartesianAxis::Bottom ); 0052 yAxis->setPosition ( KChart::CartesianAxis::Left ); 0053 m_lines->addAxis( xAxis ); 0054 m_lines->addAxis( yAxis ); 0055 // Make the lines thicker 0056 for ( int iColumn = 0; iColumn < columnCount; ++iColumn ) { 0057 QPen linePen( m_lines->pen( iColumn ) ); 0058 linePen.setWidth( 3 ); 0059 m_lines->setPen( iColumn, linePen ); 0060 } 0061 // Register the diagram at the coordinate plane 0062 m_chart->coordinatePlane()->replaceDiagram( m_lines ); 0063 0064 // Add a legend 0065 Legend* legend = new Legend( m_lines, m_chart ); 0066 legend->setPosition( Position::South ); 0067 legend->setAlignment( Qt::AlignCenter ); 0068 legend->setShowLines( true ); 0069 legend->setTitleText(""); 0070 legend->setOrientation( Qt::Horizontal ); 0071 legend->addDiagram( m_lines ); 0072 m_chart->addLegend( legend ); 0073 } 0074 0075 void MainWindow::on_showDataset1CB_toggled( bool checked ) 0076 { 0077 setHidden( 0, ! checked ); 0078 } 0079 0080 void MainWindow::on_showDataset2CB_toggled( bool checked ) 0081 { 0082 setHidden( 1, ! checked ); 0083 } 0084 0085 void MainWindow::on_showDataset3CB_toggled( bool checked ) 0086 { 0087 setHidden( 2, ! checked ); 0088 } 0089 0090 void MainWindow::setHidden( int dataset, bool hidden ) 0091 { 0092 m_lines->setHidden( dataset, hidden ); 0093 m_chart->update(); 0094 } 0095 0096 void MainWindow::openFile(const QString &path) 0097 { 0098 QString fileName; 0099 if (path.isNull()) 0100 fileName = QFileDialog::getOpenFileName(this, tr("Choose a data file"), 0101 "", "*.cht"); 0102 else 0103 fileName = path; 0104 0105 if (!fileName.isEmpty()) { 0106 QFile file(fileName); 0107 0108 if (file.open(QFile::ReadOnly | QFile::Text)) { 0109 QTextStream stream(&file); 0110 QString line; 0111 0112 m_model->removeRows(0, m_model->rowCount(QModelIndex()), QModelIndex()); 0113 0114 int row = 0; 0115 do { 0116 line = stream.readLine(); 0117 if (!line.isEmpty()) { 0118 0119 m_model->insertRows(row, 1, QModelIndex()); 0120 0121 QStringList pieces = line.split(',', Qt::SkipEmptyParts); 0122 m_model->setData(m_model->index(row, 0, QModelIndex()), pieces.value(0)); 0123 m_model->setData(m_model->index(row, 1, QModelIndex()), pieces.value(1)); 0124 m_model->setData(m_model->index(row, 2, QModelIndex()), pieces.value(2)); 0125 ++row; 0126 } 0127 } while (!line.isEmpty()); 0128 0129 file.close(); 0130 } 0131 } 0132 } 0133