WinForm用户控件实现读取XML文件,生成柱形图,折线图,饼图:
4.ChartControl.cs代码:
1.新建Windows应用程序 ChartControl;
2.添加用户控件 ChartControl;
3.ChartControl.Designer.cs代码:
- namespace ChartControl
- {
- partial class ChartControl
- {
- /// <summary>
- /// 必需的设计器变量。
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #region 组件设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要
- /// 使用代码编辑器修改此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.label1 = new System.Windows.Forms.Label();
- this.label2 = new System.Windows.Forms.Label();
- this.label3 = new System.Windows.Forms.Label();
- this.SuspendLayout();
- //
- // label1
- //
- this.label1.Location = new System.Drawing.Point(104, 16);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(160, 24);
- this.label1.TabIndex = 0;
- this.label1.Text = "label1";
- //
- // label2
- //
- this.label2.Location = new System.Drawing.Point(8, 56);
- this.label2.Name = "label2";
- this.label2.TabIndex = 1;
- this.label2.Text = "label2";
- //
- // label3
- //
- this.label3.Location = new System.Drawing.Point(288, 320);
- this.label3.Name = "label3";
- this.label3.TabIndex = 2;
- this.label3.Text = "label3";
- //
- // ChartControl
- //
- this.BackColor = System.Drawing.Color.White;
- this.Controls.Add(this.label3);
- this.Controls.Add(this.label2);
- this.Controls.Add(this.label1);
- this.Name = "ChartControl";
- this.Size = new System.Drawing.Size(448, 360);
- this.Load += new System.EventHandler(this.ChartControl_Load);
- this.ResumeLayout(false);
- }
- #endregion
- private System.Windows.Forms.Label label1;
- private System.Windows.Forms.Label label2;
- private System.Windows.Forms.Label label3;
- }
- }
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Text;
- using System.Windows.Forms;
- using System.Xml;
- namespace ChartControl
- {
- public partial class ChartControl : UserControl
- {
- public ChartControl()
- {
- InitializeComponent();
- }
- private void ChartControl_Load(object sender, EventArgs e)
- {
- AllInit();
- }
- #region 读写XML文件的函数
- /// <summary>
- /// 写XML文件
- /// </summary>
- /// <param name="strXmlPath">文件路径+文件名</param>
- /// <param name="strAppKey">Key Name</param>
- /// <param name="strAppValue">Key Value</param>
- public void SetXmlFileValue(string strXmlPath, string strAppKey, string strAppValue)
- {
- XmlDocument xDoc = new XmlDocument();
- xDoc.Load(strXmlPath);
- XmlNode xNode;
- XmlElement xElem1;
- XmlElement xElem2;
- xNode = xDoc.SelectSingleNode("//appSettings");
- xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + strAppKey + "']");
- if (xElem1 != null)
- {
- xElem1.SetAttribute("value", strAppValue);
- }
- else
- {
- xElem2 = xDoc.CreateElement("add");
- xElem2.SetAttribute("key", strAppKey);
- xElem2.SetAttribute("value", strAppValue);
- xNode.AppendChild(xElem2);
- }
- xDoc.Save(strXmlPath);
- }
- /// <summary>
- /// 读XML文件
- /// </summary>
- /// <param name="strXmlPath">文件路径+文件名</param>
- /// <param name="strAppKey">Key Name</param>
- /// <param name="strAppValue">Key Value</param>
- public void GetXmlFileValue(string strXmlPath, string strAppKey, ref string strAppValue)
- {
- XmlDocument xDoc = new XmlDocument();
- xDoc.Load(strXmlPath);
- XmlNode xNode;
- XmlElement xElem1;
- xNode = xDoc.SelectSingleNode("//appSettings");
- xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + strAppKey + "']");
- if (xElem1 != null)
- {
- strAppValue = xElem1.GetAttribute("value");
- }
- else
- {
- //MessageBox.Show("There is not any information!");
- }
- }
- #endregion
- #region 变量声明
- public int FormHeight;
- public int FormWidth;
- public int Num = 1;
- public int Max = 1;
- public int Min = 1;
- public int JNum = 1;//维数
- public int Polebold;
- public int HeightXS = 100;//高度系数
- public int[] StartAngl = new int[100];//开始角度
- public int[] SweepAngl = new int[100]; //跨越弧度
- public int AllSweep;
- public int KuanDu;
- public int[,] X = new int[100, 100];
- public int[,] Y = new int[100, 100];
- public int[,] YY = new int[100, 100];
- public string[] TypeName = new string[100];
- public string[,] TypeValue = new string[100, 100];
- public string appvalue;
- public string PathFileName = System.Windows.Forms.Application.StartupPath + "//XMLChart.xml";
- System.Windows.Forms.Label[] TypeNameLabel;
- System.Windows.Forms.Label[,] TypeValueLabel;
- #endregion
- #region 自定义函数
- public void AllInit()
- {
- FormHeight = this.Height;
- FormWidth = this.Width;
- GetXmlFileValue(PathFileName, "DiagramName", ref appvalue);
- label1.Text = appvalue + "(此图表控件是测试版)";
- GetXmlFileValue(PathFileName, "YName", ref appvalue);
- label2.Text = appvalue;
- GetXmlFileValue(PathFileName, "XName", ref appvalue);
- label3.Text = appvalue;
- label1.AutoSize = true;
- label1.Top = 5;
- label1.Left = (FormWidth - label1.Width) / 2;
- label2.AutoSize = true;
- label3.AutoSize = true;
- label2.Top = 15;
- label2.Left = 5;
- label3.Top = FormHeight - 10;
- label3.Left = FormWidth - label3.Width - 5;
- GetXmlFileValue(PathFileName, "Num", ref appvalue);
- Num = Int32.Parse(appvalue);
- GetXmlFileValue(PathFileName, "JNum", ref appvalue);
- JNum = Int32.Parse(appvalue);
- }
- public void HistogramBegain()//Histogram 初始化
- {
- for (int i = 1; i <= Num; i++)
- {
- GetXmlFileValue(PathFileName, "ID" + i.ToString(), ref TypeName[i]);
- for (int j = 1; j <= JNum; j++)
- {
- GetXmlFileValue(PathFileName, "ID" + j.ToString() + "Num" + i.ToString(), ref TypeValue[j, i]);
- }
- }
- Polebold = FormWidth * 2 / (3 * Num * JNum);
- Max = Int32.Parse(TypeValue[1, 1]);
- Min = Int32.Parse(TypeValue[1, 1]);
- for (int i = 1; i <= Num; i++)
- {
- for (int j = 1; j <= JNum; j++)
- {
- if (Int32.Parse(TypeValue[j, i]) > Max)
- {
- Max = Int32.Parse(TypeValue[j, i]);
- }
- if (Int32.Parse(TypeValue[j, i]) < Min)
- {
- Min = Int32.Parse(TypeValue[j, i]);
- }
- }
- }
- for (int i = 1; i < 100; i++)
- {
- if (Max > (FormHeight - 50))
- {
- HeightXS = HeightXS / 2;
- Max = Max / 2;
- }
- if (Max < (FormHeight - 50) / 2)
- {
- HeightXS = HeightXS * 2;
- Max = Max * 2;
- }
- if (Max < (FormHeight - 50) && Max > (FormHeight - 50) / 2)
- {
- break;
- }
- }
- for (int i = 1; i <= Num; i++)
- {
- for (int j = 1; j <= JNum; j++)
- {
- X[j, i] = (label3.Left - (label2.Left + label2.Width)) / Num * i - (label2.Left + label2.Width) + (Polebold + 1) * (j - 1) - label3.Width;//????
- Y[j, i] = label3.Top - 5 - (Int32.Parse(TypeValue[j, i]) * HeightXS / 100);
- YY[j, i] = Int32.Parse(TypeValue[j, i]);
- YY[j, i] = YY[j, i] * HeightXS / 100;
- }
- }
- TypeNameLabel = new System.Windows.Forms.Label[100];
- for (int i = 1; i <= Num; i++)
- {
- TypeNameLabel[i] = new Label();
- TypeNameLabel[i].Top = label3.Top;
- TypeNameLabel[i].Text = TypeName[i];
- TypeNameLabel[i].AutoSize = true;
- TypeNameLabel[i].Left = X[1, i] + Polebold * JNum / 2;
- this.Controls.Add(TypeNameLabel[i]);
- }
- TypeValueLabel = new System.Windows.Forms.Label[100, 100];
- for (int i = 1; i <= Num; i++)
- {
- for (int j = 1; j <= JNum; j++)
- {
- TypeValueLabel[j, i] = new Label();
- TypeValueLabel[j, i].Top = Y[j, i] - 20;
- TypeValueLabel[j, i].Text = TypeValue[j, i];
- TypeValueLabel[j, i].AutoSize = true;
- TypeValueLabel[j, i].Left = X[j, i];
- this.Controls.Add(TypeValueLabel[j, i]);
- }
- }
- }
- public void FlexDiagramBegain()//Flex Diagram 初始化
- {
- for (int i = 1; i <= Num; i++)
- {
- GetXmlFileValue(PathFileName, "ID" + i.ToString(), ref TypeName[i]);
- for (int j = 1; j <= JNum; j++)
- {
- GetXmlFileValue(PathFileName, "ID" + j.ToString() + "Num" + i.ToString(), ref TypeValue[j, i]);
- }
- }
- TypeNameLabel = new System.Windows.Forms.Label[100];
- for (int i = 1; i <= Num; i++)
- {
- TypeNameLabel[i] = new Label();
- TypeNameLabel[i].Top = label3.Top;
- TypeNameLabel[i].Text = TypeName[i];
- TypeNameLabel[i].AutoSize = true;
- TypeNameLabel[i].Left = (label3.Left - (label2.Left + label2.Width)) / Num * i - (label2.Left + label2.Width) - TypeNameLabel[i].Width / 2;
- this.Controls.Add(TypeNameLabel[i]);
- }
- Max = Int32.Parse(TypeValue[1, 1]