graph.cs

来自「C#自定义查询控件」· CS 代码 · 共 2,411 行 · 第 1/5 页

CS
2,411 行
字号
			}
			finally
			{
				this.CleanUp();
			}
			return bRet;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		virtual public bool DrawGraph(ref Stream obOutputStream)
		{
			bool bRet = false;
            
			ImageFormat imgFormat = ImageFormat.Bmp;
 
			// Cretae the graphics object on which we will draw the charts.
			bRet = this.CreateGraphics();

			DrawGraph();

			try
			{
				this.m_obGraphics.DrawImage(this.m_obBitmap, 0, 0);
				this.m_obBitmap.Save(obOutputStream, imgFormat);
				bRet = true;
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.Message);
				throw ex;
			}
			finally
			{
				this.CleanUp();
			}
			return bRet;
		}


		/// <summary>
		/// 
		/// </summary>
		/// <param name="obGraphics"></param>
		/// <returns></returns>
		virtual public bool DrawGraph(Graphics obGraphics)
		{
			// Check that inpout parameter is not null.
			if (null == obGraphics)
			{
				throw new Exception("Ref Grpahics input object can not be null");
			}
			// Assign the input Graphics object reference to intrnal variable.
			this.m_obGraphics = obGraphics;
			// Calculate object's clipping region. This value will be used to
			// set the width and height parameters for chart.
			RectangleF clipRect = m_obGraphics.VisibleClipBounds;
			Width = (Int32)clipRect.Width;
			Height = (Int32)clipRect.Height;

			DrawGraph();
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		private void DrawLogo()
		{
			// Check if the flag is set for drawing compnay logo or not.
			if (!this.HasCompanyLogo)
			{
				return;
			}

			// Get the size of logo image.
			SizeF imgSize = DrawingUtil.GetImageSize(this.LogoImageFile);

			PointF imgPt = new PointF();
			RectangleF rect;
			switch (LogoPosition)
			{
				case PositionType.LeftTop:
					rect = this.m_TopLeftRectangle;
					break;
				case PositionType.RightTop:
					rect = this.m_TopRightRectangle;
					break;
				case PositionType.LeftBottom:
					rect = this.m_BottomLeftRectangle;
					break;
				case PositionType.RightBottom:
					rect = this.m_BottomRightRectangle;
					break;
				case PositionType.Middle:
					rect = this.m_ChartRectangle;
					break;
				default:
					throw new ArgumentException("Invalid position type specified for logo", "LogoPosition");
			}

			// If the image size if more than the bounding rectangle, then we will have to compress it.
			System.Drawing.Image obImg = System.Drawing.Image.FromFile(LogoImageFile);
			if (imgSize.Width > rect.Width || imgSize.Height > rect.Height)
			{
				imgPt.X = rect.X;
				imgPt.Y = rect.Y;

				m_obGraphics.DrawImage(obImg.GetThumbnailImage((int)rect.Width, (int)rect.Height, null, IntPtr.Zero), imgPt);
			}
			else
			{
				imgPt.X = rect.X + (rect.Width - imgSize.Width)/2.0F;
				imgPt.Y = rect.Y + (rect.Height - imgSize.Height)/2.0F;
				m_obGraphics.DrawImage(obImg, imgPt);
			}
			
			obImg.Dispose();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		virtual protected bool DrawGraphAxis(bool bDrawGrid)
		{
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		virtual protected bool CalculateAxisLocation()
		{
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		virtual protected bool DrawGridLines()
		{
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		virtual protected bool DrawAxisLines(bool bDrawGrid)
		{
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		virtual protected bool DrawTickers()
		{
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		virtual protected bool DrawLegends()
		{
			//this.CalculateLegendsRectangle(this.m_sLegendCount);
			switch (this.LegendAlignment)
			{
				case LegendAlignmentType.Horizontal:
					return this.DrawHorizontalLegends();
				case LegendAlignmentType.Vertical:
					return this.DrawVerticalLegends();
			}
			return true;
		}

		virtual protected bool DrawBorder()
		{
			Pen obPen = null;
			try
			{
				obPen = new Pen(new SolidBrush(Color.Red));
				this.m_obGraphics.DrawRectangle(obPen, 0, 0, this.m_iWidth, this.m_iHeight);
			}
			catch (OutOfMemoryException ex)
			{
				Trace.WriteLine(ex.Message);
				throw ex;
			}
			finally
			{
				if (null != obPen)
				{
					obPen.Dispose();
				}
			}
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		virtual protected bool DrawData()
		{
			return false;
		}

		virtual protected bool CalculateAxis()
		{
			/*
			Point x1, x2, y1, y2;
			try
			{
				int iOriginFromBottom = 0;
				x1 = new Point();
				x2 = new Point();
				y1 = new Point();
				y2 = new Point();
				this.m_Origin = new Point();

				// Calculate location of origin from bottom.
				double factor = (double)this.MinYValue/(this.MaxYValue - this.MinYValue);
				iOriginFromBottom = (int)(this.m_iYAxisSpan * factor);
				switch (this.m_QuadrantToShow)
				{
					case QuadrantType.Quad1:
						//this.m_Origin.X = this.m_iAxisPaddingLeft;
						this.m_Origin.X = (int)this.LeftSpacing;
						//this.m_Origin.Y = this.m_iHeight - this.m_iAxisPaddingBottom;
						this.m_Origin.Y = (int)(this.m_iHeight - this.BottomSpacing);
						x1.X = y2.X = y1.X = this.m_Origin.X;
						x1.Y = y2.Y = this.m_Origin.Y;
						x2.X = x1.X + this.m_iXAxisSpan;
						x2.Y = x1.Y;
						//y1.Y = this.m_iAxisPaddingTop;
						y1.Y = (int)this.TopSpacing;
						break;
					case QuadrantType.Quad2:
						//this.m_Origin.X = this.m_iAxisPaddingLeft + this.m_iXAxisSpan;
						//this.m_Origin.Y = this.m_iHeight - this.m_iAxisPaddingBottom;
						this.m_Origin.X = (int)(this.LeftSpacing + this.m_iXAxisSpan);
						this.m_Origin.Y = (int)(this.m_iHeight - this.BottomSpacing);
						//x1.X = this.m_iAxisPaddingLeft;
						x1.X = (int)this.LeftSpacing;
						x1.Y = x2.Y = y2.Y = this.m_Origin.Y;
						x2.X = y1.X = y2.X = this.m_Origin.X;
						//y1.Y = this.m_iAxisPaddingTop;
						y1.Y = (int)this.TopSpacing;
						break;
					case QuadrantType.Quad3:
						//this.m_Origin.X = this.m_iAxisPaddingLeft + this.m_iXAxisSpan;
						//this.m_Origin.Y = this.m_iAxisPaddingTop;
						this.m_Origin.X = (int)(this.LeftSpacing + this.m_iXAxisSpan);
						this.m_Origin.Y = (int)this.TopSpacing;
						//x1.X = this.m_iAxisPaddingLeft;
						x1.X = (int)this.LeftSpacing;
						x1.Y = x2.Y = y1.Y = this.m_Origin.Y;
						x2.X = y1.X = y2.X = this.m_Origin.X;
						y2.Y = y1.Y + this.m_iYAxisSpan;
						break;
					case QuadrantType.Quad4:
						//this.m_Origin.X = this.m_iAxisPaddingLeft;
						//this.m_Origin.Y = this.m_iAxisPaddingTop;
						this.m_Origin.X = (int)this.LeftSpacing;
						this.m_Origin.Y = (int)this.TopSpacing;
						x1.X = y1.X = y2.X = this.m_Origin.X;
						x1.Y = y1.Y = x2.Y = this.m_Origin.Y;
						x2.X = x1.X + this.m_iXAxisSpan;
						y2.Y = y1.Y + this.m_iYAxisSpan;
						break;
					case QuadrantType.Quad12:
						//this.m_Origin.X = this.m_iAxisPaddingLeft + this.m_iXAxisSpan/2;
						//this.m_Origin.Y = this.m_iHeight - this.m_iAxisPaddingBottom;
						this.m_Origin.X = (int)(this.LeftSpacing + this.m_iXAxisSpan/2);
						this.m_Origin.Y = (int)(this.m_iHeight - this.BottomSpacing);
						//x1.X = this.m_iAxisPaddingLeft;
						x1.X = (int)this.LeftSpacing;
						x1.Y = y2.Y = x2.Y = this.m_Origin.Y;
						x2.X = x1.X + this.m_iXAxisSpan;
						y2.X = y1.X = this.m_Origin.X;
						//y1.Y = this.m_iAxisPaddingTop;
						y1.Y = (int)this.TopSpacing;
						break;
					case QuadrantType.Quad23:
						//this.m_Origin.X = this.m_iAxisPaddingLeft + this.m_iXAxisSpan;
						//this.m_Origin.Y = this.m_iHeight - this.m_iAxisPaddingBottom - this.m_iYAxisSpan/2;
						this.m_Origin.X = (int)(this.LeftSpacing + this.m_iXAxisSpan);
						this.m_Origin.Y = (int)(this.m_iHeight - this.BottomSpacing - this.m_iYAxisSpan/2);
						//x1.X = this.m_iAxisPaddingLeft;
						x1.X = (int)this.LeftSpacing;
						x1.Y = x2.Y = this.m_Origin.Y;
						x2.X = y1.X = y2.X = this.m_Origin.X;
						//y1.Y = this.m_iAxisPaddingTop;
						y1.Y = (int)this.TopSpacing;
						y2.Y = y1.Y + this.m_iYAxisSpan;
						break;
					case QuadrantType.Quad34:
						//this.m_Origin.X = this.m_iAxisPaddingLeft + this.m_iXAxisSpan/2;
						//this.m_Origin.Y = this.m_iAxisPaddingTop;
						this.m_Origin.X = (int)(this.LeftSpacing + this.m_iXAxisSpan/2);
						this.m_Origin.Y = (int)this.TopSpacing;
						//x1.X = this.m_iAxisPaddingLeft;
						x1.X = (int)this.LeftSpacing;
						x1.Y = x2.Y = y1.Y = this.m_Origin.Y;
						y1.X = y2.X = this.m_Origin.X;
						x2.X = x1.X + this.m_iXAxisSpan;
						y2.Y = y1.Y + this.m_iYAxisSpan;
						break;
					case QuadrantType.Quad14:
						//this.m_Origin.X = this.m_iAxisPaddingLeft;
						//this.m_Origin.Y = this.m_iHeight - this.m_iAxisPaddingBottom - this.m_iYAxisSpan/2;
						this.m_Origin.X = (int)this.LeftSpacing;
						this.m_Origin.Y = (int)(this.m_iHeight - this.BottomSpacing + iOriginFromBottom);
						x1.X = y1.X = y2.X = this.m_Origin.X;
						x1.Y = x2.Y = this.m_Origin.Y;
						x2.X = x1.X + this.m_iXAxisSpan;
						//y1.Y = this.m_iAxisPaddingTop;
						y1.Y = (int)this.TopSpacing;
						y2.Y = y1.Y + this.m_iYAxisSpan;
						break;
					case QuadrantType.Quad1234:
						//this.m_Origin.X = this.m_iAxisPaddingLeft + this.m_iXAxisSpan/2;
						//this.m_Origin.Y = this.m_iHeight - this.m_iAxisPaddingBottom - this.m_iYAxisSpan/2;
						this.m_Origin.X = (int)(this.LeftSpacing + this.m_iXAxisSpan/2);
						this.m_Origin.Y = (int)(this.m_iHeight - this.BottomSpacing - this.m_iYAxisSpan/2);

						//x1.X = this.m_iAxisPaddingLeft;
						x1.X = (int)this.LeftSpacing;
						x2.X = x1.X + this.m_iXAxisSpan;
						x1.Y = x2.Y = this.m_Origin.Y;
						y1.X = y2.X = this.m_Origin.X;
						//y1.Y = this.m_iAxisPaddingTop;
						y1.Y = (int)this.TopSpacing;
						y2.Y = y1.Y + this.m_iYAxisSpan;
						break;
				}

				this.m_ChartAxis.Origin = this.m_Origin;
				this.m_ChartAxis.X1 = x1;
				this.m_ChartAxis.X2 = x2;
				this.m_ChartAxis.Y1 = y1;
				this.m_ChartAxis.Y2 = y2;
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.Message);
				return false;
			}
			*/
			return true;
		}

		/// <summary>
		/// Function is used to calculate the span of x and y axis.
		/// </summary>
		/// <returns></returns>
		virtual protected bool CalculateAxisSpan()
		{
			//this.m_iXAxisSpan = this.m_iWidth - this.m_iAxisPaddingLeft - this.m_iAxisPaddingRight;
			//this.m_iYAxisSpan = this.m_iHeight - this.m_iAxisPaddingBottom - this.m_iAxisPaddingTop;
			this.m_iXAxisSpan = (int)(this.m_iWidth - this.LeftSpacing - this.RightSpacing);
			this.m_iYAxisSpan = (int)(this.m_iHeight - this.BottomSpacing - this.TopSpacing);

			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		virtual protected bool CalculateAxisLabelLocations()
		{
			/*
			SizeF xLabelSize;
			SizeF yLabelSize; 
			try
			{
				xLabelSize = this.m_obGraphics.MeasureString(this.m_strXAxisLabel, this.XAxisLabelFont);
				yLabelSize = this.m_obGraphics.MeasureString(this.m_strYAxisLabel, this.YAxisLabelFont);

				// Calculate x-axis label location
				//int iX = (int)((this.m_iXAxisSpan/2) - xLabelSize.Width + this.m_iAxisPaddingLeft);
				int iX = (int)((this.m_iXAxisSpan/2) - xLabelSize.Width + this.LeftSpacing);

				// Calculate y-axis label location
				//int iY = (int)((this.m_iYAxisSpan/2) - yLabelSize.Width + this.m_iAxisPaddingTop);
				int iY = (int)((this.m_iYAxisSpan/2) - yLabelSize.Width + this.TopSpacing);

				this.m_XAxisLabelLocation = new PointF();
				this.m_YAxisLabelLocation = new PointF();

				this.m_XAxisLabelLocation.X = iX;
				//this.m_XAxisLabelLocation.Y = this.m_iHeight - (this.m_iAxisPaddingBottom - this.m_iXAxisLabelPadding) + xLabelSize.Height/2;
				this.m_XAxisLabelLocation.Y = this.m_iHeight - (this.BottomSpacing - this.m_iXAxisLabelPadding) + xLabelSize.Height/2;

				//this.m_YAxisLabelLocation.X = this.m_iAxisPaddingLeft - this.m_iYAxisLabelPadding - yLabelSize.Height;
				this.m_YAxisLabelLocation.X = this.LeftSpacing - this.m_iYAxisLabelPadding - yLabelSize.Height;
				this.m_YAxisLabelLocation.Y = iY;
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.Message);
				return false;
			}
			*/
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="ob"></param>
		/// <param name="type"></param>
		protected void SetSmoothingMode(Graphics ob, SmoothingType type)
		{
			try
			{
				switch (type)
				{
					case SmoothingType.Antialiasing:
						ob.SmoothingMode = SmoothingMode.AntiAlias;
						break;
					case SmoothingType.HighQuality:
						ob.SmoothingMode = SmoothingMode.HighQuality;
						break;
					case SmoothingType.HighSpeed:
						ob.SmoothingMode = SmoothingMode.HighSpeed;
						break;
				}
			}
			catch (Exception ex)
			{
				Trace.Write(ex.Message);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		protected LinearGradientMode GetLGradientMode()
		{
			switch (this.LinearGradientType)
			{
				case LGradientType.LeftToRight:
					return LinearGradientMode.Horizontal;
				case LGradientType.URightToLLeft:
					return LinearGradientMode.BackwardDiagonal;
				case LGradientType.ULeftToLRight:
					return LinearGradientMode.ForwardDiagonal;
				case LGradientType.TopToBottom:
					return LinearGradientMode.Vertical;
			}
			return LinearGradientMode.Horizontal;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="ob"></param>
		/// <param name="type"></param>
		protected bool SetBackground(Graphics ob, BackgroundType type)
		{
			bool bRet = false;
			Brush obBrush = null;
			try
			{
				Rectangle rect = new Rectangle(0, 0, this.m_iWidth, this.m_iHeight);
				switch (type)
				{
					case BackgroundType.SolidColor:
						bRet = this.SetSolidColorBackground(ob, this.BackgroundColor, rect);
						break;
					case BackgroundType.LinearGradient:

⌨️ 快捷键说明

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