C#自定义控件 类似于Linechart

界面效果:

C#自定义控件 类似于Linechart_第1张图片C#自定义控件 类似于Linechart_第2张图片

 

 

 

对外提供的属性设置

       
        /// <summary>
        /// 背景色
        /// </summary>
        public Color BackColor;

        /// <summary>
        /// 边框颜色
        /// </summary>
        public Color rectanglelinecolor= Color.FromArgb(0, 60, 0);

        /// <summary>
        /// 线条颜色
        /// </summary>
        public Color linecolor = Color.FromArgb(0, 100, 0);

        /// <summary>
        /// 折线颜色
        /// </summary>
        public Color polygonalcolor = Color.FromArgb(0, 230, 0);

        /// <summary>
        /// 是否显示控件边框
        /// </summary>
        public bool rect_isvisable = false;
       /// <summary>
        /// 边框线宽
        /// </summary>
        private float linewidth = 2;

        /// <summary>
        /// 垂直线条间距
        /// </summary>
        private int vertical_spacing=14;
        /// <summary>
        /// 水平线条间距
        /// </summary>
        private int horizontal_spacing = 14;



        /// <summary>
        /// 点密度,越小越稀疏
        /// </summary>
        private float point_density = 0.7f;


        #region
        public float Point_density
        {
            get
            {
                return point_density;
            }

            set
            {
                point_density = value;

                if (value < 0 || value > 10)
                {
                    point_density = 0.7f;
                }
            }
        }

        public float Linewidth
        {
            get
            {
                return linewidth;
            }

            set
            {
                linewidth = value;
                if (value < 0)
                {
                    linewidth = 1;
                }
            }
        }

        public int Vertical_spacing
        {
            get
            {
                return vertical_spacing;
            }

            set
            {
                if (value < 0)
                {
                    value = 14;
                }
                vertical_spacing = value;
            }
        }

        public int Horizontal_spacing
        {
            get
            {
                return horizontal_spacing;
            }

            set
            {
                if (value < 0)
                {
                    value = 14;
                }
                horizontal_spacing = value;
            }
        }


        #endregion

这些要保证是大于0,我使用属性了,这样比频繁的类型转换要好

 

 

 

 

 

绘制

        protected override void OnPaint(PaintEventArgs pe)
        {
            this.drawtable();
            this.drawpolygonal();
            base.OnPaint(pe);
        }



        

        private void drawtable()//表格绘制
        {

            if (this.rect_isvisable)
            {
                this.rectanglelinecolor = Color.Black;
            }
            else
            {
                this.rectanglelinecolor = this.BackColor;
            }
            Graphics g = this.CreateGraphics();
            g.FillRectangle(new SolidBrush(this.BackColor), this.ClientRectangle);
            
            for(int i=(int) (this.linewidth/2)+this.vertical_spacing;i<this.Height;i+=this.vertical_spacing)
            {
                g.DrawLine(new Pen(this.linecolor, 1), this.ClientRectangle.X, i, this.Width, i);
            }

            for(int i=(int)(this.linewidth/2)+this.Horizontal_spacing;i<this.Width;i+=this.Horizontal_spacing)
            {
                g.DrawLine(new Pen(this.linecolor, 1), i,this.ClientRectangle.Y,i,  this.Height);
            }
            g.DrawRectangle(new Pen(this.rectanglelinecolor, this.linewidth), this.ClientRectangle.X + this.linewidth / 2, this.ClientRectangle.Y + this.linewidth / 2, this.Width - this.linewidth, this.Height - this.linewidth);
            g.Dispose();
        }

        /// <summary>
        /// 折线绘制
        /// </summary>
        private void drawpolygonal()
        {
            Point ps=new Point();
            Point pe;
            
            Graphics g = this.CreateGraphics();
            for(int i= this.polygonalpoint.Count-1,j=0; i>=0;i--,j++)
            {
                float f = this.polygonalpoint[i];
                int w = this.Width - this.Horizontal_spacing_per_pp*j;
                int h = this.Height - (int)(f * this.Height);
                pe = ps;
                ps = new Point(w, h);
                if(j!=0)
                {
                    g.DrawLine(p,pe, ps);
                }


            }
        }

 

 

链接:http://pan.baidu.com/s/1gdjod8J

 

添加到自己的项目里拖进去就可以了;和普通的控件一样使用

 

M$自己也有更好的linechart,不过插件肯定比较大的了

 

你可能感兴趣的:(C#自定义控件 类似于Linechart)