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

📄 chart2dex.cpp

📁 写入WORD;动态画曲线;查看磁盘资源等。
💻 CPP
字号:
// Chart2DEx.cpp: implementation of the CChart2DEx class.
//
//////////////////////////////////////////////////////////////////////




#include "stdafx.h"
#include "Chart2DEx.h"

#include "gridlines.h"


#include "2dconst.h"

#include "Chart2D.h"

#include "DerivedLong.h"
#include "DerivedDouble.h"
#include "Axis.h"
#include "AxisCollection.h"
#include "AxisStyle.h"
#include "ChartArea.h"
#include "Chart2DData.h"
#include "ChartGroup.h"
#include "ChartGroupCollection.h"
#include "SymbolStyle.h"
#include "ChartStyle.h"
#include "ChartStyleCollection.h"
#include "Label.h"
#include "LabelCollection.h"
#include "Border.h"
#include "Interior.h"
#include "LineStyle.h"
#include "Legend.h"
#include "Font.h"
#include "ValueLabelCollection.h"
#include "ValueLabel.h"
#include "location.h"
#include "title.h"

#include "FillStyle.h"
#include "TimeScale.h"
#include "DataCoord.h"


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

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

CChart2DEx::CChart2DEx()
{
}

CChart2DEx::~CChart2DEx()
{
	m_MarkerX.DetachDispatch();
}

BEGIN_MESSAGE_MAP(CChart2DEx, CChart2D)
	//{{AFX_MSG_MAP(CChart2DEx)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CChart2DEx message handlers

BOOL CChart2DEx::Init(long dYMax, long dXMax, int NumSeries, long dYMin,
	long dXMin)
{
	m_lArraySize = dXMax + 1;
	m_lShiftSize = dXMax;
	m_lNextPoint = 0;
	m_lNextX = 0;
	m_stepX = 1;
	///
	// Don't allow the user to change any of the following 
	//(block the property pages)
	this->SetAllowUserChanges(FALSE);

	// set up the colors and border styles
	this->GetChartArea().GetInterior().SetBackgroundColor(RGB(255, 255, 255));
	this->GetChartArea().GetBorder().SetType(oc2dBorderNone);
	this->GetLegend().GetInterior().SetBackgroundColor(RGB(255,255,255));
	this->GetLegend().GetBorder().SetType(oc2dBorderNone);
	this->GetLegend().GetBorder().SetWidth(4);
/*	this->GetLegend().GetLocation().GetLeft().SetValue(73);
	this->GetLegend().GetLocation().GetTop().SetValue(350);
	this->GetLegend().GetLocation().GetWidth().SetValue(80);
	this->GetLegend().GetLocation().GetHeight().SetValue(20);*/
	// Set the legend font point size
	CY cy;
	cy.Lo = 100000;
	cy.Hi = 0;
	this->GetLegend().GetFont().SetSize(cy);

	///
	// Get some useful subobjects
	CAxis axisX(this->GetChartArea().GetAxes().GetItem(COleVariant("x")));
	CAxis axisY(this->GetChartArea().GetAxes().GetItem(COleVariant("y")));
	//////////////////////////////////////////////////////////////////////////
	m_MarkerX = this->GetChartArea().GetMarkers().GetItem(COleVariant("x"));
	// Setup the Marker
	m_MarkerX.SetMethod(oc2dMarkerDataCoord);
	m_MarkerX.GetValue().SetX(1E+308);
	m_MarkerX.SetIsShowing(true);
	//////////////////////////////////////////////////////////////////////////
	CChart2DData data(this->GetChartGroups().GetItem(COleVariant(short(1))).GetData());
	CLabelCollection seriesLabels(this->GetChartGroups().GetItem(COleVariant(short(1))).GetSeriesLabels());
//	CChartStyle style1(this->GetChartGroups().GetItem(COleVariant(short(1))).GetSeriesLabels() );
//	style1.GetSymbol().SetColor(RGB(255,255,255));

/*	VARIANT va;
	VariantInit(&va);
	va.vt = VT_I4;//
    // Add Legend labels
    va.lVal = seriesLabels.GetCount() + 1;
    seriesLabels.Add("1#", va);
	
    va.lVal = seriesLabels.GetCount() + 1;
    seriesLabels.Add("2#", va);

    va.lVal = seriesLabels.GetCount() + 1;
    seriesLabels.Add("3#", va);*/

	axisY.GetMin().SetValue(dYMin);
	axisY.GetMax().SetValue(dYMax);

	axisX.GetMin().SetValue(dXMin);
	axisX.GetMax().SetValue(dXMax);

	// Batch the Data Object for efficiency
	data.SetIsBatched(TRUE);
	data.SetLayout(oc2dDataArray);
	data.SetNumSeries(NumSeries);
	data.SetNumPoints(1, m_lArraySize);
	data.SetFirstSeries(1);
	data.SetLastSeries(data.GetNumSeries());
	data.SetFirstPoint(1, 1);
	data.SetLastPoint(1, 0);
	data.SetX(1, 1, 0);
	
	for (int i = 1; i <= data.GetNumSeries(); i++)
	{
		data.SetY(i, 1, 0);
	}

	data.SetIsBatched(FALSE);

	for (i = 1; i <= data.GetNumSeries(); i++)
	{
		CChartStyle style(this->GetChartGroups().GetItem(COleVariant(short(1))).GetStyles().GetItem(COleVariant(short(i))));
		// Clear the shape style
		style.GetSymbol().SetShape(oc2dShapeNone);
		// Make the lines thicker
		style.GetLine().SetWidth(1);
	}

	// Change this color to not conflict with the legend background
//	CChartStyle style(this->GetChartGroups().GetItem(COleVariant(short(1))).GetStyles().GetItem(COleVariant(short(2))));
//	style.GetLine().SetColor(RGB(17, 4, 251));

	//	style.GetFill().SetColor(RGB(0, 0, 0));

	// Set the legend title
	this->GetLegend().GetText().SetText("通道");


	// Set longer X axis ticks (normally set to 2) 
	axisX.GetAxisStyle().SetTickLength(2);
	axisY.GetAxisStyle().SetTickLength(2);
	return TRUE;
}

