一个画图类,类似ms_chart的线图,适用于科学仿真数据分析(一)

类别:VC语言 点击:0 评论:0 推荐:

      MS_CHART控件在显示线性图时根据数据自动调整比例,无法控制其显示效果(至少我没有找到),于是自己写了一个专门用来作数值分析线性图显示的类.从csdn和其他VC相关上学了不少东西,所以拿出来请高手门批评指正,也算一点贡献吧.
     使用方法: 在user_define控件中设定类为 LYH_LINE_CHART;
     使用m_Chart.SubclassDlgItem(IDC_MYCHART,this);进行初始化.其它的就不用俺说了.下面是类的实现代码:

// LyhLineChart.cpp : implementation file

// Written by Yuhai Liu ([email protected])
//[Usage]
//1.Init window by CLyhLineChart.SubclassDlgItem(ID(DlgItem),ParentWnd);
//2.use AddLineToChart() to add a serial data declare
//3.use DeleteLine() to delete a serial data. And after this,take care of
//the Index changes
//4.use AddData(x,y,nIndex) to add a point to special line
// Copyright (c) 2003.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name is included. If
// the source code in  this file is used in any commercial application
// then a simple email would be nice.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage whatsoever.
// It's free - so you get what you pay for.
//
#include "stdafx.h"
#include "mychart.h"
#include "LyhLineChart.h"

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

/////////////////////////////////////////////////////////////////////////////
// CLyhLineChart

CLyhLineChart::CLyhLineChart()
{
 CLyhLineChart::RegisterWndClass(AfxGetInstanceHandle());
// AddLineToChart();
}

CLyhLineChart::~CLyhLineChart()
{
 int nCount = m_items.GetSize();
 
 for (int i = 0; i < nCount; i++)
  delete m_items.GetAt(i);
 m_items.RemoveAll();
}


BEGIN_MESSAGE_MAP(CLyhLineChart, CWnd)
 file://{{AFX_MSG_MAP(CLyhLineChart)
 ON_WM_PAINT()
 ON_WM_SIZE()
 file://}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CLyhLineChart message handlers

BOOL CLyhLineChart::RegisterWndClass(HINSTANCE hInstance)
{
 WNDCLASS wc;
 wc.lpszClassName = "LYH_LINE_CHART"; // matches class name in client
 wc.hInstance = hInstance;
 wc.lpfnWndProc = ::DefWindowProc;
 wc.hCursor = ::LoadCursor(NULL, IDC_ARROW);
 wc.hIcon = 0;
 wc.lpszMenuName = NULL;
 wc.hbrBackground = (HBRUSH) ::GetStockObject(LTGRAY_BRUSH);//默认的windows颜色
 wc.style = CS_GLOBALCLASS; // To be modified
 wc.cbClsExtra = 0;
 wc.cbWndExtra = 0;

 return (::RegisterClass(&wc) != 0);
}

BOOL CLyhLineChart::AddLineToChart(COLORREF color)
{
 CLineChartItem* pItem = new CLineChartItem;
 COLORREF linecolor;
 linecolor=color;
 if(color==DEFAULTCOLOR){
  int nCount=m_items.GetSize();
  switch(nCount){
  case 0: linecolor=RGB(55,0,255); break;
  case 1: linecolor=RGB(0,255,55); break;
  case 2: linecolor=RGB(255,0,55); break;
  case 3: linecolor=RGB(255,255,255); break;
  default:linecolor=RGB(0,0,0); break;
  }
  pItem->m_colorLine = linecolor;
 } else pItem->m_colorLine = color;
 pItem->m_legend.Format("Line %d",m_items.GetSize()+1);
 try
 {
  m_items.Add(pItem);
  CClientDC dc(this);
  CRect rcClient;
  GetClientRect(rcClient);
  if (m_MemDC.GetSafeHdc() == NULL)
  {
   m_MemDC.CreateCompatibleDC(&dc);

   m_Bitmap.CreateCompatibleBitmap(&dc,rcClient.Width(),rcClient.Height());
   m_Bitmap.SetBitmapDimension(rcClient.Width(),rcClient.Height());
   m_MemDC.SelectObject(m_Bitmap);
   m_Bitmap.DeleteObject();

   CFont font;
   font.CreateFont(m_set.fontsize,0, 0,0, FW_NORMAL,0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
   CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,DEFAULT_PITCH,m_set.fontname);
   m_MemDC.SelectObject(&font);
   font.DeleteObject();
   
   CBrush bkBrush(m_set.bkColor);
   if(m_set.bBorder){
    CPen mpen(PS_SOLID,m_set.BorderWidth,m_set.BorderColor);
    CBrush *oldbrush=m_MemDC.SelectObject(&bkBrush);
    m_MemDC.Rectangle(rcClient);
    m_MemDC.SelectObject(oldbrush);
    mpen.DeleteObject();
   }else  m_MemDC.FillRect(rcClient,&bkBrush);
   bkBrush.DeleteObject();
  }
 
  InvalidateRect(rcClient, FALSE);
  return TRUE;
 }
 catch (CMemoryException* e)
 {
  if (pItem !=NULL)
   delete pItem;
  e->Delete();
  return FALSE;
 }
}

