📄 bargraph.cs
字号:
Pen obPen = null;
SolidBrush obTextBrush = null;
PointF ptTicker1 = new PointF();
PointF ptTicker2 = new PointF();
try
{
obTextBrush = new SolidBrush(color);
obPen = new Pen(obTextBrush);
switch (align)
{
case Alignment.Horizontal:
ptTicker1.X = (float)(ptX + dWidth/2.0);
ptTicker2.X = ptTicker1.X;
ptTicker1.Y = (float)ptY;
ptTicker2.Y = ptTicker1.Y + sLength;
obGraphics.DrawLine(obPen, ptTicker1, ptTicker2);
break;
case Alignment.Vertical:
ptTicker1.X = (float)ptX;
ptTicker1.Y = (float)(ptY + dHeight/2.0);
ptTicker2.X = ptTicker1.X - sLength;
ptTicker2.Y = ptTicker1.Y;
obGraphics.DrawLine(obPen, ptTicker1, ptTicker2);
break;
}
// Time to draw ticker text.
if (this.DrawXTickerText)
{
switch (direction)
{
case TextDirectionType.Horizontal:
this.DrawHorizontalTickerText(obGraphics, strLegend, ptTicker2, this.m_XTickFont, this.m_TickColor, align);
break;
case TextDirectionType.Diagonal:
this.DrawDiagonalTickerText(obGraphics, strLegend, ptTicker2, this.m_XTickFont, this.m_TickColor, align);
break;
}
}
}
finally
{
if (null != obPen)
{
obPen.Dispose();
}
if (null != obTextBrush)
{
obTextBrush.Dispose();
}
}
return true;
}
protected bool DrawIslandLegendText(Graphics obGraphics, Font textFont, double ptX, double ptY, double dWidth,
string strLegend, Alignment align, Color color)
{
Pen obPen = null;
SolidBrush obTextBrush = null;
PointF pt = new PointF();
SizeF textSize;
try
{
// Calculate size of text string.
textSize = obGraphics.MeasureString(strLegend, textFont);
obTextBrush = new SolidBrush(color);
obPen = new Pen(obTextBrush);
switch (align)
{
case Alignment.Horizontal:
pt.X = (float)(ptX + dWidth/2.0);
pt.Y = (float)(ptY + this.m_sTickerSize);
// Check if the user is drawing data legends on axis or not.
if (this.DrawXTickerText)
{
pt.Y = (float)(ptY + this.m_sTickerSize);
if (this.m_TickerTextDirection == TextDirectionType.Diagonal ||
this.m_TickerTextDirection == TextDirectionType.Vertical)
{
pt.Y += (this.m_fMaxLegendTextWidth);
}
else
{
pt.Y += (this.m_fMaxLegendTextHeight);
}
}
break;
case Alignment.Vertical:
pt.X = (float)(ptX - this.m_sTickerSize);
pt.Y = (float)(ptY - textSize.Height/2.0F);
// Check if the user is drawing data legends on axis or not.
if (this.DrawXTickerText)
{
pt.X = (float)(ptX - this.m_sTickerSize);
if (this.m_TickerTextDirection == TextDirectionType.Diagonal ||
this.m_TickerTextDirection == TextDirectionType.Horizontal)
{
pt.X -= (this.m_fMaxLegendTextWidth);
}
else
{
pt.X -= (this.m_fMaxLegendTextHeight);
}
}
break;
}
// Time to draw ticker text.
if (this.DrawXAxisLegend)
{
switch (m_IslandLegendTextDirection)
{
case TextDirectionType.Horizontal:
this.DrawHorizontalTickerText(obGraphics, strLegend, pt, this.m_XTickFont, this.m_TickColor, align);
break;
case TextDirectionType.Diagonal:
if (align == Alignment.Vertical)
{
pt.Y -= (float)dWidth/2.0F;
}
this.DrawDiagonalTickerText(obGraphics, strLegend, pt, this.m_XTickFont, this.m_TickColor, align);
break;
case TextDirectionType.Vertical:
this.DrawVerticalTickerText(obGraphics, strLegend, pt, this.m_fIslandDrawWidth, this.m_XTickFont, this.m_TickColor, align);
break;
}
}
}
finally
{
if (null != obPen)
{
obPen.Dispose();
}
if (null != obTextBrush)
{
obTextBrush.Dispose();
}
}
return true;
}
/// <summary>
///
/// </summary>
/// <param name="obGraphics"></param>
/// <param name="strText"></param>
/// <param name="pt"></param>
/// <param name="font"></param>
protected void DrawHorizontalTickerText(Graphics obGraphics, string strText, PointF pt, Font font, Color color, Alignment align)
{
StringFormat drawFormat = null;
float fWidth = 0.0F;
SizeF textSize = obGraphics.MeasureString(strText, font);
// Calculate width of string.
fWidth = textSize.Width;
if (align == Alignment.Horizontal)
{
pt.X -= (float)(fWidth/2.0F);
if (pt.X < 0)
{
pt.X = 0.0F;
}
}
else
{
drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.DirectionRightToLeft;
pt.Y -= textSize.Height/2.0F;
}
//obGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
if (drawFormat == null)
{
obGraphics.DrawString(strText, font, new SolidBrush(color), pt);
}
else
{
obGraphics.DrawString(strText, font, new SolidBrush(color), pt, drawFormat);
}
}
/// <summary>
///
/// </summary>
/// <param name="obGraphics"></param>
/// <param name="strText"></param>
/// <param name="pt"></param>
protected void DrawDiagonalTickerText(Graphics obGraphics, string strText, PointF pt, Font font, Color color, Alignment align)
{
GraphicsContainer container = null;
StringFormat drawFormat = null;
try
{
SizeF textSize = obGraphics.MeasureString(strText, font);
container = obGraphics.BeginContainer();
if (align == Alignment.Horizontal)
{
obGraphics.TranslateTransform(pt.X, pt.Y);
obGraphics.RotateTransform(45F);
}
else
{
drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.DirectionRightToLeft;
obGraphics.TranslateTransform(pt.X-6, pt.Y - textSize.Height/2.0F);
obGraphics.RotateTransform(-45F);
}
obGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
if (null == drawFormat)
{
obGraphics.DrawString(strText, font, new SolidBrush(color), new Point(-3, 0));
}
else
{
obGraphics.DrawString(strText, font, new SolidBrush(color), new Point(-3, 0), drawFormat);
}
}
finally
{
if (null != container)
{
obGraphics.EndContainer(container);
}
}
}
/// <summary>
///
/// </summary>
/// <param name="obGraphics"></param>
/// <param name="strText"></param>
/// <param name="pt"></param>
protected void DrawVerticalTickerText(Graphics obGraphics, string strText, PointF pt, float fHeight, Font font, Color color, Alignment align)
{
GraphicsContainer container = null;
StringFormat drawFormat = null;
float fY = 0.0F;
try
{
SizeF textSize = obGraphics.MeasureString(strText, font);
container = obGraphics.BeginContainer();
if (align == Alignment.Horizontal)
{
/*
obGraphics.TranslateTransform(pt.X, pt.Y);
obGraphics.RotateTransform(45F);
*/
}
else
{
//fY = pt.Y - (fHeight/2.0F - textSize.Height/2.0F);
fY = pt.Y - (fHeight);
drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.DirectionRightToLeft;
obGraphics.TranslateTransform(pt.X-textSize.Height, fY);
obGraphics.RotateTransform(-90F);
}
obGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
if (null == drawFormat)
{
obGraphics.DrawString(strText, font, new SolidBrush(color), new Point(0, 0));
}
else
{
obGraphics.DrawString(strText, font, new SolidBrush(color), new Point(0, 0), drawFormat);
}
}
finally
{
if (null != container)
{
obGraphics.EndContainer(container);
}
}
}
/// <summary>
///
/// </summary>
/// <param name="diagonal"></param>
/// <returns></returns>
protected float GetTextRectangleWidth(float diagonal)
{
return (float)(diagonal * Math.Cos(45.0*(Math.PI/180.0)));
}
/// <summary>
///
/// </summary>
/// <param name="diagonal"></param>
/// <returns></returns>
protected float GetTextRectangleHeight(float diagonal)
{
return (float)(diagonal * Math.Sin(45.0*(Math.PI/180.0)));
}
/// <summary>
///
/// </summary>
override 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>
override 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_sLegendHeight, 0.0, (Color)this.m_arrColors[i]);
textPt.X += (m_sLegendTextSpacing + this.m_sLegendWidth);
this.m_obGraphics.DrawString(this.m_LegendsList[i], this.m_LegendFont, obTextBrush, textPt);
}
*/
int idx = 0;
int iFactor = DataPointCount/m_sLegendRowCount;
for (int j = 0; j < this.m_sLegendRowCount; j++)
{
iSum = 0;
if (j > 0)
{
yStart += (this.m_sLegendHeight + m_sLegendsBuffer);
}
for (int k = 0; k < this.m_iMaxLegendsInRow; k++)
{
xStart = (int)m_LegendRectangle.X + this.m_sLegendsBuffer;
if (idx < DataPointCount)
{
if (idx > 0 && k > 0)
{
iSum += ((int)(float)this.m_fMaxLegendTextWidth + m_sLegendTextSpacing);
}
xStart += iSum;
xStart += k * (this.m_sLegendWidth + m_sLegendTextSpacing);
textPt.Y = yStart;
textPt.X = xStart;
this.DrawBar(this.m_obGraphics, xStart, yStart, this.m_sLegendWidth, this.m_sLegendHeight, 0.0, (Color)this.m_arrColors[idx]);
textPt.X += (m_sLegendTextSpacing + this.m_sLegendWidth);
this.m_obGraphics.DrawString(this.m_LegendsList[idx], this.m_LegendFont, obTextBrush, textPt);
idx++;
}
Trace.WriteLine(idx.ToString());
}
}
}
finally
{
if (null != obBorderPen)
{
obBorderPen.Dispose();
}
if (null != obBgBrush)
{
obBgBrush.Dispose();
}
if (null != obTextBrush)
{
obTextBrush.Dispose();
}
}
return true;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
override protected bool DrawAxisLabels()
{
this.m_obGraphics.DrawString(this.XAxisLabel, this.XAxisLabelFont, new SolidBrush(XAxisLabelColor), this.m_XAxisLabelLocation);
// YAxis will be drawn vertical.
StringFormat drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.DirectionVertical | StringFormatFlags.DirectionRightToLeft;
this.m_obGraphics.DrawString(this.YAxisLabel, this.YAxisLabelFont, new SolidBrush(YAxisLabelColor), this.m_YAxisLabelLocation, drawFormat);
return true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -