WinForm 用户控件 柱形图 折线图 饼图

WinForm用户控件实现读取XML文件,生成柱形图,折线图,饼图:1.新建Windows应用程序 ChartControl;2.添加用户控件 ChartControl;3.ChartControl.Designer.cs代码:namespace ChartControl{    partial class ChartControl    {        ///          /// 必需的
摘要由CSDN通过智能技术生成
WinForm用户控件实现读取XML文件,生成柱形图,折线图,饼图:

1.新建Windows应用程序 ChartControl;

2.添加用户控件 ChartControl;

3.ChartControl.Designer.cs代码:
  1. namespace ChartControl
  2. {
  3.     partial class ChartControl
  4.     {
  5.         /// <summary> 
  6.         /// 必需的设计器变量。
  7.         /// </summary>
  8.         private System.ComponentModel.IContainer components = null;
  9.         /// <summary> 
  10.         /// 清理所有正在使用的资源。
  11.         /// </summary>
  12.         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
  13.         protected override void Dispose(bool disposing)
  14.         {
  15.             if (disposing && (components != null))
  16.             {
  17.                 components.Dispose();
  18.             }
  19.             base.Dispose(disposing);
  20.         }
  21.         #region 组件设计器生成的代码
  22.         /// <summary> 
  23.         /// 设计器支持所需的方法 - 不要
  24.         /// 使用代码编辑器修改此方法的内容。
  25.         /// </summary>
  26.         private void InitializeComponent()
  27.         {
  28.             this.label1 = new System.Windows.Forms.Label();
  29.             this.label2 = new System.Windows.Forms.Label();
  30.             this.label3 = new System.Windows.Forms.Label();
  31.             this.SuspendLayout();
  32.             // 
  33.             // label1
  34.             // 
  35.             this.label1.Location = new System.Drawing.Point(104, 16);
  36.             this.label1.Name = "label1";
  37.             this.label1.Size = new System.Drawing.Size(160, 24);
  38.             this.label1.TabIndex = 0;
  39.             this.label1.Text = "label1";
  40.             // 
  41.             // label2
  42.             // 
  43.             this.label2.Location = new System.Drawing.Point(8, 56);
  44.             this.label2.Name = "label2";
  45.             this.label2.TabIndex = 1;
  46.             this.label2.Text = "label2";
  47.             // 
  48.             // label3
  49.             // 
  50.             this.label3.Location = new System.Drawing.Point(288, 320);
  51.             this.label3.Name = "label3";
  52.             this.label3.TabIndex = 2;
  53.             this.label3.Text = "label3";
  54.             // 
  55.             // ChartControl
  56.             // 
  57.             this.BackColor = System.Drawing.Color.White;
  58.             this.Controls.Add(this.label3);
  59.             this.Controls.Add(this.label2);
  60.             this.Controls.Add(this.label1);
  61.             this.Name = "ChartControl";
  62.             this.Size = new System.Drawing.Size(448, 360);
  63.             this.Load += new System.EventHandler(this.ChartControl_Load);
  64.             this.ResumeLayout(false);
  65.         }
  66.         #endregion
  67.         private System.Windows.Forms.Label label1;
  68.         private System.Windows.Forms.Label label2;
  69.         private System.Windows.Forms.Label label3;
  70.     }
  71. }
4.ChartControl.cs代码:
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Data;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Xml;
  10. namespace ChartControl
  11. {
  12.     public partial class ChartControl : UserControl
  13.     {
  14.         public ChartControl()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.         private void ChartControl_Load(object sender, EventArgs e)
  19.         {
  20.             AllInit();
  21.         }
  22.         #region 读写XML文件的函数
  23.         /// <summary>
  24.         /// 写XML文件
  25.         /// </summary>
  26.         /// <param name="strXmlPath">文件路径+文件名</param>
  27.         /// <param name="strAppKey">Key Name</param>
  28.         /// <param name="strAppValue">Key Value</param>
  29.         public void SetXmlFileValue(string strXmlPath, string strAppKey, string strAppValue)
  30.         {
  31.             XmlDocument xDoc = new XmlDocument();
  32.             xDoc.Load(strXmlPath);
  33.             XmlNode xNode;
  34.             XmlElement xElem1;
  35.             XmlElement xElem2;
  36.             xNode = xDoc.SelectSingleNode("//appSettings");
  37.             xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + strAppKey + "']");
  38.             if (xElem1 != null)
  39.             {
  40.                 xElem1.SetAttribute("value", strAppValue);
  41.             }
  42.             else
  43.             {
  44.                 xElem2 = xDoc.CreateElement("add");
  45.                 xElem2.SetAttribute("key", strAppKey);
  46.                 xElem2.SetAttribute("value", strAppValue);
  47.                 xNode.AppendChild(xElem2);
  48.             }
  49.             xDoc.Save(strXmlPath);
  50.         }
  51.         /// <summary>
  52.         /// 读XML文件
  53.         /// </summary>
  54.         /// <param name="strXmlPath">文件路径+文件名</param>
  55.         /// <param name="strAppKey">Key Name</param>
  56.         /// <param name="strAppValue">Key Value</param>
  57.         public void GetXmlFileValue(string strXmlPath, string strAppKey, ref string strAppValue)
  58.         {
  59.             XmlDocument xDoc = new XmlDocument();
  60.             xDoc.Load(strXmlPath);
  61.             XmlNode xNode;
  62.             XmlElement xElem1;
  63.             xNode = xDoc.SelectSingleNode("//appSettings");
  64.             xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + strAppKey + "']");
  65.             if (xElem1 != null)
  66.             {
  67.                 strAppValue = xElem1.GetAttribute("value");
  68.             }
  69.             else
  70.             {
  71.                 //MessageBox.Show("There is not any information!");
  72.             }
  73.         }
  74.         #endregion
  75.         #region 变量声明
  76.         public int FormHeight;
  77.         public int FormWidth;
  78.         public int Num = 1;
  79.         public int Max = 1;
  80.         public int Min = 1;
  81.         public int JNum = 1;//维数
  82.         public int Polebold;
  83.         public int HeightXS = 100;//高度系数
  84.         public int[] StartAngl = new int[100];//开始角度
  85.         public int[] SweepAngl = new int[100]; //跨越弧度
  86.         public int AllSweep;
  87.         public int KuanDu;
  88.         public int[,] X = new int[100, 100];
  89.         public int[,] Y = new int[100, 100];
  90.         public int[,] YY = new int[100, 100];
  91.         public string[] TypeName = new string[100];
  92.         public string[,] TypeValue = new string[100, 100];
  93.         public string appvalue;
  94.         public string PathFileName = System.Windows.Forms.Application.StartupPath + "//XMLChart.xml";
  95.         System.Windows.Forms.Label[] TypeNameLabel;
  96.         System.Windows.Forms.Label[,] TypeValueLabel;
  97.         #endregion
  98.         #region 自定义函数
  99.         public void AllInit()
  100.         {
  101.             FormHeight = this.Height;
  102.             FormWidth = this.Width;
  103.             GetXmlFileValue(PathFileName, "DiagramName"ref appvalue);
  104.             label1.Text = appvalue + "(此图表控件是测试版)";
  105.             GetXmlFileValue(PathFileName, "YName"ref appvalue);
  106.             label2.Text = appvalue;
  107.             GetXmlFileValue(PathFileName, "XName"ref appvalue);
  108.             label3.Text = appvalue;
  109.             label1.AutoSize = true;
  110.             label1.Top = 5;
  111.             label1.Left = (FormWidth - label1.Width) / 2;
  112.             label2.AutoSize = true;
  113.             label3.AutoSize = true;
  114.             label2.Top = 15;
  115.             label2.Left = 5;
  116.             label3.Top = FormHeight - 10;
  117.             label3.Left = FormWidth - label3.Width - 5;
  118.             GetXmlFileValue(PathFileName, "Num"ref appvalue);
  119.             Num = Int32.Parse(appvalue);
  120.             GetXmlFileValue(PathFileName, "JNum"ref appvalue);
  121.             JNum = Int32.Parse(appvalue);
  122.         }
  123.         public void HistogramBegain()//Histogram 初始化
  124.         {
  125.             for (int i = 1; i <= Num; i++)
  126.             {
  127.                 GetXmlFileValue(PathFileName, "ID" + i.ToString(), ref TypeName[i]);
  128.                 for (int j = 1; j <= JNum; j++)
  129.                 {
  130.                     GetXmlFileValue(PathFileName, "ID" + j.ToString() + "Num" + i.ToString(), ref TypeValue[j, i]);
  131.                 }
  132.             }
  133.             Polebold = FormWidth * 2 / (3 * Num * JNum);
  134.             Max = Int32.Parse(TypeValue[1, 1]);
  135.             Min = Int32.Parse(TypeValue[1, 1]);
  136.             for (int i = 1; i <= Num; i++)
  137.             {
  138.                 for (int j = 1; j <= JNum; j++)
  139.                 {
  140.                     if (Int32.Parse(TypeValue[j, i]) > Max)
  141.                     {
  142.                         Max = Int32.Parse(TypeValue[j, i]);
  143.                     }
  144.                     if (Int32.Parse(TypeValue[j, i]) < Min)
  145.                     {
  146.                         Min = Int32.Parse(TypeValue[j, i]);
  147.                     }
  148.                 }
  149.             }
  150.             for (int i = 1; i < 100; i++)
  151.             {
  152.                 if (Max > (FormHeight - 50))
  153.                 {
  154.                     HeightXS = HeightXS / 2;
  155.                     Max = Max / 2;
  156.                 }
  157.                 if (Max < (FormHeight - 50) / 2)
  158.                 {
  159.                     HeightXS = HeightXS * 2;
  160.                     Max = Max * 2;
  161.                 }
  162.                 if (Max < (FormHeight - 50) && Max > (FormHeight - 50) / 2)
  163.                 {
  164.                     break;
  165.                 }
  166.             }
  167.             for (int i = 1; i <= Num; i++)
  168.             {
  169.                 for (int j = 1; j <= JNum; j++)
  170.                 {
  171.                     X[j, i] = (label3.Left - (label2.Left + label2.Width)) / Num * i - (label2.Left + label2.Width) + (Polebold + 1) * (j - 1) - label3.Width;//????
  172.                     Y[j, i] = label3.Top - 5 - (Int32.Parse(TypeValue[j, i]) * HeightXS / 100);
  173.                     YY[j, i] = Int32.Parse(TypeValue[j, i]);
  174.                     YY[j, i] = YY[j, i] * HeightXS / 100;
  175.                 }
  176.             }
  177.             TypeNameLabel = new System.Windows.Forms.Label[100];
  178.             for (int i = 1; i <= Num; i++)
  179.             {
  180.                 TypeNameLabel[i] = new Label();
  181.                 TypeNameLabel[i].Top = label3.Top;
  182.                 TypeNameLabel[i].Text = TypeName[i];
  183.                 TypeNameLabel[i].AutoSize = true;
  184.                 TypeNameLabel[i].Left = X[1, i] + Polebold * JNum / 2;
  185.                 this.Controls.Add(TypeNameLabel[i]);
  186.             }
  187.             TypeValueLabel = new System.Windows.Forms.Label[100, 100];
  188.             for (int i = 1; i <= Num; i++)
  189.             {
  190.                 for (int j = 1; j <= JNum; j++)
  191.                 {
  192.                     TypeValueLabel[j, i] = new Label();
  193.                     TypeValueLabel[j, i].Top = Y[j, i] - 20;
  194.                     TypeValueLabel[j, i].Text = TypeValue[j, i];
  195.                     TypeValueLabel[j, i].AutoSize = true;
  196.                     TypeValueLabel[j, i].Left = X[j, i];
  197.                     this.Controls.Add(TypeValueLabel[j, i]);
  198.                 }
  199.             }
  200.         }
  201.         public void FlexDiagramBegain()//Flex Diagram 初始化
  202.         {
  203.             for (int i = 1; i <= Num; i++)
  204.             {
  205.                 GetXmlFileValue(PathFileName, "ID" + i.ToString(), ref TypeName[i]);
  206.                 for (int j = 1; j <= JNum; j++)
  207.                 {
  208.                     GetXmlFileValue(PathFileName, "ID" + j.ToString() + "Num" + i.ToString(), ref TypeValue[j, i]);
  209.                 }
  210.             }
  211.             TypeNameLabel = new System.Windows.Forms.Label[100];
  212.             for (int i = 1; i <= Num; i++)
  213.             {
  214.                 TypeNameLabel[i] = new Label();
  215.                 TypeNameLabel[i].Top = label3.Top;
  216.                 TypeNameLabel[i].Text = TypeName[i];
  217.                 TypeNameLabel[i].AutoSize = true;
  218.                 TypeNameLabel[i].Left = (label3.Left - (label2.Left + label2.Width)) / Num * i - (label2.Left + label2.Width) - TypeNameLabel[i].Width / 2;
  219.                 this.Controls.Add(TypeNameLabel[i]);
  220.             }
  221.             Max = Int32.Parse(TypeValue[1, 1]
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
WinForm chart件是一种用于在Windows Forms应用程序中显示表的件。它提供了丰富的功能和灵活性,能够帮助开发者轻松地创建各种类型的表,例如折线图、柱状等。 WinForm chart件具有以下特点: 1. 数据绑定:可以通过数据绑定功能,将数据源与件相关联,实现动态的数据显示和更新。 2. 各种表类型:WinForm chart件支持多种表类型,包括折线图、柱状等。开发者可以根据需要选择合适的表类型,展示不同类型的数据。 3. 自定义样式:开发者可以自定义表的样式,包括颜色、字体、边框等,以满足不同的设计要求。 4. 交互功能:WinForm chart件支持用户交互功能,例如鼠标悬停显示数据标签、缩放、平移等。这使得用户能够更加直观地了解表中的数据。 5. 数据分析功能:WinForm chart件提供了丰富的数据分析功能,例如计算平均值、最大值、最小值等。这些功能能够帮助开发者深入分析和理解表中的数据。 6. 打印和导出:WinForm chart件支持打印和导出功能,开发者可以将表保存为像文件或打印出来,方便与他人共享和使用。 总之,WinForm chart件是一款功能强大、易于使用的件,通过它,开发者可以轻松地创建并展示各种类型的表,为用户提供更好的数据可视化体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值