File indexing completed on 2024-06-16 04:09:02

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 <math.h>
0010 
0011 #include <QStandardItemModel>
0012 #include <KChartChart>
0013 #include <KChartLineDiagram>
0014 #include <KChartDataValueAttributes>
0015 #include <KChartFrameAttributes>
0016 
0017 #include <KChartPlotter>
0018 #include <KChartCartesianAxis>
0019 #include <KChartCartesianCoordinatePlane>
0020 #include <KChartLegend>
0021 
0022 #include <QApplication>
0023 
0024 using namespace KChart;
0025 
0026 int main( int argc, char** argv )
0027 {
0028     QApplication app( argc, argv );
0029 
0030     const int points = 420;
0031     const double xMin = 0;
0032     const double xMax = 20;
0033     const double step = ( xMax - xMin ) / ( points - 1 );
0034 
0035     QStandardItemModel model( points, 6 );
0036 
0037     double x = xMin;
0038     for ( int n = 0; n < points; ++n, x += step) {
0039         QModelIndex index = model.index( n, 0 );
0040         model.setData( index, x );
0041         index = model.index( n, 1 );
0042         model.setData( index, sin( x ) * 100.0 );
0043 
0044         index = model.index( n, 2 );
0045         model.setData( index, x );
0046         index = model.index( n, 3 );
0047         model.setData( index, x );
0048 
0049         index = model.index( n, 4 );
0050         model.setData( index, x );
0051         index = model.index( n, 5 );
0052         model.setData( index, x * x * x );
0053     }
0054 
0055     model.setHeaderData( 0, Qt::Horizontal, QString::fromLatin1( "100 * sin(x)" ) );
0056     model.setHeaderData( 2, Qt::Horizontal, QString::fromLatin1( "x" ) );
0057     model.setHeaderData( 4, Qt::Horizontal, QString::fromLatin1( "x^3" ) );
0058 
0059     KChart::Chart* chart = new KChart::Chart();
0060 
0061     KChart::AbstractCartesianDiagram* diagram = new KChart::Plotter;
0062     diagram->setModel( &model );
0063     chart->coordinatePlane()->replaceDiagram( diagram );
0064 
0065     KChart::CartesianAxis* xAxis = new KChart::CartesianAxis( diagram );
0066     KChart::CartesianAxis* yAxis = new KChart::CartesianAxis( diagram );
0067     xAxis->setPosition( KChart::CartesianAxis::Bottom );
0068     yAxis->setPosition( KChart::CartesianAxis::Left );
0069     diagram->addAxis( xAxis );
0070     diagram->addAxis( yAxis );
0071 
0072     KChart::Legend* legend = new KChart::Legend( diagram, chart );
0073     KChart::FrameAttributes legendAtt = legend->frameAttributes();
0074     legendAtt.setCornerRadius( 9 );
0075     legend->setFrameAttributes( legendAtt );
0076     legend->setPosition( KChart::Position::East );
0077     legend->setAlignment( Qt::AlignCenter );
0078     legend->setTitleText( "Legend" );
0079     chart->addLegend( legend );
0080 
0081     KChart::CartesianCoordinatePlane* cart_plane = dynamic_cast<KChart::CartesianCoordinatePlane*>(chart->coordinatePlane());
0082     Q_ASSERT(cart_plane);
0083 
0084     cart_plane->setAxesCalcModeX(KChart::AbstractCoordinatePlane::Logarithmic);
0085     cart_plane->setAxesCalcModeY(KChart::AbstractCoordinatePlane::Logarithmic);
0086 
0087     // Set the vertical range from 15 to 75 - with a logarithmic axis I actually get 1 to 100
0088     //cart_plane->setVerticalRange(QPair<qreal,qreal>( 0.005, 1000 ) );
0089 
0090     // Set the horizontal range from 1 to 9 - with a linear axis this works OK
0091     cart_plane->setHorizontalRange(QPair<qreal,qreal>( 0.001, 100 ) );
0092 
0093     chart->show();
0094 
0095     int ret = app.exec();
0096 
0097     delete chart;
0098 
0099     return ret;
0100 }