~registry/codeblocks/trunk

« back to all changes in this revision

Viewing changes to src/plugins/contrib/wxContribItems/wxchart/src/bar3dchartpoints.cpp

  • Committer: mortenmacfly
  • Date: 2012-02-22 14:40:26 UTC
  • Revision ID: svn-v4:98b59c6a-2706-0410-b7d6-d2fa1a1880c9:trunk:7835
* merged wxSmith branch into trunk
* this brings tons of new wxSmith items, including KWIC, wxImagePanel, wxGridBagSizer and more
* based on work of the community, mainly cryogen
* for more information, see changelog of wxSmith branch
* also, re-factoring of contributed wxWidgets items for wxSmith
* PLEASE DO A CLEAN CHECKOUT AND RE-BUILD EVERYTHING FROM SCRATCH!

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/////////////////////////////////////////////////////////////////////////////
 
2
// Name:        bar3dchartpoints.cpp
 
3
// Purpose:     wxChart
 
4
// Author:      Paolo Gava
 
5
// Modified by:
 
6
// Created:
 
7
// Copyright:   (C) 2006, Paolo Gava
 
8
// RCS-ID:      $Id: bar3dchartpoints.cpp 7551 2011-11-01 18:13:27Z jenslody $
 
9
// Licence:     wxWindows licence
 
10
/////////////////////////////////////////////////////////////////////////////
 
11
 
 
12
//----------------------------------------------------------------------------
 
13
// Headers
 
14
//----------------------------------------------------------------------------
 
15
 
 
16
// c++
 
17
#include <cmath>
 
18
 
 
19
// wx
 
20
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
 
21
#pragma implementation "bar3dchartpoints.h"
 
22
#endif
 
23
 
 
24
// For compilers that support precompilation, includes "wx.h".
 
25
#include <wx/wxprec.h>
 
26
 
 
27
#ifdef __BORLANDC__
 
28
#pragma hdrstop
 
29
#endif
 
30
 
 
31
#ifndef WX_PRECOMP
 
32
    #include <wx/wx.h>
 
33
#endif
 
34
 
 
35
#include "wx/label.h"
 
36
#include "wx/bar3dchartpoints.h"
 
37
#include "wx/chartsizes.h"
 
38
 
 
39
 
 
40
//+++-S-cf-------------------------------------------------------------------
 
41
//  NAME:       ctor
 
42
//  DESC:       has to be created on the heap!
 
43
//  PARAMETERS: std::string name, 
 
44
//              ChartColor c
 
45
//  RETURN:     None
 
46
//----------------------------------------------------------------------E-+++
 
47
wxBar3DChartPoints::wxBar3DChartPoints(
 
48
    wxString name,
 
49
    ChartColor c,
 
50
    bool showlabel   
 
51
) : wxChartPoints(wxChartPointsTypes::Bar3D()),
 
52
    m_Name(name), 
 
53
    m_Color(c),
 
54
    m_Zoom(1),
 
55
    m_BarTag(NONE),
 
56
    m_ShowLabel( showlabel)
 
57
{
 
58
}
 
59
 
 
60
//+++-S-cf-------------------------------------------------------------------
 
61
//  NAME:       GetXVal()
 
62
//  DESC:       
 
63
//  PARAMETERS: int n
 
64
//  RETURN:     int
 
65
//----------------------------------------------------------------------E-+++
 
66
ChartValue wxBar3DChartPoints::GetXVal(
 
67
    int n
 
68
) const
 
69
{
 
70
    return ( m_Points.GetXVal(n) );
 
71
}
 
72
 
 
73
//+++-S-cf-------------------------------------------------------------------
 
74
//  NAME:       GetYVal()
 
75
//  DESC:       
 
76
//  PARAMETERS: int n
 
77
//  RETURN:     int
 
78
//----------------------------------------------------------------------E-+++
 
79
ChartValue wxBar3DChartPoints::GetYVal(
 
80
    int n
 
81
) const
 
82
{
 
83
    return ( m_Points.GetYVal(n) );
 
84
}
 
85
 
 
86
//+++-S-cf-------------------------------------------------------------------
 
87
//  NAME:       GetName()
 
88
//  DESC:       
 
89
//  PARAMETERS: int n
 
90
//  RETURN:     std::string
 
91
//----------------------------------------------------------------------E-+++
 
92
wxString wxBar3DChartPoints::GetName(
 
93
    int n
 
94
) const
 
95
{
 
96
    return ( m_Points.GetName(n) );
 
97
}
 
98
 
 
99
//+++-S-cf-------------------------------------------------------------------
 
100
//  NAME:       GetCount()
 
