分享一个winForm下的Chart控件

http://www.cnblogs.com/zzy0471/archive/2011/01/23/1942316.html

 

本来可以用MsChart的,但是客户有两个需求(绘制指定值的分割线;以对数方式显示数据)不好满足,只好自己写了一个,代码很乱,不过可以给有类似需求的同学以参考,顺便也请大家帮忙看看bug,因为要在项目中用,时间匆忙,也未做仔细测试,请感兴趣的园友帮忙看看,给点意见,谢谢。

  本控件和Ms控件相比,功能比较简单,实用为上,没有那么多属性可以设置,以下是为满足特定需求而添加的属性

  • 添加了一个用于插入分割线的属性
  • 添加了一种以对数形式显示数据的图表

  暂时这样,用到其他功能时再加,:-D。以下是使用示例:

     示例1:折线图

     示例2:区域图

     示例3:点图

     示例4:饼图

     示例5:对数折线图

  示例1:折线图

代码
    
    
private void InitChart()
{
this .chart1.BackColor = Color.Black;

this .chart1.ChartArea.ChartType = Enums.ChartType.Line;
this .chart1.ChartArea.Data = this .dsTest1.Tables[ 0 ];
this .chart1.ChartArea.XValueMember = " F_X " ;

StringInfo chartTitle
= new StringInfo();
chartTitle.Value
= " LineChart " ;
chartTitle.Color
= Color.DarkBlue;
chartTitle.Size
= 15 ;
this .chart1.ChartArea.ChartTitle = chartTitle;

this .chart1.ChartArea.AxisLineColor = Color.DarkSeaGreen;
this .chart1.ChartArea.AxisLineWidth = 2 ;

PartingLine line
= new PartingLine();
line.YValue
= 50 ;
line.LineColor
= Color.GreenYellow;
this .chart1.ChartArea.PartingLines.Add(line);

StringInfo axisXLable
= new StringInfo();
axisXLable.Size
= 12 ;
axisXLable.Color
= Color.Silver;
axisXLable.Value
= " 时间 " ;
this .chart1.ChartArea.AxisX.Lable = axisXLable;

this .chart1.ChartArea.AxisX.Interval = 1 ;
this .chart1.ChartArea.AxisX.IntervalType = Enums.InervalType.Hours;
this .chart1.ChartArea.AxisX.TimeFormat = " MM-dd HH:mm " ;
this .chart1.ChartArea.AxisX.Grid.LineColor = Color.RosyBrown;
this .chart1.ChartArea.AxisX.Grid.LineStyle = Enums.LineStyle.DashDot;
this .chart1.ChartArea.AxisX.ShowGrid = true ;

StringInfo axisYLable
= new StringInfo();
axisYLable.Size
= 12 ;
axisYLable.Color
= Color.Silver;
axisYLable.Value
= " 数据 " ;
this .chart1.ChartArea.AxisY1.Lable = axisYLable;

this .chart1.ChartArea.AxisY1.Interval = 10 ;
this .chart1.ChartArea.AxisY1.IntervalType = Enums.InervalType.Number;
this .chart1.ChartArea.AxisY1.Grid.LineColor = Color.SaddleBrown;
this .chart1.ChartArea.AxisY1.Grid.LineStyle = Enums.LineStyle.DashDot;
this .chart1.ChartArea.AxisY1.ShowGrid = true ;

Series series1
= new Series();
series1.Name
= " SeriesOne " ;
series1.Color
= Color.Red;
series1.Width
= 1 ;
series1.YValueMember
= " F_Y1 " ;
this .chart1.ChartArea.Series.Add(series1);

Series series2
= new Series();
series2.Name
= " SeriesTwo " ;
series2.Color
= Color.RoyalBlue;
series2.Width
= 1 ;
series2.YValueMember
= " F_Y2 " ;
this .chart1.ChartArea.Series.Add(series2);

Series series3
= new Series();
series3.Name
= " SeriesThree " ;
series3.Color
= Color.SeaShell;
series3.Width
= 1 ;
series3.YValueMember
= " F_Y3 " ;
this .chart1.ChartArea.Series.Add(series3);
}

 

示例2:区域图

代码
    
    
private void InitChart()
{
this .chart1.BackColor = Color.Black;

this .chart1.ChartArea.ChartType = Enums.ChartType.Area;
this .chart1.ChartArea.Data = this .dsTest1.Tables[ 0 ];
this .chart1.ChartArea.XValueMember = " F_Y1 " ;

StringInfo chartTitle
= new StringInfo();
chartTitle.Value
= " AreaChart " ;
chartTitle.Color
= Color.DarkBlue;
chartTitle.Size
= 15 ;
this .chart1.ChartArea.ChartTitle = chartTitle;

this .chart1.ChartArea.AxisLineColor = Color.DarkSeaGreen;
this .chart1.ChartArea.AxisLineWidth = 2 ;

StringInfo axisXLable
= new StringInfo();
axisXLable.Size
= 12 ;
axisXLable.Color
= Color.Silver;
axisXLable.Value
= " 时间 " ;
this .chart1.ChartArea.AxisX.Lable = axisXLable;

this .chart1.ChartArea.AxisX.Interval = 0.5 ;
this .chart1.ChartArea.AxisX.IntervalType = Enums.InervalType.Number;
// this.chart1.ChartArea.AxisX.TimeFormat = "yyyy-MM";
this .chart1.ChartArea.AxisX.Grid.LineColor = Color.RosyBrown;
this .chart1.ChartArea.AxisX.Grid.LineStyle = Enums.LineStyle.DashDot;
this .chart1.ChartArea.AxisX.ShowGrid = true ;

StringInfo axisYLable
= new StringInfo();
axisYLable.Size
= 12 ;
axisYLable.Color
= Color.Silver;
axisYLable.Value
= " 数据 " ;
this .chart1.ChartArea.AxisY1.Lable = axisYLable;

this .chart1.ChartArea.AxisY1.Interval = 10 ;
this .chart1.ChartArea.AxisY1.IntervalType = Enums.InervalType.Number;
this .chart1.ChartArea.AxisY1.Grid.LineColor = Color.SaddleBrown;
this .chart1.ChartArea.AxisY1.Grid.LineStyle = Enums.LineStyle.DashDot;
this .chart1.ChartArea.AxisY1.ShowGrid = true ;

Series series1
= new Series();
series1.Name
= " SeriesOne " ;
// series1.Color = Color.Red;
series1.Width = 1 ;
series1.YValueMember
= " F_Y1 " ;
this .chart1.ChartArea.Series.Add(series1);

Series series2
= new Series();
series2.Name
= " SeriesTwo " ;
// series2.Color = Color.RoyalBlue;
series2.Width = 1 ;
series2.YValueMember
= " F_Y2 " ;
this .chart1.ChartArea.Series.Add(series2);

Series series3
= new Series();
series3.Name
= " SeriesThree " ;
// series3.Color = Color.SeaShell;
series3.Width = 1 ;
series3.YValueMember
= " F_Y3 " ;
this .chart1.ChartArea.Series.Add(series3);
}

 

示例3:点图

代码
    
    
private void InitChart()
{
this .chart1.BackColor = Color.Black;

this .chart1.ChartArea.ChartType = Enums.ChartType.Dot;
this .chart1.ChartArea.Data = this .dsTest1.Tables[ 0 ];
this .chart1.ChartArea.XValueMember = " F_X " ;

StringInfo chartTitle
= new StringInfo();
chartTitle.Value
= " LineChart " ;
chartTitle.Color
= Color.DarkBlue;
chartTitle.Size
= 15 ;
this .chart1.ChartArea.ChartTitle = chartTitle;

this .chart1.ChartArea.AxisLineColor = Color.DarkSeaGreen;
this .chart1.ChartArea.AxisLineWidth = 2 ;

PartingLine line
= new PartingLine();
line.YValue
= 50 ;
line.LineColor
= Color.GreenYellow;
this .chart1.ChartArea.PartingLines.Add(line);

StringInfo axisXLable
= new StringInfo();
axisXLable.Size
= 12 ;
axisXLable.Color
= Color.Silver;
axisXLable.Value
= " 时间 " ;
this .chart1.ChartArea.AxisX.Lable = axisXLable;

this .chart1.ChartArea.AxisX.Interval = 1 ;
this .chart1.ChartArea.AxisX.IntervalType = Enums.InervalType.Hours;
this .chart1.ChartArea.AxisX.TimeFormat = " MM-dd HH:mm " ;
this .chart1.ChartArea.AxisX.Grid.LineColor = Color.RosyBrown;
this .chart1.ChartArea.AxisX.Grid.LineStyle = Enums.LineStyle.DashDot;
this .chart1.ChartArea.AxisX.ShowGrid = true ;

StringInfo axisYLable
= new StringInfo();
axisYLable.Size
= 12 ;
axisYLable.Color
= Color.Silver;
axisYLable.Value
= " 数据 " ;
this .chart1.ChartArea.AxisY1.Lable = axisYLable;

this .chart1.ChartArea.AxisY1.Interval = 10 ;
this .chart1.ChartArea.AxisY1.IntervalType = Enums.InervalType.Number;
this .chart1.ChartArea.AxisY1.Grid.LineColor = Color.SaddleBrown;
this .chart1.ChartArea.AxisY1.Grid.LineStyle = Enums.LineStyle.DashDot;
this .chart1.ChartArea.AxisY1.ShowGrid = true ;

Series series1
= new Series();
series1.Name
= " SeriesOne " ;
series1.Color
= Color.Red;
series1.Width
= 4 ; // 最小设置为2,再小了看不见了
series1.YValueMember = " F_Y1 " ;
this .chart1.ChartArea.Series.Add(series1);
}

 