BOOL CLyhLineChart::SetAutoRange( BOOL bAuto,double minx, double maxx, double miny, double maxy)
{
 m_set.xMax=maxx;
 m_set.yMax=maxy;
 m_set.xMin=minx;
 m_set.yMin=miny;
 m_set.autorange=bAuto;
 return bAuto;
}

void CLyhLineChart::Refresh()
{
 CRect rect;
 GetClientRect(rect);
 if(m_set.autorange) CalRange();
 DrawPic();
 InvalidateRect(rect,m_set.bStretch);
}

 

void CLyhLineChart::OnPaint()
{
 CPaintDC dc(this); // device context for painting
 CRect rcClient;
 GetClientRect(rcClient);
 // draw scale
 if (m_MemDC.GetSafeHdc() != NULL)
  dc.BitBlt(0, 0, rcClient.Width(), rcClient.Height(), &m_MemDC, 0, 0, SRCCOPY);

}

void CLyhLineChart::DrawPic()
{


 CRect rcClient,rect;
 GetClientRect(rcClient);
 rect=rcClient;
 UINT i;
 if(m_MemDC.GetSafeHdc()!=NULL)
 {
  CClientDC dc(this);
  CFont font;
  CString fontname,CurStr;
  BOOL xVer=false;//是否垂直显示X坐标
  int  xDec=0;//X坐标显示的小数点位数
  int  yDec=0;//Y坐标显示的小数点位数
  CPoint point;
  CSize size,size1;
  double dx,dy,temp;
    
  m_Bitmap.CreateCompatibleBitmap(&dc,rcClient.Width(),rcClient.Height());
  m_Bitmap.SetBitmapDimension(rcClient.Width(),rcClient.Height());
  m_MemDC.SelectObject(m_Bitmap);
  m_Bitmap.DeleteObject();
  
  size1=m_MemDC.GetTextExtent((LPCTSTR)"a",1);//获得窗口对于字体的高度
  size =size1;
  m_MemDC.GetTextFace(fontname);
  file://预留标题、坐标显示空白
  rcClient.right=rect.right-m_set.RIGHTSPACE;//此处空白为坐标轴上短线的长度
  rcClient.left=rect.left+m_set.RIGHTSPACE;
  rcClient.bottom=rect.bottom-m_set.RIGHTSPACE;
  if(!m_set.YTitle.IsEmpty()) rcClient.left=rcClient.left+size.cy*3/2+m_set.CHARSPACE;
  if(!m_set.Title.IsEmpty()) rcClient.top=rect.top+size.cy*2;
   else rcClient.top=rect.top+m_set.RIGHTSPACE;
  if(!m_set.XTitle.IsEmpty()) rcClient.bottom=rcClient.bottom-size.cy*3/2+m_set.CHARSPACE;
  if(!m_set.FootNote.IsEmpty()) rcClient.bottom=rcClient.bottom-size.cy*3/2;
  if(m_set.bLegend) rcClient.bottom=rcClient.bottom-size.cy*3/2;
  file://确保坐标的间隔在10之上,避免数值过小时坐标显示的数值一样
  yDec=CalDecNum(m_set.yMin,m_set.yMax,CurStr);
  size=m_MemDC.GetTextExtent(CurStr);
  rcClient.left=rcClient.left+size.cx+m_set.CHARSPACE;//+size.cy/2;
  xDec=CalDecNum(m_set.xMin,m_set.xMax,CurStr);
  size=m_MemDC.GetTextExtent(CurStr);
  if(size.cx>(int)(rcClient.Width()/m_set.HLineNum)) xVer=true;
  if(xVer){ rcClient.bottom=rcClient.bottom-size.cx-m_set.CHARSPACE;
    rcClient.right-=size.cy/2;
  }else{ rcClient.bottom=rcClient.bottom-m_set.CHARSPACE-size.cy*3/2;
   rcClient.right-=size.cx/2;
  }
  file://消除窗口宽度不能整除带来的误差
  int iWidth=m_set.HLineNum*(int)(rcClient.Width()/m_set.HLineNum);
  int iHeight=m_set.VLineNum*(int)(rcClient.Height()/m_set.VLineNum);
  rcClient.left=rcClient.right-iWidth;
  rcClient.top=rcClient.bottom-iHeight;
  file://清除窗口内容
  CBrush bkBrush(m_set.bkColor);
  file://边框
  if(m_set.bBorder){
   CPen mpen(PS_SOLID,m_set.BorderWidth,m_set.BorderColor);
   CBrush *oldbrush=m_MemDC.SelectObject(&bkBrush);
   m_MemDC.Rectangle(rect);
   m_MemDC.SelectObject(oldbrush);
   mpen.DeleteObject();
  }else  m_MemDC.FillRect(rcClient,&bkBrush);
  bkBrush.DeleteObject();

  int bkmode=m_MemDC.SetBkMode(TRANSPARENT);
  file://输出标题字符串
  COLORREF crOldColor=m_MemDC.SetTextColor(m_set.TileColor);;
  font.CreateFont(size1.cy,0, 0,0, FW_BOLD,0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
   CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,DEFAULT_PITCH,fontname);
  m_MemDC.SelectObject(&font);//<-
  font.DeleteObject();//->

  size=m_MemDC.GetTextExtent(m_set.Title);
  point.x=rect.left+rect.Width()/2-size.cx/2;//位于窗口中央
  point.y=rect.top+size.cy/2;
  m_MemDC.TextOut(point.x,point.y,m_set.Title);
  
  if (rcClient.bottom<rcClient.top || rcClient.right<rcClient.left){
   return; }//窗口太小,输出标题后返回
  file://输出Y标题
  m_MemDC.SetTextColor(m_set.YTileColor);
  font.CreateFont(size1.cy,0,900,900,FW_NORMAL,0,0,0,1,OUT_DEFAULT_PRECIS,
   CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH,fontname);//fontname);//"Times New Roman");
  m_MemDC.SelectObject(&font);//<-
  font.DeleteObject();//->
  size=m_MemDC.GetTextExtent(m_set.YTitle);
  point.x=rect.left+size.cy/2;
  point.y=rcClient.top+rcClient.Height()/2+size.cx/2;//位于表格中央
  m_MemDC.TextOut(point.x,point.y,m_set.YTitle);
  
  file://输出X标题
  font.CreateFont(size1.cy,0, 0,0, FW_BOLD,0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
   CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,DEFAULT_PITCH,fontname);
  m_MemDC.SelectObject(&font);
  font.DeleteObject();
  m_MemDC.SetTextColor(m_set.XTileColor);
  size=m_MemDC.GetTextExtent(m_set.XTitle);
  point.y=rect.bottom-size.cy*3/2;
  if(!m_set.FootNote.IsEmpty()) point.y=point.y-size.cy*3/2;
  if(m_set.bLegend) point.y=point.y-size.cy*3/2;
  point.x=rcClient.left+rcClient.Width()/2-size.cx/2;//位于表格中央
  m_MemDC.TextOut(point.x,point.y,m_set.XTitle);
  file://输出脚注
  m_MemDC.SetTextColor(m_set.FootColor);
  size=m_MemDC.GetTextExtent(m_set.FootNote);
  point.x=rect.left+rect.Width()/2-size.cx/2;
  point.y=rect.bottom-size.cy*3/2;
  m_MemDC.TextOut(point.x,point.y,m_set.FootNote);
  file://画框线和坐标线
  m_MemDC.SetTextColor(m_set.axiednumcolor);
  CPen pen;
  pen.CreatePen(PS_SOLID,1,m_set.InBorderColor);
  CPen *oldPen=m_MemDC.SelectObject(&pen);
  iWidth=(int)(rcClient.Width()/m_set.HLineNum);
  iHeight=(int)(rcClient.Height()/m_set.VLineNum);
  dx=(m_set.xMax-m_set.xMin)/(double)m_set.HLineNum;
  dy=(m_set.yMax-m_set.yMin)/(double)m_set.VLineNum;
  int dotpos;
  if(xVer){
   font.CreateFont(size1.cy,0,-900,900,FW_NORMAL,0,0,0,1,OUT_DEFAULT_PRECIS,
   CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH,fontname);//"Times New Roman");
   m_MemDC.SelectObject(&font);//<-
   font.DeleteObject();//->
  }
  for(i=0; i<=m_set.HLineNum; i++)
  {
   temp=m_set.xMin+(double)i*dx;
   CurStr.Format("%f",temp);
   dotpos=CurStr.Find('.');
   if(xDec>0) CurStr=CurStr.Left(dotpos+xDec+1);
    else CurStr=CurStr.Left(dotpos);
   size=m_MemDC.GetTextExtent(CurStr);
   if(xVer){
    point.x=rcClient.left+i*iWidth+size.cy/2;
    point.y=rcClient.bottom+m_set.RIGHTSPACE+m_set.CHARSPACE;
   }else{
    point.x=rcClient.left+i*iWidth-size.cx/2;
    point.y=rcClient.bottom+m_set.RIGHTSPACE;
   }
   m_MemDC.TextOut(point.x,point.y,CurStr);//输出坐标数据
   m_MemDC.MoveTo(rcClient.left+i*iWidth,rcClient.bottom);
   m_MemDC.LineTo(rcClient.left+i*iWidth,rcClient.bottom+m_set.RIGHTSPACE);
   if(m_set.bxInBorder)
   {
   m_MemDC.MoveTo(rcClient.left+i*iWidth,rcClient.top);
   m_MemDC.LineTo(rcClient.left+i*iWidth,rcClient.bottom);
   }
  }
  if(xVer){
   font.CreateFont(size1.cy,0,0,0,FW_NORMAL,0,0,0,1,OUT_DEFAULT_PRECIS,
   CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH,fontname);//"Times New Roman");
   m_MemDC.SelectObject(&font);//<-
   font.DeleteObject();//->
  }
  for(i=0; i<=m_set.VLineNum;i++)
  {
   temp=m_set.yMax-(double)i*dy;
   CurStr.Format("%f",temp);
   dotpos=CurStr.Find('.');
   if(yDec>0) CurStr=CurStr.Left(dotpos+yDec+1);
    else CurStr=CurStr.Left(dotpos);
   size=m_MemDC.GetTextExtent(CurStr);
   point.x=rcClient.left-m_set.RIGHTSPACE-m_set.CHARSPACE-size.cx;
   point.y=rcClient.top+i*iHeight-size.cy/2;
   m_MemDC.TextOut(point.x,point.y,CurStr);
   m_MemDC.MoveTo(rcClient.left-m_set.RIGHTSPACE,rcClient.top+i*iHeight);
   m_MemDC.LineTo(rcClient.left,rcClient.top+i*iHeight);
   if(m_set.byInBorder)
   {
   m_MemDC.MoveTo(rcClient.left,rcClient.top+i*iHeight);
   m_MemDC.LineTo(rcClient.right,rcClient.top+i*iHeight);
   }
  }
  pen.DeleteObject();
  m_MemDC.SelectObject(oldPen);
  file://输出图形
  int nCount = m_items.GetSize();
  CLineChartItem* pItem;
  CPoint ptOld, ptNew;
  DataStru CurData;
  BOOL bSplit=false;
  file://输出图例
  size=m_MemDC.GetTextExtent(m_set.XTitle);//图例的字体和X轴暂且设为一致
  point.x=rcClient.left;
  point.y=rect.bottom-size.cy;//画线的高度
  if(!m_set.FootNote.IsEmpty()) point.y=point.y-size.cy*3/2;
  if(m_set.bLegend)
  {
   for(i=0; i<(unsigned)nCount; i++)
   {
    pItem = m_items.GetAt(i);
    pen.CreatePen(pItem->iStyle,pItem->iWidth,pItem->m_colorLine);
    CPen *oldpen=m_MemDC.SelectObject(&pen);
    m_MemDC.MoveTo(point);
    point.x+=3*m_set.RIGHTSPACE;//线长设为间隔的3倍,maybe caculate from Rectangle's width?
    m_MemDC.LineTo(point);
    m_MemDC.SelectObject(oldpen);
    pen.DeleteObject();
    point.x+=size.cy/2;//all space for half of font height
    point.y=point.y-size.cy/2;
    m_MemDC.SetTextColor(pItem->legendcolor);
    m_MemDC.TextOut(point.x,point.y,pItem->m_legend);
    size=m_MemDC.GetTextExtent(pItem->m_legend);
    point.x+=size.cx+size.cy;//图例之间的距离为字体的高度
    point.y+=size.cy/2;
   }
  }
  m_MemDC.SetBkMode(bkmode);
  m_MemDC.SetTextColor(crOldColor);
  file://zuotu
  for(i=0; i<(unsigned)nCount; i++)
  {
   pItem = m_items.GetAt(i);
   POSITION pos=pItem->m_data.GetHeadPosition();
   if(pos!=NULL){
    CPen LinePen(pItem->iStyle,pItem->iWidth,pItem->m_colorLine);
    CPen *oldpen=m_MemDC.SelectObject(&LinePen);
    file://画线
    CurData=pItem->m_data.GetHead();
    dx=(CurData.x-m_set.xMin)/(m_set.xMax-m_set.xMin);
    dy=(CurData.y-m_set.yMin)/(m_set.yMax-m_set.yMin);
    ptOld.x=rcClient.left+(int)(dx*(double)rcClient.Width());
    ptOld.y=rcClient.bottom-(int)(dy*(double)rcClient.Height());
    while(pos!=NULL)
    {
     CurData=pItem->m_data.GetNext(pos);
     if(CurData.x!=NODATA){
      dx=(CurData.x-m_set.xMin)/(m_set.xMax-m_set.xMin);
      dy=(CurData.y-m_set.yMin)/(m_set.yMax-m_set.yMin);
      ptNew.x=rcClient.left+(int)(dx*(double)rcClient.Width());
      ptNew.y=rcClient.bottom-(int)(dy*(double)rcClient.Height());
      if(bSplit){ ptOld=ptNew; bSplit=false; }
      m_MemDC.MoveTo(ptOld); ptOld=ptNew;
      m_MemDC.LineTo(ptNew);
     }else bSplit=true;
    }
    m_MemDC.SelectObject(oldpen);
    LinePen.DeleteObject();
   }
  }
 }
}

