1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
/**
* SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
*
* This file is part of the KD Chart library.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "mainwindow.h"
#include <QStandardItemModel>
#include <QFileDialog>
#include <KChartChart>
#include <KChartDataValueAttributes>
#include <KChartLegend>
#include <KChartLineDiagram>
#include <KChartTextAttributes>
#include <KChartThreeDLineAttributes>
using namespace KChart;
MainWindow::MainWindow( QWidget* parent ) :
QWidget( parent )
{
setupUi( this );
QHBoxLayout* chartLayout = new QHBoxLayout( chartFrame );
m_chart = new Chart();
m_chart->setGlobalLeading( 20, 20, 20, 20 );
chartLayout->addWidget( m_chart );
// Initialize the model, and fill it with data
const int rowCount = 8;
const int columnCount = 3;
m_model = new QStandardItemModel(rowCount, columnCount, this);
m_model->setHeaderData(0, Qt::Horizontal, tr("Product A"));
m_model->setHeaderData(1, Qt::Horizontal, tr("Product B"));
m_model->setHeaderData(2, Qt::Horizontal, tr("Product C"));
openFile(":/Charts/qtdata.cht");
// Set up the diagram
m_lines = new LineDiagram();
// Register the data model at the diagram
m_lines->setModel( m_model );
// Add axes to the diagram
CartesianAxis *xAxis = new CartesianAxis( m_lines );
CartesianAxis *yAxis = new CartesianAxis ( m_lines );
xAxis->setPosition ( KChart::CartesianAxis::Bottom );
yAxis->setPosition ( KChart::CartesianAxis::Left );
m_lines->addAxis( xAxis );
m_lines->addAxis( yAxis );
// Make the lines thicker
for ( int iColumn = 0; iColumn < columnCount; ++iColumn ) {
QPen linePen( m_lines->pen( iColumn ) );
linePen.setWidth( 3 );
m_lines->setPen( iColumn, linePen );
}
// Register the diagram at the coordinate plane
m_chart->coordinatePlane()->replaceDiagram( m_lines );
// Add a legend
Legend* legend = new Legend( m_lines, m_chart );
legend->setPosition( Position::South );
legend->setAlignment( Qt::AlignCenter );
legend->setShowLines( true );
legend->setTitleText("");
legend->setOrientation( Qt::Horizontal );
legend->addDiagram( m_lines );
m_chart->addLegend( legend );
}
void MainWindow::on_showDataset1CB_toggled( bool checked )
{
setHidden( 0, ! checked );
}
void MainWindow::on_showDataset2CB_toggled( bool checked )
{
setHidden( 1, ! checked );
}
void MainWindow::on_showDataset3CB_toggled( bool checked )
{
setHidden( 2, ! checked );
}
void MainWindow::setHidden( int dataset, bool hidden )
{
m_lines->setHidden( dataset, hidden );
m_chart->update();
}
void MainWindow::openFile(const QString &path)
{
QString fileName;
if (path.isNull())
fileName = QFileDialog::getOpenFileName(this, tr("Choose a data file"),
"", "*.cht");
else
fileName = path;
if (!fileName.isEmpty()) {
QFile file(fileName);
if (file.open(QFile::ReadOnly | QFile::Text)) {
QTextStream stream(&file);
QString line;
m_model->removeRows(0, m_model->rowCount(QModelIndex()), QModelIndex());
int row = 0;
do {
line = stream.readLine();
if (!line.isEmpty()) {
m_model->insertRows(row, 1, QModelIndex());
QStringList pieces = line.split(',', Qt::SkipEmptyParts);
m_model->setData(m_model->index(row, 0, QModelIndex()), pieces.value(0));
m_model->setData(m_model->index(row, 1, QModelIndex()), pieces.value(1));
m_model->setData(m_model->index(row, 2, QModelIndex()), pieces.value(2));
++row;
}
} while (!line.isEmpty());
file.close();
}
}
}
|