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

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 #include "AdjustedCartesianAxis.h"
0011 
0012 #include <KChartCartesianCoordinatePlane>
0013 #include <KChartChart>
0014 #include <KChartGridAttributes>
0015 #include <KChartLegend>
0016 #include <KChartLineDiagram>
0017 #include <KChartTextAttributes>
0018 
0019 #include <QDebug>
0020 #include <QLinearGradient>
0021 #include <QTextDocument>
0022 
0023 MainWindow::MainWindow( QWidget* parent ) :
0024     QWidget( parent )
0025 {
0026     setupUi( this );
0027 
0028     QHBoxLayout* chartLayout = new QHBoxLayout( m_chartFrame );
0029     m_chart = new KChart::Chart;
0030     chartLayout->addWidget( m_chart );
0031 
0032     m_model.loadFromCSV( ":/data" );
0033 
0034     // Set up the diagram
0035     m_lines = new KChart::LineDiagram();
0036     m_lines->setModel( &m_model );
0037 
0038     m_xAxis = new KChart::CartesianAxis( m_lines );
0039     KChart::TextAttributes ta( m_xAxis->textAttributes() );
0040 
0041     AdjustedCartesianAxis *yAxis = new AdjustedCartesianAxis( m_lines );
0042     yAxis->setBounds( 3, 6 );
0043     m_xAxis->setPosition( KChart::CartesianAxis::Bottom );
0044     yAxis->setPosition( KChart::CartesianAxis::Left );
0045 
0046 // set the following to 0, to see the default Abscissa labels (== X headers, as read from the data file)
0047 #if 1
0048     QStringList daysOfWeek;
0049     daysOfWeek << "Monday" << "Tuesday" << "Wednesday" << "Thursday" << "Friday" << "Saturday" << "Sunday";
0050     m_xAxis->setLabels( daysOfWeek );
0051 
0052     //QStringList shortDays;
0053     //shortDays << "Mon" << "Tue" << "Wed" << "Thu" << "Fri" << "Sat" << "Sun";
0054     //m_xAxis->setShortLabels( shortDays );
0055 #endif
0056 
0057 // Use HTML for drawing the text in the axis labels.
0058 #if 0
0059     QStringList htmlStyles;
0060     htmlStyles << "<b>Bold</b>" << "<i>Italic</i>" << "<u>Underline</u>" << "<font color='red'>Red</font>";
0061     m_xAxis->setLabels( htmlStyles );
0062     ta.setTextDocument(new QTextDocument);
0063 #endif
0064 
0065     // Illustration of custom ticks
0066     QList<qreal> ticks;
0067     ticks.append( 0.5 );
0068     ticks.append( 3.5 );
0069     ticks.append( 4.2 );
0070     ticks.append( 6.5 );
0071     m_xAxis->setCustomTicks(ticks);
0072     yAxis->setCustomTicks(ticks);
0073 
0074     // rotate abscissa labels by -60 degrees:
0075     ta.setRotation( -60 );
0076 
0077     m_xAxis->setTextAttributes( ta );
0078     m_lines->addAxis( m_xAxis );
0079     m_lines->addAxis( yAxis );
0080     m_chart->coordinatePlane()->replaceDiagram( m_lines );
0081     // Set up the legend
0082     m_xAxis->setCustomTickLength( 11 );
0083     yAxis->setCustomTickLength( 11 );
0084     m_legend = new KChart::Legend( m_lines, m_chart );
0085     m_legend->setPosition( KChart::Position::East );
0086     m_legend->setAlignment( Qt::AlignTop );
0087     m_chart->addLegend( m_legend );
0088 
0089     connect( m_annotations, SIGNAL(toggled(bool)), SLOT(annotationsToggled(bool)) );
0090     connect( m_linesOnAnnotations, SIGNAL(toggled(bool)), SLOT(gridLinesOnAnnotationsToggled(bool)) );
0091 }
0092 
0093 void MainWindow::annotationsToggled( bool showAnnotations )
0094 {
0095     QMap< qreal, QString > annotations;
0096     if ( showAnnotations ) {
0097         // set custom axis labels at custom positions
0098         annotations[ 0.5 ] = "Left";
0099         annotations[ 3.5 ] = "Center";
0100         annotations[ 4.2 ] = "Off Center";
0101         annotations[ 6.5 ] = "Right";
0102     }
0103     m_xAxis->setAnnotations( annotations );
0104     m_chart->update();
0105 }
0106 
0107 void MainWindow::gridLinesOnAnnotationsToggled( bool onAnnotations )
0108 {
0109         // Draw grid lines where the annotations are
0110     KChart::CartesianCoordinatePlane* plane =
0111         static_cast< KChart::CartesianCoordinatePlane* >( m_chart->coordinatePlane() );
0112     KChart::GridAttributes ga = plane->gridAttributes( Qt::Horizontal );
0113     ga.setLinesOnAnnotations( onAnnotations );
0114     plane->setGridAttributes( Qt::Horizontal, ga );
0115     m_chart->update();
0116 }