void CLyhLineChart::CalRange()
{
 
 double dx,dy,px,py;
 file://进行归零计算
 dx=m_set.xMax-m_set.xMin;
 dy=m_set.yMax-m_set.yMin;
 px=dx/(double)m_set.HLineNum;
 py=dy/(double)m_set.VLineNum;
 file://如果间隔区间大于最小值,则将其归零
 if(m_set.xMin>=0.0 && m_set.xMin<px) m_set.xMin=0.0;
 if(m_set.yMin>=0.0 && m_set.yMin<py) m_set.yMin=0.0;

 file://进行取整计算,确保坐标值的显示友好
 dx=m_set.xMax-m_set.xMin;
 dy=m_set.yMax-m_set.yMin;
 px=dx/(double)m_set.HLineNum;
 py=dy/(double)m_set.VLineNum;
 file://整除计算后有剩余则扩大区间,确保图形位于坐标标识范围内
 file://判断0值线,确保0的出现,如果0以下不够一格则增大abd(xmin),
 if(m_set.xMin<0.0 && m_set.xMax>0.0){
  double kx1,kx2,x3,x4;
  kx1=m_set.xMin; kx2=m_set.xMax;
  int num1=(int)(-kx1/px);
  if(num1==0)
  {
   num1++;//如果按比例分不到一格则强行设为1
  }
   else if((-kx1-(double)num1*px)>px/2) num1++;//if abs(xmin)>表格间隔的1/2则算一格
  int num2=m_set.HLineNum-num1;//剩下above 0 的线数
  x3=-kx1/(double)num1;//below 0 line space
  x4=kx2/(double)num2;
  px=x4;
  if(x3>x4) px=x3;//取较大的值为根据
  m_set.xMin=-px*(double)num1;//根据新的范围值,重新界定范围标识
  m_set.xMax=px*(double)num2;
 }
 if(m_set.yMin<0.0 && m_set.yMax>0.0){
  double ky1,ky2,y3,y4;
  ky1=m_set.yMin; ky2=m_set.yMax;
  int num1=(int)(-ky1/py);
  if(num1==0) num1++;//如果按比例分不到一格则强行设为1
   else if((-ky1-(double)num1*py)>py/2) num1++;
  int num2=m_set.VLineNum-num1;//剩下的线数
  y3=-ky1/(double)num1;
  y4=ky2/(double)num2;
  py=y4;
  if(y3>y4) py=y3;//扩容
  m_set.yMin=-(double)num1*py;//根据新的范围值,重新界定范围标识
  m_set.yMax=(double)num2*py;
 }
}

