File indexing completed on 2024-06-16 04:08:55

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 <KChartChart>
0012 #include <KChartBarDiagram>
0013 #include <KChartTextAttributes>
0014 #include <KChartRulerAttributes>
0015 #include <KChartFrameAttributes>
0016 
0017 using namespace KChart;
0018 
0019 MainWindow::MainWindow( QWidget* parent ) :
0020     QWidget( parent )
0021 {
0022     setupUi( this );
0023 
0024     QHBoxLayout* chartLayout = new QHBoxLayout( chartFrame );
0025     m_chart = new Chart();
0026     m_chart->setGlobalLeading( 10, 10, 10, 10 );
0027     chartLayout->addWidget( m_chart );
0028     hSBar->setVisible( false );
0029     vSBar->setVisible( false );
0030 
0031     m_model.loadFromCSV( ":/data" );
0032 
0033     // Set up the diagram
0034     m_lines = new BarDiagram();
0035     m_lines->setModel( &m_model );
0036 
0037     // create and position axis
0038     CartesianAxis *topAxis = new CartesianAxis( m_lines );
0039     CartesianAxis *leftAxis = new CartesianAxis( m_lines );
0040     RulerAttributes rulerAttr = topAxis->rulerAttributes();
0041     rulerAttr.setTickMarkPen( 0.9999999, QPen( Qt::red ) );
0042     rulerAttr.setTickMarkPen( 2.0, QPen( Qt::green ) );
0043     rulerAttr.setTickMarkPen( 3.0, QPen( Qt::blue ) );
0044     rulerAttr.setShowMinorTickMarks(true);
0045     //rulerAttr.setShowMajorTickMarks(false);
0046     topAxis->setRulerAttributes( rulerAttr );
0047     CartesianAxis *rightAxis = new CartesianAxis( m_lines );
0048     CartesianAxis *bottomAxis = new CartesianAxis( m_lines );
0049     topAxis->setPosition( CartesianAxis::Top );
0050     leftAxis->setPosition( CartesianAxis::Left );
0051     rightAxis->setPosition( CartesianAxis::Right );
0052     bottomAxis->setPosition( CartesianAxis::Bottom );
0053 
0054 // set the margin that should be used between the displayed labels and the ticks to zero
0055 #if 0
0056     RulerAttributes ra = bottomAxis->rulerAttributes();
0057     ra.setLabelMargin( 0 );
0058     bottomAxis->setRulerAttributes( ra );
0059 #endif
0060 
0061 // show a red frame around the bottom axis
0062 #if 0
0063     FrameAttributes fa( bottomAxis->frameAttributes() );
0064     fa.setPen( QPen( QBrush( QColor( "#ff0000" ) ), 1.0 ) );
0065     fa.setVisible( true );
0066     bottomAxis->setFrameAttributes( fa );
0067 #endif
0068     
0069     // set axis titles
0070     topAxis->setTitleText( "Abscissa Top configured size and color" );
0071     leftAxis->setTitleText( "left Ordinate: fonts configured" );
0072     rightAxis->setTitleText( "right Ordinate: default settings" );
0073     bottomAxis->setTitleText( "Abscissa Bottom" );
0074 
0075     // configure titles text attributes
0076     TextAttributes taTop( topAxis->titleTextAttributes() );
0077     taTop.setPen( QPen( Qt::red ) );
0078     topAxis->setTitleTextAttributes ( taTop );
0079 
0080     TextAttributes taLeft( leftAxis->titleTextAttributes() );
0081     taLeft.setRotation( 180 );
0082     Measure me( taLeft.fontSize() );
0083     me.setValue( me.value() * 0.8 );
0084     taLeft.setFontSize( me );
0085 
0086 // Set the following to 1, to hide the left axis title
0087 //  - no matter if a title text is set or not
0088 #if 0
0089     taLeft.setVisible( false );
0090 #endif
0091     leftAxis->setTitleTextAttributes( taLeft );
0092 
0093     TextAttributes taBottom( bottomAxis->titleTextAttributes () );
0094     taBottom.setPen( QPen( Qt::blue ) );
0095     bottomAxis->setTitleTextAttributes( taBottom );
0096 
0097     // configure labels text attributes
0098     TextAttributes taLabels( topAxis->textAttributes() );
0099     taLabels.setPen( QPen( Qt::darkGreen ) );
0100     taLabels.setRotation( 90 );
0101     topAxis->setTextAttributes( taLabels );
0102     leftAxis->setTextAttributes( taLabels );
0103     bottomAxis->setTextAttributes( taLabels );
0104 
0105 
0106 // Set the following to 0, to see the default Abscissa labels
0107 // (== X headers, as read from the data file)
0108 #if 1
0109     // configure labels and their shortened versions
0110     QStringList daysOfWeek;
0111     daysOfWeek << "M O N D A Y" << "Tuesday" << "Wednesday"
0112                << "Thursday" << "Friday" ;
0113     topAxis->setLabels( daysOfWeek );
0114 
0115     QStringList shortDays;
0116     shortDays << "MON" << "Tue" << "Wed"
0117               << "Thu" << "Fri";
0118     topAxis->setShortLabels( shortDays );
0119 
0120     QStringList bottomLabels;
0121     bottomLabels << "Team A" << "Team B" << "Team C";
0122     bottomAxis->setLabels( bottomLabels );
0123 
0124     QStringList shortBottomLabels;
0125     shortBottomLabels << "A" << "B";
0126     bottomAxis->setShortLabels( shortBottomLabels );
0127 #endif
0128 
0129     // add axis
0130     m_lines->addAxis( topAxis );
0131     m_lines->addAxis( leftAxis );
0132     m_lines->addAxis( rightAxis );
0133     m_lines->addAxis( bottomAxis );
0134 
0135     // assign diagram to chart view
0136     m_chart->coordinatePlane()->replaceDiagram( m_lines );
0137 }