1 /**
2  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
3  *
4  * This file is part of the KD Chart library.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "mainwindow.h"
21 
22 #include <KChartChart>
23 #include <KChartAbstractCoordinatePlane>
24 #include <KChartLineDiagram>
25 #include <KChartLegend>
26 
27 #include <QDebug>
28 #include <QPainter>
29 
30 using namespace KChart;
31 
MainWindow(QWidget * parent)32 MainWindow::MainWindow( QWidget* parent ) :
33     QWidget( parent )
34 {
35     setupUi( this );
36 
37     QHBoxLayout* chartLayout = new QHBoxLayout( chartFrame );
38     m_chart = new Chart();
39     m_chart->setGlobalLeadingTop( 10 );
40     m_chart->setGlobalLeadingRight( 10 );
41     chartLayout->addWidget( m_chart );
42     hSBar->setVisible( false );
43     vSBar->setVisible( false );
44 
45     m_model.loadFromCSV( ":/data" );
46 
47     // Set up the diagram
48     m_lines = new LineDiagram();
49     m_lines->setModel( &m_model );
50 
51     CartesianAxis *xAxis = new CartesianAxis( m_lines );
52     CartesianAxis *yAxis = new CartesianAxis ( m_lines );
53     xAxis->setPosition ( KChart::CartesianAxis::Bottom );
54     yAxis->setPosition ( KChart::CartesianAxis::Left );
55 
56     xAxis->setTitleText ( "Abscissa axis at the bottom" );
57     yAxis->setTitleText ( "Ordinate axis at the left side" );
58 
59     m_lines->addAxis( xAxis );
60     m_lines->addAxis( yAxis );
61     m_chart->coordinatePlane()->replaceDiagram( m_lines );
62 
63     connect( m_chart, SIGNAL(propertiesChanged()), SLOT(applyNewZoomParameters()) );
64 
65     // Set up the legend
66     m_legend = new Legend( m_lines, m_chart );
67     m_chart->addLegend( m_legend );
68     m_legend->setPosition( KChartEnums::PositionEast );
69     m_legend->setAlignment( Qt::AlignTop );
70 }
71 
72 
on_zoomFactorSB_valueChanged(double factor)73 void MainWindow::on_zoomFactorSB_valueChanged( double factor )
74 {
75     if ( factor > 1 ) {
76         hSBar->setVisible( true );
77         vSBar->setVisible( true );
78     } else {
79         hSBar->setValue( 500 );
80         vSBar->setValue( 500 );
81         hSBar->setVisible( false );
82         vSBar->setVisible( false );
83     }
84     m_chart->coordinatePlane()->setZoomFactorX( factor );
85     m_chart->coordinatePlane()->setZoomFactorY( factor );
86     m_chart->update();
87 }
88 
on_adjustGridCB_toggled(bool checked)89 void MainWindow::on_adjustGridCB_toggled( bool checked )
90 {
91     static_cast <CartesianCoordinatePlane*>( m_chart->coordinatePlane() )
92             ->setAutoAdjustGridToZoom( checked );
93     m_chart->update();
94 }
95 
on_rubberBandZoomCB_toggled(bool checked)96 void MainWindow::on_rubberBandZoomCB_toggled( bool checked )
97 {
98     m_chart->coordinatePlane()->setRubberBandZoomingEnabled( checked );
99 }
100 
on_hSBar_valueChanged(int hPos)101 void MainWindow::on_hSBar_valueChanged( int hPos )
102 {
103     m_chart->coordinatePlane()->setZoomCenter( QPointF(hPos/1000.0, vSBar->value()/1000.0) );
104     m_chart->update();
105 }
106 
on_vSBar_valueChanged(int vPos)107 void MainWindow::on_vSBar_valueChanged( int vPos )
108 {
109     m_chart->coordinatePlane()->setZoomCenter( QPointF( hSBar->value()/1000.0, vPos/1000.0) );
110     m_chart->update();
111 }
112 
applyNewZoomParameters()113 void MainWindow::applyNewZoomParameters()
114 {
115     hSBar->blockSignals( true );
116     vSBar->blockSignals( true );
117 
118     hSBar->setValue( qRound( m_chart->coordinatePlane()->zoomCenter().x() * 1000 ) );
119     vSBar->setValue( qRound( m_chart->coordinatePlane()->zoomCenter().y() * 1000 ) );
120     zoomFactorSB->setValue( m_chart->coordinatePlane()->zoomFactorX() );
121 
122     hSBar->blockSignals( false );
123     vSBar->blockSignals( false );
124 }
125