⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chartbase.cpp

📁 这是本人两年前兼职为某个公司做的石油钻进设计软件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// DrawBase.cpp: implementation of the CChartBase class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "CVenus.h"
#include "ChartBase.h"
#include "Series.h"
#include "Legend.h"
#include "Aspect.h"
#include "Walls.h"
#include "Panel.h"
#include "Gradient.h"
#include "Titles.h"
#include "PolarSeries.h"
#include "FastLineSeries.h"
#include "CircleLabels.h"
#include "Pointer.h"
#include "ChartFont.h"
#include "Strings.h"
#include "ValueList.h"
#include "Marks.h"
#include "Scroll.h"
#include "Axes.h"
#include "Axis.h"
#include "Zoom.h"
#include "VolumeSeries.h"
#include "Pen.h"
#include "Canvas.h"
#include "Brush.h"
#include "Point3DSeries.h"
#include "PointSeries.h"
#include "AxisLabels.h"
#include "AxisTitle.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNCREATE(CChartBase, CTChart)

CChartBase::CChartBase()
{
	m_bShowCursor = FALSE;
	m_bMoveCursor = FALSE;
	m_lCursor     = 1;
	m_dLineLen    = 0;
	m_lStep       = 1; 
	m_bMouseSel   = FALSE;
	m_lSelEndX    = 0;
	m_bZoom       = FALSE;
	m_bGraphPre   = FALSE;
	m_dZoomXMin   = m_dZoomXMax = m_dZoomYMin = m_dZoomYMax = 0;
	cross         = AfxGetApp()->LoadStandardCursor(IDC_CROSS);
	arrow         = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
	m_bDrawLineMark = FALSE;
	m_iGraphLoc   = 0;
}

CChartBase::~CChartBase()
{
}

BEGIN_EVENTSINK_MAP(CChartBase, CTChart)
    //{{AFX_EVENTSINK_MAP(CChartBase)
	ON_EVENT(CChartBase, 30000, 19 /* OnMouseDown */, OnMouseDownTChart, VTS_I4 VTS_I4 VTS_I4 VTS_I4)
	ON_EVENT(CChartBase, 30000, 20 /* OnMouseMove */, OnMouseMoveTChart, VTS_I4 VTS_I4 VTS_I4)
	ON_EVENT(CChartBase, 30000, 21 /* OnMouseUp */, OnMouseUpTChart, VTS_I4 VTS_I4 VTS_I4 VTS_I4)
	ON_EVENT(CChartBase, 30000, 1  /* OnAfterDraw */, OnAfterDrawTChart, VTS_NONE)
	ON_EVENT(CChartBase, 30000, 7 /* OnClickSeries */, OnOnClickSeriesTchart, VTS_I4 VTS_I4 VTS_I4 VTS_I4 VTS_I4 VTS_I4)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

/*************************************************************************
 *
 * InitCtrl()
 *
 * 功  能: 初始化TeeChart
 *
 * 参  数: DWORD dwStyle:    窗口风格
 *         const RECT& rect: 图表区域
 *         CWnd* pParentWnd: 父窗口指针
 *         UINT uiID:        资源ID
 *         CString Caption:  控件标题  
 *
 * 返回值: 无
 *
 *************************************************************************/
BOOL CChartBase::Create(DWORD dwStyle, const RECT& rect, UINT uiID, CWnd* pParentWnd, CString Caption)
{
	if(pParentWnd == NULL) return FALSE;

	// 创建TeeChart
	if( !((CTChart*)this)->Create("", dwStyle, rect, pParentWnd, uiID) )
	{
		return FALSE;
	}

	m_cCaption  = Caption;

	return TRUE;
}
/*************************************************************************
 *
 * InitChart()
 *
 * 功  能: 设置CHART样式
 *
 * 参  数: BYTE type: 作图类别
 *         
 * 返回值: 无
 *
 *************************************************************************/
