NCBI C++ ToolKit
pie_graph.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: pie_graph.cpp 23958 2011-06-24 15:24:59Z wuliangs $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software / database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software / database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Andrey Yazhuk
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 #include <gui/graph/pie_graph.hpp>
34 #include <gui/opengl/glutils.hpp>
35 
36 #include <math.h>
37 
39 
41 {
42  SetColor(CRgbaColor(0.0f, 0.0f, 0.0f));
43 }
44 
45 
47 {
48  IPieDataSource* pPieDS = dynamic_cast<IPieDataSource*>(pDS);
49  bool bOk = pPieDS!= NULL;
50  CGraphBase::SetDataSource(bOk ? pDS : NULL);
51 
53  return bOk;
54 }
55 
56 
58 {
59  _ASSERT(pPane);
60  IPieDataSource* pSource = GetPieDataSource();
61  if (pPane && pSource) {
62  pPane->OpenOrtho();
63  try {
64  INumericArray* pValues = pSource->GetValueArray();
65  IColorArray* pColors = pSource->GetColorArray();
66  //IStringArray* pLabels = pSource->GetLabelsArray();
67 
68  size_t N = pValues->GetSize();
69  double Sum = 0.0;
70  for (size_t i =0; i<N; i++ ) {
71  Sum += ::fabs(pValues->GetElem((int)i));
72  }
73 
74  // choose number of segments
75  double D = 0.5 *(pPane->GetViewport().Width() + pPane->GetViewport().Height());
76  double MinD = min(pPane->GetViewport().Width(), pPane->GetViewport().Height());
77  double SegL = sqrt(MinD); //pixels
78  double NSegmPerDegree = 3.14 * D / (SegL * 360);
79 
80  double kA = 360.0 / Sum;
81  double aStart = 0.0;
82 
83  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
84  GLUquadricObj *pQObj = gluNewQuadric();
85 
86 
87  for (size_t i = 0; i < N; i++) {
88  double V = ::fabs(pValues->GetElem(i));
89  double aSweep = V * kA;
90  int Slices = 1 + (int) ceil(aSweep * NSegmPerDegree);
91 
92  // drawing sector
93  glColorC(pColors->GetElem(i));
94  gluQuadricDrawStyle(pQObj, GLU_FILL);
95  gluPartialDisk(pQObj, 0, 1, Slices, 1, aStart, aSweep);
96 
97  //drawing perimeter
99  gluQuadricDrawStyle(pQObj, GLU_SILHOUETTE);
100  gluPartialDisk(pQObj, 0, 1, Slices, 1, aStart, aSweep);
101 
102  aStart += aSweep;
103  }
104  gluDeleteQuadric(pQObj);
105  }
106  catch(CException& e){
107  ERR_POST(e.ReportAll());
108  }
109  catch(std::exception& e) {
110  ERR_POST(e.what());
111  }
112  pPane->Close();
113  }
114 }
115 
116 
118 {
119  m_Limits.Init(-1.0, -1.0, 1.0, 1.0);
120 }
121 
122 
124 {
125  return false;
126 }
127 
128 
130 {
132  return pDS ? pDS->GetLabelsArray() : NULL;
133 }
134 
135 
137 {
139  return pDS ? pDS->GetColorArray() : NULL;
140 }
141 
142 
144 {
145  return NULL;
146 }
147 
148 
149 /////////////////////////////////////////////////////////////////////////////
150 /// CPieDataSource
151 CPieDataSource::CPieDataSource(int SectorsN, const string& Name)
152  : CSeriesBase(SectorsN),
153 m_Name(Name)
154 {
155 }
156 
157 
159 {
161 
162  TValueAdapter* pValAd = new TValueAdapter(m_Length);
163  AddArray(static_cast<IDataArray*>(pValAd));
164 
165  TColorAdapter* pCAd = new TColorAdapter(m_Length);
166  AddArray(static_cast<IDataArray*>(pCAd));
167 
168  TStringAdapter* pLabAd = new TStringAdapter(m_Length);
169  AddArray(static_cast<IDataArray*>(pLabAd));
170 }
171 
172 
class CGlPane
Definition: glpane.hpp:62
class CRgbaColor provides a simple abstraction for managing colors.
Definition: rgba_color.hpp:58
class CSeriesBase CSeries represents a set of colinear IDataArrays and provides basic access and mana...
class CTypedArrayAdapter<Type, TBase>
interface IDataArray
Definition: igraph_data.hpp:50
IGraphDataSource.
Definition: igraph.hpp:55
IPieDataSource.
Definition: pie_graph.hpp:53
class ITypedDataArray<Type>
Definition: igraph_data.hpp:90
#define NULL
Definition: ncbistd.hpp:225
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
string ReportAll(TDiagPostFlags flags=eDPF_Exception) const
Report all exceptions.
Definition: ncbiexpt.cpp:370
virtual const char * what(void) const noexcept
Standard report (includes full backlog).
Definition: ncbiexpt.cpp:342
CTypedArrayAdapter< IDataArray::eNumeric > TValueAdapter
Definition: pie_graph.hpp:103
void AddArray(IDataArray *pArray)
CTypedArrayAdapter< IDataArray::eColor > TColorAdapter
Definition: pie_graph.hpp:106
virtual void Render(CGlPane *pViewport, TElemVector *elems=NULL)
Definition: pie_graph.cpp:57
virtual IColorArray * GetColorArray()
Definition: pie_graph.cpp:136
vector< SGraphElem * > TElemVector
Definition: igraph.hpp:72
IPieDataSource * GetPieDataSource()
Definition: pie_graph.hpp:87
virtual void CreateArrays()
Definition: pie_graph.cpp:158
virtual bool SetDataSource(IGraphDataSource *pDS)
Definition: pie_graph.cpp:46
virtual IStringArray * GetLabelsArray()=0
virtual INumericArray * GetValueArray()=0
CRgbaColor m_Color
Definition: igraph.hpp:124
virtual size_t GetSize()=0
TModelRect m_Limits
Definition: igraph.hpp:121
virtual void SetColor(CRgbaColor Color)
Definition: igraph.hpp:110
virtual void CreateArrays()
Definition: igraph_data.cpp:89
virtual IStringArray * GetLabelArray()
Definition: pie_graph.cpp:129
virtual INumericArray * GetMarkerArray()
Definition: pie_graph.cpp:143
virtual bool SetDataSource(IGraphDataSource *pDS)
Definition: igraph.cpp:61
CTypedArrayAdapter< IDataArray::eString > TStringAdapter
Definition: pie_graph.hpp:107
virtual TValueType GetElem(size_t i)=0
virtual void CalculateLimits()
Definition: pie_graph.cpp:117
CPieDataSource(int SectorsN, const string &Name="")
CPieDataSource.
Definition: pie_graph.cpp:151
virtual bool ShowMarkers()
Definition: pie_graph.cpp:123
virtual IColorArray * GetColorArray()=0
T Height() const
Definition: glrect.hpp:90
bool OpenOrtho()
Definition: glpane.hpp:427
void Init()
Definition: glrect.hpp:62
T Width() const
Definition: glrect.hpp:86
TVPRect & GetViewport(void)
Definition: glpane.hpp:332
void glColorC(const CRgbaColor &color)
Definition: glutils.hpp:177
void Close(void)
Definition: glpane.cpp:178
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
int i
#define fabs(v)
Definition: ncbi_dispd.c:46
T min(T x_, T y_)
#define _ASSERT
#define N
Definition: crc32.c:57
Modified on Tue Apr 23 07:38:42 2024 by modify_doxy.py rev. 669887