.net 简单图表控件 ,介绍测试示例使用部分 [c/s桌面应用程序控件] II

   努力写完了b/s的示例部分,现在在把c/s的示例部分也加上,c/s的和b/s的差不多,只有一点儿下差别,不用配置那些xml,不用创建文件夹,因为c/s可以不使用保持图片的方式读取图片,可以直接给控件绑定背景图片更方便,所以c/s不需要再次保存图表生成的图片,可以通过里面的SaveImage(string path)方法把图片保存本地,也可以通过GetImage()方法获取图表图片作为某个控件的背景,其他的一些属性和b/s的属性一致,这里就不在唠叨了.

  不知道c/s的程序怎么表达呈现出来,就简单的做了一个类似任务管理器的一个小示例.

  这里不用怎么配置,只要将dll复制到bin目录下并引用,然后在工具箱中添加即可.

新建一个应用程序,直接写入以下相应文件代码:

  示例代码及控件下载

  下面的是设计页面(Form1.Designer.cs)文件的部分代码(首先类库的引用要正确):

展示

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.button1 = new System.Windows.Forms.Button();
            this.pictureBox2 = new System.Windows.Forms.PictureBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.panel1 = new System.Windows.Forms.Panel();
            this.pictureBox3 = new System.Windows.Forms.PictureBox();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
            this.panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // timer1
            // 
            this.timer1.Interval = 1;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(3, 3);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 3;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // pictureBox2
            // 
            this.pictureBox2.Location = new System.Drawing.Point(3, 32);
            this.pictureBox2.Name = "pictureBox2";
            this.pictureBox2.Size = new System.Drawing.Size(109, 110);
            this.pictureBox2.TabIndex = 4;
            this.pictureBox2.TabStop = false;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label1.ForeColor = System.Drawing.Color.Lime;
            this.label1.Location = new System.Drawing.Point(154, 5);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(62, 16);
            this.label1.TabIndex = 5;
            this.label1.Text = "label1";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(84, 8);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(71, 12);
            this.label2.TabIndex = 6;
            this.label2.Text = "当前使用率:";
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.pictureBox3);
            this.panel1.Controls.Add(this.pictureBox1);
            this.panel1.Controls.Add(this.button1);
            this.panel1.Controls.Add(this.label2);
            this.panel1.Controls.Add(this.pictureBox2);
            this.panel1.Controls.Add(this.label1);
            this.panel1.Location = new System.Drawing.Point(12, 12);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(863, 467);
            this.panel1.TabIndex = 7;
            // 
            // pictureBox3
            // 
            this.pictureBox3.Location = new System.Drawing.Point(451, 32);
            this.pictureBox3.Name = "pictureBox3";
            this.pictureBox3.Size = new System.Drawing.Size(109, 110);
            this.pictureBox3.TabIndex = 8;
            this.pictureBox3.TabStop = false;
            // 
            // pictureBox1
            // 
            this.pictureBox1.Location = new System.Drawing.Point(303, 32);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(109, 110);
            this.pictureBox1.TabIndex = 7;
            this.pictureBox1.TabStop = false;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(885, 491);
            this.Controls.Add(this.panel1);
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "CPU使用率";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Timer timer1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.PictureBox pictureBox2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.PictureBox pictureBox3;
        private System.Windows.Forms.PictureBox pictureBox1;

  下面是设计页面的后台(Form1.cs)文件的命名空间之内的代码:

