// ClientFromChart.cpp : implementation file
//
#include "stdafx.h"
#include "ylgl.h"
#include "ClientFromChart.h"
#include "MyMdi.H"
#include "VcPlot.h"
#include "VcAxis.h"
#include "VcValueScale.h"
#include "VcSeriesCollection.h"
#include "VcSeries.h"
#include "VcPen.h"
#include "VcCategoryScale.h"
#include "VcColor.h"
#include "VcDataGrid.h"
#include "VcBackdrop.h"
#include "VcFill.h"
#include "VcBrush.h"
#include "VcDataPoints.h"
#include "VcDataPoint.h"
#include "VcDataPointLabel.h"
#include "VcAxisTitle.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// ClientFromChart IDC_STATIC1
IMPLEMENT_DYNCREATE(ClientFromChart, MyFormView)

ClientFromChart::ClientFromChart()
: MyFormView(ClientFromChart::IDD)
{
	//{{AFX_DATA_INIT(ClientFromChart)
	m_year = atoi(g_date.Mid(0, 4));
	m_month = atoi(g_date.Mid(5, 2));
	m_day = 12;
	m_radio1 = 1;
	m_bInit = 0;
	//}}AFX_DATA_INIT
	g_pMainWnd->GetOcx();
}

ClientFromChart::~ClientFromChart()
{
}

void ClientFromChart::DoDataExchange(CDataExchange* pDX)
{
	MyFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(ClientFromChart)
	DDX_Control(pDX, IDC_SPIN3, m_spinday);
	DDX_Control(pDX, IDC_SPIN2, m_spinmonth);
	DDX_Control(pDX, IDC_SPIN1, m_spinyear);
	DDX_Control(pDX, IDC_STATIC1, m_static1);
	DDX_Text(pDX, IDC_EDITyear, m_year);
	DDV_MinMaxUInt(pDX, m_year, 1900, 3000);
	DDX_Text(pDX, IDC_EDITmonth, m_month);
	DDV_MinMaxUInt(pDX, m_month, 1, 12);
	DDX_Text(pDX, IDC_EDITday, m_day);
	DDV_MinMaxUInt(pDX, m_day, 1, 31);
	DDX_Radio(pDX, IDC_RADIO3, m_radio1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(ClientFromChart, MyFormView)
	//{{AFX_MSG_MAP(ClientFromChart)
	ON_BN_CLICKED(IDC_BUTclose, OnBUTclose)
	ON_EN_CHANGE(IDC_EDITyear, OnChangeEDITyear)
	ON_EN_CHANGE(IDC_EDITmonth, OnChangeEDITmonth)
	ON_EN_CHANGE(IDC_EDITday, OnChangeEDITday)
	ON_BN_CLICKED(IDC_RADIO3, OnRadio3)
	ON_BN_CLICKED(IDC_RADIO4, OnRadio4)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_RADIO5, OnRadio5)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// ClientFromChart diagnostics

#ifdef _DEBUG
void ClientFromChart::AssertValid() const
{
	MyFormView::AssertValid();
}

void ClientFromChart::Dump(CDumpContext& dc) const
{
	MyFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// ClientFromChart message handlers

void ClientFromChart::OnInitialUpdate()
{
	MyFormView::OnInitialUpdate();
	// TODO: Add your specialized code here and/or call the base class
	CMyMdi Mdi;
	Mdi.SetSubView((CWnd*)GetParent(), (CWnd*)this);

	// Here we create the outbar control using the splitter as its parent
	// and setting its id to the first pane.
	CRect rc2;
	GetWindowRect(rc2);
	::MoveWindow(m_hWnd, g_rc.left, g_rc.top, g_rc.Width(), g_rc.Height(), TRUE);

	EnumChildWindows(m_hWnd, (WNDENUMPROC)EnumChildProc, 0);



	m_static1.SetFont(&g_titlefont);

	m_spinyear.SetRange(1900, 3000);
	m_spinmonth.SetRange(1, 12);
	m_spinday.SetRange(1, 31);


	CString version = _T("Version 1.0");
	m_day = AfxGetApp()->GetProfileInt(version, "rowcount4", 12);
	UpdateData(false);


	m_bInit = 1;
	DateChange();
}

void ClientFromChart::FillGrid()
{

}

void ClientFromChart::OnBUTclose()
{
	// TODO: Add your control notification handler code here
	GetParent()->SendMessage(WM_CLOSE);
}

void ClientFromChart::OnChangeEDITyear()
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the MyFormView::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	DateChange();
	// TODO: Add your control notification handler code here

}

void ClientFromChart::OnChangeEDITmonth()
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the MyFormView::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	DateChange();
	// TODO: Add your control notification handler code here

}

void ClientFromChart::OnChangeEDITday()
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the MyFormView::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	if (m_bInit == 0)return;
	DateChange();
	UpdateData();
	CString version = _T("Version 1.0");
	AfxGetApp()->WriteProfileInt(version, "rowcount4", m_day);
	// TODO: Add your control notification handler code here

}

void ClientFromChart::DateChange()
{
	if (m_bInit == 0)return;
	m_spinyear.EnableWindow(0);
	m_spinmonth.EnableWindow(0);
	m_spinday.EnableWindow(0);
	UpdateData();

	CString date1, date2;
	date1.Format("%d-%02d-01", m_year, m_month);
	date2.Format("%d-%02d-31", m_year, m_month);

	CString filter = "[date]>='" + date1 + "' and [date]<='" + date2 + "' and [from]<>''";

	g_sendhead.code[0] = 92;
	g_sendhead.tabcount = 1;
	g_sendhead.bsql = 0;
	g_pMainWnd->ProcessChatMessageRequest2(filter);
	if (g_bSendOK == 0)
	{
		m_spinyear.EnableWindow(1);
		m_spinmonth.EnableWindow(1);
		m_spinday.EnableWindow(1);
		return;
	}
	DataToArray(&m_List1array);
	KindChange();
	m_spinyear.EnableWindow(1);
	m_spinmonth.EnableWindow(1);
	m_spinday.EnableWindow(1);
}

void ClientFromChart::KindChange()
{
	if (m_day < 1)return;
	int chartcount = m_List1array.GetSize() / m_day;
	if (m_List1array.GetSize() % m_day)chartcount++;

	for (int i = 0; i < m_pChartArray.GetSize(); i++)
	{
		CMSChart *pChart = m_pChartArray.ElementAt(i);
		pChart->ShowWindow(SW_HIDE);
	}
	if (chartcount == 0)
		return;
	while (m_pChartArray.GetSize() < chartcount)
	{
		CMSChart *pChart = new CMSChart;
		CRect rc(0, 0, 0, 0);
		WCHAR pwchLicenseKey[] =
		{
			0x0038, 0x0045, 0x0031, 0x0034, 0x0037, 0x0043,
			0x0036, 0x0039, 0x002D, 0x0042, 0x0044, 0x0035,
			0x0030, 0x002D, 0x0031, 0x0031, 0x0064, 0x0031,
			0x002D, 0x0042, 0x0031, 0x0033, 0x0037, 0x002D,
			0x0030, 0x0030, 0x0030, 0x0030, 0x0046, 0x0038,
			0x0037, 0x0035, 0x0033, 0x0046, 0x0035, 0x0044
		};
		BSTR bstrLicense = ::SysAllocStringLen(pwchLicenseKey,
			sizeof(pwchLicenseKey) / sizeof(WCHAR));


		pChart->Create("", WS_VISIBLE | WS_CHILD, rc, this, m_pChartArray.GetSize(), NULL, 0, bstrLicense);

		::SysFreeString(bstrLicense);
		m_pChartArray.Add(pChart);
		InitChartStyle(pChart);
		SetChartStyle(pChart);
	}
	CRect rc;
	GetDlgItem(IDC_STATIC2)->GetWindowRect(rc);
	ScreenToClient(rc);
	int hei = rc.Height() / chartcount;
	for (i = 0; i < chartcount; i++)
	{
		CMSChart *pChart = m_pChartArray.ElementAt(i);
		rc.bottom = rc.top + hei;
		pChart->MoveWindow(rc);
		pChart->ShowWindow(SW_SHOW);
		rc.top += hei;

		int nRowCount = min(m_day, m_List1array.GetSize() - i*m_day);
		pChart->SetRowCount(nRowCount);

		for (int row = 1; row <= nRowCount; ++row)
		{
			pChart->SetRow(row);
			pChart->SetRowLabel(m_List1array.ElementAt(i*m_day + row - 1).ElementAt(0));
			pChart->GetDataGrid().SetData(row, 1, atof(m_List1array.ElementAt(i*m_day + row - 1).ElementAt(1)), 0);
		}
		pChart->Refresh();
	}
	OnRadio3();
}

