【C#上位机】| (chart)控件 | -System.Windows.Forms.DataVisualization.dll

一、 介绍:图表控件Microsoft Chart Controls for Microsoft .NET Framework 3.5,通过它,可让您的项目及报表,轻松套用各种功能强大的 2D、3D、实时变化的动态图表;且透过 AJAX,可让图表及里面的数据,每秒钟都持续更新;使用者透过浏览器,可和图表做各种互动设定

2. 应用

要想利用这个功能强大的控件,首先必须引用以下DLL和相关文件: 在WinForm应用程序中要想使用该图表控件,需引用如下DLL:

>System.Windows.Forms.DataVisualization.Design.dll
>System.Windows.Forms.DataVisualization.dll
>System.Windows.Forms.DataVisualization.xml

二、采用WinForm程序使用该图表控件

  1. 创建一个WinForm工程: DemoCollection

  2. 添加一个Form窗体: FrmChartDemo

  3. 添加所需的DLL引用

  4. 在该窗体的Load事件函数中动态创建好Chart对象实例

  5. 添加一个Timer控件,在Timer控件的Tick事件函数中向Chart中添加坐标值(X值和Y值),然后在窗体中绘制出来.

  6. FrmChartDemo的后台代码如下:

FrmChartDemo.cs:

using System.Windows.Forms.DataVisualization.Charting;
namespace DemoCollection

{
    public partial class FrmChartDemo : Form
    {
        #region the variable definiton
        private Chart chart1;
        private Random random = new Random();
        private int maxYValue = 20;
        private int minYValue = 0;
        #endregion
        #region Ctor
        public FrmChartDemo()
       {
            InitializeComponent()this.Load += new EventHandler(FrmChartDemo_Load);
            this.timer1.Tick += new EventHandler(timer1_Tick); 
     #endregion

        #region the event handler
        void FrmChartDemo_Load(object sender, EventArgs e)
        {
            #region from BBVS porject
            // Create a Chart
            chart1 = new Chart();
            // Create Chart Area
            ChartArea chartArea1 = new ChartArea();
            // Add Chart Area to the Chart
            chart1.ChartAreas.Add(chartArea1);
           #region Set the Chart
            // Set the chart style
            chart1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;

            chart1.BackSecondaryColor = System.Drawing.Color.White;

            chart1.BorderlineColor = System.Drawing.Color.FromArgb(((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));

            chart1.BorderlineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;

            chart1.BorderlineWidth = 2;

            chart1.BorderSkin.SkinStyle = System.Windows.Forms.DataVisualization.Charting.BorderSkinStyle.Emboss;

            chart1.BackColor = Color.SteelBlue;

            chart1.Dock = DockStyle.Fill;



            chartArea1.Area3DStyle.Inclination = 15;

            chartArea1.Area3DStyle.IsClustered = true;

            chartArea1.Area3DStyle.IsRightAngleAxes = false;

            chartArea1.Area3DStyle.Perspective = 10;

            chartArea1.Area3DStyle.Rotation = 10;

            chartArea1.Area3DStyle.WallWidth = 0;

            // 设置是否启用 3D 效果

            //chartArea1.Area3DStyle.Enable3D = true

            chartArea1.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea1.AxisX.LabelStyle.Format = "hh:mm:ss";
            chartArea1.AxisX.LabelStyle.Interval = 5D; // 10D;
           chartArea1.AxisX.LabelStyle.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Seconds;

            chartArea1.AxisX.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.AxisX.MajorGrid.Interval = 5D; // 10D;
            chartArea1.AxisX.MajorGrid.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Seconds;
            chartArea1.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.AxisX.MajorTickMark.Interval = 5D; // 10D;
            chartArea1.AxisX.MajorTickMark.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Seconds;

            chartArea1.AxisY.IsLabelAutoFit = false;
            chartArea1.AxisY.IsStartedFromZero = false;
            chartArea1.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea1.AxisY.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));

            #region 设置 Y 轴的 最大值和 最小值

            chartArea1.AxisY.Maximum = this.maxYValue + 6; 
            chartArea1.AxisY.Minimum = this.minYValue - 6;

            #endregion

            chartArea1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(165)))), ((int)(((byte)(191)))), ((int)(((byte)(228)))));
            chartArea1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            chartArea1.BackSecondaryColor = System.Drawing.Color.White;
            chartArea1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;

            chartArea1.InnerPlotPosition.Auto = false;
            chartArea1.InnerPlotPosition.Height = 85F;
            chartArea1.InnerPlotPosition.Width = 86F;
            chartArea1.InnerPlotPosition.X = 8.3969F;
            chartArea1.InnerPlotPosition.Y = 3.63068F;

            chartArea1.Name = "Default";
            chartArea1.Position.Auto = false;
            chartArea1.Position.Height = 86.76062F;
            chartArea1.Position.Width = 88F;
            chartArea1.Position.X = 5.089137F;
            chartArea1.Position.Y = 5.895753F;
            chartArea1.ShadowColor = System.Drawing.Color.Transparent;

            #endregion

            #region Create a Legend

            Legend legend1 = new Legend();
            legend1.Alignment = System.Drawing.StringAlignment.Far;
            legend1.BackColor = System.Drawing.Color.Transparent;
            legend1.DockedToChartArea = "Default";
            legend1.Docking = System.Windows.Forms.DataVisualization.Charting.Docking.Top;
            legend1.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            legend1.IsTextAutoFit = false;
            legend1.LegendStyle = System.Windows.Forms.DataVisualization.Charting.LegendStyle.Row;
            legend1.Name = "Default";
            chart1.Legends.Add(legend1);

            #endregion

            #region Create a Series

            Series series1 = new Series();
            series1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
            series1.ChartArea = "Default";
            series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.FastLine;//Line;
            series1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(64)))), ((int)(((byte)(10)))));

            series1.Legend = "Default";
            series1.Name = "Series1";
            series1.ShadowOffset = 1;
            series1.YValuesPerPoint = 2;
            chart1.Series.Add(series1);

            // 设置是否在 Chart 中显示 坐标点值
            series1.IsValueShownAsLabel = true;

            #endregion

            // Set chart control location
            chart1.Location = new System.Drawing.Point(0, 0);

            // Add chart control to the form
            this.Controls.AddRange(new System.Windows.Forms.Control[] { this.chart1 });

            #endregion
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            double tempValue = 0.0;

            // 随机产生 温度值
            tempValue = random.Next(this.minYValue, this.maxYValue);

            DateTime timeStamp = DateTime.Now;
            double xValue = DateTime.Now.ToOADate();

            // 向 Chart中 添加 X轴 和 Y轴的 值
            chart1.Series["Series1"].Points.AddXY(xValue, tempValue);

            // remove all points from the source series older than 20 seconds.
            double removeBefore = timeStamp.AddSeconds((double)(20) * (-1)).ToOADate();

            //remove oldest values to maintain a constant number of data points
            if (chart1.Series[0].Points.Count > 0)
            {
                while (chart1.Series[0].Points[0].XValue < removeBefore)
                {
                    chart1.Series[0].Points.RemoveAt(0);
                }

                chart1.ChartAreas[0].AxisX.Minimum = chart1.Series[0].Points[0].XValue;
                chart1.ChartAreas[0].AxisX.Maximum = DateTime.FromOADate(chart1.Series[0].Points[0].XValue).AddSeconds(20).ToOADate();
            }
            else
            {
                //chart1.ChartAreas[0].AxisX.Minimum = (double)minTempThreshold;
                //chart1.ChartAreas[0].AxisX.Maximum = (double)maxTempThreshold;
            }
            chart1.Invalidate();
        }
        #endregion
    }
}

参考:https://blog.csdn.net/a5pansq/article/details/89430307

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值