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

📄 linegraph.cs

📁 C#自定义查询控件
💻 CS
字号:
// Disclaimer and Copyright Information
// LineGraph.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.Xml;
using System.Drawing;

namespace CommonComponent.CommonChart
{
	/// <summary>
	/// 
	/// </summary>
	public abstract class LineGraph : CommonChart.AxialGraph
	{
		protected float m_fXScale;
		protected float m_fYScale;
		protected float m_fThicknessRatio;
		protected ArrayList m_arrMarkerColors;
		public LineGraph()
		{
			m_fXScale = 1.0F;
			m_fYScale = 1.0F;
			m_fThicknessRatio = 0.02F;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		protected bool CalculateXScale()
		{
			m_fXScale = (this.m_iXAxisSpan)/(float)(this.MaxXValue - this.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 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;
			Pen obSubGridPen = null;
			try
			{
				// Draw the grid lines now.
				if (HasGridLines && this.YTickSpacing > 0)
				{
					SizeF rectText;
					string strVal = "";

					PointF pt1 = new PointF();
					PointF pt2 = new PointF();
					PointF pt3 = new PointF();
					PointF pt4 = new PointF();
					PointF stPt = new PointF();
					PointF endPt = new PointF();

					obGridPen = new Pen(GridColor);
					obGridPen.DashStyle = LineUtil.GetDashStyle(this.GridType);

					if (this.HasSubGridLines)
					{
						obSubGridPen = new Pen(SubGridColor);
						obSubGridPen.DashStyle = LineUtil.GetDashStyle(SubGridType);
					}
					
					int iYTickCount = (int)((this.MaxYValue - this.MinYValue)/this.YTickSpacing);
					int iXTickCount = (int)((this.MaxXValue - this.MinXValue)/this.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;

						if (HasGridLines && bDrawGrid)
						{
							this.m_obGraphics.DrawLine(obGridPen, pt1, pt2);
						}

						if (this.HasSubGridLines && bDrawGrid)
						{
							if (i > 0)
							{
								pt3.X = this.m_ChartAxis.X1.X;
								pt4.X = pt1.X + this.m_iXAxisSpan;
								pt3.Y = this.m_ChartAxis.Y2.Y - ((i - 1) * this.YTickSpacing) * this.m_fYScale;
								pt4.Y = pt3.Y;

								stPt.X = pt3.X;
								endPt.X = pt4.X;

								float diff = pt3.Y - pt2.Y;

								for (int k = 1; k <= this.SubGridLines; k++)
								{
									endPt.Y = stPt.Y = pt3.Y - diff*k/(SubGridLines+1);
									this.m_obGraphics.DrawLine(obSubGridPen, stPt, endPt);
								}
							}
						}

						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;

						if (this.HasGridLines)
						{
							this.m_obGraphics.DrawLine(obGridPen, pt1, pt2);
						}

						if (this.HasSubGridLines)
						{
							if (i > 0)
							{
								pt3.X = this.m_ChartAxis.Y2.X + ((i - 1) * this.XTickSpacing) * this.m_fXScale;
								pt4.X = pt3.X;
								pt3.Y = this.m_ChartAxis.Y2.Y;
								pt4.Y = this.m_ChartAxis.Y1.Y;

								stPt.Y = pt1.Y;
								endPt.Y = pt2.Y;

								float diff = pt1.X - pt3.X;

								for (int j = 1; j <= this.SubGridLines; j++)
								{
									endPt.X = stPt.X = pt3.X + diff*j/(SubGridLines+1);
									this.m_obGraphics.DrawLine(obSubGridPen, stPt, endPt);
								}
							}
						}

						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 CalculateAxisLocation()
		{
			// Calculate the origin of axis.
			CalculateAxis();
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="obGraphics"></param>
		/// <param name="textFont"></param>
		/// <param name="ptX"></param>
		/// <param name="ptY"></param>
		/// <param name="strVal"></param>
		/// <param name="color"></param>
		/// <returns></returns>
		protected bool DrawDataValue(Graphics obGraphics, Font textFont, float ptX, float ptY, string strVal, Color color)
		{
			SolidBrush obTextBrush = null;
			PointF pt = new PointF();
			SizeF textSize;
			try
			{
				obTextBrush = new SolidBrush(color);
				// Calculate the size of text string.
				textSize = obGraphics.MeasureString(strVal, textFont);
				pt.X = ptX - textSize.Width/2.0F;
				pt.Y = ptY - textSize.Height;
				obGraphics.DrawString(strVal, textFont, obTextBrush, pt);
			}
			finally
			{
				if (null != obTextBrush)
				{
					obTextBrush.Dispose();
				}
			}
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="nLegendCount"></param>
		override protected void CalculateLegendsRectangle_H(short nLegendCount)
		{
			float fHeight = 0.0F;
			// In this case, LEgends height will be the size of the marker that
			// will be displayed on the legend line. THis marker can be any one
			// that has been picked up by user of automatically selected by
			// engine.
			m_sLegendHeight = 3;	//TODO: Patameterize this value.
			m_sLegendWidth = 20;	//TODO: Patameterize this value.

			// Width = Buffer + LegendWidth + TextSpacing + MaxTextWidth + Buffer
			float fWidth = this.m_sLegendWidth;
			fWidth += this.m_sLegendTextSpacing;
			fWidth += this.m_fAvgIslandLegendTextWidth;
			// TODO: Add bufering attribute to class.
			fWidth += 2;
			fWidth += 2;

			fWidth *= nLegendCount;
			// If the total width is more than axis span, switch to
			// vertical legend mode.
			if (fWidth > this.m_iXAxisSpan)
			{
				this.LegendAlignment = LegendAlignmentType.Vertical;
				CalculateLegendsRectangle_V(nLegendCount);
				return;
			}

			fHeight += this.m_fMaxIslandLegendTextHeight;
			// TODO: Add attributes for buffers
			fHeight += 2;
			fHeight += 2;
			
			m_LegendRectangle.X = (int)(this.m_ChartAxis.X2.X - fWidth);
			m_LegendRectangle.Y = this.m_ChartAxis.Y1.Y;
			m_LegendRectangle.Width = (int)fWidth;
			m_LegendRectangle.Height = (int)fHeight;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="nLegendCount"></param>
		override protected void CalculateLegendsRectangle_V(short nLegendCount)
		{
			float fHeight = 0.0F;
			// In this case, LEgends height will be the size of the marker that
			// will be displayed on the legend line. THis marker can be any one
			// that has been picked up by user of automatically selected by
			// engine.
			m_sLegendHeight = 3;	//TODO: Patameterize this value.
			m_sLegendWidth = 20;		//TODO: Patameterize this value.

			fHeight += nLegendCount * m_sLegendHeight;
			fHeight += nLegendCount * this.m_sVertcalLegendSpacing;
			fHeight += 2;

			// Width = Buffer + LegendWidth + TextSpacing + MaxTextWidth + Buffer
			float fWidth = this.m_sLegendWidth;
			fWidth += this.m_sLegendTextSpacing;
			fWidth += this.m_fMaxIslandLegendTextWidth;
			// TODO: Add bufering attribute to class.
			fWidth += 2;
			fWidth += 2;

			
			m_LegendRectangle.X = (int)(this.m_ChartAxis.X2.X - fWidth);
			m_LegendRectangle.Y = this.m_ChartAxis.Y1.Y;
			m_LegendRectangle.Width = (int)fWidth;
			m_LegendRectangle.Height = (int)fHeight;
		}

		/// <summary>
		/// 
		/// </summary>
		override protected bool DrawVerticalLegends()
		{
			Pen obBorderPen = null;
			Pen obLegendPen = null;
			SolidBrush obBgBrush = null;
			SolidBrush obTextBrush = null;
			try
			{
				obBgBrush = new SolidBrush(this.BackgroundColor);
				obBorderPen = new Pen(new SolidBrush(Color.Black));
				obTextBrush = new SolidBrush(this.m_LegendColor);

				//this.m_obGraphics.DrawRectangle(obBorderPen, this.m_LegendRectangle);
				this.m_obGraphics.FillRectangle(obBgBrush, this.m_LegendRectangle);
				DrawingUtil.DrawRectangle(m_obGraphics, obBorderPen, m_LegendRectangle);

				// Now draw the legends.
				PointF textPt = new PointF();
				PointF linePt = new PointF();
				RectangleF markerRect = new RectangleF();
				markerRect.Width = markerRect.Height = 6.0F;
				int x = (int)m_LegendRectangle.X + m_sLegendsBuffer;
				linePt.X = x;
				textPt.X = x + this.m_sLegendWidth + this.m_sLegendTextSpacing;
				int yStart = (int)m_LegendRectangle.Y + m_sLegendsBuffer;
				int y = 0;
				markerRect.X = linePt.X + m_sLegendWidth/2.0F - 3.0F;

				for (short i = 0; i < this.m_sLegendCount; i++)
				{
					Color lineColor = (Color)this.m_arrColors[i];
					SolidBrush obMarkerBrush = new SolidBrush(lineColor);
					obLegendPen = new Pen(obMarkerBrush);

					y = yStart + i * (this.m_sLegendHeight + this.m_sVertcalLegendSpacing);

					linePt.Y = y + this.m_fMaxIslandLegendTextHeight/2.0F;
					// Draw legend line
					this.m_obGraphics.DrawLine(obLegendPen, linePt.X, linePt.Y, linePt.X + m_sLegendWidth, linePt.Y);
					// Draw legend marker.
					markerRect.Y = linePt.Y - 3.0F;
					this.m_obGraphics.FillEllipse(obMarkerBrush, markerRect);

					
					textPt.Y = yStart + i * (this.m_sLegendHeight + this.m_sVertcalLegendSpacing);

					this.m_obGraphics.DrawString(this.m_IslandLegendsList[i], this.m_LegendFont, obTextBrush, textPt);

					obLegendPen.Dispose();
					obMarkerBrush.Dispose();
				}
			}
			finally
			{
				if (null != obBorderPen)
				{
					obBorderPen.Dispose();
				}
				if (null != obBgBrush)
				{
					obBgBrush.Dispose();
				}
				if (null != obTextBrush)
				{
					obTextBrush.Dispose();
				}
			}
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		override protected bool DrawHorizontalLegends()
		{
			Pen obBorderPen = null;
			Pen obLegendPen = null;
			SolidBrush obBgBrush = null;
			SolidBrush obTextBrush = null;
			try
			{
				obBgBrush = new SolidBrush(this.BackgroundColor);
				obBorderPen = new Pen(new SolidBrush(Color.Black));
				obTextBrush = new SolidBrush(this.m_LegendColor);

				this.m_obGraphics.FillRectangle(obBgBrush, this.m_LegendRectangle);
				//this.m_obGraphics.DrawRectangle(obBorderPen, this.m_LegendRectangle);
				DrawingUtil.DrawRectangle(m_obGraphics, obBorderPen, m_LegendRectangle);

				// Now draw the legends.
				
				PointF textPt = new PointF();
				PointF linePt = new PointF();
				RectangleF markerRect = new RectangleF();
				int xStart = (int)this.m_LegendRectangle.X;
				textPt.X = linePt.X = this.m_LegendRectangle.X;
				linePt.Y = (this.m_LegendRectangle.Y + this.m_LegendRectangle.Height/2.0F);
				textPt.Y = this.m_LegendRectangle.Y + (this.m_LegendRectangle.Height/2.0F - m_fMaxIslandLegendTextHeight/2.0F);
				markerRect.Y = this.m_LegendRectangle.Y + (this.m_LegendRectangle.Height/2.0F - 3);
				markerRect.Width = markerRect.Height = 6.0F;
				for (short i = 0; i < this.m_sLegendCount; i++)
				{
					Color lineColor = (Color)this.m_arrColors[i];
					SolidBrush obMarkerBrush = new SolidBrush(lineColor);
					obLegendPen = new Pen(obMarkerBrush);
					xStart += m_sLegendTextSpacing;
					if (i > 0)
					{
						xStart += (int)(float)this.m_arrIslandLegendTextWidth[i-1];
					}
					linePt.X = xStart;
					// First draw legend line.
					this.m_obGraphics.DrawLine(obLegendPen, linePt.X, linePt.Y, linePt.X + m_sLegendWidth, linePt.Y);
					// And now we will draw the legend marker.
					markerRect.X = linePt.X + m_sLegendWidth/2.0F - 3.0F;
					this.m_obGraphics.FillEllipse(obMarkerBrush, markerRect);

					xStart += (m_sLegendTextSpacing + m_sLegendWidth);
					textPt.X = xStart;

					this.m_obGraphics.DrawString(this.m_IslandLegendsList[i], this.m_LegendFont, obTextBrush, textPt);

					obLegendPen.Dispose();
					obMarkerBrush.Dispose();
				}
			}
			finally
			{
				if (null != obBorderPen)
				{
					obBorderPen.Dispose();
				}
				if (null != obBgBrush)
				{
					obBgBrush.Dispose();
				}
				if (null != obTextBrush)
				{
					obTextBrush.Dispose();
				}
			}
			return true;
		}
	}
}

⌨️ 快捷键说明

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