void CChart2DEx::Draw(BOOL isStepX, double* pointY)
{
	CAxis axisX(this->GetChartArea().GetAxes().GetItem(COleVariant("x")));
	CChart2DData data(this->GetChartGroups().GetItem(COleVariant(short(1))).GetData());
	double dMinimum;
	static count=1;

	// The new points will not fit
	
	if (data.GetLastPoint(1) >= data.GetNumPoints(1))
	{
		// Move the current points to the left.
		this->SetIsBatched(TRUE);

		data.ShiftPoints(1, m_lShiftSize, 1, m_lArraySize - m_lShiftSize + 1);
		m_lNextPoint = m_lShiftSize + 1;
		count++;
	//	data.SetNumPoints(1, m_lArraySize*count);
		

		dMinimum = axisX.GetMin().GetValue();
		dMinimum += m_lArraySize - m_lShiftSize;
		axisX.GetMin().SetValue(dMinimum);
		axisX.GetMax().SetValue(dMinimum + m_lArraySize - 1);
		data.SetLastPoint(1, m_lShiftSize);
		this->SetIsBatched(FALSE);
	} // if
	else
	{
		m_lNextPoint = data.GetLastPoint(1) + 1;
	} // else

	data.SetX(1, m_lNextPoint, m_lNextX);

	for (int idxY = 1; idxY <= data.GetNumSeries(); idxY++)
	{
		data.SetY(idxY, m_lNextPoint, *(pointY++));	
	}

	m_lNextPoint += 1;
	if (isStepX)
		m_lNextX += m_stepX;

	data.DrawNewPoints(1, 1);
}

