summaryrefslogtreecommitdiff
path: root/plugins/HistoryStats/src/column_splittimeline.cpp
blob: 50d1d6ca7bc07e4d43ef7171786c16a8c9884fd8 (plain)
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
#include "stdafx.h"
#include "column_splittimeline.h"

/*
 * ColSplitTimeline
 */

ColSplitTimeline::ColSplitTimeline()
	: m_nSource(0), m_nSourceType(2), m_nIgnoreOld(0), m_nVisMode(0),
	m_nHODGroup(1), m_nDOWGroup(1), m_nBlockUnit(0), m_nUnitsPerBlock(6),
	m_nBlocks(28), m_nGraphAlign(1), m_nCustomGroup(1), m_bTopPerColumn(true),
	m_hSource(nullptr), m_hIgnoreOld(nullptr), m_hVisMode(nullptr),
	m_hHODGroup(nullptr), m_hDOWGroup(nullptr), m_hBlockUnit(nullptr), m_hUnitsPerBlock(nullptr),
	m_hBlocks(nullptr), m_hGraphAlign(nullptr), m_hCustomGroup(nullptr),m_hTopPerColumn(nullptr),
	m_nTimeDiv(3600), m_nTimeOffset(0),
	m_nTimelineWidth(0), m_nBlockOffset(0), m_nNumBlocks(0)
{
}

void ColSplitTimeline::impl_copyConfig(const Column* pSource)
{
	const ColSplitTimeline& src = *reinterpret_cast<const ColSplitTimeline*>(pSource);

	m_nSource        = src.m_nSource;
	m_nSourceType    = src.m_nSourceType;
	m_nIgnoreOld     = src.m_nIgnoreOld;
	m_nVisMode       = src.m_nVisMode;
	m_nHODGroup      = src.m_nHODGroup;
	m_nDOWGroup      = src.m_nDOWGroup;
	m_nBlockUnit     = src.m_nBlockUnit;
	m_nUnitsPerBlock = src.m_nUnitsPerBlock;
	m_nBlocks        = src.m_nBlocks;
	m_nGraphAlign    = src.m_nGraphAlign;
	m_nCustomGroup   = src.m_nCustomGroup;
	m_bTopPerColumn  = src.m_bTopPerColumn;
}

void ColSplitTimeline::impl_configRead(const SettingsTree& settings)
{
	m_nSource        = settings.readIntRanged(con::KeySource, 0, 0, 2);
	m_nSourceType    = settings.readIntRanged(con::KeySourceType, 2, 0, 3);
	m_nIgnoreOld     = settings.readIntRanged(con::KeyIgnoreOld, 0, 0, 1000);
	m_nVisMode       = settings.readIntRanged(con::KeyVisMode, 0, 0, 2);
	m_nHODGroup      = settings.readIntRanged(con::KeyHODGroup, 1, 1, 1000);
	m_nDOWGroup      = settings.readIntRanged(con::KeyDOWGroup, 1, 1, 1000);
	m_nBlockUnit     = settings.readIntRanged(con::KeyBlockUnit, 0, 0, 2);
	m_nUnitsPerBlock = settings.readIntRanged(con::KeyUnitsPerBlock, 6, 1, 100);
	m_nBlocks        = settings.readIntRanged(con::KeyBlocks, 28, 1, 49);
	m_nGraphAlign    = settings.readIntRanged(con::KeyGraphAlign, 1, 0, 1);
	m_nCustomGroup   = settings.readIntRanged(con::KeyCustomGroup, 1, 1, 1000);
	m_bTopPerColumn  = settings.readBool(con::KeyTopPerColumn, true);
}

void ColSplitTimeline::impl_configWrite(SettingsTree& settings) const
{
	settings.writeInt (con::KeySource, m_nSource);
	settings.writeInt (con::KeySourceType, m_nSourceType);
	settings.writeInt (con::KeyIgnoreOld, m_nIgnoreOld);
	settings.writeInt (con::KeyVisMode, m_nVisMode);
	settings.writeInt (con::KeyHODGroup, m_nHODGroup);
	settings.writeInt (con::KeyDOWGroup, m_nDOWGroup);
	settings.writeInt (con::KeyBlockUnit, m_nBlockUnit);
	settings.writeInt (con::KeyUnitsPerBlock, m_nUnitsPerBlock);
	settings.writeInt (con::KeyBlocks, m_nBlocks);
	settings.writeInt (con::KeyGraphAlign, m_nGraphAlign);
	settings.writeInt (con::KeyCustomGroup, m_nCustomGroup);
	settings.writeBool(con::KeyTopPerColumn, m_bTopPerColumn);
}

