winform |
Download控件源码 |
|
webForm |
Download控件源码 |
|
|
|
|
思路概要:
1 首先确定曲线轴的长度和宽度,确定原点,便于坐标转换
2 分别绘制X轴 Y轴,并且绘制刻度
3 根据实际数据在坐标轴上绘制数据标识
4 绘制曲线,计算实际坐标与数轴坐标之间的转换
5 响应各种鼠标事件
实现:
vs2005 +C# 实现(包括winform和webform)两种控件
注意问题:
1 winform控件问题需要解决闪烁问题,所以每次鼠标事件后都只重新绘制部分曲线或者坐标轴。背景上华坐标轴,pictureBox上画曲线。
2 控件图标问题,添加16*16象素bmp格式图片,Build Action设置为“Embedded Resource”,曲线类头部引用为
“[System.Drawing.ToolboxBitmap(typeof(hocylan_Curve.Icon), "UcDate.bmp")]”
3 部分变量定义和坐标转换:
坐标转换:
float x,y分别为鼠标在数轴上的实际值,返回该点对应坐标轴的值
private string TransPositionToValue(float x, float y)
{
int xPointIndex = Convert.ToInt32((x - XYO.X) / XSlice);//对应x轴的点号
string xValue;
float yValue;
try
{
if (xPointIndex < Keys.Length && xPointIndex > 0)
{
xValue = Keys[xPointIndex];
// Keys[xPointIndex];
}
else
{
xValue = "-";
}
//Y的实际值
yValue = ((XYO.Y - y) * YSliceValue) / YSlice + YSliceBegin;
return "(" + x.ToString() + "," + y.ToString() + "),(" + xValue.ToString() + "," + yValue.ToString("#.##") + YUnit + ")";
}
catch (Exception ex)
{
return "(" + x.ToString() + "," + y.ToString() + "),(" + ex.Message.ToString() + ")";
}
}
变量定义:
private Graphics objGraphics; //Graphics 类提供将对象绘制到显示设备的方法
private Pen pen1 = new Pen(Color.Red, 2);
private Point XYO = new Point();//坐标原点
private Point XZ = new Point(); //X轴终点
private Point YZ = new Point(); //Y轴终点
private Bitmap objBitmap; //位图对象
private int m_Width = 624; //图像宽度
private int m_Height = 400; //图像高度
private string m_Title = "曲线图"; //标题
private float m_XSlice; //X轴刻度宽度
private float m_YSlice; //Y轴刻度宽度
private int xPointScaleNum = 15;//X轴点数(刻度数)
private int xInScaleNum = 1;//X轴大刻度间的小刻度
private float m_YSliceValue = 40; //Y轴刻度的数值宽度
private float m_YSliceBegin = 10; //Y轴刻度开始值
private float m_YSliceEnd = 200; //Y轴刻度最大值
private int yPointScaleNum;//Y轴大点数
private int m_YInSliceNum = 5; //Y轴刻度大刻度间的小刻度
private float m_Tension = 0.0f;//设置张力
private string m_yUnit = "KV"; //单位
private string m_xUnit = ""; //单位
private string m_XAxisText = "X轴说明文字"; //X轴说明文字
private string m_YAxisText = "Y轴说明文字"; //Y轴说明文字
private string[] m_Keys = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }; //键
private float[][] m_LineValue;//=new float[]{{120.5f, 23.0f, 23.90f, 0f, 22f, 100f },{ 20.5f, 123.0f, 3.90f, 92f, 122f, 190f },{200.5f, 123.0f, 90.90f, 100f, 0f, 50f }, };
private Color[] lineColor = new Color[] { Color.Yellow, Color.Red, Color.White, Color.Green, Color.Orange, Color.BlueViolet };
private Color m_BgColor = Color.DeepSkyBlue; //背景
private Color m_TextColor = Color.Black; //文字颜色
private Color m_BorderColor = Color.Black; //整体边框颜色
private Color m_AxisColor = Color.Blue; //轴线颜色
private Color m_GriddingColor = Color.Blue; //轴线颜色
private Color m_AxisTextColor = Color.Black; //轴说明文字颜色
private Color m_SliceTextColor = Color.Black; //刻度文字颜色
private Color m_SliceColor = Color.Black; //刻度颜色
private Color m_CurveColor = Color.Red; //曲线颜色
代码和资源:
winform |
Download控件源码 |
|
webForm |
Download控件源码 |
|
|
|
|