int CLyhLineChart::CalDecNum(double minnum, double maxnum,CString &data)
{
 double dy;
 int decnum=0;
 dy=maxnum-minnum;//间隔不能小于一定值,在此设为9.9
  while (dy<9.9) { dy=dy*10.0; decnum++;}//计算小数点后的位数
 dy=maxnum;
 file://找出数字可能最大的一个
 if(dy<0.0){
  if(-dy<-minnum)  dy=minnum;  
 }else if(dy<=-minnum) dy=minnum;
 data.Format("%f",dy);
 int dotpos=data.Find('.');
 if(decnum>0) data=data.Left(dotpos+decnum+1);
  else data=data.Left(dotpos);
 return decnum;
}

int CLyhLineChart::AddData(double x, double y,int LineIndex)
{
 int nCount=m_items.GetSize();
 if(LineIndex>nCount || LineIndex<1) return 0;
 CLineChartItem* pItem = m_items.GetAt(LineIndex-1);
 DataStru CurData;
 if(!m_set.autorange)
 {
  if (x>m_set.xMax) x=m_set.xMax;
  if (x<m_set.xMin) x=m_set.xMin;
  if (y>m_set.yMax) y=m_set.yMax;
  if (y<m_set.yMin) y=m_set.yMin;
 }
 CurData.x=x; CurData.y=y;
 pItem->m_data.AddTail(CurData);
 if(m_set.autorange){
  if(pItem->m_data.GetCount()==1)
  {
   m_set.xMax=x;
   m_set.xMin=x;
   m_set.yMax=y;
   m_set.yMin=y;
  }else
  {
  if(m_set.xMax<x) m_set.xMax=x;
  if(m_set.xMin>x) m_set.xMin=x;
  if(m_set.yMax<y) m_set.yMax=y;
  if(m_set.yMin>y) m_set.yMin=y;
  }
 }
 return 1;
}