void ColSplitTimeline::impl_configToUI(OptionsCtrl& Opt, OptionsCtrl::Item hGroup)
{
	OptionsCtrl::Group hTemp;
	OptionsCtrl::Radio hTempRadio;

	m_hSource                 = Opt.insertCombo(hGroup, TranslateT("Data source"));
	m_hIgnoreOld              = Opt.insertEdit(hGroup, TranslateT("Drop everything older than (days, 0=no limit)"), L"", OptionsCtrl::OCF_NUMBER);
	hTemp                     = Opt.insertGroup(hGroup, TranslateT("\"Split\" type"));
		m_hVisMode             = Opt.insertRadio(hTemp, nullptr, TranslateT("Hours of day"), OptionsCtrl::OCF_DISABLECHILDSONUNCHECK);
			m_hHODGroup         = Opt.insertEdit (m_hVisMode, TranslateT("Number of days to group"), L"", OptionsCtrl::OCF_NUMBER);
		hTempRadio             = Opt.insertRadio(hTemp, m_hVisMode, TranslateT("Days of week"), OptionsCtrl::OCF_DISABLECHILDSONUNCHECK);
			m_hDOWGroup         = Opt.insertEdit (hTempRadio, TranslateT("Number of weeks to group"), L"", OptionsCtrl::OCF_NUMBER);
		hTempRadio             = Opt.insertRadio(hTemp, m_hVisMode, TranslateT("Custom (for experts only)"), OptionsCtrl::OCF_DISABLECHILDSONUNCHECK);
			hTemp               = Opt.insertGroup(hTempRadio, TranslateT("Column setup"));
				m_hBlockUnit     = Opt.insertCombo(hTemp, TranslateT("Block unit"));
				m_hUnitsPerBlock = Opt.insertEdit (hTemp, TranslateT("Units per block"), L"", OptionsCtrl::OCF_NUMBER);
				m_hBlocks        = Opt.insertEdit (hTemp, TranslateT("Blocks per column"), L"", OptionsCtrl::OCF_NUMBER);
			hTemp               = Opt.insertGroup(hTempRadio, TranslateT("Graph alignment"));
				m_hGraphAlign    = Opt.insertRadio(hTemp, nullptr, TranslateT("Align on day boundary"));
				                   Opt.insertRadio(hTemp, m_hGraphAlign, TranslateT("Align on week boundary"));
			m_hCustomGroup      = Opt.insertEdit (hTempRadio, TranslateT("Number of columns to group"), L"", OptionsCtrl::OCF_NUMBER);
	m_hTopPerColumn           = Opt.insertCheck(hGroup, TranslateT("Calculate maximum per column (not per graph)"));

	static const wchar_t* sourceTexts[] = {
		LPGENW("Characters (incoming)"),
		LPGENW("Characters (outgoing)"),
		LPGENW("Characters (all)"),
		LPGENW("Characters (in/out ratio)"),
		LPGENW("Messages (incoming)"),
		LPGENW("Messages (outgoing)"),
		LPGENW("Messages (all)"),
		LPGENW("Messages (in/out ratio)"),
		LPGENW("Chats (incoming)"),
		LPGENW("Chats (outgoing)"),
		LPGENW("Chats (all)"),
		LPGENW("Chats (in/out ratio)"),
	};

	array_each_(i, sourceTexts)
	{
		Opt.addComboItem(m_hSource, TranslateW(sourceTexts[i]));
	}

	static const wchar_t* unitTexts[] = {
		LPGENW("Hours"),
		LPGENW("Days"),
		LPGENW("Weeks"),
	};

	array_each_(i, unitTexts)
	{
		Opt.addComboItem(m_hBlockUnit, TranslateW(unitTexts[i]));
	}

	Opt.setComboSelected(m_hSource       , 4 * m_nSource + m_nSourceType);
	Opt.setEditNumber   (m_hIgnoreOld    , m_nIgnoreOld                 );
	Opt.setRadioChecked (m_hVisMode      , m_nVisMode                   );
	Opt.setEditNumber   (m_hHODGroup     , m_nHODGroup                  );
	Opt.setEditNumber   (m_hDOWGroup     , m_nDOWGroup                  );
	Opt.setComboSelected(m_hBlockUnit    , m_nBlockUnit                 );
	Opt.setEditNumber   (m_hUnitsPerBlock, m_nUnitsPerBlock             );
	Opt.setEditNumber   (m_hBlocks       , m_nBlocks                    );
	Opt.setRadioChecked (m_hGraphAlign   , m_nGraphAlign                );
	Opt.setEditNumber   (m_hCustomGroup  , m_nCustomGroup               );
	Opt.checkItem       (m_hTopPerColumn , m_bTopPerColumn              );
}