void ClientFromChart::OnRadio3()
{
	// TODO: Add your control notification handler code here
	UpdateData();
	if (m_radio1 == 0)
	{
		for (int i = 0; i < m_pChartArray.GetSize(); i++)
		{
			CMSChart *pChart = m_pChartArray.ElementAt(i);
			pChart->SetChartType(3);
		}
	}
	else if (m_radio1 == 1)
	{
		for (int i = 0; i < m_pChartArray.GetSize(); i++)
		{
			CMSChart *pChart = m_pChartArray.ElementAt(i);
			pChart->SetChartType(1);
		}
	}
	else if (m_radio1 == 2)
	{
		for (int i = 0; i < m_pChartArray.GetSize(); i++)
		{
			CMSChart *pChart = m_pChartArray.ElementAt(i);
			pChart->SetChartType(14);
		}
	}
}

void ClientFromChart::OnRadio4()
{
	// TODO: Add your control notification handler code here
	OnRadio3();
}

void ClientFromChart::OnRadio5()
{
	// TODO: Add your control notification handler code here
	OnRadio3();
}

void ClientFromChart::SetChartStyle(CMSChart *pChart)
{
	pChart->SetColumnCount(1);
	pChart->SetColumn(1);
	//	pChart->SetColumnLabel((LPCTSTR)"ȫ��");

	pChart->GetPlot().GetSeriesCollection().GetItem(1).GetDataPoints().GetItem(-1).GetBrush().GetFillColor().Set(255, 0, 0);
	// �߿�(�Ե���ͼ��Ч)
	pChart->GetPlot().GetSeriesCollection().GetItem(1).GetPen().SetWidth(30);
	// ���ݵ�������ʾ����ֵ��ģʽ(������״ͼ�͵���ͼ��Ч)
	// 0: ����ʾ	1: ��ʾ����״ͼ��
	// 2: ��ʾ����״ͼ���Ϸ�	3: ��ʾ����״ͼ���м�	4: ��ʾ����״ͼ���·�
	pChart->GetPlot().GetSeriesCollection().GetItem(1).GetDataPoints().GetItem(-1).GetDataPointLabel().SetLocationType(1);

}

void ClientFromChart::InitChartStyle(CMSChart *pChart)
{
	VARIANT var;
	pChart->GetBackdrop().GetFill().SetStyle(1);
	pChart->GetBackdrop().GetFill().GetBrush().GetFillColor().Set(255, 255, 255);
	pChart->SetShowLegend(false);
	pChart->SetChartType(3);
	// ջģʽ
	pChart->SetStacking(FALSE);
	pChart->GetPlot().GetAxis(1, var).GetValueScale().SetAuto(1);	// ���Զ���עY��̶�
	pChart->GetPlot().GetAxis(0, var).GetCategoryScale().SetAuto(0);			// ���Զ���עX��̶�
	pChart->GetPlot().GetAxis(0, var).GetCategoryScale().SetDivisionsPerLabel(1);// ÿ�̶�һ����ע
	pChart->GetPlot().GetAxis(0, var).GetCategoryScale().SetDivisionsPerTick(1); // ÿ�̶�һ���̶���
}

void ClientFromChart::OnDestroy()
{

	for (int i = 0; i < m_pChartArray.GetSize(); i++)
	{
		CMSChart *pChart = m_pChartArray.ElementAt(i);
		pChart->DestroyWindow();
		delete pChart;
	}
	m_pChartArray.RemoveAll();
	MyFormView::OnDestroy();
}

BOOL ClientFromChart::PreTranslateMessage(MSG* pMsg)
{
	// TODO: Add your specialized code here and/or call the base class
	try
	{
		if (pMsg->message == WM_KEYDOWN)
		{
			switch (pMsg->wParam)
			{
			case 0x43: // copy
				if ((GetKeyState(VK_CONTROL) & 0x80))
				{
					GetFocus()->SendMessage(WM_COPY);
					return TRUE;
				}
				break;
			case 0x56: //Ctrl + V:
				if ((GetKeyState(VK_CONTROL) & 0x80))
				{
					GetFocus()->SendMessage(WM_PASTE);
					return TRUE;
				}
				break;
			case 0x58: // cut
				if ((GetKeyState(VK_CONTROL) & 0x80))
				{
					GetFocus()->SendMessage(WM_CUT);
					return TRUE;
				}
				break;
			case 0x5A: //undo
			case 0x59: //redo
				if ((GetKeyState(VK_CONTROL) & 0x80))
				{
					GetFocus()->SendMessage(WM_UNDO);
					return TRUE;
				}
				break;
			}
		}
		return MyFormView::PreTranslateMessage(pMsg);
	}
	catch (...)
	{
	}
}