int CLyhLineChart::BatchAddData(double *x, double *y, int nCount, int nIndex)
{
 int nNum=m_items.GetSize();
 if(nIndex>nNum || nIndex<1) return 0;
 if(nCount<=0) return 0;
 CLineChartItem* pItem = m_items.GetAt(nIndex-1);
 if(pItem->m_data.GetCount()<=0)
 {
   m_set.xMax=x[0];
   m_set.xMin=x[0];
   m_set.yMax=y[0];
   m_set.yMin=y[0];
 }
 for (int i=0; i<nCount; i++)
 {
  DataStru CurData;
  CurData.x=x[i]; CurData.y=y[i];
  pItem->m_data.AddTail(CurData);
  if(m_set.autorange){
   if(m_set.xMax<x[i]) m_set.xMax=x[i];
   if(m_set.xMin>x[i]) m_set.xMin=x[i];
   if(m_set.yMax<y[i]) m_set.yMax=y[i];
   if(m_set.yMin>y[i]) m_set.yMin=y[i];
   
  }
 }
 return 1;
}

int CLyhLineChart::AddSplitData(int nIndex)
{
 if(nIndex>m_items.GetSize() || nIndex<1) return 0;
 CLineChartItem* pItem = m_items.GetAt(nIndex-1);
 DataStru CurData;
 CurData.x=NODATA; CurData.y=NODATA;
 pItem->m_data.AddTail(CurData);
 return 1;
}