示例4:饼图

代码
    
    
private void InitChart()
{
this .chart1.ChartArea.Data = _testDt;
this .chart1.ChartArea.ChartType = Ultra.OpenChart.Entities.Enums.ChartType.Pie;
StringInfo title
= new StringInfo();
title.Value
= " PieChart " ;
title.Size
= 14 ;
title.Color
= Color.Salmon;
this .chart1.ChartArea.ChartTitle = title;
chart1.ChartArea.XValueMember
= " Country " ;

Series series
= new Series();
series.YValueMember
= " Number " ;
this .chart1.ChartArea.Series.Add(series);
}

 

示例5:对数折线图

代码
    
    
private void InitChart()
{
this .chart1.BackColor = Color.Black;

this .chart1.ChartArea.ChartType = Enums.ChartType.LogarithmLine;
this .chart1.ChartArea.Data = this .dsTest1.Tables[ 0 ];
this .chart1.ChartArea.XValueMember = " F_X " ;
this .chart1.ChartArea.BaseNumber = 10 ;

StringInfo chartTitle
= new StringInfo();
chartTitle.Value
= " LogarithmLineChart " ;
chartTitle.Color
= Color.DarkBlue;
chartTitle.Size
= 15 ;
this .chart1.ChartArea.ChartTitle = chartTitle;

this .chart1.ChartArea.AxisLineColor = Color.DarkSeaGreen;

StringInfo axisXLable
= new StringInfo();
axisXLable.Size
= 12 ;
axisXLable.Color
= Color.Silver;
axisXLable.Value
= " 时间 " ;
this .chart1.ChartArea.AxisX.Lable = axisXLable;

this .chart1.ChartArea.AxisX.Interval = 0.5 ;
this .chart1.ChartArea.AxisX.IntervalType = Enums.InervalType.Hours;
this .chart1.ChartArea.AxisX.TimeFormat = " MM-dd HH:mm " ;
this .chart1.ChartArea.AxisX.MinVAlue = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, 0 , 0 ).ToOADate();

this .chart1.ChartArea.AxisY1.Grid.LineColor = Color.RosyBrown;
this .chart1.ChartArea.AxisY1.Grid.LineStyle = Enums.LineStyle.DashDot;
this .chart1.ChartArea.AxisY1.ShowGrid = true ;
this .chart1.ChartArea.AxisY1.MinVAlue = 0.001 ;

StringInfo axisYLable
= new StringInfo();
axisYLable.Size
= 12 ;
axisYLable.Color
= Color.Silver;
axisYLable.Value
= " 数据 " ;
this .chart1.ChartArea.AxisY1.Lable = axisYLable;

this .chart1.ChartArea.AxisY1.Interval = 1 ;
this .chart1.ChartArea.AxisY1.IntervalType = Enums.InervalType.Number;

Series series1
= new Series();
series1.Name
= " SeriesOne " ;
series1.Color
= Color.Sienna;
series1.Width
= 1 ;
series1.YValueMember
= " F_Y2 " ;
this .chart1.ChartArea.Series.Add(series1);

PartingLine line
= new PartingLine();
line.LineColor
= Color.RosyBrown;
line.LineWidth
= 2 ;
line.YValue
= 1500 ;
this .chart1.ChartArea.PartingLines.Add(line);
}

 

  此外,还有对数点图和对数区域图,都差不多,不再赘述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值