LineChart.cpp
上传用户:hbytqc8
上传日期:2014-07-31
资源大小:527k
文件大小:8k
- // LineChart.cpp : implementation file
- //
- #include "stdafx.h"
- #include "LanFlow.h"
- #include "LineChart.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CLineChart
- CLineChart::CLineChart(CSize cs)
- {
- mark=true;
- legend=true;
- percent=false;
- xSum=0;
- ySum=0;
- Count=0;
- bkcolor=RGB(250,250,80);
- title="My Chart";
- xLegend="xLegend";
- yLegend="yLegend";
- tFont.CreatePointFont(80,"楷体_GB2132",NULL);
- ChartSize=cs;
- first=0;
- srand((unsigned)time(NULL));
- LineData ld;
- for (int i=0;i<5;i++)
- {
- ld.x=rand()%100;
- ld.y=rand()%50;
- ld.xP="";
- ld.xP="";
- ld.xTime="127.0.0.1";
- ld.yTime="127.0.0.1";
- ArrayData.Add(ld);
- xSum+=ld.x;
- ySum+=ld.y;
- }
- yMax=100;
- }
- CLineChart::~CLineChart()
- {
- }
- BEGIN_MESSAGE_MAP(CLineChart, CStatic)
- //{{AFX_MSG_MAP(CLineChart)
- ON_WM_PAINT()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CLineChart message handlers
- void CLineChart::OnPaint()
- {
- CPaintDC dc(this); // device context for painting
- int height,width;
- CPen pen,*pPen;
- CBrush br,*pB;
- COLORREF color;
- br.CreateSolidBrush(bkcolor);
- pB=dc.SelectObject(&br);
- dc.Rectangle(0,0,ChartSize.cx,ChartSize.cy);
- dc.SelectObject(pB);
- br.DeleteObject();
- int n1=dc.SetBkMode(TRANSPARENT);
- CPoint Start,xEnd,yEnd,temp1,temp2;
- //确定坐标系,画X轴;
- Start.x=60;
- Start.y=ChartSize.cy-40;
- dc.MoveTo(Start);
- xEnd.x=ChartSize.cx-40;
- xEnd.y=Start.y;
- width=xEnd.x-Start.x;
- dc.LineTo(xEnd.x+5,xEnd.y);
- dc.TextOut(xEnd.x+8,xEnd.y-8,"地址");
- //画X轴箭头;
- dc.LineTo(xEnd.x,xEnd.y-3);
- dc.MoveTo(xEnd.x+5,xEnd.y);
- dc.LineTo(xEnd.x,xEnd.y+3);
- //画Y轴;
- dc.MoveTo(Start);
- yEnd.y=40;
- yEnd.x=Start.x;
- height=Start.y-yEnd.y;
- //计算Y轴每点对应的数值,取整会影响精度;
- float n=(float)yMax/(height-10);
- height=(height-10)/5;
- //Y轴以及箭头;
- dc.LineTo(yEnd);
- dc.LineTo(yEnd.x-3,yEnd.y+5);
- dc.MoveTo(yEnd);
- dc.LineTo(yEnd.x+3,yEnd.y+5);
- dc.TextOut(yEnd.x-20,yEnd.y-20,"Bytes");
- //画Y轴虚线,标注刻度值;
- pen.CreatePen(PS_DOT,1,RGB(0,0,0));
- pPen=dc.SelectObject(&pen);
- dc.TextOut(60-9,Start.y-7,"0");
- CString yText;
- int yCo;
- for (int i=0;i<5;i++)
- {
- dc.MoveTo(Start.x,Start.y-(i+1)*height);
- dc.LineTo(Start.x+ChartSize.cx-100,Start.y-(i+1)*height);
- yCo=(i+1)*(yMax/5);
- yText.Format("%d",yCo);
- dc.TextOut(60-yText.GetLength()*9,Start.y-(i+1)*height-7,yText);
- }
- //画X轴坐标线;
- int xW=(width/5+0.5);
- for (i=1;i<=5;i++)
- {
- dc.MoveTo(Start.x+i*xW,Start.y);
- dc.LineTo(Start.x+i*xW,Start.y-5*height);
- }
- dc.SelectObject(pPen);
- pen.DeleteObject();
- //画曲线X;
- pen.CreatePen(PS_SOLID,1,RGB(0,0,255));
- pPen=dc.SelectObject(&pen);
- color=RGB(0,0,255);
- br.CreateSolidBrush(color);
- pB=dc.SelectObject(&br);
- CString data;
- // int n1;
- for (i=first;i<first+5;i++)
- {
- temp1.x=Start.x+(i-first)*xW+25;
- temp1.y=Start.y-(int)(ArrayData[i].x/n+0.5);
- temp2.x=temp1.x+20;
- temp2.y=Start.y;
- dc.Rectangle(temp1.x,temp1.y,temp2.x,temp2.y);
- if (mark)
- {
- if (percent)
- dc.TextOut(temp1.x-5,temp1.y-20,ArrayData[i].xP);
- else
- {
- data.Format("%d",ArrayData[i].x);
- dc.TextOut(temp1.x-5,temp1.y-20,data);
- }
- }
- }
- dc.SelectObject(pPen);
- pen.DeleteObject();
- dc.SelectObject(pB);
- br.DeleteObject();
- pen.CreatePen(PS_SOLID,1,RGB(255,0,0));
- pPen=dc.SelectObject(&pen);
- color=RGB(255,0,0);
- br.CreateSolidBrush(color);
- pB=dc.SelectObject(&br);
- //画曲线Y;
- // n1=dc.SetBkMode(TRANSPARENT);
- for (i=first;i<first+5;i++)
- {
- temp1.x=Start.x+(i-first)*xW+45;
- temp1.y=Start.y-(int)(ArrayData[i].y/n+0.5);
- temp2.x=temp1.x+20;
- temp2.y=Start.y;
- dc.Rectangle(temp1.x,temp1.y,temp2.x,temp2.y);
- if ((ArrayData[i].y!=0)&&(mark))
- {
- if (percent)
- dc.TextOut(temp1.x+1,temp1.y-20,ArrayData[i].yP);
- else
- {
- data.Format("%d",ArrayData[i].y);
- dc.TextOut(temp1.x+1,temp1.y-20,data);
- }
- }
- }
- dc.SelectObject(pPen);
- pen.DeleteObject();
- dc.SelectObject(pB);
- br.DeleteObject();
- CFont* pOld=dc.SelectObject(&tFont);
- for (i=first;i<first+5;i++)
- {
- dc.TextOut(Start.x+(i-first)*xW+4,Start.y+8,ArrayData[i].xTime);
- if (ArrayData[i].yTime.GetLength()>0)
- {
- dc.TextOut(Start.x+(i-first)*xW+4,Start.y+20,ArrayData[i].yTime);
- }
- }
- dc.SelectObject(pOld);
-
- //画标题;
- dc.TextOut(yEnd.x+ChartSize.cx/4,4,title);
- if (legend)
- {
- pen.CreatePen(PS_SOLID,1,RGB(0,255,0));
- pPen=dc.SelectObject(&pen);
- dc.MoveTo(yEnd.x+xEnd.x-180,yEnd.y-10);
- dc.LineTo(yEnd.x+xEnd.x-160,yEnd.y-10);
- dc.TextOut(yEnd.x+xEnd.x-150,yEnd.y-20,yLegend);
- dc.SelectObject(pPen);
- pen.DeleteObject();
- pen.CreatePen(PS_SOLID,1,RGB(0,0,255));
- pPen=dc.SelectObject(&pen);
- dc.MoveTo(yEnd.x+xEnd.x-260,yEnd.y-10);
- dc.LineTo(yEnd.x+xEnd.x-240,yEnd.y-10);
- dc.TextOut(yEnd.x+xEnd.x-230,yEnd.y-20,xLegend);
- dc.SelectObject(pPen);
- pen.DeleteObject();
- dc.SetBkMode(n1);
- }
- // Do not call CStatic::OnPaint() for painting messages
- }
- //增加数据项到队列末尾,原来每个项依次前移一项;增加的
- //数据如果超出Y轴目前最大值,则最大值以100为单位递增,直
- //到大于输入的数据为止;最后重绘图表;
- void CLineChart::Append(int x1,int y1,CString xtt,CString ytt)
- {
- int temp;
- if (x1>y1)
- temp=x1;
- else
- temp=y1;
- while (yMax<temp)
- yMax+=100;
- LineData ld;
- ld.x=x1;
- ld.y=y1;
- ld.xP="";
- ld.yP="";
- ld.xTime=xtt;
- ld.yTime=ytt;
- if (Count<5)
- ArrayData[Count]=ld;
- else
- ArrayData.Add(ld);
- Count+=1;
- xSum+=x1;
- ySum+=y1;
- }
- //初始化图表数据;
- void CLineChart::Init()
- {
- yMax=100;
- xSum=0;
- ySum=0;
- Count=0;
- // LineData ld;
- // ArrayData.RemoveAll();
- for(int i=0;i<5;i++)
- {
- ArrayData[i].x=0;
- ArrayData[i].y=0;
- ArrayData[i].xP="";
- ArrayData[i].yP="";
- ArrayData[i].xTime="";
- ArrayData[i].yTime="";
- }
- ArrayData.RemoveAt(5,ArrayData.GetSize()-5);
- // this->Invalidate();
- }
- void CLineChart::SetTitle(CString str)
- {
- title=str;
- this->Invalidate();
- }
- void CLineChart::SetLegend(CString xStr,CString yStr)
- {
- xLegend=xStr;
- yLegend=yStr;
- this->Invalidate();
- }
- void CLineChart::SetFirst(bool tf)
- {
- int n=ArrayData.GetSize();
- if ((tf)&&(first<n-5))
- first++;
- if ((!(tf))&&(first>0))
- first--;
- this->Invalidate();
- }
- void CLineChart::SetMark()
- {
- if (mark)
- mark=false;
- else
- mark=true;
- this->Invalidate();
- }
- void CLineChart::SetLegend()
- {
- if (legend)
- legend=false;
- else
- legend=true;
- this->Invalidate();
- }
- void CLineChart::SetMyBKColor(COLORREF color)
- {
- bkcolor=color;
- this->Invalidate();
- }
- void CLineChart::WorkPercent()
- {
- int n;
- if ((xSum!=0)&&(ySum!=0))
- for (int i=0;i<ArrayData.GetSize();i++)
- {
- n=(int)(100*ArrayData[i].x/xSum);
- ArrayData[i].xP.Format("%d",n);
- ArrayData[i].xP+="%";
- n=(int)(100*ArrayData[i].y/ySum);
- ArrayData[i].yP.Format("%d",n);
- ArrayData[i].yP+="%";
- }
- this->Invalidate();
- }
- void CLineChart::SetPercent()
- {
- if (percent)
- percent=false;
- else
- percent=true;
- this->Invalidate();
- }
- CString CLineChart::GetText()
- {
- CString text,temp,data;
- text="";
- for (int i=0;i<ArrayData.GetSize();i++)
- {
- temp="";
- if (ArrayData[i].xTime.GetLength()!=0)
- {
- if (ArrayData[i].xTime.Find('.')==-1)
- {
-
- temp="地址:"+temp+ArrayData[i].yTime+" Input: ";
- data.Format("%d",ArrayData[i].x);
- temp=temp+data+" OutPut: ";
- data.Format("%d",ArrayData[i].y);
- temp+=data;
- temp=temp+" "+ArrayData[i].xP+" "+ArrayData[i].yP;
- temp=temp+"rn";
-
- }
- else
- {
- temp=temp+ArrayData[i].xTime+"-->"+ArrayData[i].yTime;
- data.Format("%d",ArrayData[i].x);
- temp=temp+"数据: "+data+ArrayData[i].xP+"rn";
- }
-
- text+=temp;
- }
- }
- return text;
- }