void CChartBase::InitChart(BYTE type)
{
	COleVariant title((CString)m_cCaption, VT_BSTR);
	m_btType = type;

	// 增加系列
	switch(type)
	{
	case CHART_CIRCLE: 
		AddSeries(12);  // Polar
		AddSeries(12);
		break;
	case CHART_AXIS:
		break;
	case CHART_DIST:
/*		AddSeries(6);   // FastLine
		AddSeries(14);  // Volume
		GetSeries(1).GetAsVolume().GetLinePen().SetStyle(1);
		GetSeries(0).GetAsFastLine().GetLinePen().SetColor(RGB(0, 0, 255));*/
		break;
	case CHART_3D:
		AddSeries(19);  // Point3D
		break;
	}

	// 设置轴名称字体
	GetAxis().GetLeft().GetTitle().GetFont().SetName("宋体");	
	GetAxis().GetLeft().GetTitle().GetFont().SetSize(9);
	GetAxis().GetLeft().GetLabels().GetFont().SetColor(RGB(255,0,0));
	GetAxis().GetBottom().GetTitle().GetFont().SetName("宋体");	
	GetAxis().GetBottom().GetTitle().GetFont().SetSize(9);	
	GetAxis().GetBottom().GetLabels().GetFont().SetColor(RGB(255,0,0));

	// 设置刻度间隔
	GetAxis().GetBottom().GetLabels().SetSeparation(50);
	GetAxis().GetLeft().GetLabels().SetSeparation(50);

	// 设置刻度颜色
	GetAxis().GetBottom().GetMinorTicks().SetColor(RGB(0, 0, 0));
	GetAxis().GetBottom().GetTicks().SetColor(RGB(0, 0, 0));
	GetAxis().GetLeft().GetMinorTicks().SetColor(RGB(0, 0, 0));
	GetAxis().GetLeft().GetTicks().SetColor(RGB(0, 0, 0));

	// 设置绘制起始刻度
	GetAxis().GetBottom().GetLabels().SetRoundFirstLabel(TRUE);
	GetAxis().GetLeft().GetLabels().SetRoundFirstLabel(TRUE);
	GetAxis().GetBottom().GetLabels().SetOnAxis(TRUE);
	GetAxis().GetLeft().GetLabels().SetOnAxis(TRUE);

	// 面板
	GetPanel().SetBevelInner(0);
	GetPanel().SetBevelOuter(0);
	GetPanel().SetBorderStyle(1);
	GetPanel().GetGradient().SetVisible(TRUE);
	GetPanel().GetGradient().SetStartColor(RGB(255, 255, 255));
	GetPanel().GetGradient().SetEndColor(/*RGB(255, 255, 255)*/RGB(128, 255, 255));

	// 图例
	GetLegend().SetVisible(FALSE);

	// 墙
	GetWalls().SetVisible(FALSE);

	// 标题
	GetHeader().GetFont().SetName("宋体");
	GetHeader().GetFont().SetSize(9);
	GetHeader().GetText().Clear();
	GetHeader().GetText().Add(title);

	switch(type)
	{
	case CHART_CIRCLE:
		// 3D
		GetAspect().SetView3D(FALSE);
		// Axis
		GetAxis().GetLeft().SetMinMax(0, 63);
		GetAxis().GetBottom().SetMinMax(0, 63);
		GetAxis().GetLeft().GetLabels().SetVisible(FALSE);
		GetAxis().GetBottom().GetLabels().SetVisible(FALSE);
		GetAxis().GetRight().GetLabels().SetVisible(FALSE);
		GetAxis().GetTop().GetLabels().SetVisible(FALSE);
		break;
	case CHART_AXIS:
		// 3D
		GetAspect().SetView3D(FALSE);
		// 旋转、缩放
		GetScroll().SetEnable(FALSE);
		GetZoom().SetEnable(FALSE);
		break;
	case CHART_DIST:
		// 三维
		GetAspect().SetView3D(FALSE);
		// 旋转、缩放
		GetScroll().SetEnable(FALSE);
		GetZoom().SetEnable(FALSE);
		// MARK
		break;
	case CHART_3D:
		// 三维
		GetAspect().SetView3D(TRUE);
		GetAspect().SetChart3DPercent(60);
		// 旋转、缩放
		GetZoom().SetEnable(FALSE);
		GetScroll().SetEnable(FALSE);
			
		break;		
	}

	if(type != CHART_AXIS && type != CHART_DIST)
	{// 设置鼠标样式
		GetSeries(0).SetCursor(-21);
	}
}
/*************************************************************************
 *
 * OnMouseDownTChart()
 *
 * 功  能: 处理鼠标键按下
 *
 * 参  数: long Button: 鼠标被按下的键
 *         long Shift:  shift键是否被按下
 *         long X:      X坐标
 *         long Y:      Y坐标
 *         
 * 返回值: 无
 *
 *************************************************************************/