展示

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ChartsDataLists Chartslists = new ChartsDataLists();
            Chartslists.Add(new ChartsValue(new ChartsColumn("c0"), new ChartsRow("r0"), 55), true);
            Chartslists.Add(new ChartsValue(new ChartsColumn("c0"), new ChartsRow("r1"), 66), true);
            Chartslists.Add(new ChartsValue(new ChartsColumn("c0"), new ChartsRow("r2"), 77), true);
            Chartslists.Add(new ChartsValue(new ChartsColumn("c1"), new ChartsRow("r0"), 88), true);
            Chartslists.Add(new ChartsValue(new ChartsColumn("c1"), new ChartsRow("r1"), 66), true);
            Chartslists.Add(new ChartsValue(new ChartsColumn("c1"), new ChartsRow("r2"), 33), true);

            label1.Text = "0 %";
            button1.Text = "开始/停止";
            timer1.Interval = 100;

            for (int i = 0; i < 20; i++)
            {
                lists.Add(0);
            }
            #region lc
            lc.IsAppendInfo = false;
            lc.IsTitle = false;
            lc.RHeight = 342;
            lc.RWidth = 200;
            lc.XInitGap = 0;
            lc.BackGroundColor = Color.Transparent;
            lc.YGroupValue = 3;
            lc.YGroupLength = 10;
            lc.YGroupCount = 34;
            lc.IsCutlineShow = false;
            lc.TopHeight = 30;
            lc.RightWidth = 10;
            lc.YTextSeparateCount = 5;
            lc.YUnit = "(%)";
            lc.NodeSize = 0;
            lc.IsPointInfo = false;
            lc.AllAvgLineStyle = ChartsBase.LineStyle.None;
            lc.IsXText = false;
            lc.DataSource = lists;// newlists.GetDatas();//lists;//data
            lc.XGroupLength = 10;
            lc.XLineStyle = ChartsBase.LineStyle.SolidLine;
            lc.YLineStyle = ChartsBase.LineStyle.SolidLine;
            lc.GroupStyle = ChartsBase.GroupStyles.Row;
            lc.YInitGap = 0;
            lc.XLineColor = Color.Green;
            lc.YLineColor = Color.Green;
            lc.ListColors.Add(Color.Lime);
            lc.ChartsBackColor = Color.Black;
            lc.ChartsBorderColor = Color.Green;
            lc.IsScale = false;
            //lc.LineShowStyle = ChartsBase.LineShowStyles.SmoothBorken;

            Size s = new Size();
            s.Width = lc.Width + 1;
            s.Height = lc.Height + 1;
            //lc.Size = s;
            lc.ReinstallSize();

            pictureBox2.Size = s;
            pictureBox2.Image = lc.GetImage();
            #endregion

            #region bc
            bc.IsAppendInfo = false;
            bc.IsTitle = false;
            bc.RHeight = 342;
            bc.RWidth = 50;
            bc.XInitGap = 5;
            bc.BackGroundColor = Color.Transparent;
            bc.YGroupValue = 3;
            bc.YGroupLength = 10;
            bc.YGroupCount = 34;
            bc.IsCutlineShow = false;
            bc.TopHeight = 30;
            bc.RightWidth = 10;
            bc.YTextSeparateCount = 5;
            bc.YUnit = "(%)";
            bc.IsPointInfo = false;
            bc.IsXText = false;
            bc.DataSource = new int[] { 0 };// newlists.GetDatas();//lists;//data
            bc.XGroupLength = 40;
            bc.ScaleLenght = 0;
            bc.XLineStyle = ChartsBase.LineStyle.None;
            bc.YLineStyle = ChartsBase.LineStyle.SolidLine;
            bc.GroupStyle = ChartsBase.GroupStyles.Row;
            bc.YInitGap = 0;
            bc.XLineColor = Color.Green;
            bc.YLineColor = Color.Green;
            bc.ListColors.Add(Color.Lime);
            bc.ChartsBackColor = Color.Black;
            bc.ChartsBorderColor = Color.Green;
            bc.IsScale = false;
            //lc.LineShowStyle = ChartsBase.LineShowStyles.SmoothBorken;

            s = new Size();
            s.Width = bc.Width + 1;
            s.Height = bc.Height + 1;
            //lc.Size = s;
            bc.ReinstallSize();

            pictureBox1.Size = s;
            pictureBox1.Image = bc.GetImage();
            #endregion

            #region pc
            pc.IsAppendInfo = false;
            pc.IsTitle = true;
            pc.TitleColor = Color.Red;
            pc.TitleFont = new Font("微软雅黑", 21, FontStyle.Bold);
            pc.TitleTop = 30;
            pc.BackGroundColor = Color.Transparent;
            pc.IsCutlineShow = false;
            pc.TopHeight = 100;
            pc.RightWidth = 100;
            pc.BottomHeight = 100;
            pc.Diameter = 190;
            pc.LeftWidth = 100;
            pc.OutTextStyle = ChartsBase.ShowInfoStyle.None;
            pc.InTextStyle = ChartsBase.ShowInfoStyle.None;
            pc.DataSource = new int[] { 0 };// newlists.GetDatas();//lists;//data
            pc.ListColors.Add(Color.Lime);
            pc.ListColors.Add(Color.Black);
            pc.ChartsBorderStyle = ChartsBase.LineStyle.BoldLine;

            pc.ChartsBorderStyle = ChartsBase.LineStyle.None;

            s = new Size();
            s.Width = pc.Width + 1;
            s.Height = pc.Height + 1;
            //lc.Size = s;
            pc.ReinstallSize();

            pictureBox3.Size = s;
            pictureBox3.Image = pc.GetImage();
            #endregion


            //this.Controls.Add(lc);
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {

                if (!isimg)
                {
                    label1.Text = cv.ToString("0.0") + " %";
                    pictureBox2.Image = pbit0;
                    pictureBox1.Image = pbit1;
                    pictureBox3.Image = pbit2;
                    isimg = true;
                }

            }
            catch { }
        }

        float cv = 0;
        bool istimeerstart;
        bool isimg;
        Bitmap pbit0;
        Bitmap pbit1;
        Bitmap pbit2;
        List<double> lists = new List<double>();
        CList<double> newlists = new CList<double>(20);
        Thread td;
        PerformanceCounter pcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total");
        LineCharts lc = new LineCharts();
        BarCharts bc = new BarCharts();
        PieCharts pc = new PieCharts();
        
        private void button1_Click(object sender, EventArgs e)
        {
            if (istimeerstart)
            {
                timer1.Stop();

                if (td != null)
                {
                    td.Abort();
                }

                istimeerstart = false;
            }
            else
            {
                timer1.Start();

                td = new Thread(MyReaderCPU);
                td.Start();

                istimeerstart = true;
            }
        }
        private void MyReaderCPU()
        {
            while (true)
            {
                if (isimg)
                {
                    isimg = false;

                    try
                    {
                        cv = pcCpuLoad.NextValue();

                        if (lists.Count > 20)
                        {
                            lists.RemoveAt(0);
                        }
                        lists.Add(cv);
                        //newlists.Add(cv);
                        //lc.DataSource = newlists.GetDatas();

                        pbit0 = lc.GetImage();

                        bc.DataSource = new float[] { cv };
                        pbit1 = bc.GetImage();

                        pc.Title = cv.ToString("0.0") + "%";
                        pc.DataSource = new float[] { cv, 100 - cv };
                        pbit2 = pc.GetImage();

                    }
                    catch { }

                }
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (td != null)
            {
                td.Abort();
            }
        }

    }

    public struct CList<T>
    {
        public CList(int c)
        {
            count = c;
            cindex = 0;
            datas = new T[count];
            newdatas = new T[count];
        }
        private int count;
        /// <summary>
        /// 获取对象个数
        /// </summary>
        public int Count
        {
            get { return count; }
        }
        private T[] datas;
        private T[] newdatas;//副本
        private int cindex;
        /// <summary>
        /// 追加一个值
        /// </summary>
        /// <param name="d">值</param>
        public void Add(T d)
        {
            int id = cindex % count;
            datas[id] = d;
            cindex++;
        }
        /// <summary>
        /// 返回对象集合
        /// </summary>
        /// <returns></returns>
        public T[] GetDatas()
        {
            for (int i = 0; i < count; i++)
            {
                newdatas[i] = datas[(cindex + i) % count];
            }
            return newdatas;
        }
    }

  效果图如下:

展示

  重新生成,如果不报错可以运行查看.

  注意:这些代码是很早的时候写的,当时的注释不是很多,现在只是拿过来用一用引砖抛玉,不过那些代码都是平常使用到的,只有一些,如获取cpu的参数的是我从网上找的,人家就那么写的,里面还有自定义的类,只是为了方便显示图表的数据而创建.