void CLyhLineChart::SetLineStyle(int nStyle, int nWidth, COLORREF crColor,int nIndex)
{
 if(nIndex>m_items.GetSize() || nIndex<1) return;
 CLineChartItem* pItem = m_items.GetAt(nIndex-1);
 pItem->iStyle=nStyle;
 pItem->iWidth=nWidth;
 pItem->m_colorLine=crColor;
}

void CLyhLineChart::SetTitle(CString csTitle, COLORREF crColor)
{
 if(crColor!=DEFAULTCOLOR) m_set.TileColor=crColor;
 m_set.Title=csTitle;
}

COLORREF CLyhLineChart::GetTitle(CString &csTitle)
{
 csTitle=m_set.Title;
 return m_set.TileColor;
}

CString CLyhLineChart::SetXTitle(CString csXTitle, COLORREF crColor)
{
 CString str;
 str=m_set.XTitle;
 m_set.XTitle=csXTitle;
 if(crColor!=DEFAULTCOLOR) m_set.XTileColor=crColor;
 return str;
}

CString CLyhLineChart::SetYTitle(CString csYTitle, COLORREF crColor)
{
 CString str;
 str=m_set.YTitle;
 m_set.YTitle=csYTitle;
 if(crColor!=DEFAULTCOLOR) m_set.YTileColor=crColor;
 return str;
}

