📄 blockschart2d.cs
字号:
// Disclaimer and Copyright Information
// BarGraph2D.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.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
namespace CommonComponent.CommonChart
{
/// <summary>
///
/// </summary>
public class BlocksChart2D : CommonChart.BarGraph2D
{
protected bool m_bDrawBlockOutline;
protected float m_fBlockUnit;
protected short m_sBlocksBuffer;
protected int m_iMaxBlocks;
public BlocksChart2D()
{
this.m_GraphType = Type.BlocksGraph2D;
m_bDrawBlockOutline = true;
m_fBlockUnit = 100.0F;
m_sBlocksBuffer = 2;
m_iMaxBlocks = 0;
}
/// <summary>
///
/// </summary>
public float BlockUnit
{
get
{
return this.m_fBlockUnit;
}
set
{
this.m_fBlockUnit = value;
}
}
/// <summary>
///
/// </summary>
public short BlocksBuffer
{
get
{
return this.m_sBlocksBuffer;
}
set
{
this.m_sBlocksBuffer = value;
}
}
/// <summary>
///
/// </summary>
public bool DrawBlockOutline
{
get
{
return this.m_bDrawBlockOutline;
}
set
{
this.m_bDrawBlockOutline = value;
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
override protected bool CalculateBarHeightScale()
{
float fMaxBlocks = 0.0F;
if (Alignment.Vertical == this.GraphAlignment)
{
m_dBarHeightScale = this.m_iXAxisSpan/((double)(this.MaxYValue - this.MinYValue));
}
else
{
fMaxBlocks = this.MaxYValue/this.BlockUnit;
m_iMaxBlocks = (int)fMaxBlocks;
if (m_iMaxBlocks < fMaxBlocks)
{
m_iMaxBlocks++;
}
m_dBarHeightScale = (this.m_iYAxisSpan - (fMaxBlocks - 1)*this.BlocksBuffer)/((double)(this.MaxYValue - this.MinYValue));
}
return true;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
override protected bool DrawDataForVerticalAlignment()
{
// *********** NOTE **************************
// For now redirect the implementation to
// Horizontal alignment
// ********************************************
return this.DrawDataForHorizontalAlignment();
/*
try
{
ArrayList dataList = (ArrayList)this.m_obIslandList[0];
if (null == dataList || dataList.Count == 0)
{
Trace.WriteLine("There is no data list present in the array.");
return false;
}
int nCount = dataList.Count;
if (false == this.CalculateBarWidth(nCount))
{
Trace.WriteLine("Failed to calculate bar width");
return false;
}
if (false == this.CalculateBarHeightScale())
{
Trace.WriteLine("Failed to calculate the scale factor");
return false;
}
if (this.UseRandomColors)
{
if (this.UseRandomColors)
{
if (false == ColorUtil.GenerateRandomColors(nCount, out m_arrColors))
{
Trace.WriteLine("Failed to geenrate random color");
return false;
}
}
}
else
{
ColorUtil.GenerateColorValues(this.Colors, out m_arrColors);
}
PointF pt = new PointF();
pt.X = (float)(this.m_ChartAxis.Origin.X);
pt.Y = this.m_ChartAxis.Origin.Y;
double nHeight = 0;
double nWidth = 0;
float fBlockCount = 0.0F;
float fPartialBlock = 0.0F;
int nCompleteBlocks = 0;
double diff = m_dEqualBarWidth - m_dDrawBarWidth;
for (int i = 0; i < nCount; i++)
{
Color fillColor = (Color)this.m_arrColors[i];
SolidBrush obBrush = new SolidBrush(fillColor);
GraphDataObject obData = (GraphDataObject)dataList[i];
if (null != obData)
{
// Calculate number of blocks that will be needed to display the data.
fBlockCount = (float)(obData.Y/this.BlockUnit);
nCompleteBlocks = (int)Math.Round((double)fBlockCount);
fPartialBlock = fBlockCount - nCompleteBlocks;
// First draw complete blocks
for (int nCmp = 0; nCmp < nCompleteBlocks; nCmp++)
{
}
// Draw partial block.
pt.Y = (float)(this.m_ChartAxis.Origin.Y - ((i+1) * this.m_dEqualBarWidth) + diff);
// First draw the front rectangle.
nHeight = this.m_dDrawBarWidth;
nWidth = (obData.Y * this.m_dBarHeightScale);
this.DrawBar(this.m_obGraphics, (int)pt.X, (int)pt.Y, (float)nWidth, (float)nHeight, 0.0, fillColor);
}
obBrush.Dispose();
}
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
return false;
}
return true;
*/
}
/// <summary>
///
/// </summary>
/// <returns></returns>
override protected bool DrawDataForHorizontalAlignment()
{
Pen obOutlinePen = null;
try
{
if (false == this.CalculateBarHeightScale())
{
Trace.WriteLine("Failed to calculate the scale factor");
return false;
}
if (DrawBlockOutline)
{
obOutlinePen = new Pen(new SolidBrush(Color.Black));
}
PointF [] rect = new PointF[4];
double ptX = 0.0;
double ptY = 0.0;
Rectangle rectFill = new Rectangle();
float fBlockCount = 0.0F;
float fPartialBlock = 0.0F;
int nCompleteBlocks = 0;
int nHeight = 0;
int nCompleteBlockHeight = 0;
int nWidth = 0;
int nCmp = 0;
//int nStartX = (int)(this.m_ChartAxis.Origin.X);
int nStartX = (int)(this.m_ChartAxis.Origin.X + + m_fIslandSpacing/2.0F);
for (int nIdx = 0; nIdx < this.m_obDataIslandNodes.Count; nIdx++)
{
ArrayList obIslandDataList = (ArrayList)this.m_obIslandList[nIdx];
int nCount = obIslandDataList.Count;
for (int i = 0; i < nCount; i++)
{
Color fillColor = (Color)this.m_arrColors[i];
Brush obBrush = null;
GraphDataObject obData = (GraphDataObject)obIslandDataList[i];
if (null != obData)
{
// Calculate number of blocks that will be needed to display the data.
fBlockCount = (float)(obData.Y/this.BlockUnit);
nCompleteBlocks = (int)fBlockCount;
fPartialBlock = fBlockCount - nCompleteBlocks;
nCompleteBlockHeight = (int)(this.BlockUnit * this.m_dBarHeightScale);
nWidth = (int)this.m_dDrawBarWidth;
//rect[0].X = (int)(this.m_ChartAxis.Origin.X + (i * this.m_dEqualBarWidth));
rect[0].X = (int)(nStartX + nIdx*(this.m_fIslandWidth + this.m_fIslandSpacing) + i * this.m_dEqualBarWidth);
// First draw complete blocks
for (nCmp = 0; nCmp < nCompleteBlocks; nCmp++)
{
nHeight = nCompleteBlockHeight;
rect[0].Y = this.m_ChartAxis.Origin.Y - nCmp*(nHeight+this.BlocksBuffer);
rect[1].X = rect[0].X + nWidth;
rect[1].Y = rect[0].Y;
rect[2].X = rect[1].X;
rect[2].Y = rect[1].Y - nHeight;
rect[3].X = rect[0].X;
rect[3].Y = rect[2].Y;
rectFill.X = (int)rect[0].X;
rectFill.Y = (int)rect[0].Y;
rectFill.Width = (int)m_dEqualBarWidth;
rectFill.Height = nHeight;
if (this.UseGradientColors == true)
{
obBrush = DrawingUtil.GetLinearGradientBrush(rectFill, fillColor, GraphGradientFactor, LinearGradientMode.Horizontal);
}
else
{
obBrush = new SolidBrush(fillColor);
}
this.m_obGraphics.FillPolygon(obBrush, rect);
if (this.DrawBlockOutline)
{
this.m_obGraphics.DrawPolygon(obOutlinePen, rect);
}
obBrush.Dispose();
}
// Draw partial block.
nHeight = (int)(this.BlockUnit * fPartialBlock * this.m_dBarHeightScale);
rect[0].Y = this.m_ChartAxis.Origin.Y;
if (nCmp > 0)
{
rect[0].Y = this.m_ChartAxis.Origin.Y - (nCmp*(nCompleteBlockHeight + this.BlocksBuffer));
}
rect[1].X = rect[0].X + nWidth;
rect[1].Y = rect[0].Y;
rect[2].X = rect[1].X;
rect[2].Y = rect[1].Y - nHeight;
rect[3].X = rect[0].X;
rect[3].Y = rect[2].Y;
rectFill.X = (int)rect[0].X;
rectFill.Y = (int)rect[0].Y;
rectFill.Width = (int)m_dEqualBarWidth;
rectFill.Height = nHeight;
// ------- NOTE ------------
/*
* It is very important that you do not try to create a gradient brush with a
* rectangle that has ZERO width or height. Otherwise creation of brush
* will throw an eception at you.
*/
if (nHeight > 0)
{
if (this.UseGradientColors == true)
{
obBrush = DrawingUtil.GetLinearGradientBrush(rectFill, fillColor, GraphGradientFactor, LinearGradientMode.Horizontal);
}
else
{
obBrush = new SolidBrush(fillColor);
}
this.m_obGraphics.FillPolygon(obBrush, rect);
if (this.DrawBlockOutline)
{
this.m_obGraphics.DrawPolygon(obOutlinePen, rect);
}
}
if (this.HasTickers)
{
// Draw the ticker.
try
{
this.DrawChartTicker(this.m_obGraphics, rect[0].X, this.m_ChartAxis.Origin.Y, nWidth, nHeight, 3,
this.m_LegendsList[i], this.m_TickerTextDirection, this.GraphAlignment, this.m_TickColor);
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
}
obBrush.Dispose();
}
}
// Draw Island legend.
if (this.DrawXAxisLegend)
{
ptX = (nStartX + nIdx*(this.m_fIslandWidth + this.m_fIslandSpacing));
ptY = this.m_ChartAxis.Origin.Y;
this.DrawIslandLegendText(m_obGraphics, this.m_XTickFont, ptX, ptY, this.m_fIslandDrawWidth,
m_IslandLegendsList[nIdx], this.GraphAlignment, this.m_TickColor);
}
}
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
return false;
}
finally
{
if (obOutlinePen != null)
{
obOutlinePen.Dispose();
}
}
return true;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
override protected bool DrawAxisLabels()
{
return true;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
override protected bool DrawAxisForHorizontalAlignment(bool bDrawGrid)
{
Pen obPen = null;
Pen obGridPen = null;
try
{
CalculateBarHeightScale();
// Draw the grid lines now.
if (this.BlockUnit > 0)
{
SizeF rectText;
string strVal = "";
PointF pt1 = new PointF();
PointF pt2 = new PointF();
obGridPen = new Pen(GridColor);
int nCompleteBlockHeight = (int)(this.BlockUnit * this.m_dBarHeightScale);
for (int i = 0; i <= this.m_iMaxBlocks; i++)
{
pt1.X = (float)(this.m_ChartAxis.X1.X);
pt1.Y = (float)(this.m_ChartAxis.X1.Y - (i*(nCompleteBlockHeight + this.BlocksBuffer)));
if (pt1.Y < this.m_ChartAxis.Y1.Y)
{
pt1.Y = this.m_ChartAxis.Y1.Y;
}
pt2.X = pt1.X + this.m_iXAxisSpan;
pt2.Y = pt1.Y;
if (this.HasGridLines && bDrawGrid)
{
this.m_obGraphics.DrawLine(obGridPen, pt1, pt2);
}
strVal = (MinYValue + (i * BlockUnit)).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);
}
}
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);
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
return false;
}
finally
{
if (obPen != null)
{
obPen.Dispose();
}
}
return true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -