📄 graph.cs
字号:
// Check if the Color array has been set for gradient colors or not.
if (this.m_GradientColors != null && this.m_GradientColors.Length > 0)
{
if (this.m_GradientColors.Length == 1)
{
obBrush = DrawingUtil.GetLinearGradientBrush(rect,
this.m_GradientColors[0],
this.m_sBgGradientFactor,
this.GetLGradientMode());
}
else
{
obBrush = DrawingUtil.GetLinearGradientBrush(rect,
this.m_GradientColors[0],
this.m_GradientColors[1],
this.GetLGradientMode());
}
}
else
{
obBrush = DrawingUtil.GetLinearGradientBrush(rect,
this.BackgroundColor,
this.m_sBgGradientFactor,
this.GetLGradientMode());
}
bRet = this.SetGradientBackground(ob, rect, obBrush);
break;
case BackgroundType.CircularGradient:
if (this.m_GradientColors != null && this.m_GradientColors.Length > 0)
{
if (this.m_GradientColors.Length == 1)
{
obBrush = DrawingUtil.GetCircularGradientBrush(rect,
this.m_GradientColors[0],
this.m_sBgGradientFactor,
this.CircularGradientType);
}
else
{
obBrush = DrawingUtil.GetCircularGradientBrush(rect,
this.m_GradientColors[0],
this.m_GradientColors[1],
this.CircularGradientType);
}
}
else
{
obBrush = DrawingUtil.GetCircularGradientBrush(rect,
this.BackgroundColor,
this.m_sBgGradientFactor,
this.CircularGradientType);
}
if (this.SetSolidColorBackground(ob, this.BackgroundColor, rect))
{
bRet = this.SetGradientBackground(ob, rect, obBrush);
}
break;
case BackgroundType.HatchedLines:
obBrush = DrawingUtil.CreateHatchBrush(BackgroundHatchType, HatchColor, BackgroundColor);
bRet = this.SetHatchedBackground(ob, rect, (HatchBrush)obBrush);
break;
case BackgroundType.Image:
bRet = this.SetImageBackground(ob, rect, this.BackgroundImage);
break;
}
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
throw ex;
}
finally
{
if (null != obBrush)
{
obBrush.Dispose();
}
}
return bRet;
}
/// <summary>
///
/// </summary>
/// <param name="ob"></param>
/// <param name="color"></param>
/// <param name="rect"></param>
/// <returns></returns>
private bool SetSolidColorBackground(Graphics ob, Color color, Rectangle rect)
{
bool bRet = true;
SolidBrush obBrush = null;
try
{
obBrush = new SolidBrush(color);
ob.FillRectangle(obBrush, rect);
}
catch (OutOfMemoryException ex)
{
Trace.WriteLine(ex.Message);
throw ex;
}
finally
{
if (null != obBrush)
{
obBrush.Dispose();
}
obBrush = null;
}
return bRet;
}
/// <summary>
///
/// </summary>
/// <param name="ob"></param>
/// <param name="obBrush"></param>
/// <returns></returns>
private bool SetGradientBackground(Graphics ob, Rectangle rect, Brush obBrush)
{
ob.FillRectangle(obBrush, rect);
return true;
}
/// <summary>
///
/// </summary>
/// <param name="ob"></param>
/// <param name="rect"></param>
/// <param name="obBrush"></param>
/// <returns></returns>
private bool SetHatchedBackground(Graphics ob, Rectangle rect, HatchBrush obBrush)
{
ob.FillRectangle(obBrush, rect);
return true;
}
private bool SetImageBackground(Graphics ob, Rectangle rect, string strImagePath)
{
bool bRet = true;
// Some sanity check.
if (strImagePath.Length == 0)
{
Trace.WriteLine("No image path supplied");
throw new ArgumentException("No file path specified for background image", "ImagePath");
}
Bitmap bgBmp = null;
try
{
bgBmp = new Bitmap(strImagePath);
int nHeight = bgBmp.Height;
int nWidth = bgBmp.Width;
Rectangle destRect = new Rectangle();
// Center the image within the bounding rectangle of chart.
destRect.X = rect.X + (rect.Width/2 - nWidth/2);
destRect.Y = rect.Y + (rect.Height/2 - nHeight/2);
if (destRect.X < 0)
{
destRect.X = rect.X;
}
if (destRect.Y < 0)
{
destRect.Y = rect.Y;
}
destRect.Width = (nWidth <= rect.Width) ? nWidth : rect.Width;
destRect.Height = (nHeight <= rect.Height) ? nHeight : rect.Height;
SetSolidColorBackground(ob, this.BackgroundColor, rect);
ob.DrawImage(bgBmp, destRect);
}
finally
{
if (null != bgBmp)
{
bgBmp.Dispose();
}
}
return bRet;
}
/// <summary>
/// Method can be used to get the minimum and maximum value stored in the
/// array.
/// </summary>
/// <param name="list"></param>
/// <param name="?"></param>
/// <param name="?"></param>
protected void GetMinMax(ArrayList list, ref float obMin, ref float obMax)
{
ArrayList newList = (ArrayList)list.Clone();
newList.Sort();
obMin = (float)newList[0];
obMax = (float)newList[newList.Count - 1];
}
/// <summary>
/// This method adds the legend text to StringCollection storing all
/// legend text values. This method also calculates the width and
/// height of the string and calculates the max width value.
/// </summary>
/// <param name="strLegend"></param>
protected void AddLegendTextToCollection(string strLegend)
{
// Only process if the user wants to set legends.
SizeF textSize;
this.m_LegendsList.Add(strLegend);
textSize = this.m_obGraphics.MeasureString(strLegend, this.LegendFont);
m_arrLegendTextWidth.Add(textSize.Width);
if (textSize.Width > this.m_fMaxLegendTextWidth)
{
this.m_fMaxLegendTextWidth = textSize.Width;
}
if (textSize.Height > this.m_fMaxLegendTextHeight)
{
this.m_fMaxLegendTextHeight = textSize.Height;
}
this.m_fTotalLegendTextWidth += textSize.Width;
// Calculate the average text width.
this.m_fAvgLegendTextWidth = this.m_fTotalLegendTextWidth/this.m_LegendsList.Count;
}
/// <summary>
/// This method adds the legend text to StringCollection storing all
/// legend text values. This method also calculates the width and
/// height of the string and calculates the max width value.
/// </summary>
/// <param name="strLegend"></param>
protected void AddIslandLegendTextToCollection(string strIslandLegend)
{
// Only process if the user wants to set legends.
SizeF textSize;
this.m_IslandLegendsList.Add(strIslandLegend);
textSize = this.m_obGraphics.MeasureString(strIslandLegend, this.LegendFont);
this.m_arrIslandLegendTextWidth.Add(textSize.Width);
if (textSize.Width > this.m_fMaxIslandLegendTextWidth)
{
this.m_fMaxIslandLegendTextWidth = textSize.Width;
}
if (textSize.Height > this.m_fMaxIslandLegendTextHeight)
{
this.m_fMaxIslandLegendTextHeight = textSize.Height;
}
this.m_fTotalIslandLegendTextWidth += textSize.Width;
// Calculate the average text width.
this.m_fAvgIslandLegendTextWidth = this.m_fTotalIslandLegendTextWidth/this.m_IslandLegendsList.Count;
}
/// <summary>
///
/// </summary>
protected void InitializeLegendsCollection()
{
if (this.m_LegendsList == null)
{
this.m_LegendsList = new StringCollection();
m_arrLegendTextWidth = new ArrayList();
}
if (this.m_IslandLegendsList == null)
{
this.m_IslandLegendsList = new StringCollection();
m_arrIslandLegendTextWidth = new ArrayList();
}
this.m_LegendsList.Clear();
this.m_IslandLegendsList.Clear();
}
/// <summary>
///
/// </summary>
protected void CalculateLegendsRectangle(short nLegendCount)
{
this.m_LegendRectangle = new RectangleF();
switch (this.m_LegendAlignment)
{
case LegendAlignmentType.Horizontal:
this.CalculateLegendsRectangle_H(nLegendCount);
break;
case LegendAlignmentType.Vertical:
this.CalculateLegendsRectangle_V(nLegendCount);
break;
}
}
/// <summary>
///
/// </summary>
virtual protected void CalculateLegendsRectangle_H(short nLegendCount)
{
float fHeight = 0.0F;
// If marker height is greater than the max text string height then
// use that to caluclate the rectangle height otherwise use
// maximum text string height.
// Also adjust the legend widths and heights accrodingly.
if (this.m_fMaxLegendTextHeight > m_sLegendHeight)
{
m_sLegendHeight = (short)m_fMaxLegendTextHeight;
m_sLegendWidth = m_sLegendHeight;
}
// Width = Buffer + LegendWidth + TextSpacing + MaxTextWidth + Buffer
float fWidth = this.m_sLegendWidth;
fWidth += this.m_sLegendTextSpacing;
fWidth += this.m_fAvgLegendTextWidth;
// 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 += m_sLegendHeight;
// 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>
/// The method calculates
/// </summary>
virtual protected void CalculateLegendsRectangle_V(short nLegendCount)
{
float fHeight = 0.0F;
// If marker height is greater than the max text string height then
// use that to caluclate the rectangle height otherwise use
// maximum text string height.
// Also adjust the legend widths and heights accrodingly.
if (this.m_fMaxLegendTextHeight > m_sLegendHeight)
{
m_sLegendHeight = (short)m_fMaxLegendTextHeight;
m_sLegendWidth = m_sLegendHeight;
}
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_fMaxLegendTextWidth;
// 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>
virtual protected void CalculateTickerTextDirection()
{
}
/// <summary>
///
/// </summary>
virtual protected void CalculateIslandLegendTextDirection()
{
}
/// <summary>
///
/// </summary>
virtual protected bool DrawVerticalLegends()
{
Pen obBorderPen = 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();
int x = (int)m_LegendRectangle.X + 2;
textPt.X = x + this.m_sLegendWidth + this.m_sLegendTextSpacing;
int yStart = (int)m_LegendRectangle.Y + 2;
int y = 0;
for (short i = 0; i < this.m_sLegendCount; i++)
{
y = yStart + i * (this.m_sLegendHeight + this.m_sVertcalLegendSpacing);
textPt.Y = yStart + i * (this.m_sLegendHeight + this.m_sVertcalLegendSpacing);
this.DrawBar(this.m_obGraphics, x, y, this.m_sLegendWidth, this.m_sLegendHeight, 0.0, (Color)this.m_arrColors[i]);
this.m_obGraphics.DrawString(this.m_LegendsList[i], this.m_LegendFont, obTextBrush, textPt);
}
}
finally
{
if (null != obBorderPen)
{
obBorderPen.Dispose();
}
if (null != obBgBrush)
{
obBgBrush.Dispose();
}
if (null != obTextBrush)
{
obTextBrush.Dispose();
}
}
return true;
}
/// <summary>
///
/// </summary>
virtual protected bool DrawHorizontalLegends()
{
Pen obBorderPen = 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);
DrawingUtil.DrawRectangle(m_obGraphics, obBorderPen, m_LegendRectangle);
// Now draw the legends.
PointF textPt = new PointF();
int xStart = (int)m_LegendRectangle.X;
textPt.X = this.m_LegendRectangle.X;
int yStart = (int)m_LegendRectangle.Y + m_sLegendsBuffer;
int iSum = 0;
/*
for (short i = 0; i < this.m_sLegendCount; i++)
{
xStart = (int)m_LegendRectangle.X;
if (i > 0)
{
iSum += ((int)(float)this.m_arrLegendTextWidth[i-1] + m_sLegendTextSpacing);
}
xStart += iSum;
xStart += i * (this.m_sLegendWidth + m_sLegendTextSpacing);
textPt.Y = yStart;
textPt.X = xStart;
this.DrawBar(this.m_obGraphics, xStart, yStart, this.m_sLegendWidth, this.m_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -