-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSLineChart.cpp
412 lines (352 loc) · 10.4 KB
/
SLineChart.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
#include "stdafx.h"
#include "SLineChart.h"
#include <GdiPlus.h>
#pragma comment(lib,"gdiplus")
SLineChart::SLineChart():
m_LineColor(RGBA(255,255, 255, 255)),
m_LineWidth(1),
m_FillRect(0),
m_DataEnd(0),
m_IsCurve(false),
m_DataPointSize(0),
m_DataPointCentreColor(RGBA(0, 0, 0, 0)),
m_CursorTip(true),
m_CursorInCoord(false),
m_CursorTipTextColor(RGBA(0,0,0,255)),
m_CursorTipColorBkgnd(RGBA(128,128,128,128)),
m_CursorLineColor(RGBA(128, 128, 128, 128)),
m_CursorLineWidth(1)
{
}
SLineChart::~SLineChart()
{
}
void SLineChart::CreateLine(int id, SStringT Name, COLORREF Color)
{
LineTag t;
t.Color = Color;
t.Name = Name;
m_Data[id]=t;
}
void SLineChart::AddData(int id, float Value)
{
m_Data[id].Values.Add(Value);
}
void SLineChart::Clear()
{
m_Data.RemoveAll();
}
void SLineChart::SetLineData(int id, LineTag &LineData)
{
m_Data[id] = LineData;
}
void SLineChart::OnMouseMove(UINT nFlags, CPoint point)
{
if (m_CursorTip)
{
m_CursorPoint = point;
InvalidateRect(&GetClientRect(), false);
}
return;
}
void SLineChart::OnMouseLeave()
{
if (m_CursorTip)
{
m_CursorPoint.x = m_CursorPoint.y = 0;
InvalidateRect(&GetClientRect());
}
}
void SLineChart::OnPaint(IRenderTarget *pRT)
{
__super::OnPaint(pRT);
SPainter painter;
BeforePaint(pRT, painter);
CRect rcClient = GetClientRect();
CRect CurrentDrawRect;
CurrentDrawRect = m_DataRect;
if (!m_PullOver&&m_AxisType == AXIS_LENGTHWAYS)
{
CurrentDrawRect.left += (LONG)m_XSignRatio;
CurrentDrawRect.right -= (LONG)m_XSignRatio;
}
else if (!m_PullOver&&m_AxisType == AXIS_CROSSWISE)
{
CurrentDrawRect.top += (LONG)m_YSignRatio;
CurrentDrawRect.bottom -= (LONG)m_YSignRatio;
}
//对于m_DataEnd的支持
float DataWidth;
if (m_DataEnd&&m_AxisType == AXIS_LENGTHWAYS)
{
DataWidth = m_DataEnd>1 ? m_XSignRatio*m_DataEnd:CurrentDrawRect.Width()*(float)m_DataEnd;
CurrentDrawRect.right = CurrentDrawRect.left + (LONG)DataWidth;
}
else if (m_DataEnd&&m_AxisType == AXIS_CROSSWISE)
{
DataWidth = m_DataEnd>1 ? m_YSignRatio*m_DataEnd : CurrentDrawRect.Height()*(float)m_DataEnd;
CurrentDrawRect.top = CurrentDrawRect.bottom - (LONG)DataWidth;
}
SOUI::SPOSITION pos = m_Data.GetStartPosition();
bool CountAligned=true;//数据是否是对齐的
size_t AlignedCount = 0;//CountAligned为真 有效
int m = 0;
SArray<SArray<POINT>> Pointsx;//线上的点坐标
//每条线的绘制
while (pos)
{
SArray<POINT> Points;
CoordLineData::CPair *p = m_Data.GetNext(pos);
SArray<Gdiplus::PointF> pos;
if (m == 0)
m = p->m_value.Values.GetCount();
else if (m != p->m_value.Values.GetCount())
CountAligned = false;
AlignedCount = p->m_value.Values.GetCount() > AlignedCount ? p->m_value.Values.GetCount() : AlignedCount;
size_t Count = p->m_value.Values.GetCount() - 1;
Count = Count < m_Text.GetCount() ? m_Text.GetCount() - 1 : Count;
for (size_t i = 0; i < p->m_value.Values.GetCount(); i++)
{
POINT po;
if (m_AxisType == AXIS_LENGTHWAYS)
{
po.x = GetDataDrawRect().left + (m_PullOver ? 0 : m_XSignRatio) + CurrentDrawRect.Width() / (float)(Count)*i;
po.y = GetDataDrawRect().bottom - (m_ValueRatio*(p->m_value.Values[i] - m_MinValue));
}
else if (m_AxisType == AXIS_CROSSWISE)
{
po.x = GetDataDrawRect().left + (m_ValueRatio*(p->m_value.Values[i] - m_MinValue));
po.y = GetDataDrawRect().bottom - (m_PullOver ? 0 : m_YSignRatio) - CurrentDrawRect.Height() / (float)Count*i;
}
Points.Add(po);
if (m_FillRect||m_IsCurve)
{
Gdiplus::PointF Pof;
Pof.X = (float)po.x;
Pof.Y = (float)po.y;
pos.Add(Pof);
}
}
if (!m_IsCurve)
{
CAutoRefPtr<SOUI::IPen> Pen;
pRT->CreatePen(PS_SOLID, p->m_value.Color, m_LineWidth, &Pen);
pRT->SelectObject(Pen);
pRT->DrawLines(&Points.GetAt(0), Points.GetCount());
}
else
{
////曲线的绘制
HDC hdc = pRT->GetDC();
Gdiplus::Graphics gps(hdc);
gps.SetSmoothingMode(Gdiplus::SmoothingModeHighQuality);
Gdiplus::Pen curPen(Gdiplus::Color(GetAValue(p->m_value.Color), GetRValue(p->m_value.Color), GetGValue(p->m_value.Color), GetBValue(p->m_value.Color)), m_LineWidth);
gps.DrawCurve(&curPen, &pos[0], pos.GetCount());
pRT->ReleaseDC(hdc);
}
//数据点的绘制
if (m_DataPointSize > 0)
{
CAutoRefPtr<SOUI::IPen> pPen;
pRT->CreatePen(PS_SOLID, p->m_value.Color, m_LineWidth, &pPen);
pRT->SelectObject(pPen);
CAutoRefPtr<SOUI::IBrush> pBrush;
CAutoRefPtr<SOUI::IBrush> poldBrush;
poldBrush = (IBrush*)pRT->GetCurrentObject(OT_BRUSH);
pRT->CreateSolidColorBrush(m_DataPointCentreColor == RGBA(0, 0, 0, 0) ? p->m_value.Color : m_DataPointCentreColor, &pBrush);
pRT->SelectObject(pBrush);
for (size_t i = 0; i < Points.GetCount(); i++)
{
CRect Rect;
Rect.left = Points[i].x - m_DataPointSize / 2;
Rect.top = Points[i].y - m_DataPointSize / 2;
Rect.bottom = Rect.top + m_DataPointSize;
Rect.right = Rect.left + m_DataPointSize;
pRT->FillSolidEllipse(Rect, p->m_value.Color);
}
pRT->SelectObject(poldBrush);
}
//绘制填充
if (m_FillRect&&(!m_IsCurve))
{
//闭合线段
if (m_AxisType == AXIS_LENGTHWAYS)
{
Gdiplus::PointF Pof;
Pof.X = pos[pos.GetCount() - 1].X;
Pof.Y = m_DataRect.bottom;
pos.Add(Pof);
Pof.X = pos[0].X;
Pof.Y = m_DataRect.bottom;
pos.Add(Pof);
Pof.X = pos[0].X;
Pof.Y = pos[0].Y;
pos.Add(Pof);
}
else if (m_AxisType == AXIS_CROSSWISE)
{
Gdiplus::PointF Pof;
Pof.X = m_DataRect.left;
Pof.Y = pos[pos.GetCount() - 1].Y;
pos.Add(Pof);
Pof.X = m_DataRect.left;
Pof.Y = pos[0].Y;
pos.Add(Pof);
Pof.X = pos[0].X;
Pof.Y = pos[0].Y;
pos.Add(Pof);
}
//绘制闭合区域
HDC hdc=pRT->GetDC();
Gdiplus::Graphics gps(hdc);
gps.SetSmoothingMode(Gdiplus::SmoothingModeHighQuality);
Gdiplus::SolidBrush blackBrush(Gdiplus::Color(m_FillRect, GetRValue(p->m_value.Color), GetGValue(p->m_value.Color), GetBValue(p->m_value.Color)));
gps.FillPolygon(&blackBrush, &pos[0], pos.GetCount());
pRT->ReleaseDC(hdc);
}
Pointsx.Add(Points);
}
//对于鼠标的响应 //线上的点需要对齐才能生效
if (CountAligned&&m_CursorTip&&m_CursorPoint.x != 0 && m_CursorPoint.y != 0)
{
m_CursorInCoord = false;
if (CurrentDrawRect.PtInRect(m_CursorPoint))
{
int index=0;
CPoint CursorLinePos[2];
//绘制一条跟随光标的线
if(m_AxisType == AXIS_LENGTHWAYS)
{
AlignedCount--;
float space = (CurrentDrawRect.Width() / (float)AlignedCount);
index = ((m_CursorPoint.x + (space / 2) - CurrentDrawRect.left) / (space));
CursorLinePos[1].x = CursorLinePos[0].x = CurrentDrawRect.left + index* space;
CursorLinePos[0].y = CurrentDrawRect.top;
CursorLinePos[1].y = CurrentDrawRect.bottom;
}
else if (m_AxisType == AXIS_CROSSWISE)
{
AlignedCount--;
float space = (CurrentDrawRect.Height() / (float)AlignedCount);
index = ((CurrentDrawRect.bottom-(m_CursorPoint.y - space/2)) / space);
CursorLinePos[1].y = CursorLinePos[0].y = CurrentDrawRect.bottom - index* space;
CursorLinePos[0].x = CurrentDrawRect.left;
CursorLinePos[1].x = CurrentDrawRect.right;
}
CAutoRefPtr<SOUI::IPen> pPen;
pRT->CreatePen(PS_SOLID, m_CursorLineColor, m_CursorLineWidth, &pPen);
pRT->SelectObject(pPen);
pRT->DrawLines(CursorLinePos, 2);
//准备绘制提示框
CRect rect;
rect.top = m_CursorPoint.y + 15;
rect.left = m_CursorPoint.x + 15;
SOUI::SPOSITION pos = m_Data.GetStartPosition();
int MaxTextWidth=0;
int MaxTextHeight = 0;
SArray<SStringT> TipTexts;
int n = 0;
SIZE size;
while (pos)
{
CoordLineData::CPair *p = m_Data.GetNext(pos);
SStringT str;
SStringT strValue;
NumberToScaleStr(p->m_value.Values[index], m_Decimal,strValue);
str.Format(L"%s:%s", p->m_value.Name, strValue);
TipTexts.Add(str);
pRT->MeasureText(str, str.GetLength(), &size);
if (MaxTextWidth < size.cx) MaxTextWidth = size.cx;
MaxTextHeight += size.cy;
//数据点放大显示
CAutoRefPtr<SOUI::IBrush> pBrush;
CAutoRefPtr<SOUI::IPen> pPen;
pRT->CreateSolidColorBrush(m_DataPointCentreColor == RGBA(0, 0, 0, 0) ? p->m_value.Color : m_DataPointCentreColor, &pBrush);
pRT->CreatePen(PS_SOLID, p->m_value.Color, 1, &pPen);
pRT->SelectObject(pPen);
pRT->SelectObject(pBrush);
CRect Rect;
Rect.left = Pointsx[n][index].x - (m_DataPointSize*1.8f) / 2.f;
Rect.top = Pointsx[n][index].y - (m_DataPointSize*1.8f) / 2.f;
Rect.bottom = Rect.top + m_DataPointSize*1.8f;
Rect.right = Rect.left + m_DataPointSize*1.8f;
pRT->FillSolidEllipse(Rect, p->m_value.Color);
n++;
}
rect.bottom = rect.top + MaxTextHeight;
rect.right = rect.left + MaxTextWidth;
//调整提示框位置
if (rect.bottom > rcClient.bottom) rect.OffsetRect(CPoint(0, -(rect.bottom - rcClient.bottom)));
if (rect.right > rcClient.right) rect.OffsetRect(CPoint(-(rect.right - rcClient.right), 0));
//绘制提示框
pRT->FillSolidRoundRect(rect, CPoint(2, 2), m_CursorTipColorBkgnd);
if (m_CursorTipFont.GetFontPtr() != NULL) pRT->SelectObject(m_CursorTipFont.GetFontPtr());
pRT->SetTextColor(m_CursorTipTextColor);
//绘制提示框中的文字
for (size_t i = 0; i < TipTexts.GetCount();i++)
{
pRT->DrawText(TipTexts[i], TipTexts[i].GetLength(),rect, DT_LEFT | DT_VCENTER /*| DT_SINGLELINE*/);
rect.OffsetRect(0, size.cy);
}
m_CursorInCoord = true;
}
}
AfterPaint(pRT, painter);
}
void SLineChart::GetMaxMin(float &Max, float &Min)
{
SOUI::SPOSITION pos = m_Data.GetStartPosition();
bool flag = false;
while (pos)
{
CoordLineData::CPair *p = m_Data.GetNext(pos);
for (size_t i = 0; i<p->m_value.Values.GetCount(); i++)
{
float TempValue = p->m_value.Values[i];
if (i == 0 && !flag)
{
Max = Min = TempValue;
flag = true;
}
if (Min > TempValue)
Min = TempValue;
if (Max < TempValue)
Max = TempValue;
}
}
}
void SLineChart::SetLineWidth(int Width)
{
m_LineWidth = Width;
}
void SLineChart::SetFillRect(int Alpha)
{
m_FillRect = Alpha;
}
void SLineChart::SetDataEnd(float End)
{
m_DataEnd = End;
}
void SLineChart::SetCurveChart(bool Curve)
{
m_IsCurve = Curve;
}
void SLineChart::SetLineValue(int id, int index, float Value)
{
m_Data[id].Values[index] = Value;
}
SLineChart::LineTag &SLineChart::GetLineData(int id)
{
return m_Data[id];
}
void SLineChart::PopTopPushBack(int id,float Value)
{
for (size_t i = 0; i < m_Data[id].Values.GetCount(); i++)
{
if (i > 0)
{
m_Data[id].Values[i - 1] = m_Data[id].Values[i];
}
}
m_Data[id].Values[m_Data[id].Values.GetCount() - 1] = Value;
}