summaryrefslogtreecommitdiffstats
path: root/kmymoney2/reports/kreportchartview.cpp
blob: e9c9060067141c570ebd8da3bf26151c316d2af0 (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/***************************************************************************
                          kreportchartview.cpp
                             -------------------
    begin                : Sun Aug 14 2005
    copyright            : (C) 2004-2005 by Ace Jones
    email                : <ace.j@hotpop.com>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif
#ifdef HAVE_KDCHART

// ----------------------------------------------------------------------------
// QT Includes

// ----------------------------------------------------------------------------
// KDE Includes

// ----------------------------------------------------------------------------
// Project Includes

#include "kreportchartview.h"
#include <KDChartDataRegion.h>

using namespace reports;

KReportChartView::KReportChartView( TQWidget* tqparent, const char* name ): KDChartWidget(tqparent,name)
{
    // ********************************************************************
    // Set KMyMoney's Chart Parameter Defaults
    // ********************************************************************
    this->setPaletteBackgroundColor( TQt::white );

    KDChartParams* _params = new KDChartParams();
    _params->setChartType( KDChartParams::Line );
    _params->setAxisLabelStringParams( KDChartAxisParams::AxisPosBottom,&m_abscissaNames,0);
    _params->setDataSubduedColors();

    /**
    // use line marker, but only circles.
    _params->setLineMarker( true );
    _params->setLineMarkerSize( TQSize(8,8) );
    _params->setLineMarkerStyle( 0, KDChartParams::LineMarkerCircle );
    _params->setLineMarkerStyle( 1, KDChartParams::LineMarkerCircle );
    _params->setLineMarkerStyle( 2, KDChartParams::LineMarkerCircle );
    **/

    // initialize parameters
    this->setParams(_params);

    // initialize data
    KDChartTableData* _data = new KDChartTableData();
    this->setData(_data);

    // ********************************************************************
    // Some Examplatory Chart Table Data
    // ********************************************************************

    /**
    // 1st series
    this->data()->setCell( 0, 0,    17.5   );
    this->data()->setCell( 0, 1,   125     );  // highest value
    this->data()->setCell( 0, 2,     6.67  );  // lowest value
    this->data()->setCell( 0, 3,    33.333 );
    this->data()->setCell( 0, 4,    30     );
    // 2nd series
    this->data()->setCell( 1, 0,    40     );
    this->data()->setCell( 1, 1,    40     );
    this->data()->setCell( 1, 2,    45.5   );
    this->data()->setCell( 1, 3,    45     );
    this->data()->setCell( 1, 4,    35     );
    // 3rd series
    this->data()->setCell( 2, 0,    25     );
    // missing value: setCell( 2, 1,   25 );
    this->data()->setCell( 2, 2,    30     );
    this->data()->setCell( 2, 3,    45     );
    this->data()->setCell( 2, 4,    40     );
    **/

    // ********************************************************************
    // Tooltip Setup
    // ********************************************************************
    label = new TQLabel( this );
    label->hide();
    // mouse tracking on will force the mouseMoveEvent() method to be called from TQt
    label->setMouseTracking( true );
    label->setFrameStyle( TQFrame::PopupPanel | TQFrame::Raised );
    label->tqsetAlignment( AlignRight );
    label->setAutoResize( true );
}

/**
 * This function implements mouseMoveEvents
  */
void KReportChartView::mouseMoveEvent( TQMouseEvent* event )
{
    TQPoint translate, pos; // some movement helpers
    uint dataset;          // the current dataset (eg. category)
    uint datasets;         // the total number of datasets
    double value;          // the value of the region
    double pivot_sum;      // the sum over all categories in the current pivot point

    // the data region in which the cursor was last time
    static uint previous;

    // if mouse tracking is disabled, don't show any tooltip
    if ( !this->hasMouseTracking() )
        return ;

    // find the data region below the current mouse location
    // ..by going through every data region and checking whether it
    //   contains the mouse pointer
    KDChartDataRegion* current = 0;
    TQPtrListIterator < KDChartDataRegion > it( *(this->dataRegions()) );
    while ( ( current = it.current() ) ) {
        ++it;
        if ( current->tqcontains( event->pos() ) )
        {
            // we found the data region that contains the mouse
            value = this->data()->cellVal(current->row, current->col).toDouble();

            // get the dataset that the region corresponds to
            if ( this->getAccountSeries() )
            {
              dataset = current->row;
              datasets= this->data()->rows();
              pivot_sum = value * 100.0 / this->data()->colSum(current->col);
            }
            else
            {
              dataset = current->col;
              datasets= this->data()->cols();
              pivot_sum = value * 100.0 / this->data()->rowSum(current->row);
            }

            // if we entered a new data region or the label was invisible
            if ( !label->isVisible() || previous != dataset )
            {
              // if there is more than one dataset, show percentage
              if(datasets > 1)
              {
                // set the tooltip text
                label->setText(TQString("<h2>%1</h2><strong>%2</strong><br>(%3\%)")
                    .tqarg(this->params()->legendText( dataset ))
                    .tqarg(value, 0, 'f', 2)
                    .tqarg(pivot_sum, 0, 'f', 2)
                    );
              }
              else // if there is only one dataset, don't show percentage
              {
                // set the tooltip text
                label->setText(TQString("<h2>%1</h2><strong>%2</strong>")
                    .tqarg(this->params()->legendText( dataset ))
                    .tqarg(value, 0, 'f', 2)
                    );
              }

              previous = dataset;
            }

            translate.setX( -10 - label->width());
            translate.setY( 20);

            // display the label near the cursor
            pos = event->pos() + translate;

            // but don't let the label leave the visible area
            if( pos.x() < 0 )
                pos.setX(0);
            if( pos.y() < 0 )
                pos.setY(0);
            if( pos.x() + label->width() > this->width() )
                pos.setX( this->width() - label->width() );
            if( pos.y() + label->height() > this->height() )
                pos.setY( this->height() - label->height() );

            // now set the label position and show the label
            label->move( pos );
            label->show();

            // In a more abstract class, we would emit a dateMouseMove event:
            //emit this->dataMouseMove( event->pos(), current->row, current->col );

            return ;
        }
    }
    // if the cursor was not found in any data region, hide the label
    label->hide();
}

void KReportChartView::setProperty(int row, int col, int id)
{
#ifdef HAVE_KDCHART_SETPROP
  this->data()->setProp(row, col, id);
#else
  this->data()->cell(row, col).setPropertySet(id);
#endif
}

#endif