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

📄 areagraph.cs

📁 C#自定义查询控件
💻 CS
字号:
// Disclaimer and Copyright Information
// AreaGraph.cs : 
//
// All rights reserved.
//
// Written by Pardesi Services, LLC
// Version 1.01
//
// Distribute freely, except: don't remove our name from the source or
// documentation (don't take credit for my work), mark your changes (don't
// get me blamed for your possible bugs), don't alter or remove this
// notice.
// No warrantee of any kind, express or implied, is included with this
// software; use at your own risk, responsibility for damages (if any) to
// anyone resulting from the use of this software rests entirely with the
// user.
//
// Send bug reports, bug fixes, enhancements, requests, flames, etc. to
// softomatix@CommonComponent.com
///////////////////////////////////////////////////////////////////////////////
//

using System;
using System.Diagnostics;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml;

namespace CommonComponent.CommonChart
{
	/// <summary>
	/// 
	/// </summary>
	public class AreaGraph : CommonChart.AxialGraph
	{
		protected float m_fXScale;
		protected float m_fYScale;

		public AreaGraph()
		{
			// 
			// TODO: Add constructor logic here
			//
			m_fXScale = 1.0F;
			m_fYScale = 1.0F;
			this.m_GraphType = Type.AreaGraph;
			this.m_QuadrantToShow = QuadrantType.Quad1;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		protected bool CalculateXScale()
		{
			m_fXScale = (this.m_iXAxisSpan)/(float)(MaxXValue - MinXValue);
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		protected bool CalculateYScale()
		{
			m_fYScale = (this.m_iYAxisSpan)/(float)(this.MaxYValue - this.MinYValue);
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		override protected bool CalculateAxisLocation()
		{
			// Calculate the origin of axis.
			CalculateAxis();
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		override protected bool DrawGraphAxis(bool bDrawGrid)
		{
			// Calculate the width of x-axis and y-axis lines
			CalculateAxisSpan();
			CalculateXScale();
			CalculateYScale();

			// Calculate the axis location.
			CalculateAxisLocation();
			CalculateAxisLabelLocations();
			DrawAxisLines(bDrawGrid);
			DrawAxisLabels();
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		override protected bool DrawAxisLines(bool bDrawGrid)
		{
			Pen obPen = null;
			Pen obGridPen = null;
			try
			{
				// Draw the grid lines now.
				if (HasGridLines && bDrawGrid && this.YTickSpacing > 0)
				{
					SizeF rectText;
					string strVal = "";
					PointF pt1 = new PointF();
					PointF pt2 = new PointF();
					obGridPen = new Pen(GridColor);
					
					int iYTickCount = (int)((this.MaxYValue - this.MinYValue)/YTickSpacing);
					int iXTickCount = (int)((this.MaxXValue - this.MinXValue)/XTickSpacing);

					// Draw Y Grid Lines
					for (int i = 0; i <= iYTickCount; i++)
					{
						pt1.X = this.m_ChartAxis.X1.X;
						pt2.X = pt1.X + this.m_iXAxisSpan;
						pt1.Y = this.m_ChartAxis.Y2.Y - (i * this.YTickSpacing) * this.m_fYScale;
						pt2.Y = pt1.Y;
						
						this.m_obGraphics.DrawLine(obGridPen, pt1, pt2);
						
						strVal = (MinYValue + (i * YTickSpacing)).ToString();
						rectText = this.m_obGraphics.MeasureString(strVal, this.m_YTickFont);
						pt1.X = pt1.X - (rectText.Width + 2);
						this.m_obGraphics.DrawString(strVal, this.m_YTickFont, new SolidBrush(this.m_TitleColor), pt1);

					}

					// Draw XGrid Lines
					for (int i = 0; i <= iXTickCount; i++)
					{
						pt1.X = this.m_ChartAxis.Y2.X + (i * this.XTickSpacing) * this.m_fXScale;
						pt1.Y = this.m_ChartAxis.Y2.Y;
						pt2.X = pt1.X;
						pt2.Y = this.m_ChartAxis.Y1.Y;
						
						this.m_obGraphics.DrawLine(obGridPen, pt1, pt2);
						

						strVal = (MinXValue + (i * XTickSpacing)).ToString();
						rectText = this.m_obGraphics.MeasureString(strVal, this.m_YTickFont);
						pt1.X = pt1.X - (rectText.Width + 2);
						this.m_obGraphics.DrawString(strVal, this.m_XTickFont, new SolidBrush(this.m_TitleColor), pt1);

					}
				}

				obPen = new Pen(new SolidBrush(Color.Black));
				this.m_obGraphics.DrawLine(obPen, this.m_ChartAxis.X1, this.m_ChartAxis.X2);
				this.m_obGraphics.DrawLine(obPen, this.m_ChartAxis.Y2, this.m_ChartAxis.Y1);
			}
			finally
			{
				if (obPen != null)
				{
					obPen.Dispose();
				}
			}
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		override protected bool DrawData()
		{
			//TODO: For now we will process only first array of data list. Later
			//		on implementation needs to take into account that there may
			//		be more than one list.
			
			Brush obAreaBrush = null;
			Pen obPen = null;
	
			ArrayList dataList = (ArrayList)this.m_obIslandList[0];
			if (null == dataList || dataList.Count == 0)
			{
				throw new Exception("There is no data list present in the array.");
			}

			int nCount = dataList.Count;

			if (this.UseRandomColors)
			{
				ColorUtil.GenerateRandomColors(this.m_obDataIslandNodes.Count, out m_arrColors);
			}
			else
			{
				ColorUtil.GenerateColorValues(this.Colors, out m_arrColors);
			}

			for (int nIdx = 0; nIdx < this.m_obDataIslandNodes.Count; nIdx++)
			{
				Color areaColor = (Color)this.m_arrColors[nIdx];
				ArrayList obIslandDataList = (ArrayList)this.m_obIslandList[nIdx];
				
				// Create a polygon for specifying all the data points. We will
				// need two extra points for specifying the starting and closing
				// point to close the polygon.
				PointF [] areaPolygon = new PointF[obIslandDataList.Count + 2];

				for (int i = 0; i < obIslandDataList.Count; i++)
				{
					GraphDataObject obData1 = (GraphDataObject)obIslandDataList[i];
					if (null != obData1)
					{
						areaPolygon[i+1].X = this.m_ChartAxis.Origin.X + (float)(obData1.X * this.m_fXScale);
						areaPolygon[i+1].Y = this.m_ChartAxis.Origin.Y - (float)(obData1.Y * this.m_fYScale);
					}
				}

				// Now add first and last point.
				areaPolygon[0].X = areaPolygon[1].X;
				areaPolygon[0].Y = this.m_ChartAxis.Origin.Y;

				areaPolygon[nCount+1].X = areaPolygon[nCount].X;
				areaPolygon[nCount+1].Y = this.m_ChartAxis.Origin.Y;

				if (this.UseGradientColors == true)
				{
					obAreaBrush = DrawingUtil.GetPathGradientBrush(areaPolygon, areaColor, GraphGradientFactor, LinearGradientMode.Horizontal);
				}
				else
				{
					obAreaBrush = new SolidBrush(areaColor);
				}

				obPen = new Pen(new SolidBrush(areaColor));

				// Draw the polygon now.
				this.m_obGraphics.FillPolygon(obAreaBrush, areaPolygon);
				this.m_obGraphics.DrawPolygon(obPen, areaPolygon);
				obAreaBrush.Dispose();
				obPen.Dispose();
			}
			return true;
		}
	}
}

⌨️ 快捷键说明

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