CString CLyhLineChart::SetFootNote(CString csFootNote, COLORREF crColor)
{
 CString str;
 str=m_set.FootNote;
 m_set.FootNote=csFootNote;
 if(crColor!=DEFAULTCOLOR) m_set.FootColor=crColor;
 return str;
}

BOOL CLyhLineChart::ShowLegeng(BOOL bShow)
{
 BOOL old;
 old=m_set.bLegend;
 m_set.bLegend=bShow;
 return old;
}

void CLyhLineChart::SetLegend(CString csLegend, int nIndex,COLORREF crColor )
{
 if(nIndex>m_items.GetSize() || nIndex<1) return;
 CLineChartItem* pItem = m_items.GetAt(nIndex-1);
 if(crColor!=DEFAULTCOLOR) pItem->legendcolor=crColor;
 pItem->m_legend=csLegend;
}

void CLyhLineChart::DeleteLine(int nIndex)
{
 if(nIndex>m_items.GetSize() || nIndex<1) return;

 CLineChartItem* pItem = m_items.GetAt(nIndex-1);
 delete pItem;
 m_items.RemoveAt(nIndex-1);
}

 

int CLyhLineChart::GetIndex(CString csLegend)
{
 CLineChartItem* pItem;
 for(int i=0; i<m_items.GetSize(); i++)
 {
  pItem=m_items.GetAt(i);
  if(pItem->m_legend==csLegend) return i;
 }
 return -1;
}

int CLyhLineChart::GetLineNum()
{
 return m_items.GetSize();
}

int CLyhLineChart::SetXDivision(int nDiv)
{
 int i;
 if(nDiv<1) nDiv=1;
 i=m_set.HLineNum;
 m_set.HLineNum=nDiv;
 return i;
}

int CLyhLineChart::SetYDivision(int nDiv)
{
 int i;
 if(nDiv<1) nDiv=1;
 i=m_set.VLineNum;
 m_set.VLineNum=nDiv;
 return i;
}

void CLyhLineChart::ShowXGrid(BOOL bSign)
{
 m_set.bxInBorder=bSign;
}

void CLyhLineChart::ShowYGrid(BOOL bSign)
{
 m_set.byInBorder=bSign;
}

COLORREF CLyhLineChart::SetBkColor(COLORREF crColor)
{
 COLORREF color;
 color=m_set.bkColor;
 m_set.bkColor=crColor;
 return color;
}

void CLyhLineChart::SetPicFont(int fontsize, CString fontname)
{
 m_set.fontname=fontname;
 m_set.fontsize=fontsize;
}

void CLyhLineChart::SetAxiedColor(COLORREF crColor)
{
 m_set.axiednumcolor=crColor;
}

 

void CLyhLineChart::OnSize(UINT nType, int cx, int cy)
{
 CWnd::OnSize(nType, cx, cy);
 
 // TODO: Add your message handler code here
 DrawPic();
 Invalidate(false);

}

 

本文地址:http://com.8s8s.com/it/it1209.htm