101
//  DESC:       
 
102
//  PARAMETERS: None
 
103
//  RETURN:     int
 
104
//----------------------------------------------------------------------E-+++
 
105
int wxBar3DChartPoints::GetCount() const
 
106
{
 
107
    return ( m_Points.GetCount() );
 
108
}
 
109
 
 
110
//+++-S-cf-------------------------------------------------------------------
 
111
//  NAME:       GetColor()
 
112
//  DESC:       
 
113
//  PARAMETERS: int n
 
114
//  RETURN:     ChartColor
 
115
//----------------------------------------------------------------------E-+++
 
116
ChartColor wxBar3DChartPoints::GetColor(
 
117
    int WXUNUSED(n)
 
118
) const
 
119
{
 
120
    return ( 0 );
 
121
}
 
122
 
 
123
//+++-S-cf-------------------------------------------------------------------
 
124
//  NAME:       GetMaxX()
 
125
//  DESC:       
 
126
//  PARAMETERS: None
 
127
//  RETURN:     int
 
128
//----------------------------------------------------------------------E-+++
 
129
ChartValue wxBar3DChartPoints::GetMaxX() const
 
130
{
 
131
    return ( m_Points.GetMaxX() );
 
132
}
 
133
 
 
134
//+++-S-cf-------------------------------------------------------------------
 
135
//  NAME:       GetMaxY()
 
136
//  DESC:       
 
137
//  PARAMETERS: None
 
138
//  RETURN:     int
 
139
//----------------------------------------------------------------------E-+++
 
140
ChartValue wxBar3DChartPoints::GetMaxY() const
 
141
{
 
142
    return ( m_Points.GetMaxY() );
 
143
}
 
144
 
 
145
//+++-S-cf-------------------------------------------------------------------
 
146
//  NAME:       GetMinX()
 
147
//  DESC:       
 
148
//  PARAMETERS: None
 
149
//  RETURN:     int
 
150
//----------------------------------------------------------------------E-+++
 
151
ChartValue wxBar3DChartPoints::GetMinX() const
 
152
{
 
153
    return ( m_Points.GetMinX() );
 
154
}
 
155
 
 
156
//+++-S-cf-------------------------------------------------------------------
 
157
//  NAME:       GetMinY()
 
158
//  DESC:       
 
159
//  PARAMETERS: None
 
160
//  RETURN:     int
 
161
//----------------------------------------------------------------------E-+++
 
162
ChartValue wxBar3DChartPoints::GetMinY() const
 
163
{
 
164
    return ( m_Points.GetMinY() );
 
165
}
 
166
 
 
167
//+++-S-cf-------------------------------------------------------------------
 
168
//  NAME:       SetZoom
 
169
//  DESC:       Set zoom
 
170
//  PARAMETERS: double
 
171
//  RETURN:     None
 
172
//----------------------------------------------------------------------E-+++
 
173
void wxBar3DChartPoints::SetZoom(
 
174
    double z
 
175
)
 
176
{
 
177
    m_Zoom = z;
 
178
}
 
179
 
 
180
//+++-S-cf-------------------------------------------------------------------
 
181
//  NAME:       GetZoom
 
182
//  DESC:       Get zoom
 
183
//  PARAMETERS: double
 
184
//  RETURN:     None
 
185
//----------------------------------------------------------------------E-+++
 
186
double wxBar3DChartPoints::GetZoom()
 
187
{
 
188
    return ( m_Sizes->GetXZoom() );
 
189
}
 
190
 
 
191
//+++-S-cf-------------------------------------------------------------------
 
192
//  NAME:       SetSizes
 
193
//  DESC:       Set sizes for drawing
 
194
//  PARAMETERS: ChartSizes sizes
 
195
//  RETURN:     None
 
196
//----------------------------------------------------------------------E-+++
 
197
void wxBar3DChartPoints::SetSizes(
 
198
    wxChartSizes *sizes
 
199
)
 
200
{
 
201
    m_Sizes = sizes;
 
202
}
 
203
 
 
204
//+++-S-cf-------------------------------------------------------------------
 
205
//  NAME:       GetSizes
 
206
//  DESC:       Get sizes for drawing
 
207
//  PARAMETERS: None
 
208
//  RETURN:     ChartSizes sizes
 
209
//----------------------------------------------------------------------E-+++
 
210
wxChartSizes* wxBar3DChartPoints::GetSizes() const
 
211
{
 
212
    return ( m_Sizes );
 
213
}
 
214
 
 
215
//+++-S-cf-------------------------------------------------------------------
 
216
//  NAME:       GetColor()
 
217
//  DESC:       
 
218
//  PARAMETERS: None
 
219
//  RETURN:     unsigned long
 
220
//----------------------------------------------------------------------E-+++
 
221
ChartColor wxBar3DChartPoints::GetColor() const
 
222
{
 
223
    return ( m_Color );
 
224
}
 
225
 
 
226
//+++-S-cf-------------------------------------------------------------------
 
227
//  NAME:       SetColor()
 
228
//  DESC:       
 
229
//  PARAMETERS: ChartColor c
 
230
//  RETURN:     None
 
231
//----------------------------------------------------------------------E-+++
 
232
void wxBar3DChartPoints::SetColor(
 
233
    ChartColor c
 
234
)
 
235
{
 
236
    m_Color =  c;
 
237
}
 
238
 
 
239
//+++-S-cf-------------------------------------------------------------------
 
240
//  NAME:       GetName()
 
241
//  DESC:       
 
242
//  PARAMETERS: None
 
243
//  RETURN:     ChartColor
 
244
//----------------------------------------------------------------------E-+++
 
245
wxString wxBar3DChartPoints::GetName() const
 
246
{
 
247
    return ( m_Name );
 
248
}
 
249
 
 
250
//+++-S-cf-------------------------------------------------------------------
 
251
//  NAME:       SetName()
 
252
//  DESC:       
 
253
//  PARAMETERS: None
 
254
//  RETURN:     None
 
255
//----------------------------------------------------------------------E-+++
 
256
void wxBar3DChartPoints::SetName(
 
257
    wxString name
 
258
)
 
259
{
 
260
    m_Name =  name;
 
261
}
 
262
 
 
263
//+++-S-cf-------------------------------------------------------------------
 
264
//  NAME:       Add()
 
265
//  DESC:       Add point
 
266
//  PARAMETERS: std::string name, 
 
267
//              ChartValue x, 
 
268
//              ChartValue y
 
269
//  RETURN:     None
 
270
//----------------------------------------------------------------------E-+++
 
271
void wxBar3DChartPoints::Add(
 
272
    wxString name, 
 
273
    ChartValue x, 
 
274
    ChartValue y
 
275
)
 
276
{
 
277
    m_Points.Add( name, x, y );
 
278
}
 
279
 
 
280
//+++-S-cf-------------------------------------------------------------------
 
281
//  NAME:       Add()
 
282
//  DESC:       Add point
 
283
//  PARAMETERS: std::string name, 
 
284
//              ChartValue x, 
 
285
//              ChartValue y,
 
286
//              ChartColor c
 
287
//  RETURN:     None
 
288
//----------------------------------------------------------------------E-+++
 
289
void wxBar3DChartPoints::Add(
 
290
    wxString name, 
 
291
    ChartValue x, 
 
292
    ChartValue y,
 
293
    ChartColor WXUNUSED(c)
 
294
)
 
295
{
 
296
    //-----------------------------------------------------------------------
 
297
    // discard color
 
298
    //-----------------------------------------------------------------------
 
299
    Add( name, x, y );
 
300
}
 
301
 
 
302
//+++-S-cf-------------------------------------------------------------------
 
303
//  NAME:       SetDisplay()
 
304
//  DESC:       Set Display option
 
305
//  PARAMETERS: wxDISPLAY_LABEL d
 
306
//  RETURN:     None
 
307
//----------------------------------------------------------------------E-+++
 
308
void wxBar3DChartPoints::SetDisplayTag(
 
309
    wxDISPLAY_LABEL d
 
310
)
 
311
{
 
312
    m_BarTag = d;
 
313
}
 
314
 
 
315
//+++-S-cf-------------------------------------------------------------------
 
316
//  NAME:       GetDisplay()
 
317
//  DESC:       Get Display option
 
318
//  PARAMETERS: None
 
319
//  RETURN:     wxDISPLAY_LABEL
 
320
//----------------------------------------------------------------------E-+++
 
321
wxDISPLAY_LABEL wxBar3DChartPoints::GetDisplayTag() const
 
322
{
 
323
    return ( m_BarTag );
 
324
}
 
325
 
 
326
//+++-S-cf-------------------------------------------------------------------
 
327
//  NAME:       Draw()
 
328
//  DESC:       Draw Bar chart
 
329
//  PARAMETERS: CHART_HPAINT hp, 
 
330
//              CHART_HRECT hr
 
331
//  RETURN:     None
 
332
//----------------------------------------------------------------------E-+++
 
333
void wxBar3DChartPoints::Draw(
 
334
    CHART_HPAINT hp, 
 
335
    CHART_HRECT hr
 
336
)
 
337
{
 
338
    //-----------------------------------------------------------------------
 
339
    // get number of bars
 
340
    //-----------------------------------------------------------------------
 
341
    double iNodes = ceil( static_cast<double>(GetCount()) );
 
342
 
 
343
    //-----------------------------------------------------------------------
 
344
    // Get sizes
 
345
    //-----------------------------------------------------------------------
 
346
    wxChartSizes *sizes = GetSizes();
 
347
 
 
348
    //-----------------------------------------------------------------------
 
349
    // get max height
 
350
    //-----------------------------------------------------------------------
 
351
    double ValMax = ceil( sizes->GetMaxY() );
 
352
    if ( ValMax == 0 ) 
 
353
        ValMax = 1;
 
354
    
 
355
    hp->SetBrush( wxBrush(GetColor(), wxSOLID) );
 
356
    //hp->SetPen( *wxTRANSPARENT_PEN );
 
357
    //hp->SetPen( *wxLIGHT_GREY_PEN );
 
358
    hp->SetPen( wxPen(wxChartColors::GetDarkColor(GetColor(), 15)) );
 
359
 
 
360
    double x, y, w, h;
 
361
        
 
362
    //-----------------------------------------------------------------------
 
363
    // Get the real width of the bar
 
364
    // Assume 80% of sizes->GetWidthBar3d()
 
365
    //-----------------------------------------------------------------------
 
366
    w = sizes->GetWidthBar3d() * 0.80;
 
367
    
 
368
    //-----------------------------------------------------------------------
 
369
    // Get how mutch high deep the bar should start.
 
370
    // Suppose we to use 30 degree
 
371
    // rad = 2 * pi / 360 * grd
 
372
    //-----------------------------------------------------------------------
 
373
    h = ( sizes->GetWidthBar3d() - w ) / cos( 0.5236 ) * sin( 0.5236 );
 
374
    h *= GetZoom();
 
375
    
 
376
    for ( int iNode = 0; iNode < iNodes; ++ iNode )
 
377
    {
 
378
        //-------------------------------------------------------------------
 
379
        // Get x-position for iNode bar
 
380
        //-------------------------------------------------------------------
 
381
        double xVal  = ceil( GetXVal(iNode) );
 
382
        x = hr->x + GetZoom() * xVal * ( 
 
383
                sizes->GetWidthBar() * sizes->GetNumBar() + 
 
384
                sizes->GetWidthBar3d() * sizes->GetNumBar3d() + 
 
385
                sizes->GetGap() );
 
386
 
 
387
        //-------------------------------------------------------------------
 
388
        // Get y-position for iNode bar
 
389
        //-------------------------------------------------------------------
 
390
        y = hr->y + ( (hr->h - sizes->GetSizeHeight() )* GetYVal(iNode) ) / ValMax ;
 
391
 
 
392
        hp->DrawRectangle( static_cast<int>(ceil(x)),
 
393
                           static_cast<int>(ceil(hr->h - y)),
 
394
                           static_cast<int>(ceil(w * GetZoom())), 
 
395
                           static_cast<int>(ceil(y)));
 
396
       
 
397
        //-------------------------------------------------------------------
 
398
        // Draw Poligon next to main Rectangle 
 
399
        //-------------------------------------------------------------------
 
400
                
 
401
        hp->SetBrush( wxBrush(wxChartColors::GetDarkColor(GetColor(), 
 
402
            10), wxSOLID) );
 
403
        
 
404
        //-------------------------------------------------------------------
 
405
        // Make sure that the difference y and y - h is at lease 1
 
406
        //-------------------------------------------------------------------
 
407
        int y1 = static_cast<int>(ceil(static_cast<double>(hr->h)));
 
408
        int y2 = static_cast<int>(ceil(static_cast<double>(hr->h - h)));
 
409
        int y3 = static_cast<int>(ceil(static_cast<double>(hr->h - y - h)));
 
410
        int y4 = static_cast<int>(ceil(static_cast<double>(hr->h - y)));
 
411
        int d = y1 - y2 > y4 - y3 ? y1 - y2 : y4 - y3;
 
412
        if ( d == 0 )
 
413
            d += 1;
 
414
        
 
415
        wxPoint next[] = { 
 
416
            wxPoint( 
 
417
                static_cast<int>(ceil(x + w * GetZoom())), 
 
418
                y1 ),
 
419
            wxPoint( 
 
420
                static_cast<int>(ceil(x + sizes->GetWidthBar3d() * GetZoom())), 
 
421
                y1 - d ),
 
422
            wxPoint( 
 
423
                static_cast<int>(ceil(x + sizes->GetWidthBar3d() * GetZoom())), 
 
424
                y4 - d ),
 
425
            wxPoint( 
 
426
                static_cast<int>(ceil(x + w * GetZoom())), 
 
427
                y4 ),
 
428
           };
 
429
        
 
430
        hp->DrawPolygon( 4, next );
 
431
 
 
432
        //-------------------------------------------------------------------
 
433
        // Draw Poligon on top of main Rectangle 
 
434
        //-------------------------------------------------------------------
 
435
        wxPoint top[] = { 
 
436
            wxPoint( 
 
437
                static_cast<int>(ceil(x)), 
 
438
                y4 ),
 
439
            wxPoint( 
 
440
                static_cast<int>(ceil(x + w * GetZoom())), 
 
441
                y4 ),
 
442
            wxPoint( 
 
443
                static_cast<int>(ceil(x + sizes->GetWidthBar3d() * GetZoom())), 
 
444
                y4 - d ),
 
445
            wxPoint( 
 
446
                static_cast<int>(ceil(x + (sizes->GetWidthBar3d() - w) * GetZoom())), 
 
447
                y4 - d ),
 
448
       };
 
449
    
 
450
        hp->DrawPolygon( 4, top );
 
451
 
 
452
        hp->SetBrush( wxBrush(GetColor(), wxSOLID) );
 
453
        
 
454
        //-------------------------------------------------------------------
 
455
        // Only draw Label if user wants it
 
456
        //-------------------------------------------------------------------
 
457
        if (!m_ShowLabel)
 
458
            continue;
 
459
 
 
460
        wxString lbl;
 
461
        wxLabel wxLbl;
 
462
        switch ( GetDisplayTag() )
 
463
        {
 
464
        case XVALUE:
 
465
            lbl.Printf( wxT("%d"), static_cast<int>(xVal));
 
466
            wxLbl.Draw( hp, static_cast<int>(ceil(x)), 
 
467
                        static_cast<int>(ceil(hr->h - y)), 
 
468
                        GetColor(),
 
469
                        lbl,
 
470
                        UP);
 
471
            break;
 
472
        case YVALUE:
 
473
            lbl.Printf( wxT("%d"), static_cast<int>(GetYVal(iNode)));
 
474
                        
 
475
            wxLbl.Draw( hp, static_cast<int>(ceil(x)), 
 
476
                        static_cast<int>(ceil(hr->h - y)), 
 
477
                        GetColor(),
 
478
                        lbl,
 
479
                        UP );
 
480
            break;
 
481
        case XVALUE_FLOAT:
 
482
            lbl.Printf( wxT("%4.1f"), xVal);
 
483
            wxLbl.Draw( hp, static_cast<int>(ceil(x)), 
 
484
                        static_cast<int>(ceil(hr->h - y)), 
 
485
                        GetColor(),
 
486
                        lbl,
 
487
                        UP);
 
488
            break;
 
489
        case YVALUE_FLOAT:
 
490
            lbl.Printf( wxT("%4.1f"), GetYVal(iNode));
 
491
        
 
492
            wxLbl.Draw( hp, static_cast<int>(ceil(x)), 
 
493
                        static_cast<int>(ceil(hr->h - y)), 
 
494
                        GetColor(),
 
495
                        lbl,
 
496
                        UP );
 
497
            break;
 
498
            case NAME:
 
499
            lbl = GetName(iNode).c_str();
 
500
            wxLbl.Draw( hp, static_cast<int>(ceil(x)), 
 
501
                        static_cast<int>(ceil(hr->h - y)), 
 
502
                        GetColor(),
 
503
                        lbl,
 
504
                        UP );
 
505
            break;
 
506
        default:
 
507
            break;      
 
508
        }
 
509
    }
 
510
 
 
511
    hp->SetPen( *wxBLACK_PEN );
 
512
}
 
513
 
 
514
//+++-S-cf-------------------------------------------------------------------
 
515
//  NAME:       CreateWxBarChartPoints()
 
516
//  DESC:       
 
517
//  PARAMETERS: wxString name
 
518
//              ChartColor c
 
519
//              bool showlabel
 
520
//  RETURN:     CBarChartPoints*
 
521
//----------------------------------------------------------------------E-+++
 
522
wxBar3DChartPoints* wxBar3DChartPoints::CreateWxBar3DChartPoints(
 
523
    wxString name,
 
524
    ChartColor c,
 
525
    bool showlabel
 
526
)
 
527
{
 
528
    if ( c == wxCHART_NOCOLOR )
 
529
        c = wxChartColors::GetColor();
 
530
 
 
531
    return new wxBar3DChartPoints( name, c, showlabel );
 
532
}
 
533