Graph.cpp
Upload User: tdk119
Upload Date: 2008-01-21
Package Size: 51k
Code Size: 6k
Category:

Graph Drawing

Development Platform:

Visual C++

  1. //2006.3.24 
  2. //Devang
  3. //Graph class for EMS2
  4. //
  5. //此类为显示曲线,
  6. #include "stdafx.h"
  7. #include "Graph.h"
  8. #include "math.h"
  9. CGraph::CGraph()
  10. {
  11. m_pDC = NULL;
  12. m_data = NULL;
  13. m_color = RGB(0, 128, 64);
  14. m_origin = CPoint(0, 0);
  15. m_scale = 1;
  16. bViewX = TRUE;
  17. XColor = RGB(0,0,0);
  18. XMin = 0;
  19. XMax = 100;
  20. bViewY = TRUE;
  21. YColor = RGB(0,0,0);
  22. YMin = 0;
  23. YMax = 100;
  24. bViewGrid = TRUE;
  25. GridColor = RGB(192,192,192);
  26. GridWidth = 50;
  27. }
  28. CGraph::~CGraph()
  29. {
  30. m_pDC = NULL;
  31. m_data = NULL;
  32. }
  33. BOOL CGraph::SetDC(CDC *dc)
  34. {
  35. m_pDC = dc;
  36. return TRUE;
  37. }
  38. CDC *CGraph::GetDC()
  39. {
  40. return m_pDC;
  41. }
  42. BOOL CGraph::SetData(float *data)
  43. {
  44. m_data = data;
  45. return TRUE;
  46. }
  47. float * CGraph::GetData()
  48. {
  49. return m_data;
  50. }
  51. BOOL CGraph::SetGraphRect(CRect rect)
  52. {
  53. m_rect = rect;
  54. return TRUE;
  55. }
  56. CRect CGraph::GetGraphRect()
  57. {
  58. return m_rect;
  59. }
  60. BOOL CGraph::SetXrect(CRect rect)
  61. {
  62. Xrect = rect;
  63. return TRUE;
  64. }
  65. CRect CGraph::GetXrect()
  66. {
  67. return Xrect;
  68. }
  69. BOOL CGraph::SetYrect(CRect rect)
  70. {
  71. Yrect = rect;
  72. return TRUE;
  73. }
  74. CRect CGraph::GetYrect()
  75. {
  76. return Yrect;
  77. }
  78. BOOL CGraph::SetXColor(COLORREF color)
  79. {
  80. XColor = color;
  81. return TRUE;
  82. }
  83. COLORREF CGraph::GetXColor()
  84. {
  85. return XColor;
  86. }
  87. BOOL CGraph::SetYColor(COLORREF color)
  88. {
  89. YColor = color;
  90. return TRUE;
  91. }
  92. BOOL CGraph::SetX(float min, float max)
  93. {
  94. XMin = min;
  95. XMax = max;
  96. return TRUE;
  97. }
  98. float CGraph::GetXmin()
  99. {
  100. return XMin;
  101. }
  102. float CGraph::GetXmax()
  103. {
  104. return XMax;
  105. }
  106. COLORREF CGraph::GetYColor()
  107. {
  108. return YColor;
  109. }
  110. BOOL CGraph::SetY(float min, float max)
  111. {
  112. YMin = min;
  113. YMax = max;
  114. return TRUE;
  115. }
  116. float CGraph::GetYmin()
  117. {
  118. return YMin;
  119. }
  120. float CGraph::GetYmax()
  121. {
  122. return YMax;
  123. }
  124. BOOL CGraph::SetGridWidth(UINT width)
  125. {
  126. GridWidth = width;
  127. return TRUE;
  128. }
  129. UINT CGraph::GetGridWidth()
  130. {
  131. return GridWidth;
  132. }
  133. BOOL CGraph::IsViewX(BOOL bView)
  134. {
  135. bViewX = bView;
  136. return bViewX;
  137. }
  138. BOOL CGraph::IsViewY(BOOL bView)
  139. {
  140. bViewY = bView;
  141. return bViewY;
  142. }
  143. BOOL CGraph::IsViewGrid(BOOL bView)
  144. {
  145. bViewGrid = bView;
  146. return bViewGrid;
  147. }
  148. BOOL CGraph::SetGridColor(COLORREF color)
  149. {
  150. GridColor = color;
  151. return TRUE;
  152. }
  153. COLORREF CGraph::GetGridColor()
  154. {
  155. return GridColor;
  156. }
  157. BOOL CGraph::PaintX()
  158. {
  159. CPen pen(PS_SOLID, 0, XColor);
  160. CPen *oldpen = m_pDC->SelectObject(&pen);
  161. UINT align = m_pDC->SetTextAlign(TA_CENTER);
  162. m_pDC->MoveTo(Xrect.left, Xrect.top);
  163. m_pDC->LineTo(Xrect.right, Xrect.top);
  164. m_pDC->SetTextAlign(align);
  165. m_pDC->SelectObject(oldpen);
  166. return TRUE;
  167. }
  168. BOOL CGraph::PaintY()
  169. {
  170. CPen pen(PS_SOLID, 0, YColor);
  171. CPen *oldpen = m_pDC->SelectObject(&pen);
  172. UINT align = m_pDC->SetTextAlign(TA_CENTER);
  173. m_pDC->MoveTo(Yrect.right, Yrect.top);
  174. m_pDC->LineTo(Yrect.right, Yrect.bottom);
  175. m_pDC->SetTextAlign(align);
  176. m_pDC->SelectObject(oldpen);
  177. return TRUE;
  178. }
  179. BOOL CGraph::Paint()
  180. {
  181. if (bViewX)
  182. {
  183. PaintX();
  184. }
  185. if (bViewY)
  186. {
  187. PaintY();
  188. }
  189. if (bViewGrid)
  190. {
  191. PaintGrid();
  192. }
  193. return PaintGraph();
  194. return TRUE;
  195. }
  196. CPoint CGraph::GetOrigin() //输出图形的原点
  197. {
  198. return m_origin;
  199. }
  200. BOOL CGraph::PaintGrid()
  201. {
  202. CPen pen(PS_DOT, 0, GridColor);
  203. CPen *oldpen = m_pDC->SelectObject(&pen);
  204. int oldmode = m_pDC->SetBkMode(TRANSPARENT);
  205. int i;
  206. for(i=m_rect.top+GridWidth; i<m_rect.bottom; )
  207. {
  208. m_pDC->MoveTo(m_rect.left, i);
  209. m_pDC->LineTo(m_rect.right, i);
  210. i += GridWidth;
  211. }
  212. for(i=m_rect.left+GridWidth; i<m_rect.right;)
  213. {
  214. m_pDC->MoveTo(i, m_rect.top);
  215. m_pDC->LineTo(i, m_rect.bottom);
  216. i += GridWidth;
  217. }
  218. m_pDC->SetBkMode(oldmode);
  219. m_pDC->SelectObject(oldpen);
  220. return TRUE;
  221. }
  222. BOOL CGraph::SetGraphColor(COLORREF color)
  223. {
  224. m_color = color;
  225. return TRUE;
  226. }
  227. COLORREF CGraph::GetGraphColor()
  228. {
  229. return m_color;
  230. }
  231. int CGraph::SetDataLength(int length)
  232. {
  233. int ol = m_length;
  234. m_length = length;
  235. return ol;
  236. }
  237. int CGraph::GetDataLength()
  238. {
  239. return m_length;
  240. }
  241. BOOL CGraph::PaintGraph()
  242. {
  243. if(m_data == NULL)
  244. return FALSE;
  245. CPen pen(PS_SOLID, 0, m_color);
  246. CPen *oldpen = m_pDC->SelectObject(&pen);
  247. float widthscale = GetWidthScale();
  248. float heightscale = GetHeightScale();
  249. int i;
  250. int x,y,width,height;
  251. x = m_origin.x;
  252. y = m_origin.y;
  253. width = m_rect.Width();
  254. height = m_rect.Height();
  255. m_pDC->MoveTo(x, (int)(y - m_data[0]*heightscale));
  256. if (width/(widthscale*m_scale) > m_length)
  257. {
  258. m_scale = 1;
  259. }
  260. for (i=0; x<width; x++,i++)
  261. {
  262. m_pDC->LineTo(x, (int)(y - m_data[(int)(i/(widthscale*m_scale))]*heightscale));
  263. }
  264. m_pDC->SelectObject(oldpen);
  265. return TRUE;
  266. }
  267. float CGraph::GetWidthScale()
  268. {
  269. return (float)(m_rect.Width())/(float)(m_length);
  270. }
  271. float CGraph::GetHeightScale()
  272. {
  273. float min,max,absmax;
  274. CPoint origin;
  275. CalculMinMax(m_data, m_length, absmax, min, max);
  276. origin.x = m_rect.left;
  277. if ((max >=0) && (min >= 0))
  278. origin.y = m_rect.bottom;
  279. else if((max <= 0) && (min <= 0))
  280. origin.y = m_rect.top;
  281. else
  282. origin.y = (long)(m_rect.Height()*fabs(max)/(max-min));
  283. SetOrigin(origin);
  284. return (float)(m_rect.Height())/(float)(max-min);
  285. }
  286. BOOL CGraph::SetOrigin(CPoint origin)
  287. {
  288. m_origin = origin;
  289. return TRUE;
  290. }
  291. BOOL CGraph::SetGraphScale(int scale)
  292. {
  293. m_scale = scale;
  294. return TRUE;
  295. }
  296. int CGraph::GetGraphScale()
  297. {
  298. return m_scale;
  299. }
  300. float CGraph::CalculMinMax(float *a, int count, float &absmax, float &min, float &max)
  301. {
  302. int i;
  303. min = max = a[0];
  304. for(i=0; i<count; i++)
  305. {
  306. if(a[i] > max)
  307. max = a[i];
  308. if(a[i] < min)
  309. min = a[i];
  310. }
  311. absmax = fabsf(min)>fabsf(max)?fabsf(min):fabsf(max);
  312. return absmax;
  313. }