在.Net4.0框架中,微软已经将Mschart控件集成了进来,以前一直在web下面用过,原来winform下的Mschart控件更加简单更加方便,今天我们用mschart绘制一个多曲线图,发现MsChart 的显示效果还是不错的,话不多说,直接上效果图:

利用MsChart控件绘制多曲线图表-LMLPHP

代码如下,供大家参考:

public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
InitializeChart();
this.Load += new EventHandler(FrmMain_Load);
this.myChart.GetToolTipText += new EventHandler<ToolTipEventArgs>(myChart_GetToolTipText);
} void FrmMain_Load(object sender, EventArgs e)
{
float[][] data = new float[][];
//第一条数据
data[] = new float[] { 1.3f, 2.5f, 2.1f, 3.3f, 2.8f, 3.9f, 4.3f, 3.6f, 4.2f, 3.6f };
//第二条数据
data[] = new float[] { -2f, -1.3f, 0.1f, 0.5f, -1.5f, 0.7f, 1f, 1.4f, 1.9f, 2f, 2.6f, 3.1f };
//第三条数据
data[] = new float[] { 7.8f, 9.2f, 6.5f, 8.3f, 9.0f, 5.9f, 6.3f, 7.2f, 8.8f, 9.8f }; for (int i = ; i < data.Length; i++)
{
//横坐标时间
DateTime dt = DateTime.Now.Date;
Series series = this.SetSeriesStyle(i);
for (int j = ; j < data[i].Length; j++)
{
series.Points.AddXY(dt, data[i][j]);
dt = dt.AddDays();
}
this.myChart.Series.Add(series);
}
} private void myChart_GetToolTipText(object sender, ToolTipEventArgs e)
{
if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint)
{
int i = e.HitTestResult.PointIndex;
DataPoint dp = e.HitTestResult.Series.Points[i];
e.Text = string.Format("时间:{0};数值:{1:F1} ", DateTime.FromOADate(dp.XValue),dp.YValues[]);
}
} /// <summary>
/// 初始化Char控件样式
/// </summary>
public void InitializeChart()
{
#region 设置图表的属性
//图表的背景色
myChart.BackColor = Color.FromArgb(, , );
//图表背景色的渐变方式
myChart.BackGradientStyle = GradientStyle.TopBottom;
//图表的边框颜色、
myChart.BorderlineColor = Color.FromArgb(, , );
//图表的边框线条样式
myChart.BorderlineDashStyle = ChartDashStyle.Solid;
//图表边框线条的宽度
myChart.BorderlineWidth = ;
//图表边框的皮肤
myChart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
#endregion #region 设置图表的Title
Title title = new Title();
//标题内容
title.Text = "多曲线图演示";
//标题的字体
title.Font = new System.Drawing.Font("Microsoft Sans Serif", , FontStyle.Bold);
//标题字体颜色
title.ForeColor = Color.FromArgb(, , );
//标题阴影颜色
title.ShadowColor = Color.FromArgb(, , , );
//标题阴影偏移量
title.ShadowOffset = ; myChart.Titles.Add(title);
#endregion #region 设置图表区属性
//图表区的名字
ChartArea chartArea = new ChartArea("Default");
//背景色
chartArea.BackColor = Color.FromArgb(, , , );
//背景渐变方式
chartArea.BackGradientStyle = GradientStyle.TopBottom;
//渐变和阴影的辅助背景色
chartArea.BackSecondaryColor = Color.White;
//边框颜色
chartArea.BorderColor = Color.FromArgb(, , , );
//阴影颜色
chartArea.ShadowColor = Color.Transparent; //设置X轴和Y轴线条的颜色和宽度
chartArea.AxisX.LineColor = Color.FromArgb(, , , );
chartArea.AxisX.LineWidth = ;
chartArea.AxisY.LineColor = Color.FromArgb(, , , );
chartArea.AxisY.LineWidth = ; //设置X轴和Y轴的标题
chartArea.AxisX.Title = "横坐标标题";
chartArea.AxisY.Title = "纵坐标标题"; //设置图表区网格横纵线条的颜色和宽度
chartArea.AxisX.MajorGrid.LineColor = Color.FromArgb(, , , );
chartArea.AxisX.MajorGrid.LineWidth = ;
chartArea.AxisY.MajorGrid.LineColor = Color.FromArgb(, , , );
chartArea.AxisY.MajorGrid.LineWidth = ; myChart.ChartAreas.Add(chartArea);
#endregion #region 图例及图例的位置
Legend legend = new Legend();
legend.Alignment = StringAlignment.Center;
legend.Docking = Docking.Bottom; this.myChart.Legends.Add(legend);
#endregion
} //设置Series样式
private Series SetSeriesStyle(int i)
{
Series series = new Series(string.Format("第{0}条数据", i + )); //Series的类型
series.ChartType = SeriesChartType.Line;
//Series的边框颜色
series.BorderColor = Color.FromArgb(, , , );
//线条宽度
series.BorderWidth = ;
//线条阴影颜色
series.ShadowColor = Color.Black;
//阴影宽度
series.ShadowOffset = ;
//是否显示数据说明
series.IsVisibleInLegend = true;
//线条上数据点上是否有数据显示
series.IsValueShownAsLabel = false;
//线条上的数据点标志类型
series.MarkerStyle = MarkerStyle.Circle;
//线条数据点的大小
series.MarkerSize = ;
//线条颜色
switch (i)
{
case :
series.Color = Color.FromArgb(, , , );
break;
case :
series.Color = Color.FromArgb(, , , );
break;
case :
series.Color = Color.FromArgb(, , , );
break;
}
return series;
}
}

另外附上微软Demo的下载地址:http://archive.msdn.microsoft.com/mschart/Release/ProjectReleases.aspx?ReleaseId=4418

Demo 分为WEB版本和WinForm版本,样式和图表内容很全,希望对大家有所帮助。

出处:http://www.cnblogs.com/lxblog/archive/2012/05/21/2511823.html

05-11 04:06