void CChart2DEx::DrawGrad(double Gx, double Gy)
{
	if (Gx)
	{
		//画网格线
		CAxis axisX(this->GetChartArea().GetAxes().GetItem(COleVariant("x")));

		CGridLines gridX = axisX.GetMajorGrid();
		gridX.GetSpacing().SetIsDefault(TRUE);
		gridX.GetSpacing().SetValue(Gx);
		gridX.GetStyle().SetColor(RGB(128, 64, 0));
		//		gridX.GetStyle().SetPattern(oc2dLineDotted);
	}
	if (Gy)
	{
		CAxis axisY(this->GetChartArea().GetAxes().GetItem(COleVariant("y")));

		CGridLines gridY = axisY.GetMajorGrid();
		gridY.GetSpacing().SetIsDefault(TRUE);
		gridY.GetSpacing().SetValue(Gy);
		gridY.GetStyle().SetColor(RGB(128, 64, 0));
		//		gridY.GetStyle().SetPattern(oc2dLineDotted);
	}
}
void CChart2DEx::SetTextX(CString& strx)  // 设置x轴的名称
{
	CAxis axisX(this->GetChartArea().GetAxes().GetItem(COleVariant("x")));

	axisX.GetTitle().SetText(strx);
}
void CChart2DEx::SetTextY(CString& stry)  // 设置y轴的名称
{
	CAxis axisY(this->GetChartArea().GetAxes().GetItem(COleVariant("y")));

	axisY.GetTitle().SetText(stry);
}

void CChart2DEx::SetBGColor(COLORREF color)  //设置背景色
{
	this->GetInterior().SetBackgroundColor(color);
}

void CChart2DEx::SetStep(double step)  //设置每点x轴增加量
{
	m_stepX = step;
}

void CChart2DEx::Saveimgto(CString filepath)// save image to file.
{
	SaveImageAsJpeg(filepath, 100, FALSE, FALSE, FALSE);
}
void CChart2DEx::OnSize(UINT nType, int cx, int cy)
{
	CChart2D::OnSize(nType, cx, cy);

	// Size the chart to fit the window
	if ((cx > 0) && (cy > 0))
	{
		this->MoveWindow(0, 0, cx, cy);
	}
}

void CChart2DEx::RenewChart()
{
	CAxis axisX(GetChartArea().GetAxes().GetItem(COleVariant("x")));
	CChart2DData data(GetChartGroups().GetItem(COleVariant(short(1))).GetData());
	SetIsBatched(TRUE);

	data.SetLastPoint(1, 0);

	SetIsBatched(FALSE);

	m_lNextPoint = 0;
	m_lNextX = 0;
}

float CChart2DEx::GetYValue1(int idx, long x)
{
	long XIndex;
	double XVal, YVal;
	COleDateTime XDate;
	CString csDateTime;
	CString csTimeZone;
	CString csLabelText;
	CChartGroup m_cGroup = this->GetChartGroups().GetItem(COleVariant(short(1)));
	CAxis m_AxisX = this->GetChartArea().GetAxes().GetItem(COleVariant("x"));
	m_cGroup.CoordToDataCoord(x, 40, &XVal, &YVal);
	if (XVal != 1E+308)
	{
		XIndex = (int) (XVal - m_cGroup.GetData().GetX(1, 1)) + 1;
		if ((XIndex >= 1) && (XIndex <= m_cGroup.GetData().GetNumPoints(1)))
		{
			// Update the contents of the label in the upper left hand corner of the
			//  window to represent the data value  that the mouse is currently over
			XDate = m_AxisX.GetTimeScale().ValueToDate(XVal);
			m_MarkerX.GetValue().SetX(XVal);
		/*	float fValue = m_cGroup.GetData().GetY(1, XIndex);
			CString str;
			if (idx == 1)
				str.Format("扭矩 %.2f 牛.米", fValue);
			else
				str.Format("力矩 %.2f 牛", fValue);
			this->GetHeader().GetText().SetText(str);
			if (idx >= 0)
				return fValue;
			else
				return XVal;*/
		}
	}
	// Update the location of the marker
	m_MarkerX.GetValue().SetX(XVal);
	return 0;
}

⌨️ 快捷键说明

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