-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChartDateTimeAxis.cpp
411 lines (381 loc) · 9.87 KB
/
ChartDateTimeAxis.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
/*
*
* ChartDateTimeAxis.cpp
*
* Written by Cédric Moonen ([email protected])
*
*
*
* This code may be used for any non-commercial and commercial purposes in a compiled form.
* The code may be redistributed as long as it remains unmodified and providing that the
* author name and this disclaimer remain intact. The sources can be modified WITH the author
* consent only.
*
* This code is provided without any garanties. I cannot be held responsible for the damage or
* the loss of time it causes. Use it at your own risks
*
* An e-mail to notify me that you are using this code is appreciated also.
*
*/
#include "stdafx.h"
#include "ChartDateTimeAxis.h"
#include "ChartCtrl.h"
#include <sstream>
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
CChartDateTimeAxis::CChartDateTimeAxis()
: CChartAxis(), m_strDTTickFormat(),
m_bAutoTickFormat(true), m_BaseInterval(tiDay),
m_iDTTickIntervalMult(1), m_dFirstTickValue(0)
{
m_ReferenceTick.SetDate(2000,1,1);
}
CChartDateTimeAxis::~CChartDateTimeAxis()
{
}
void CChartDateTimeAxis::SetTickIncrement(bool bAuto,
TimeInterval Interval,
int Multiplier)
{
m_bAutoTicks = bAuto;
if (!m_bAutoTicks)
{
m_BaseInterval = Interval;
m_iDTTickIntervalMult = Multiplier;
}
}
void CChartDateTimeAxis::SetTickLabelFormat(bool bAutomatic,
const TChartString& strFormat)
{
m_bAutoTickFormat = bAutomatic;
m_strDTTickFormat = strFormat;
m_pParentCtrl->RefreshCtrl();
}
double CChartDateTimeAxis::GetFirstTickValue() const
{
double dRetVal = m_dFirstTickValue;
if (m_bDiscrete)
{
COleDateTime dtTick((DATE)m_dFirstTickValue);
COleDateTimeSpan dtSpan;
switch (m_BaseInterval)
{
case tiSecond:
dtSpan.SetDateTimeSpan(0,0,0,m_iDTTickIntervalMult);
dtTick -= dtSpan;
break;
case tiMinute:
dtSpan.SetDateTimeSpan(0,0,m_iDTTickIntervalMult,0);
dtTick -= dtSpan;
break;
case tiHour:
dtSpan.SetDateTimeSpan(0,m_iDTTickIntervalMult,0,0);
dtTick -= dtSpan;
break;
case tiDay:
dtSpan.SetDateTimeSpan(m_iDTTickIntervalMult,0,0,0);
dtTick -= dtSpan;
break;
case tiMonth:
dtTick = AddMonthToDate(dtTick,-m_iDTTickIntervalMult);
break;
case tiYear:
dtTick = AddMonthToDate(dtTick,-12*m_iDTTickIntervalMult);
break;
}
}
return dRetVal;
}
bool CChartDateTimeAxis::GetNextTickValue(double dCurrentTick, double& dNextTick) const
{
if (m_MinValue == m_MaxValue)
return false;
COleDateTime dtTick((DATE)dCurrentTick);
COleDateTimeSpan dtSpan;
switch (m_BaseInterval)
{
case tiSecond:
dtSpan.SetDateTimeSpan(0,0,0,m_iDTTickIntervalMult);
dtTick += dtSpan;
break;
case tiMinute:
dtSpan.SetDateTimeSpan(0,0,m_iDTTickIntervalMult,0);
dtTick += dtSpan;
break;
case tiHour:
dtSpan.SetDateTimeSpan(0,m_iDTTickIntervalMult,0,0);
dtTick += dtSpan;
break;
case tiDay:
dtSpan.SetDateTimeSpan(m_iDTTickIntervalMult,0,0,0);
dtTick += dtSpan;
break;
case tiMonth:
dtTick = AddMonthToDate(dtTick,m_iDTTickIntervalMult);
break;
case tiYear:
dtTick = AddMonthToDate(dtTick,12*m_iDTTickIntervalMult);
break;
}
dNextTick = (DATE)dtTick;
if (dNextTick <= m_MaxValue)
return true;
else
return false;
}
TChartString CChartDateTimeAxis::GetTickLabel(double TickValue) const
{
COleDateTime tickTime((DATE)TickValue);
TChartString strLabel = tickTime.Format(m_strDTTickFormat.c_str());
return strLabel;
}
long CChartDateTimeAxis::ValueToScreenDiscrete(double dValue) const
{
// In discrete mode, all values between two ticks relates
// to the middle of the interval (there's no other values than
// the tick values).
double tickAfter;
double tickBefore = GetTickBeforeVal(dValue);
GetNextTickValue(tickBefore, tickAfter);
long tickPosBefore = ValueToScreenStandard(tickBefore);
long tickPosAfter = ValueToScreenStandard(tickAfter);
return tickPosBefore + (tickPosAfter-tickPosBefore)/2;
}
long CChartDateTimeAxis::GetTickPos(double TickVal) const
{
// The tick is always at the same position,
// even if the axis is discrete.
return ValueToScreenStandard(TickVal);
}
COleDateTime CChartDateTimeAxis::AddMonthToDate(const COleDateTime& Date,
int iMonthsToAdd) const
{
COleDateTime newDate;
int nMonths = Date.GetMonth()-1 + iMonthsToAdd;
int nYear = Date.GetYear() + nMonths/12;;
// We can 'add' a negative number of months
if (nMonths<0)
{
nYear = Date.GetYear() - (-nMonths)/12;
nMonths += (-nMonths)/12 * 12;
}
newDate.SetDateTime(nYear,nMonths%12+1,Date.GetDay(),Date.GetHour(),
Date.GetMinute(),Date.GetSecond());
return newDate;
}
void CChartDateTimeAxis::RefreshTickIncrement()
{
if (!m_bAutoTicks)
return;
if (m_MaxValue == m_MinValue)
{
m_iDTTickIntervalMult = 1;
return;
}
int PixelSpace;
if (m_bIsHorizontal)
PixelSpace = 60;
else
PixelSpace = 20;
int MaxTickNumber = (int)fabs((m_EndPos-m_StartPos)/PixelSpace * 1.0);
if (MaxTickNumber == 0)
MaxTickNumber = 1;
COleDateTime StartDate(m_MinValue);
COleDateTime EndDate(m_MaxValue);
COleDateTimeSpan minTickInterval = (EndDate - StartDate)/MaxTickNumber;
double Seconds = minTickInterval.GetTotalSeconds();
double Minutes = minTickInterval.GetTotalMinutes();
double Hours = minTickInterval.GetTotalHours();
double Days = minTickInterval.GetTotalDays();
if (Seconds < 60)
{
m_BaseInterval = tiSecond;
if (Seconds > 30)
{
m_BaseInterval = tiMinute;
m_iDTTickIntervalMult = 1;
}
else if (Seconds > 10)
m_iDTTickIntervalMult = 30;
else if (Seconds > 5)
m_iDTTickIntervalMult = 10;
else if (Seconds > 2)
m_iDTTickIntervalMult = 5;
else
m_iDTTickIntervalMult = 1;
}
else if (Minutes < 60)
{
m_BaseInterval = tiMinute;
if (Minutes > 30)
{
m_BaseInterval = tiHour;
m_iDTTickIntervalMult = 1;
}
else if (Minutes > 10)
m_iDTTickIntervalMult = 30;
else if (Minutes > 5)
m_iDTTickIntervalMult = 10;
else if (Minutes > 2)
m_iDTTickIntervalMult = 5;
else
m_iDTTickIntervalMult = 2;
}
else if (Hours < 24)
{
m_BaseInterval = tiHour;
if (Hours > 12)
{
m_BaseInterval = tiDay;
m_iDTTickIntervalMult = 1;
}
else if (Hours > 6)
m_iDTTickIntervalMult = 12;
else if (Hours > 2)
m_iDTTickIntervalMult = 6;
else
m_iDTTickIntervalMult = 2;
}
else if (Days < 31)
{
m_BaseInterval = tiDay;
if (Days > 7)
{
m_BaseInterval = tiMonth;
m_iDTTickIntervalMult = 1;
}
else if (Days > 2)
{
m_BaseInterval = tiDay;
m_iDTTickIntervalMult = 7;
}
else
m_iDTTickIntervalMult = 2;
}
else if (Days < 365)
{
m_BaseInterval = tiMonth;
if (Days > 186) // Approx 6 months
{
m_BaseInterval = tiYear;
m_iDTTickIntervalMult = 1;
}
else if (Days > 124)
m_iDTTickIntervalMult = 6;
else if (Days > 62)
m_iDTTickIntervalMult = 4;
else
m_iDTTickIntervalMult = 2;
}
else
{
m_BaseInterval = tiYear;
m_iDTTickIntervalMult = (int)Days/365 + 1;
}
}
void CChartDateTimeAxis::RefreshFirstTick()
{
m_dFirstTickValue = GetTickBeforeVal(m_MinValue);
if (m_bAutoTickFormat)
RefreshDTTickFormat();
}
void CChartDateTimeAxis::SetReferenceTick(COleDateTime referenceTick)
{
m_ReferenceTick = referenceTick;
m_pParentCtrl->RefreshCtrl();
}
void CChartDateTimeAxis::RefreshDTTickFormat()
{
switch (m_BaseInterval)
{
case tiSecond:
m_strDTTickFormat = _T("%H:%M:%S");
break;
case tiMinute:
m_strDTTickFormat = _T("%H:%M");
break;
case tiHour:
m_strDTTickFormat = _T("%H:00");
break;
case tiDay:
m_strDTTickFormat = _T("%d %b");
break;
case tiMonth:
m_strDTTickFormat = _T("%b %Y");
break;
case tiYear:
m_strDTTickFormat = _T("%Y");
break;
}
}
double CChartDateTimeAxis::GetTickBeforeVal(double dValue) const
{
double precision = 0.0000000001;
if (dValue < 0)
precision = -0.0000000001;
COleDateTime tickBefore;
COleDateTime valueTime = COleDateTime(DATE(dValue+precision));
COleDateTimeSpan dtSpan = valueTime - m_ReferenceTick;
switch (m_BaseInterval)
{
case tiSecond:
{
int totalSecs = (int)dtSpan.GetTotalSeconds();
totalSecs = (totalSecs/m_iDTTickIntervalMult) * m_iDTTickIntervalMult;
int Days = totalSecs/86400; // 86400 seconds in one day
int Hours = (totalSecs%86400)/3600; // 3600 seconds in one hour
int Minutes = ((totalSecs%86400)%3600)/60; // 60 seconds in one minute
int Seconds = ((totalSecs%86400)%3600)%60;
dtSpan.SetDateTimeSpan(Days, Hours, Minutes, Seconds);
tickBefore = m_ReferenceTick + dtSpan;
}
break;
case tiMinute:
{
int totalMinutes = (int)dtSpan.GetTotalMinutes();
totalMinutes = (totalMinutes/m_iDTTickIntervalMult) * m_iDTTickIntervalMult;
int Days = totalMinutes/1440; // 1440 minutes in one day
int Hours = (totalMinutes%1440)/60; // 60 minutes in one hour
int Minutes = (totalMinutes%1440)%60;
dtSpan.SetDateTimeSpan(Days, Hours, Minutes, 0);
tickBefore = m_ReferenceTick + dtSpan;
}
break;
case tiHour:
{
int totalHours = (int)dtSpan.GetTotalHours();
totalHours = (totalHours/m_iDTTickIntervalMult) * m_iDTTickIntervalMult;
int Days = totalHours/24; // 24 hours in one day
int Hours = totalHours%24;
dtSpan.SetDateTimeSpan(Days, Hours, 0, 0);
tickBefore = m_ReferenceTick + dtSpan;
}
break;
case tiDay:
{
int totalDays = (int)dtSpan.GetTotalDays();
totalDays = (totalDays/m_iDTTickIntervalMult) * m_iDTTickIntervalMult;
dtSpan.SetDateTimeSpan(totalDays, 0, 0, 0);
tickBefore = m_ReferenceTick + dtSpan;
}
break;
case tiMonth:
{
int yearDiff = valueTime.GetYear() - m_ReferenceTick.GetYear();
int monthDiff = valueTime.GetMonth() - m_ReferenceTick.GetMonth();
int totalMonths = ((yearDiff*12+monthDiff)/m_iDTTickIntervalMult) * m_iDTTickIntervalMult;
tickBefore = AddMonthToDate(m_ReferenceTick,totalMonths);
}
break;
case tiYear:
{
int yearDiff = valueTime.GetYear() - m_ReferenceTick.GetYear();
int year = ((yearDiff)/m_iDTTickIntervalMult) * m_iDTTickIntervalMult;
tickBefore = AddMonthToDate(m_ReferenceTick,year*12);
}
break;
}
return (DATE)tickBefore;
}