File indexing completed on 2024-11-24 03:57:52

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 <QApplication>
0010 #include <KChartChart>
0011 #include <KChartPlotter>
0012 #include <KChartLineDiagram>
0013 #include <KChartCartesianAxis>
0014 #include <KChartCartesianCoordinatePlane>
0015 #include <KChartLegend>
0016 #include <QStandardItemModel>
0017 #include <KChartDataValueAttributes>
0018 #include <KChartTextAttributes>
0019 #include <KChartMarkerAttributes>
0020 
0021 #include <cmath>
0022 
0023 #define PI 3.141592653589793
0024 
0025 
0026 // enable the next line to see points instead of lines:
0027 // #define PLOTTED_POINTS
0028 
0029 
0030 int main( int argc, char** argv )
0031 {
0032     QApplication app( argc, argv );
0033 
0034 #if defined PLOTTED_POINTS
0035     const int points = 60;
0036 #else
0037     const int points = 1000;
0038 #endif
0039     const qreal xMin = -2 * PI;
0040     const qreal xMax = 2 * PI;
0041     const qreal step = ( xMax - xMin ) / ( points - 1 );
0042 
0043     QStandardItemModel model( points, 6 );
0044 
0045     double x = xMin;
0046     for ( int n = 0; n < points; ++n, x += step) {
0047         QModelIndex index = model.index( n, 0 );
0048         model.setData( index, x );
0049         index = model.index( n, 1 );
0050         model.setData( index, sin( x ) * 100.0 );
0051 
0052         index = model.index( n, 2 );
0053         model.setData( index, x );
0054         index = model.index( n, 3 );
0055         model.setData( index, x );
0056 
0057         index = model.index( n, 4 );
0058         model.setData( index, x );
0059         index = model.index( n, 5 );
0060         model.setData( index, x * x * x );
0061     }
0062 
0063     model.setHeaderData( 0, Qt::Horizontal, QString::fromLatin1( "100 * sin(x)" ) );
0064     model.setHeaderData( 2, Qt::Horizontal, QString::fromLatin1( "x" ) );
0065     model.setHeaderData( 4, Qt::Horizontal, QString::fromLatin1( "x^3" ) );
0066 
0067     KChart::Chart* chart = new KChart::Chart();
0068 
0069     KChart::AbstractCartesianDiagram* diagram = new KChart::Plotter;
0070     diagram->setModel( &model );
0071     chart->coordinatePlane()->replaceDiagram( diagram );
0072 
0073 #if defined PLOTTED_POINTS
0074     diagram->setPen( QPen(Qt::NoPen) );
0075     const int colCount = model.columnCount( diagram->rootIndex() );
0076     for ( int iColumn = 0; iColumn<colCount; ++iColumn ) {
0077         const QPen markerPen( diagram->brush( iColumn ).color() );
0078         KChart::DataValueAttributes dva( diagram->dataValueAttributes( iColumn ) );
0079         KChart::TextAttributes ta( dva.textAttributes() );
0080         KChart::MarkerAttributes ma( dva.markerAttributes() );
0081         ma.setPen( markerPen );
0082         ma.setMarkerStyle( KChart::MarkerAttributes::MarkerCircle );
0083         ma.setMarkerSize( QSize( 3,3 ) );
0084 
0085         dva.setVisible( true );
0086         ta.setVisible( false );
0087         ma.setVisible( true );
0088         dva.setTextAttributes(   ta );
0089         dva.setMarkerAttributes( ma );
0090         diagram->setDataValueAttributes( iColumn, dva );
0091     }
0092 #endif
0093 
0094     KChart::CartesianAxis* xAxis = new KChart::CartesianAxis( diagram );
0095     KChart::CartesianAxis* yAxis = new KChart::CartesianAxis( diagram );
0096     xAxis->setPosition( KChart::CartesianAxis::Bottom );
0097     yAxis->setPosition( KChart::CartesianAxis::Left );
0098     diagram->addAxis( xAxis );
0099     diagram->addAxis( yAxis );
0100 
0101     KChart::Legend* legend = new KChart::Legend( diagram, chart );
0102     legend->setPosition( KChart::Position::East );
0103     legend->setAlignment( Qt::AlignCenter );
0104     legend->setTitleText( "Legend" );
0105     chart->addLegend( legend );
0106 
0107     chart->show();
0108 
0109     return app.exec();
0110 }