用 C# 绘制曲线图Curve图增加自动适应和多曲线绘制.docx
《用 C# 绘制曲线图Curve图增加自动适应和多曲线绘制.docx》由会员分享,可在线阅读,更多相关《用 C# 绘制曲线图Curve图增加自动适应和多曲线绘制.docx(20页珍藏版)》请在冰豆网上搜索。
用C#绘制曲线图Curve图增加自动适应和多曲线绘制
用C#绘制曲线图(Curve图,增加自动适应和多曲线绘制)
在N久之前,我发过一个绘制曲线的文章 先发个绘制曲线图的:
用ASP.NETwithC#绘制曲线图(Curve图) 里面介绍了简单的绘制曲线图的方法.近来重新翻阅了这篇文章.心情大好之下又重新修改了一下绘制方法,增加自动适应数据的处理和可以根据传入的数据绘制多条曲线
照例先上图:
比以前Cool吧?
呵呵,其实就是根据数据自动计算边距和字体等.
///
/// 自动根据参数调整图像大小
///
public void Fit()
{
//计算字体距离
intFontSpace = FontSize + 5;
//计算图像边距
float fltSpace = Math.Min(Width / 6, Height / 6);
XSpace = fltSpace;
YSpace = fltSpace;
//计算X轴刻度宽度
XSlice = (Width - 2 * XSpace) / (Keys.Length - 1);
//计算Y轴刻度宽度和Y轴刻度开始值
float fltMinValue = 0;
float fltMaxValue = 0;
for (int i = 0; i < Values.Length; i++)
{
if (Values[i] < fltMinValue)
{
fltMinValue = Values[i];
}
else if (Values[i] > fltMaxValue)
{
fltMaxValue = Values[i];
}
}
if (YSliceBegin > fltMinValue)
{
YSliceBegin = fltMinValue;
}
int intYSliceCount = (int)(fltMaxValue / YSliceValue);
if (fltMaxValue % YSliceValue !
= 0)
{
intYSliceCount++;
}
YSlice = (Height - 2 * YSpace) / intYSliceCount;
}
看我把数据缩小一个级别的效果:
因为代码里面充斥了大量注释,也不敲太多文字了..完整代码奉上:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Data;
using System.Drawing.Drawing2D;
namespace SarchPMS.Business.Draw
{
public class DrawingCurve :
DrawingChart
{
///
/// 画曲线图
///
///
///
public override Bitmap DrawImage(DataSet dsParameter)
{
Curve2D cuv2D = new Curve2D();
cuv2D.Fit();
return cuv2D.CreateImage();
}
}
public class Curve2D
{
private Graphics objGraphics; //Graphics 类提供将对象绘制到显示设备的方法
private Bitmap objBitmap; //位图对象
private float fltWidth = 480; //图像宽度
private float fltHeight = 248; //图像高度
private float fltXSlice = 50; //X轴刻度宽度
private float fltYSlice = 50; //Y轴刻度宽度
private float fltYSliceValue = 20; //Y轴刻度的数值宽度
private float fltYSliceBegin = 0; //Y轴刻度开始值
private float fltTension = 0.5f;
private string strTitle = "曲线图"; //标题
private string strXAxisText = "月份"; //X轴说明文字
private string strYAxisText = "万元"; //Y轴说明文字
private string[] strsKeys = new string[] { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" }; //键
private float[] fltsValues = new float[] { 20.0f, 30.0f, 50.0f, 55.4f, 21.6f, 12.8f, 99.5f, 36.4f, 78.2f, 56.4f, 45.8f, 66.5f, 99.5f, 36.4f, 78.2f, 56.4f, 45.8f, 66.5f, 20.0f, 30.0f, 50.0f, 55.4f, 21.6f, 12.8f }; //值
private Color clrBgColor = Color.Snow; //背景色
private Color clrTextColor = Color.Black; //文字颜色
private Color clrBorderColor = Color.Black; //整体边框颜色
private Color clrAxisColor = Color.Black; //轴线颜色
private Color clrAxisTextColor = Color.Black; //轴说明文字颜色
private Color clrSliceTextColor = Color.Black; //刻度文字颜色
private Color clrSliceColor = Color.Black; //刻度颜色
private Color[] clrsCurveColors = new Color[] { Color.Red, Color.Blue }; //曲线颜色
private float fltXSpace = 100f; //图像左右距离边缘距离
private float fltYSpace = 100f; //图像上下距离边缘距离
private int intFontSize = 9; //字体大小号数
private float fltXRotateAngle = 30f; //X轴文字旋转角度
private float fltYRotateAngle = 0f; //Y轴文字旋转角度
private int intCurveSize = 2; //曲线线条大小
private int intFontSpace = 0; //intFontSpace 是字体大小和距离调整出来的一个比较适合的数字
#region 公共属性
///
/// 图像的宽度
///
public float Width
{
set
{
if (value < 100)
{
fltWidth = 100;
}
else
{
fltWidth = value;
}
}
get
{
if (fltWidth <= 100)
{
return 100;
}
else
{
return fltWidth;
}
}
}
///
/// 图像的高度
///
public float Height
{
set
{
if (value < 100)
{
fltHeight = 100;
}
else
{
fltHeight = value;
}
}
get
{
if (fltHeight <= 100)
{
return 100;
}
else
{
return fltHeight;
}
}
}
///
/// X轴刻度宽度
///
public float XSlice
{
set { fltXSlice = value; }
get { return fltXSlice; }
}
///
/// Y轴刻度宽度
///
public float YSlice
{
set { fltYSlice = value; }
get { return fltYSlice; }
}
///
/// Y轴刻度的数值宽度
///
public float YSliceValue
{
set { fltYSliceValue = value; }
get { return fltYSliceValue; }
}
///
/// Y轴刻度开始值
///
public float YSliceBegin
{
set { fltYSliceBegin = value; }
get { return fltYSliceBegin; }
}
///
/// 张力系数
///
public float Tension
{
set
{
if (value < 0.0f && value > 1.0f)
{
fltTension = 0.5f;
}
else
{
fltTension = value;
}
}
get
{
return fltTension;
}
}
///
/// 标题
///
public string Title
{
set { strTitle = value; }
get { return strTitle; }
}
///
/// 键,X轴数据
///
public string[] Keys
{
set { strsKeys = value; }
get { return strsKeys; }
}
///
/// 值,Y轴数据
///
public float[] Values
{
set { fltsValues = value; }
get { return fltsValues; }
}
///
/// 背景色
///
public Color BgColor
{
set { clrBgColor = value; }
get { return clrBgColor; }
}
///
/// 文字颜色
///
public Color TextColor
{
set { clrTextColor = value; }
get { return clrTextColor; }
}
///
/// 整体边框颜色
///
public Color BorderColor
{
set { clrBorderColor = value; }
get { return clrBorderColor; }
}
///
/// 轴线颜色
///
public Color AxisColor
{
set { clrAxisColor = value; }
get { return clrAxisColor; }
}
///
/// X轴说明文字
///
public string XAxisText
{
set { strXAxisText = value; }
get { return strXAxisText; }
}
///
/// Y轴说明文字
///
public string YAxisText
{
set { strYAxisText = value; }
get { return strYAxisText; }
}
///
/// 轴说明文字颜色
///
public Color AxisTextColor
{
set { clrAxisTextColor = value; }
get { return clrAxisTextColor; }
}
///
/// 刻度文字颜色
///
public Color SliceTextColor
{
set { clrSliceTextColor = value; }
get { return clrSliceTextColor; }
}
///
/// 刻度颜色
///
public Color SliceColor
{
set { clrSliceColor = value; }
get { return clrSliceColor; }
}
///
/// 曲线颜色
///
public Color[] CurveColors
{
set { clrsCurveColors = value; }
get { return clrsCurveColors; }
}
///
/// X轴文字旋转角度
///
public float XRotateAngle
{
get { return fltXRotateAngle; }
set { fltXRotateAngle = value; }
}
///
/// Y轴文字旋转角度
///
public float YRotateAngle
{
get { return fltYRotateAngle; }
set { fltYRotateAngle = value; }
}
///
/// 图像左右距离边缘距离
///
public float XSpace
{
get { return fltXSpace; }
set { fltXSpace = value; }
}
///