void ColSplitTimeline::impl_configFromUI(OptionsCtrl& Opt)
{
	m_nSource        = Opt.getComboSelected(m_hSource       ) / 4;
	m_nSourceType    = Opt.getComboSelected(m_hSource       ) % 4;
	m_nIgnoreOld     = Opt.getEditNumber   (m_hIgnoreOld    );
	m_nVisMode       = Opt.getRadioChecked (m_hVisMode      );
	m_nHODGroup      = Opt.getEditNumber   (m_hHODGroup     );
	m_nDOWGroup      = Opt.getEditNumber   (m_hDOWGroup     );
	m_nBlockUnit     = Opt.getComboSelected(m_hBlockUnit    );
	m_nUnitsPerBlock = Opt.getEditNumber   (m_hUnitsPerBlock);
	m_nBlocks        = Opt.getEditNumber   (m_hBlocks       );
	m_nGraphAlign    = Opt.getRadioChecked (m_hGraphAlign   );
	m_nCustomGroup   = Opt.getEditNumber   (m_hCustomGroup  );
	m_bTopPerColumn  = Opt.isItemChecked   (m_hTopPerColumn );

	// ensure constraints
	utils::ensureRange(m_nIgnoreOld, 0, 1000, 0);
	utils::ensureRange(m_nHODGroup, 1, 1000, 1);
	utils::ensureRange(m_nDOWGroup, 1, 1000, 1);
	utils::ensureRange(m_nUnitsPerBlock, 1, 100, 6);
	utils::ensureRange(m_nBlocks, 1, 49, 28);
	utils::ensureRange(m_nCustomGroup, 1, 1000, 1);
}

ext::string ColSplitTimeline::impl_contactDataGetUID() const
{
	SplitParams params = getParams();
	
	return ext::str(ext::format(L"splittimeline-|-|-|")
		% m_nSource
		% params.hours_in_block
		% params.alignment);
}

void ColSplitTimeline::impl_contactDataBeginAcquire()
{
	SplitParams params = getParams();

	m_nTimeDiv = 3600 * params.hours_in_block;
	
	if (params.alignment == 1)
	{
		time_t offset = 0;
		struct tm* offsetTM = gmtime(&offset);

		m_nTimeOffset = 86400 * ((offsetTM->tm_wday + 6) % 7);
	}
	else
	{
		m_nTimeOffset = 0;
	}
}

void ColSplitTimeline::impl_contactDataPrepare(Contact& contact) const
{
	TimelineMap* pData = new TimelineMap;

	contact.setSlot(contactDataSlotGet(), pData);
}

void ColSplitTimeline::impl_contactDataFree(Contact& contact) const
{
	TimelineMap* pData = reinterpret_cast<TimelineMap*>(contact.getSlot(contactDataSlotGet()));

	if (pData)
	{
		delete pData;
		contact.setSlot(contactDataSlotGet(), nullptr);
	}
}

void ColSplitTimeline::addToSlot(Contact& contact, bool bOutgoing, DWORD localTimestamp, int toAdd)
{
	if (toAdd > 0)
	{
		TimelineMap* pData = reinterpret_cast<TimelineMap*>(contact.getSlot(contactDataSlotGet()));
		
		InOut& io = (*pData)[(localTimestamp + m_nTimeOffset) / m_nTimeDiv];
		
		(bOutgoing ? io.out : io.in) += toAdd;
	}
}

void ColSplitTimeline::impl_contactDataAcquireMessage(Contact& contact, Message& msg)
{
	if (m_nSource == 0)
	{
		addToSlot(contact, msg.isOutgoing(), msg.getTimestamp(), msg.getLength());
	}
	else if (m_nSource == 1)
	{
		addToSlot(contact, msg.isOutgoing(), msg.getTimestamp(), 1);
	}
}

void ColSplitTimeline::impl_contactDataAcquireChat(Contact& contact, bool bOutgoing, DWORD localTimestampStarted, DWORD)
{
	if (m_nSource == 2)
	{
		addToSlot(contact, bOutgoing, localTimestampStarted, 1);
	}
}

void ColSplitTimeline::impl_contactDataMerge(Contact& contact, const Contact& include) const
{
	TimelineMap* pData = reinterpret_cast<TimelineMap*>(contact.getSlot(contactDataSlotGet()));
	const TimelineMap* pIncData = reinterpret_cast<const TimelineMap*>(include.getSlot(contactDataSlotGet()));

	citer_each_(TimelineMap, i, *pIncData)
	{
		(*pData)[i->first] += i->second;
	}
}

void ColSplitTimeline::impl_outputRenderHeader(ext::ostream& tos, int row, int rowSpan) const
{
	static const wchar_t* szTypeDesc[] = {
		LPGENW("Hours of day timeline"),
		LPGENW("Days of week timeline"),
		LPGENW("\"Split\" timeline"),
	};

	static const wchar_t* szSourceDesc[] = {
		LPGENW("incoming characters"),
		LPGENW("outgoing characters"),
		LPGENW("all characters"),
		LPGENW("in/out ratio of characters"),
		LPGENW("incoming messages"),
		LPGENW("outgoing messages"),
		LPGENW("all messages"),
		LPGENW("in/out ratio of messages"),
		LPGENW("incoming chats"),
		LPGENW("outgoing chats"),
		LPGENW("all chats"),
		LPGENW("in/out ratio of chats"),
	};

	if (row == 1)
	{
		SplitParams params = getParams();
		ext::string strTitle = str(ext::kformat(TranslateT("#{type} for #{data}"))
			% L"#{type}" * TranslateW(szTypeDesc[params.effective_vis_mode])
			% L"#{data}" * TranslateW(szSourceDesc[4 * m_nSource + m_nSourceType]));

		writeRowspanTD(tos, getCustomTitle(TranslateW(szTypeDesc[params.effective_vis_mode]), strTitle) + L"<div style=\"width: " + utils::intToString(m_nTimelineWidth) + L"px;\"></div>", row, 1, rowSpan);
	}
}

void ColSplitTimeline::impl_columnDataAfterOmit()
{
	// AFTER, i.e. contacts are trimmed to what user will see

	// init columns that are active but not acquireing
	impl_contactDataBeginAcquire();
	
	// put _all_ available contacts (including omitted/total) in one list
	Statistic::ContactListC l;

	upto_each_(i, getStatistic()->countContacts())
	{
		l.push_back(&getStatistic()->getContact(i));
	}

	if (getStatistic()->hasOmitted())
	{
		l.push_back(&getStatistic()->getOmitted());
	}

	if (getStatistic()->hasTotals())
	{
		l.push_back(&getStatistic()->getTotals());
	}

	SplitParams params = getParams();

	if (l.size() > 0)
	{
		DWORD nFirstTime = con::MaxDateTime, nLastTime = con::MinDateTime;

		citer_each_(Statistic::ContactListC, c, l)
		{
			if ((*c)->isFirstLastTimeValid())
			{
				nFirstTime = min(nFirstTime, (*c)->getFirstTime());
				nLastTime = max(nLastTime, (*c)->getLastTime());
			}
		}

		if (nFirstTime == con::MaxDateTime && nLastTime == con::MinDateTime)
		{
			nFirstTime = nLastTime = 0;
		}

		// honour ignore setting
		if (m_nIgnoreOld > 0 && nLastTime > m_nIgnoreOld * 86400 && nLastTime - nFirstTime > m_nIgnoreOld * 86400)
		{
			nFirstTime = nLastTime - m_nIgnoreOld * 86400;
		}

		// always align to day boundary
		nFirstTime = (nFirstTime / 86400) * 86400;

		if (params.alignment == 1)
		{
			// align on week boundary
			nFirstTime = ((nFirstTime + m_nTimeOffset) / 604800) * 604800 - m_nTimeOffset;
		}

		// correct with "time offset"
		nFirstTime += m_nTimeOffset;
		nLastTime += m_nTimeOffset;

		m_nBlockOffset = nFirstTime / m_nTimeDiv;
		m_nNumBlocks = nLastTime / m_nTimeDiv - m_nBlockOffset + 1;
	}
	else
	{
		m_nBlockOffset = m_nNumBlocks = 0;
	}

	m_nTimelineWidth = 3 * ((m_nNumBlocks + params.blocks_in_column * params.columns_to_group - 1) / (params.blocks_in_column * params.columns_to_group)); 
}

void ColSplitTimeline::impl_outputRenderRow(ext::ostream& tos, const Contact& contact, DisplayType display)
{
	if (m_nSourceType != 3)
	{
		outputRenderRowInOut(tos, contact, display);
	}
	else
	{
		outputRenderRowRatio(tos, contact, display);
	}
}

ColSplitTimeline::SplitParams ColSplitTimeline::getParams() const
{
	static const int unitFactors[] = { 1, 24, 168 };

	SplitParams params;

	params.effective_vis_mode = m_nVisMode;

	switch (m_nVisMode)
	{
		case 0: // hours of day
			{
				params.alignment = 0;
				params.columns_to_group = m_nHODGroup;
				params.hours_in_block = 1;
				params.blocks_in_column = 24;
			}
			break;

		case 1: // days of week
			{
				params.alignment = 1;
				params.columns_to_group = m_nDOWGroup;
				params.hours_in_block = 24;
				params.blocks_in_column = 7;
			}
			break;

		default: // custom
			{
				params.alignment = m_nGraphAlign;
				params.columns_to_group = m_nCustomGroup;
				params.hours_in_block = unitFactors[m_nBlockUnit] * m_nUnitsPerBlock;
				params.blocks_in_column = m_nBlocks;

				// heuristics for custom mode
				if (params.hours_in_block == 1 && params.blocks_in_column == 24)
				{
					params.effective_vis_mode = 0;
				}
				else if (params.hours_in_block == 24 && params.blocks_in_column == 7 && params.alignment == 1)
				{
					params.effective_vis_mode = 1;
				}
			}
			break;
	}

	return params;
}

void ColSplitTimeline::outputRenderRowInOut(ext::ostream& tos, const Contact& contact, DisplayType)
{
	SplitParams params = getParams();
	const TimelineMap* pData = reinterpret_cast<const TimelineMap*>(contact.getSlot(contactDataSlotGet()));

	int top = 0;

	if (!m_bTopPerColumn)
	{
		// calculate a global maximum
		for (int curBlock = 0; curBlock < m_nNumBlocks; curBlock += params.blocks_in_column * params.columns_to_group)
		{
			for (int partBlock = 0; partBlock < params.blocks_in_column; ++partBlock)
			{
				int part_top = 0;
				DWORD block_time = m_nBlockOffset + curBlock + partBlock;

				for (int curCol = 0; curCol < params.columns_to_group; ++curCol)
				{
					DWORD cur_time = block_time + curCol * params.blocks_in_column;

					TimelineMap::const_iterator i = pData->find(cur_time);
					
					if (i != pData->end())
					{
						part_top += getValue(i->second);
					}
				}

				top = max(top, part_top);
			}
		}

		if (top == 0)
		{
			top = 1;
		}
	}

	tos << L"<td class=\"img_middle\">";

	// draw graph
	Canvas canvas(m_nTimelineWidth, 49);

	canvas.fillBackground(con::ColorBack);

	COLORREF colorTab[256];

	utils::generateGradient(con::ColorBack, con::ColorBar, colorTab);

	HDC hDC = canvas.beginDraw();
	
	for (int curBlock = 0; curBlock < m_nNumBlocks; curBlock += params.blocks_in_column * params.columns_to_group)
	{
		int from_left = 3 * curBlock / (params.blocks_in_column * params.columns_to_group);

		if (m_bTopPerColumn)
		{
			// calculate a local maximum (per column)
			top = 0;

			for (int partBlock = 0; partBlock < params.blocks_in_column; ++partBlock)
			{
				int part_top = 0;
				DWORD block_time = m_nBlockOffset + curBlock + partBlock;

				for (int curCol = 0; curCol < params.columns_to_group; ++curCol)
				{
					DWORD cur_time = block_time + curCol * params.blocks_in_column;

					TimelineMap::const_iterator i = pData->find(cur_time);
					
					if (i != pData->end())
					{
						part_top += getValue(i->second);
					}
				}

				top = max(top, part_top);
			}

			if (top == 0)
			{
				top = 1;
			}
		}

		for (int partBlock = 0; partBlock < params.blocks_in_column; ++partBlock)
		{
			int part_top = 0;
			DWORD block_time = m_nBlockOffset + curBlock + partBlock;

			for (int curCol = 0; curCol < params.columns_to_group; ++curCol)
			{
				DWORD cur_time = block_time + curCol * params.blocks_in_column;

				TimelineMap::const_iterator i = pData->find(cur_time);
				
				if (i != pData->end())
				{
					part_top += getValue(i->second);
				}
			}

			if (part_top != 0)
			{
				RECT r = {
					from_left,
					49 * partBlock / params.blocks_in_column,
					from_left + 3,
					49 * (partBlock + 1) / params.blocks_in_column
				};

				int color = 255 * part_top / top;

				SetBkColor(hDC, colorTab[color]);
				ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &r, nullptr, 0, nullptr);
			}
		}
	}

	canvas.endDraw();

	// write PNG file
	ext::string strFinalFile;
	
	if (getStatistic()->newFilePNG(canvas, strFinalFile))
	{
		tos << L"<img src=\"" << strFinalFile << L"\" alt=\"\" />";
	}

	tos << L"</td>" << ext::endl;
}

void ColSplitTimeline::outputRenderRowRatio(ext::ostream& tos, const Contact& contact, DisplayType)
{
	SplitParams params = getParams();
	const TimelineMap* pData = reinterpret_cast<const TimelineMap*>(contact.getSlot(contactDataSlotGet()));

	tos << L"<td class=\"img_middle\">";

	// draw graph
	Canvas canvas(m_nTimelineWidth, 49);

	canvas.fillBackground(con::ColorBack);

	COLORREF inColorTab[256], outColorTab[256];

	utils::generateGradient(con::ColorBack, con::ColorIn, inColorTab);
	utils::generateGradient(con::ColorBack, con::ColorOut, outColorTab);

	HDC hDC = canvas.beginDraw();
	
	for (int curBlock = 0; curBlock < m_nNumBlocks; curBlock += params.blocks_in_column * params.columns_to_group)
	{
		int from_left = 3 * curBlock / (params.blocks_in_column * params.columns_to_group);

		for (int partBlock = 0; partBlock < params.blocks_in_column; ++partBlock)
		{
			int part_in = 0;
			int part_out = 0;
			DWORD block_time = m_nBlockOffset + curBlock + partBlock;

			for (int curCol = 0; curCol < params.columns_to_group; ++curCol)
			{
				DWORD cur_time = block_time + curCol * params.blocks_in_column;

				TimelineMap::const_iterator i = pData->find(cur_time);
					
				if (i != pData->end())
				{
					part_in += i->second.in;
					part_out += i->second.out;
				}
			}

			int part_sum = part_in + part_out;

			if (part_sum != 0)
			{
				RECT r = {
					from_left,
					49 * partBlock / params.blocks_in_column,
					from_left + 3,
					49 * (partBlock + 1) / params.blocks_in_column
				};

				COLORREF color = inColorTab[0];

				if (part_in > part_out)
				{
					color = inColorTab[255 * (2 * part_in - part_sum) / part_sum];
				}
				else if (part_in < part_out)
				{
					color = outColorTab[255 * (2 * part_out - part_sum) / part_sum];
				}

				SetBkColor(hDC, color);
				ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &r, nullptr, 0, nullptr);
			}
		}
	}

	canvas.endDraw();

	// write PNG file
	ext::string strFinalFile;

	if (getStatistic()->newFilePNG(canvas, strFinalFile))
	{
		tos << L"<img src=\"" << strFinalFile << L"\" alt=\"\" />";
	}

	tos << L"</td>" << ext::endl;
}