void CChartBase::OnMouseDownTChart(long Button, long Shift, long X, long Y)
{
	if(!ClickInRect(X, Y, m_rClip)) return;		

	// 设置鼠标活动区域
	SetCapture();
	ClientToScreen(&m_rClip);
	::ClipCursor(&m_rClip);

	if(Button == 2 && GetSeries(0).GetCount() != 0 && Shift == 0)
	{// 游标
		m_bShowCursor = TRUE;
		m_lCursor     = X;
		DrawScale(X, Y);

		CRect   rc;
		CString str;

		m_lCursor = X;

		rc.left   = X + 10;
		rc.top    = Y - 10;
		ClientToScreen(&rc);
		
		str = GetPosValue(X, Y);
		((CCVenusApp*)AfxGetApp())->m_pGraphDlg->MoveTipDlg(rc, str);
	}		

	if(Button == 2 && GetSeries(0).GetCount() != 0 && Shift == 1)
	{// 选定区域
		m_lSelBeginX   = X;
		m_lSelBeginY   = Y;

		m_rZoom.left   = X;
		m_rZoom.top    = Y;
		m_rZoom.right  = X;
		m_rZoom.bottom = Y;
		m_bMouseSel    = TRUE;
	}

	if(Button == 1 && GetSeries(0).GetCount() != 0)
	{
		::SetCursor(cross);
		m_lSelBeginX = X;
		m_lSelBeginY = Y;

		if(Shift == 0)
		{// 放大
			m_bZoom = TRUE;
		}
		else if(Shift == 1)
		{
			m_bGraphPre = TRUE;
		}
		m_rZoom.left   = X;
		m_rZoom.top    = Y;
		m_rZoom.right  = X;
		m_rZoom.bottom = Y;
	}
}
/*************************************************************************
 *
 * DrawScale()
 *
 * 功  能: 绘制游标
 *
 * 参  数: long X: X坐标
 *         
 * 返回值: 无
 *
 *************************************************************************/
void CChartBase::DrawScale(long X, long Y)
{
	if(m_dZoomXMin == 0 && m_dZoomXMax == 0 && m_dZoomYMin == 0 && m_dZoomYMax == 0)
	{
		m_dZoomXMin = GetAxis().GetBottom().GetMinimum();
		m_dZoomXMax = GetAxis().GetBottom().GetMaximum();
		m_dZoomYMin = GetAxis().GetLeft().GetMinimum();
		m_dZoomYMax = GetAxis().GetLeft().GetMaximum();
	}

	// 得到绘制区域
	int left   = GetAxis().GetBottom().CalcXPosValue(m_dZoomXMin);
	int right  = GetAxis().GetBottom().CalcXPosValue(m_dZoomXMax);
	int width  = right - left;

	int top    = GetAxis().GetLeft().CalcYPosValue( GetAxis().GetLeft().GetMaximum() );
	int bottom = GetAxis().GetLeft().CalcYPosValue( GetAxis().GetLeft().GetMinimum() );
	double i   = (double)(X - left) / width * (m_dZoomXMax - m_dZoomXMin) + m_dZoomXMin;
	double height = GetAxis().GetLeft().GetMaximum();

	m_lvScale = Y;
	m_lhScale = X;
	Repaint();
}
/*************************************************************************
 *
 * PreTranslateMessage()
 *
 * 功  能: 截获键盘事件(用于分布类型)
 *
 * 参  数: MSG* pMsg
 *         
 * 返回值: 无
 *
 *************************************************************************/
BOOL CChartBase::PreTranslateMessage(MSG* pMsg) 
{

	return CTChart::PreTranslateMessage(pMsg);
}
/*************************************************************************
 *
 * OnMouseMoveTChart()
 *
 * 功  能: 处理鼠标移动
 *
 * 参  数: long Shift:  shift键是否被按下
 *         long X:      X坐标
 *         long Y:      Y坐标
 *         
 * 返回值: 无
 *
 *************************************************************************/
void CChartBase::OnMouseMoveTChart(long Shift, long X, long Y)
{
	if(m_bShowCursor && GetSeries(0).GetCount() != 0 && Shift == 0)
	{// 游标
		CRect   rc;
		CString str;

		m_lCursor = X;
		DrawScale(X, Y);

		rc.left   = X + 10;
		rc.top    = Y - 10;
		ClientToScreen(&rc);
		
		str = GetPosValue(X, Y);
		((CCVenusApp*)AfxGetApp())->m_pGraphDlg->MoveTipDlg(rc, str);
	}

	if( GetSeries(0).GetCount() != 0 && (m_bZoom || m_bGraphPre) || (Shift == 1 && m_bMouseSel) )
	{// 放大
		CPen   pPen(PS_SOLID, 1, RGB(0,255,255));
		CClientDC pDC(this);
		double xMin, xMax, yMin, yMax;

		pDC.SetROP2(R2_XORPEN);
		pDC.SelectObject(&pPen);
		pDC.SelectStockObject(NULL_BRUSH);

		// 擦除旧矩形
		pDC.Rectangle(m_rZoom);
		m_rZoom.right  = X;
		m_rZoom.bottom = Y;
		pDC.Rectangle(m_rZoom);

		pDC.SelectStockObject(NULL_BRUSH);
		pDC.SelectObject(&pPen);

		CalcValueRgn(m_rZoom, xMin, xMax, yMin, yMax);

		if(xMax >= xMin + xMin / 1000)
		{
			MoveTip(m_rZoom.left, m_rZoom.top, xMin, xMax, yMin, yMax);
		}
	}
}
/************************************************************************* 
 *
 * MoveTip()	
 *	
 * 功  能: 移动提示信息框
 *
 * 参  数: long x, long y:           鼠标位置
 *         double xMin, double xMax: 圈定区域X方向上的参数范围
 *         double yMin, double yMax: 圈定区域Y方向上的参数范围
 *         
 * 返回值: 无
 *
 *************************************************************************/
void CChartBase::MoveTip(long x, long y, double xMin, double xMax, double yMin, double yMax)
{
	CPoint    pt;
	CString   str, str1, str2;
	POSITION  pos;

	pt.x = x;
	pt.y = y;
	pos = ((CCVenusApp*)AfxGetApp())->m_pGraphData[m_iGraphLoc].slDescription.FindIndex(4);
	str1 = ((CCVenusApp*)AfxGetApp())->m_pGraphData[m_iGraphLoc].slDescription.GetAt(pos);
	pos = ((CCVenusApp*)AfxGetApp())->m_pGraphData[m_iGraphLoc].slDescription.FindIndex(5);
	str2 = ((CCVenusApp*)AfxGetApp())->m_pGraphData[m_iGraphLoc].slDescription.GetAt(pos);
	str.Format("%s%s%.0f%s%.0f\n%s%s%.0f%s%.0f", str1, ":", xMin, "-", xMax, str2, ":", yMin, "-", yMax);
	CRect     rc;

	rc.left   = x;
	rc.top    = y;
	ClientToScreen(&rc);

	((CCVenusApp*)AfxGetApp())->m_pGraphDlg->MoveTipDlg(rc, str);
}
/*************************************************************************
 *
 * OnMouseUpTChart()
 *
 * 功  能: 处理鼠标键弹起
 *
 * 参  数: long Button: 鼠标被弹起的键
 *         long Shift:  shift键是否被弹起
 *         long X:      X坐标
 *         long Y:      Y坐标
 *         
 * 返回值: 无
 *
 *************************************************************************/
void CChartBase::OnMouseUpTChart(long Button, long Shift, long X, long Y)
{
	::ReleaseCapture();
	::ClipCursor(NULL);

	if(Button == 2 && m_bShowCursor && GetSeries(0).GetCount() != 0 && Shift == 0)	
	{// 游标
		m_bShowCursor = FALSE;
		((CCVenusApp*)AfxGetApp())->m_pGraphDlg->HideTipDlg();
		m_bMoveCursor = FALSE;
		Repaint();
	}

	if(Button == 2 && GetSeries(0).GetCount() != 0 && Shift == 1 && m_bMouseSel)
	{// 选定区域
		CString str;
		long    x0, x1;
		double  width, len_x, len_y;

		m_bMouseSel = FALSE;
		m_lSelEndX  = X;
		m_lSelEndY  = Y;

		x0    = GetAxis().GetBottom().CalcXPosValue(GetAxis().GetBottom().GetMinimum());
		x1    = GetAxis().GetBottom().CalcXPosValue(GetAxis().GetBottom().GetMaximum());
		width = GetAxis().GetBottom().GetMaximum() - GetAxis().GetBottom().GetMinimum();
		len_x = ((double)m_lSelEndX - m_lSelBeginX) / (x1 - x0) * width;

		x0    = GetAxis().GetLeft().CalcYPosValue(GetAxis().GetLeft().GetMaximum());

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -