File: mainwindow.cpp

package info (click to toggle)
kdiagram 3.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,732 kB
  • sloc: cpp: 52,386; makefile: 7; sh: 2
file content (116 lines) | stat: -rw-r--r-- 3,760 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
 * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
 *
 * This file is part of the KD Chart library.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "mainwindow.h"
#include "AdjustedCartesianAxis.h"

#include <KChartCartesianCoordinatePlane>
#include <KChartChart>
#include <KChartGridAttributes>
#include <KChartLegend>
#include <KChartLineDiagram>
#include <KChartTextAttributes>

#include <QDebug>
#include <QLinearGradient>
#include <QTextDocument>

MainWindow::MainWindow( QWidget* parent ) :
    QWidget( parent )
{
    setupUi( this );

    QHBoxLayout* chartLayout = new QHBoxLayout( m_chartFrame );
    m_chart = new KChart::Chart;
    chartLayout->addWidget( m_chart );

    m_model.loadFromCSV( ":/data" );

    // Set up the diagram
    m_lines = new KChart::LineDiagram();
    m_lines->setModel( &m_model );

    m_xAxis = new KChart::CartesianAxis( m_lines );
    KChart::TextAttributes ta( m_xAxis->textAttributes() );

    AdjustedCartesianAxis *yAxis = new AdjustedCartesianAxis( m_lines );
    yAxis->setBounds( 3, 6 );
    m_xAxis->setPosition( KChart::CartesianAxis::Bottom );
    yAxis->setPosition( KChart::CartesianAxis::Left );

// set the following to 0, to see the default Abscissa labels (== X headers, as read from the data file)
#if 1
    QStringList daysOfWeek;
    daysOfWeek << "Monday" << "Tuesday" << "Wednesday" << "Thursday" << "Friday" << "Saturday" << "Sunday";
    m_xAxis->setLabels( daysOfWeek );

    //QStringList shortDays;
    //shortDays << "Mon" << "Tue" << "Wed" << "Thu" << "Fri" << "Sat" << "Sun";
    //m_xAxis->setShortLabels( shortDays );
#endif

// Use HTML for drawing the text in the axis labels.
#if 0
    QStringList htmlStyles;
    htmlStyles << "<b>Bold</b>" << "<i>Italic</i>" << "<u>Underline</u>" << "<font color='red'>Red</font>";
    m_xAxis->setLabels( htmlStyles );
    ta.setTextDocument(new QTextDocument);
#endif

    // Illustration of custom ticks
    QList<qreal> ticks;
    ticks.append( 0.5 );
    ticks.append( 3.5 );
    ticks.append( 4.2 );
    ticks.append( 6.5 );
    m_xAxis->setCustomTicks(ticks);
    yAxis->setCustomTicks(ticks);

    // rotate abscissa labels by -60 degrees:
    ta.setRotation( -60 );

    m_xAxis->setTextAttributes( ta );
    m_lines->addAxis( m_xAxis );
    m_lines->addAxis( yAxis );
    m_chart->coordinatePlane()->replaceDiagram( m_lines );
    // Set up the legend
    m_xAxis->setCustomTickLength( 11 );
    yAxis->setCustomTickLength( 11 );
    m_legend = new KChart::Legend( m_lines, m_chart );
    m_legend->setPosition( KChart::Position::East );
    m_legend->setAlignment( Qt::AlignTop );
    m_chart->addLegend( m_legend );

    connect( m_annotations, SIGNAL(toggled(bool)), SLOT(annotationsToggled(bool)) );
    connect( m_linesOnAnnotations, SIGNAL(toggled(bool)), SLOT(gridLinesOnAnnotationsToggled(bool)) );
}

void MainWindow::annotationsToggled( bool showAnnotations )
{
    QMap< qreal, QString > annotations;
    if ( showAnnotations ) {
        // set custom axis labels at custom positions
        annotations[ 0.5 ] = "Left";
        annotations[ 3.5 ] = "Center";
        annotations[ 4.2 ] = "Off Center";
        annotations[ 6.5 ] = "Right";
    }
    m_xAxis->setAnnotations( annotations );
    m_chart->update();
}

void MainWindow::gridLinesOnAnnotationsToggled( bool onAnnotations )
{
        // Draw grid lines where the annotations are
    KChart::CartesianCoordinatePlane* plane =
        static_cast< KChart::CartesianCoordinatePlane* >( m_chart->coordinatePlane() );
    KChart::GridAttributes ga = plane->gridAttributes( Qt::Horizontal );
    ga.setLinesOnAnnotations( onAnnotations );
    plane->setGridAttributes( Qt::Horizontal, ga );
    m_chart->update();
}