-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnightcharts.cpp
469 lines (424 loc) · 13.9 KB
/
nightcharts.cpp
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
* NightCharts
* Copyright (C) 2010 by Alexander A. Avdonin, Artem N. Ivanov / ITGears Co.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* Please contact [email protected] with any questions on this license.
*/
#define _USE_MATH_DEFINES
#include "nightcharts.h"
Nightcharts::Nightcharts()//QPainter *painter)
{
font.setFamily("verdana");
font.setPixelSize(15);
//painter = painter;
//painter->setFont(font);
ctype = Nightcharts::Dpie;
cltype = Nightcharts::Vertical;
cX = 0;
cY = 0;
cW = 100;
cH = 100;
lX = cX+cW+20;
lY = cY;
shadows = true;
}
Nightcharts::~Nightcharts()
{
pieces.clear();
}
int Nightcharts::addPiece(QString name,Qt::GlobalColor color,float Percentage)
{
this->nPiece++;
pieceNC piece;
piece.addName(name);
piece.setColor(color);
piece.setPerc(Percentage);
pieces.append(piece);
return 0;
}
int Nightcharts::addPiece(QString name, QColor color, float Percentage)
{
this->nPiece++;
pieceNC piece;
piece.addName(name);
piece.setColor(color);
piece.setPerc(Percentage);
pieces.append(piece);
return 0;
}
int Nightcharts::setCords(double x, double y, double w, double h)
{
this->cX = x;
this->cY = y;
this->cW = w;
this->cH = h;
this->lX = cX+cW+20;
this->lY = cY;
return 0;
}
int Nightcharts::setLegendCords(double x, double y)
{
this->lX = x;
this->lY = y;
return 0;
}
int Nightcharts::setType(Nightcharts::type t)
{
this->ctype = t;
return 0;
}
int Nightcharts::setLegendType(Nightcharts::legend_type t)
{
this->cltype = t;
return 0;
}
int Nightcharts::setFont(QFont f)
{
this->font = f;
return 0;
}
int Nightcharts::setShadows(bool ok)
{
this->shadows = ok;
return 0;
}
int Nightcharts::draw(QPainter *painter)
{
painter->setRenderHint(QPainter::Antialiasing);
painter->setPen(Qt::NoPen);
if (this->ctype==Nightcharts::Pie)
{
pW = 0;
double pdegree = 0;
//Options
QLinearGradient gradient(cX+0.5*cW,cY,cX+0.5*cW,cY+cH*2.5);
gradient.setColorAt(1,Qt::black);
//Draw
//pdegree = (360/100)*pieces[i].pPerc;
if (shadows)
{
double sumangle = 0;
for (int i=0;i<pieces.size();i++)
{
sumangle += 3.6*pieces[i].pPerc;
}
painter->setBrush(Qt::darkGray);
painter->drawPie(cX,cY+pW+5,cW,cH,palpha*16,sumangle*16);
}
QPen pen;
pen.setWidth(2);
for (int i=0;i<pieces.size();i++)
{
gradient.setColorAt(0,pieces[i].rgbColor);
painter->setBrush(gradient);
pen.setColor(pieces[i].rgbColor);
painter->setPen(pen);
pdegree = 3.6*pieces[i].pPerc;
painter->drawPie(cX,cY,cW,cH,palpha*16,pdegree*16);
palpha += pdegree;
}
}
else if (this->ctype==Nightcharts::Dpie)
{
pW = 50;
double pdegree = 0;
QPointF p;
QLinearGradient gradient(cX-0.5*cW,cY+cH/2,cX+1.5*cW,cY+cH/2);
gradient.setColorAt(0,Qt::black);
gradient.setColorAt(1,Qt::white);
QLinearGradient gradient_side(cX,cY+cH,cX+cW,cY+cH);
gradient_side.setColorAt(0,Qt::black);
double sumangle = 0;
for (int i=0;i<pieces.size();i++)
{
sumangle += 3.6*pieces[i].pPerc;
}
if (shadows)
{
painter->setBrush(Qt::darkGray);
painter->drawPie(cX,cY+pW+5,cW,cH,palpha*16,sumangle*16);
}
int q = GetQuater(palpha+sumangle);
if (q ==2 || q==3)
{
QPointF p = GetPoint(palpha+sumangle);
QPointF points[4] =
{
QPointF(p.x(),p.y()),
QPointF(p.x(),p.y()+pW),
QPointF(cX+cW/2,cY+cH/2+pW),
QPointF(cX+cW/2,cY+cH/2)
};
gradient_side.setColorAt(1,pieces[pieces.size()-1].rgbColor);
painter->setBrush(gradient_side);
painter->drawPolygon(points,4);
}
p = GetPoint(palpha);
q = GetQuater(palpha);
if (q ==1 || q==4)
{
QPointF points[4] =
{
QPointF(p.x(),p.y()),
QPointF(p.x(),p.y()+pW),
QPointF(cX+cW/2,cY+cH/2+pW),
QPointF(cX+cW/2,cY+cH/2)
};
gradient_side.setColorAt(1,pieces[0].rgbColor);
painter->setBrush(gradient_side);
painter->drawPolygon(points,4);
}
for (int i=0;i<pieces.size();i++)
{
gradient.setColorAt(0.5,pieces[i].rgbColor);
painter->setBrush(gradient);
pdegree = 3.6*pieces[i].pPerc;
painter->drawPie(cX,cY,cW,cH,palpha*16,pdegree*16);
double a_ = Angle360(palpha);
int q_ = GetQuater(palpha);
palpha += pdegree;
double a = Angle360(palpha);
int q = GetQuater(palpha);
QPainterPath path;
p = GetPoint(palpha);
if((q == 3 || q == 4) && (q_ == 3 || q_ == 4))
{
// 1)
if (a>a_)
{
QPointF p_old = GetPoint(palpha-pdegree);
path.moveTo(p_old.x()-1,p_old.y());
path.arcTo(cX,cY,cW,cH,palpha-pdegree,pdegree);
path.lineTo(p.x(),p.y()+pW);
path.arcTo(cX,cY+pW,cW,cH,palpha,-pdegree);
}
// 2)
else
{
path.moveTo(cX,cY+cH/2);
path.arcTo(cX,cY,cW,cH,180,Angle360(palpha)-180);
path.lineTo(p.x(),p.y()+pW);
path.arcTo(cX,cY+pW,cW,cH,Angle360(palpha),-Angle360(palpha)+180);
path.lineTo(cX,cY+cH/2);
path.moveTo(p.x(),p.y());
path.arcTo(cX,cY,cW,cH,palpha-pdegree,360-Angle360(palpha-pdegree));
path.lineTo(cX+cW,cY+cH/2+pW);
path.arcTo(cX,cY+pW,cW,cH,0,-360+Angle360(palpha-pdegree));
}
}
// 3)
else if((q == 3 || q == 4) && (q_ == 1 || q_ == 2) && a>a_ )
{
path.moveTo(cX,cY+cH/2);
path.arcTo(cX,cY,cW,cH,180,Angle360(palpha)-180);
path.lineTo(p.x(),p.y()+pW);
path.arcTo(cX,cY+pW,cW,cH,Angle360(palpha),-Angle360(palpha)+180);
path.lineTo(cX,cY+cH/2);
}
// 4)
else if((q == 1 || q == 2) && (q_ == 3 || q_ == 4) && a<a_)
{
p = GetPoint(palpha-pdegree);
path.moveTo(p.x(),p.y());
path.arcTo(cX,cY,cW,cH,palpha-pdegree,360-Angle360(palpha-pdegree));
path.lineTo(cX+cW,cY+cH/2+pW);
path.arcTo(cX,cY+pW,cW,cH,0,-360+Angle360(palpha-pdegree));
}
// 5)
else if((q ==1 || q==2) && (q_==1 || q_==2) && a<a_)
{
path.moveTo(cX,cY+cH/2);
path.arcTo(cX,cY,cW,cH,180,180);
path.lineTo(cX+cW,cY+cH/2+pW);
path.arcTo(cX,cY+pW,cW,cH,0,-180);
path.lineTo(cX,cY+cH/2);
}
if (!path.isEmpty())
{
gradient_side.setColorAt(1,pieces[i].rgbColor);
painter->setBrush(gradient_side);
painter->drawPath(path);
}
}
}
else if (this->ctype==Nightcharts::Histogramm)
{
double pDist = 15;
double pW = (cW-(pieces.size())*pDist)/pieces.size();
QLinearGradient gradient(cX+cW/2,cY,cX+cW/2,cY+cH);
gradient.setColorAt(0,Qt::black);
QPen pen;
pen.setWidth(3);
for (int i=0;i<pieces.size();i++)
{
if (shadows)
{
painter->setPen(Qt::NoPen);
painter->setBrush(Qt::darkGray);
painter->drawRect(cX+pDist+i*(pW + pDist)-pDist/2,cY+cH-1,pW,-cH/100*pieces[i].pPerc+pDist/2-5);
}
gradient.setColorAt(1,pieces[i].rgbColor);
painter->setBrush(gradient);
pen.setColor(pieces[i].rgbColor);
painter->setPen(pen);
painter->drawRect(cX+pDist+i*(pW + pDist),cY+cH,pW,-cH/100*pieces[i].pPerc-5);
QString label = QString::number(pieces[i].pPerc)+"%";
painter->setPen(Qt::SolidLine);
painter->drawText(cX+pDist+i*(pW + pDist)+pW/2-painter->fontMetrics().width(label)/2,cY+cH-cH/100*pieces[i].pPerc-painter->fontMetrics().height()/2,label);
}
painter->setPen(Qt::SolidLine);
for (int i=1;i<10;i++)
{
painter->drawLine(cX-3,cY+cH/10*i,cX+3,cY+cH/10*i); //äåëåíèÿ ïî îñè Y
//painter->drawText(cX-20,cY+cH/10*i,QString::number((10-i)*10)+"%");
}
painter->drawLine(cX,cY+cH,cX,cY); //îñü Y
painter->drawLine(cX,cY,cX+4,cY+10); //ñòðåëêè
painter->drawLine(cX,cY,cX-4,cY+10);
painter->drawLine(cX,cY+cH,cX+cW,cY+cH); //îñü Õ
}
return 0;
}
int Nightcharts::drawLegend(QPainter *painter)
{
//double ptext = 25;
double angle = palpha;
painter->setPen(Qt::SolidLine);
switch(cltype)
{
/*case Nightcharts::Horizontal:
{
int dist = 5;
painter->setBrush(Qt::white);
float x = cX;
float y = cY+cH+20+dist;
//painter->drawRoundRect(cX+cW+20,cY,dist*2+200,pieces.size()*(fontmetr.height()+2*dist)+dist,15,15);
for (int i=0;i<pieces.size();i++)
{
painter->setBrush(pieces[i].rgbColor);
x += fontmetr.height()+2*dist;
if (i%3 == 0)
{
x = cX;
y += dist+fontmetr.height();
}
painter->drawRect(x,y,fontmetr.height(),fontmetr.height());
QString label = pieces[i].pname + " - " + QString::number(pieces[i].pPerc)+"%";
painter->drawText(x+fontmetr.height()+dist,y+fontmetr.height()/2+dist,label);
x += fontmetr.width(label);
}
break;
}*/
case Nightcharts::Vertical:
{
int dist = 5;
painter->setBrush(Qt::white);
//painter->drawRoundRect(cX+cW+20,cY,dist*2+200,pieces.size()*(painter->fontMetrics().height()+2*dist)+dist,15,15);
for (int i=pieces.size()-1;i>=0;i--)
{
painter->setBrush(pieces[i].rgbColor);
float x = lX+dist;
float y = lY+dist+i*(painter->fontMetrics().height()+2*dist);
painter->drawRect(x,y,painter->fontMetrics().height(),painter->fontMetrics().height());
painter->drawText(x+painter->fontMetrics().height()+dist,y+painter->fontMetrics().height()/2+dist,pieces[i].pname + " - " + QString::number(pieces[i].pPerc)+"%");
}
break;
}
case Nightcharts::Round:
for (int i=pieces.size()-1;i>=0;i--)
{
float len = 100;
double pdegree = 3.6*pieces[i].pPerc;
angle -= pdegree/2;
QPointF p = GetPoint(angle);
QPointF p_ = GetPoint(angle, cW+len,cH+len);
int q = GetQuater(angle);
if (q == 3 || q == 4)
{
p.setY(p.y()+pW/2);
p_.setY(p_.y()+pW/2);
}
painter->drawLine(p.x(),p.y(),p_.x(),p_.y());
QString label = pieces[i].pname + " - " + QString::number(pieces[i].pPerc)+"%";
float recW = painter->fontMetrics().width(label)+10;
float recH = painter->fontMetrics().height()+10;
p_.setX(p_.x()-recW/2 + recW/2*cos(angle*M_PI/180));
p_.setY(p_.y()+recH/2 + recH/2*sin(angle*M_PI/180));
painter->setBrush(Qt::white);
painter->drawRoundRect(p_.x() ,p_.y(), recW, -recH);
painter->drawText(p_.x()+5, p_.y()-recH/2+5, label);
angle -= pdegree/2;
}
break;
}
}
QPointF Nightcharts::GetPoint(double angle, double R1, double R2)
{
if (R1 == 0 && R2 == 0)
{
R1 = cW;
R2 = cH;
}
QPointF point;
double x = R1/2*cos(angle*M_PI/180);
x+=cW/2+cX;
double y = -R2/2*sin(angle*M_PI/180);
y+=cH/2+cY;
point.setX(x);
point.setY(y);
return point;
}
int Nightcharts::GetQuater(double angle)
{
angle = Angle360(angle);
if(angle>=0 && angle<90)
return 1;
if(angle>=90 && angle<180)
return 2;
if(angle>=180 && angle<270)
return 3;
if(angle>=270 && angle<360)
return 4;
}
double Nightcharts::Angle360(double angle)
{
int i = (int)angle;
double delta = angle - i;
return (i%360 + delta);
}
pieceNC::pieceNC()
{
}
void pieceNC::addName(QString name)
{
pname = name;
}
void pieceNC::setColor(Qt::GlobalColor color)
{
rgbColor = color;
}
void pieceNC::setColor(QColor color)
{
rgbColor = color;
}
void pieceNC::setPerc(float Percentage)
{
pPerc = Percentage;
}
int Nightcharts::pieceCount()
{
return pieces.count();
}