原创--C#实例编程4
聚类分析法
一、需求分析
聚类分析,亦称群分析或点群分析,它是研究多要素事物分类问题的数量方法。其基本原理是,根据样本自身的属性,用数学方法按照某些相似性或差异性指标,定量地确定样本之间的亲疏关系,并按这种亲疏关系程度对样本进行聚类。
在聚类分析中,聚类要素的选择是十分重要的,它直接影响分类结果的准确性和可靠性。
不同要素的数据往往具有不同的单位和量纲,因而其数值的差异可能是很大的,这就会对分类结果产生影响。因此当分类要素的对象确定之后,在进行聚类分析之前,还要对聚类要索进行数据处理。
假设有m个被聚类的对象,每一个被聚类对象都有x1,x2,…,xn个要素构成。它们所对应的要素数据可用表2-10给出。在聚类分析中,常用的聚类要素的数据处理方法有如下几种。
1)总和标准化。分别求出各聚类要素所对应的数据的总和,以各要素的数据除以该要素数据的总和,即
2)标准差的标准化,即
3)极大值标准化,即
4)极差的标准化,即
距离的计算
如果我们把每一个分类对象的n个聚类要素看成n维空间的n个坐标轴,则每一个分类对象的n个要素所构成的n维数据向量就是n维空间中的一个点。这样,各分类对象之间的差异性就可以由它们所对应的n维空间中点之间的距离度量。常用的距离有
1)绝对值距离
2)欧氏距离
3)明科夫斯基距离
4)切比雪夫距离。当明科夫斯基距离p→∞时,有
选择不同的距离,聚类结果会有所差异。
二.使用的知识要点
1.应用程序对DataGrid,DataSet,Table的操作
2.简单读取XML文档
3.GID+的简单应用
三.程序主界面
四.主要程序段
1.画图
重构panel1的Paint方法
private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)//画图
{
//this.InitVarValuble();
if(this.newClassCount<1) return;
Int32 RowCount = this.myds.Tables[0].Rows.Count;
this.Calzuobiao();
float max_yy=0;
for(Int32 i=0;i<this.newClassCount;i++)
{
max_yy=max_yy>Convert.ToSingle(this.Gramatrix[i,2])?max_yy:Convert.ToSingle(this.Gramatrix[i,2]);
}
float temp1=Convert.ToSingle(this.textBox2.Text);//左边宽
float temp2=Convert.ToSingle(this.textBox3.Text);//上边宽
float temp3=Convert.ToSingle(this.textBox4.Text);//间距倍数
float temp4=Convert.ToSingle(this.textBox5.Text);//距离倍数
Graphics myGa = e.Graphics;
Pen mypen = new Pen(Color.Blue,1);
mypen.DashStyle = DashStyle.Solid;
myGa.DrawLine(mypen,this.panel1.Location.X+temp1,this.panel1.Location.Y+temp2-5,this.panel1.Location.X+temp1,this.panel1.Location.Y+temp3*RowCount+100);//画出坐标y
myGa.DrawLine(mypen,this.panel1.Location.X+temp1,this.panel1.Location.Y+temp3*RowCount+100,this.panel1.Location.X+temp1-3,this.panel1.Location.Y+temp3*RowCount+95);
myGa.DrawLine(mypen,this.panel1.Location.X+temp1,this.panel1.Location.Y+temp3*RowCount+100,this.panel1.Location.X+temp1+3,this.panel1.Location.Y+temp3*RowCount+95);
myGa.DrawLine(mypen,this.panel1.Location.X+temp1,this.panel1.Location.Y+temp2-5,this.panel1.Location.X+temp1+temp4*max_yy+100,this.panel1.Location.Y+temp2-5);//画出坐标x
myGa.DrawLine(mypen,this.panel1.Location.X+temp1+temp4*max_yy+100,this.panel1.Location.Y+temp2-5,this.panel1.Location.X+temp1+temp4*max_yy+95,this.panel1.Location.Y+temp2-2);
myGa.DrawLine(mypen,this.panel1.Location.X+temp1+temp4*max_yy+100,this.panel1.Location.Y+temp2-5,this.panel1.Location.X+temp1+temp4*max_yy+95,this.panel1.Location.Y+temp2-8);
this.label1.Location = new Point(Convert.ToInt32(temp4*max_yy)+100,Convert.ToInt32(temp3*RowCount)+100);
mypen = new Pen(Color.Black,1);
Font aVertialFont = new Font("Comic Sans Ms",8,FontStyle.Italic,GraphicsUnit.Point);
Brush aBrush = Brushes.Black;
Brush bBrush = Brushes.Red;
float x1,y1,x2,y2,x3,y3;
Int32 t,t1;
for(int i=0;i<this.newClassCount;i++)
{
t=Convert.ToInt32(this.Gramatrix[i,0]);
t1=Convert.ToInt32(this.Gramatrix[i,1]);
x1=this.panel1.Top+temp1+temp4*this.zuobiao[t,0];
y1=this.panel1.Location.Y+temp2+temp3*this.zuobiao[t,1];
x2=this.panel1.Top+temp1+temp4*this.zuobiao[i+RowCount,0];
y2=this.panel1.Location.Y+temp2+temp3*this.zuobiao[i+RowCount,1];
x3=this.panel1.Top+temp1+temp4*this.zuobiao[t1,0];
y3=this.panel1.Location.Y+temp2+temp3*this.zuobiao[t1,1];
myGa.DrawLine(mypen,x1,y1,x2,y1);//横线
myGa.DrawLine(mypen,x2,y1,x2,y3);//竖线
myGa.DrawLine(mypen,x3,y3,x2,y3);//横线
if(t<RowCount)
myGa.DrawString(this.myds.Tables[0].Rows[t][0].ToString(),aVertialFont,aBrush,new Point(0,Convert.ToInt16(y1-5)));
if(t1<RowCount)
myGa.DrawString(this.myds.Tables[0].Rows[t1][0].ToString(),aVertialFont,aBrush,new Point(0,Convert.ToInt16(y3-5)));
myGa.DrawString(this.zuobiao[RowCount+i,0].ToString(),aVertialFont,bBrush,new Point(Convert.ToInt16(x2),Convert.ToInt16(y2)));
}
mypen.Dispose();
//aBrush.Dispose();
aVertialFont.Dispose();
}
private void Calzuobiao()//计算坐标
{
Int32 i;
Int32 count=0;
Int32 RowCount=this.myds.Tables[0].Rows.Count;
this.zuobiao=new float[RowCount+this.newClassCount,2];
for(i=0;i<this.newClassCount;i++)
{
if(this.Gramatrix[i,0]<RowCount)
{
this.zuobiao[Convert.ToInt32(this.Gramatrix[i,0]),0]=0;;
this.zuobiao[Convert.ToInt32(this.Gramatrix[i,0]),1]=count;
count++;
}
if(this.Gramatrix[i,1]<RowCount)
{
this.zuobiao[Convert.ToInt32(this.Gramatrix[i,1]),0]=0;
this.zuobiao[Convert.ToInt32(this.Gramatrix[i,1]),1]=count;
count++;
}
this.zuobiao[i+RowCount,1]=(float)(this.zuobiao[Convert.ToInt32(this.Gramatrix[i,0]),1]+this.zuobiao[Convert.ToInt32(this.Gramatrix[i,1]),1])/2;
this.zuobiao[i+RowCount,0]=(float)(this.Gramatrix[i,2]);
}
}
2.
三、所有程序代码
using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace 经济管理模型
{
/// <summary>
/// jlfx2 的摘要说明。
/// </summary>
public class jlfx2 : System.Windows.Forms.Form
{
private System.Data.DataSet myds = new DataSet("聚类分析");
private DataTable yushuju = new DataTable("原数据");//原数据表
private DataTable biaozhun = new DataTable("标准化数据");//标准化数据表
private DataTable juli = new DataTable("距离数据");//距离表
private double[,] julimatrix;//距离阵
private double[,] Gramatrix;//为画图存储数据
private float[,] zuobiao;
private Int32 newClassCount=0;
private string[] zhibiao;
private int julimethod=0;//计算距离的方法
private int sta_method=0;//标准化的方法
//private int coordinate_x=0,coordinate_y=0;
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.RadioButton radioButton5;
private System.Windows.Forms.RadioButton radioButton6;
private System.Windows.Forms.RadioButton radioButton3;
private System.Windows.Forms.RadioButton radioButton4;
private System.Windows.Forms.RadioButton radioButton2;
private System.Windows.Forms.RadioButton radioButton1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button8;
private System.Windows.Forms.Button button9;
private System.Windows.Forms.TabPage tabPage3;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button button10;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Button button11;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.RadioButton radioButton7;
private System.Windows.Forms.RadioButton radioButton8;
private System.Windows.Forms.RadioButton radioButton9;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public jlfx2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.radioButton9 = new System.Windows.Forms.RadioButton();
this.radioButton8 = new System.Windows.Forms.RadioButton();
this.radioButton7 = new System.Windows.Forms.RadioButton();
this.button10 = new System.Windows.Forms.Button();
this.button9 = new System.Windows.Forms.Button();
this.button8 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button4 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.button3 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.radioButton5 = new System.Windows.Forms.RadioButton();
this.radioButton6 = new System.Windows.Forms.RadioButton();
this.radioButton3 = new System.Windows.Forms.RadioButton();
this.radioButton4 = new System.Windows.Forms.RadioButton();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button11 = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.panel1 = new System.Windows.Forms.Panel();
this.label1 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.textBox5 = new System.Windows.Forms.TextBox();
this.tabPage3 = new System.Windows.Forms.TabPage();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.tabControl1.SuspendLayout();
this.tabPage1.SuspendLayout();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.tabPage2.SuspendLayout();
this.panel1.SuspendLayout();
this.tabPage3.SuspendLayout();
this.SuspendLayout();
//
// tabControl1
//
this.tabControl1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Controls.Add(this.tabPage3);
this.tabControl1.Location = new System.Drawing.Point(0, -2);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(752, 464);
this.tabControl1.TabIndex = 1;
//
// tabPage1
//
this.tabPage1.Controls.Add(this.groupBox1);
this.tabPage1.Controls.Add(this.button10);
this.tabPage1.Controls.Add(this.button9);
this.tabPage1.Controls.Add(this.button8);
this.tabPage1.Controls.Add(this.button7);
this.tabPage1.Controls.Add(this.button6);
this.tabPage1.Controls.Add(this.button5);
this.tabPage1.Controls.Add(this.dataGrid1);
this.tabPage1.Controls.Add(this.button4);
this.tabPage1.Controls.Add(this.label2);
this.tabPage1.Controls.Add(this.button3);
this.tabPage1.Controls.Add(this.textBox1);
this.tabPage1.Controls.Add(this.radioButton5);
this.tabPage1.Controls.Add(this.radioButton6);
this.tabPage1.Controls.Add(this.radioButton3);
this.tabPage1.Controls.Add(this.radioButton4);
this.tabPage1.Controls.Add(this.radioButton2);
this.tabPage1.Controls.Add(this.radioButton1);
this.tabPage1.Controls.Add(this.button2);
this.tabPage1.Controls.Add(this.button1);
this.tabPage1.Location = new System.Drawing.Point(4, 22);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Size = new System.Drawing.Size(744, 438);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "计算";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.radioButton9);
this.groupBox1.Controls.Add(this.radioButton8);
this.groupBox1.Controls.Add(this.radioButton7);
this.groupBox1.Location = new System.Drawing.Point(8, 336);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(104, 96);
this.groupBox1.TabIndex = 22;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "标准化方法";
//
// radioButton9
//
this.radioButton9.Location = new System.Drawing.Point(24, 64);
this.radioButton9.Name = "radioButton9";
this.radioButton9.Size = new System.Drawing.Size(64, 24);
this.radioButton9.TabIndex = 2;
this.radioButton9.Text = "极差";
this.radioButton9.CheckedChanged += new System.EventHandler(this.radioButton9_CheckedChanged);
//
// radioButton8
//
this.radioButton8.Location = new System.Drawing.Point(24, 40);
this.radioButton8.Name = "radioButton8";
this.radioButton8.Size = new System.Drawing.Size(64, 24);
this.radioButton8.TabIndex = 1;
this.radioButton8.Text = "标准差";
this.radioButton8.CheckedChanged += new System.EventHandler(this.radioButton8_CheckedChanged);
//
// radioButton7
//
this.radioButton7.Checked = true;
this.radioButton7.Location = new System.Drawing.Point(24, 16);
this.radioButton7.Name = "radioButton7";
this.radioButton7.Size = new System.Drawing.Size(64, 24);
this.radioButton7.TabIndex = 0;
this.radioButton7.TabStop = true;
this.radioButton7.Text = "总和";
this.radioButton7.CheckedChanged += new System.EventHandler(this.radioButton7_CheckedChanged);
//
// button10
//
this.button10.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button10.Location = new System.Drawing.Point(16, 208);
this.button10.Name = "button10";
this.button10.Size = new System.Drawing.Size(95, 23);
this.button10.TabIndex = 21;
this.button10.Text = "刷新数据";
this.button10.Click += new System.EventHandler(this.button10_Click);
//
// button9
//
this.button9.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button9.Location = new System.Drawing.Point(16, 304);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(95, 23);
this.button9.TabIndex = 20;
this.button9.Text = "查看距离数据";
this.button9.Click += new System.EventHandler(this.button9_Click);
//
// button8
//
this.button8.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button8.Location = new System.Drawing.Point(16, 272);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(95, 23);
this.button8.TabIndex = 19;
this.button8.Text = "查看标准数据";
this.button8.Click += new System.EventHandler(this.button8_Click);
//
// button7
//
this.button7.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button7.Location = new System.Drawing.Point(16, 240);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(95, 23);
this.button7.TabIndex = 18;
this.button7.Text = "查看原始数据";
this.button7.Click += new System.EventHandler(this.button7_Click);
//
// button6
//
this.button6.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button6.Location = new System.Drawing.Point(16, 144);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(95, 23);
this.button6.TabIndex = 17;
this.button6.Text = "计算距离";
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// button5
//
this.button5.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button5.Location = new System.Drawing.Point(16, 112);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(95, 23);
this.button5.TabIndex = 16;
this.button5.Text = "标准化数据";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// dataGrid1
//
this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(120, 40);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(568, 344);
this.dataGrid1.TabIndex = 15;
//
// button4
//
this.button4.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button4.Location = new System.Drawing.Point(16, 176);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(95, 23);
this.button4.TabIndex = 14;
this.button4.Text = "开始计算";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(96, 8);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(42, 16);
this.label2.TabIndex = 13;
this.label2.Text = "指标:";
//
// button3
//
this.button3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button3.Location = new System.Drawing.Point(608, 8);
this.button3.Name = "button3";
this.button3.TabIndex = 12;
this.button3.Text = "设置";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// textBox1
//
this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox1.BackColor = System.Drawing.SystemColors.Info;
this.textBox1.Location = new System.Drawing.Point(152, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(448, 20);
this.textBox1.TabIndex = 9;
this.textBox1.Text = "";
//
// radioButton5
//
this.radioButton5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.radioButton5.Location = new System.Drawing.Point(312, 400);
this.radioButton5.Name = "radioButton5";
this.radioButton5.Size = new System.Drawing.Size(88, 24);
this.radioButton5.TabIndex = 8;
this.radioButton5.Text = "中间距离法";
this.radioButton5.CheckedChanged += new System.EventHandler(this.radioButton5_CheckedChanged);
//
// radioButton6
//
this.radioButton6.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.radioButton6.Location = new System.Drawing.Point(480, 400);
this.radioButton6.Name = "radioButton6";
this.radioButton6.TabIndex = 7;
this.radioButton6.Text = "离差平方和法";
this.radioButton6.Visible = false;
this.radioButton6.CheckedChanged += new System.EventHandler(this.radioButton6_CheckedChanged);
//
// radioButton3
//
this.radioButton3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.radioButton3.Checked = true;
this.radioButton3.Location = new System.Drawing.Point(128, 400);
this.radioButton3.Name = "radioButton3";
this.radioButton3.Size = new System.Drawing.Size(88, 24);
this.radioButton3.TabIndex = 6;
this.radioButton3.TabStop = true;
this.radioButton3.Text = "最短距离法";
//
// radioButton4
//
this.radioButton4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.radioButton4.Location = new System.Drawing.Point(224, 400);
this.radioButton4.Name = "radioButton4";
this.radioButton4.Size = new System.Drawing.Size(88, 24);
this.radioButton4.TabIndex = 5;
this.radioButton4.Text = "最长距离法";
this.radioButton4.CheckedChanged += new System.EventHandler(this.radioButton4_CheckedChanged);
//
// radioButton2
//
this.radioButton2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.radioButton2.Location = new System.Drawing.Point(224, 400);
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(64, 24);
this.radioButton2.TabIndex = 4;
this.radioButton2.Text = "重心法";
this.radioButton2.Visible = false;
this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
//
// radioButton1
//
this.radioButton1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.radioButton1.Location = new System.Drawing.Point(400, 400);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(72, 24);
this.radioButton1.TabIndex = 3;
this.radioButton1.Text = "类平均法";
this.radioButton1.Visible = false;
this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
//
// button2
//
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button2.Location = new System.Drawing.Point(16, 80);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(95, 23);
this.button2.TabIndex = 1;
this.button2.Text = "保存文件";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Location = new System.Drawing.Point(16, 48);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(95, 23);
this.button1.TabIndex = 0;
this.button1.Text = "读取文件";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// tabPage2
//
this.tabPage2.Controls.Add(this.textBox2);
this.tabPage2.Controls.Add(this.button11);
this.tabPage2.Controls.Add(this.label3);
this.tabPage2.Controls.Add(this.panel1);
this.tabPage2.Controls.Add(this.label4);
this.tabPage2.Controls.Add(this.label5);
this.tabPage2.Controls.Add(this.label6);
this.tabPage2.Controls.Add(this.textBox3);
this.tabPage2.Controls.Add(this.textBox4);
this.tabPage2.Controls.Add(this.textBox5);
this.tabPage2.Location = new System.Drawing.Point(4, 22);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Size = new System.Drawing.Size(744, 438);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "谱系图";
this.tabPage2.Visible = false;
//
// textBox2
//
this.textBox2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.textBox2.BackColor = System.Drawing.SystemColors.Info;
this.textBox2.Location = new System.Drawing.Point(64, 408);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(60, 20);
this.textBox2.TabIndex = 4;
this.textBox2.Text = "100";
//
// button11
//
this.button11.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.button11.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button11.Location = new System.Drawing.Point(576, 408);
this.button11.Name = "button11";
this.button11.TabIndex = 3;
this.button11.Text = "确定";
this.button11.Click += new System.EventHandler(this.button11_Click);
//
// label3
//
this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(8, 408);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(54, 16);
this.label3.TabIndex = 1;
this.label3.Text = "左边宽:";
//
// panel1
//
this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.panel1.AutoScroll = true;
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Controls.Add(this.label1);
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(688, 400);
this.panel1.TabIndex = 0;
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
//
// label1
//
this.label1.Location = new System.Drawing.Point(512, 344);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "谱系图";
//
// label4
//
this.label4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(136, 408);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(54, 16);
this.label4.TabIndex = 2;
this.label4.Text = "上边宽:";
//
// label5
//
this.label5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(272, 408);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(66, 16);
this.label5.TabIndex = 2;
this.label5.Text = "间距倍数:";
//
// label6
//
this.label6.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(424, 408);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(66, 16);
this.label6.TabIndex = 2;
this.label6.Text = "距离倍数:";
//
// textBox3
//
this.textBox3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.textBox3.BackColor = System.Drawing.SystemColors.Info;
this.textBox3.Location = new System.Drawing.Point(200, 408);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(60, 20);
this.textBox3.TabIndex = 5;
this.textBox3.Text = "15";
//
// textBox4
//
this.textBox4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.textBox4.BackColor = System.Drawing.SystemColors.Info;
this.textBox4.Location = new System.Drawing.Point(352, 408);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(60, 20);
this.textBox4.TabIndex = 5;
this.textBox4.Text = "30";
//
// textBox5
//
this.textBox5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.textBox5.BackColor = System.Drawing.SystemColors.Info;
this.textBox5.Location = new System.Drawing.Point(504, 408);
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(60, 20);
this.textBox5.TabIndex = 6;
this.textBox5.Text = "500";
//
// tabPage3
//
this.tabPage3.Controls.Add(this.richTextBox1);
this.tabPage3.Location = new System.Drawing.Point(4, 22);
this.tabPage3.Name = "tabPage3";
this.tabPage3.Size = new System.Drawing.Size(744, 438);
this.tabPage3.TabIndex = 2;
this.tabPage3.Text = "聚类描述";
//
// richTextBox1
//
this.richTextBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.richTextBox1.BackColor = System.Drawing.Color.WhiteSmoke;
this.richTextBox1.Location = new System.Drawing.Point(0, 0);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(696, 440);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// saveFileDialog1
//
this.saveFileDialog1.DefaultExt = "xml";
this.saveFileDialog1.Filter = "XML文件 (*.xml)|*.xml|文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*\"";
this.saveFileDialog1.RestoreDirectory = true;
//
// openFileDialog1
//
this.openFileDialog1.AddExtension = false;
this.openFileDialog1.DefaultExt = "xml";
this.openFileDialog1.Filter = "XML文件 (*.xml)|*.xml|文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*";
this.openFileDialog1.RestoreDirectory = true;
//
// jlfx2
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(696, 461);
this.Controls.Add(this.tabControl1);
this.Name = "jlfx2";
this.Text = "聚类分析法";
this.Load += new System.EventHandler(this.jlfx2_Load);
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.groupBox1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.tabPage2.ResumeLayout(false);
this.panel1.ResumeLayout(false);
this.tabPage3.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void button3_Click(object sender, System.EventArgs e)
{
try
{
this.InitVarValuble();
this.zhibiao = this.textBox1.Text.ToString().Trim().Split(',');
//this.yushuju = new DataTable("原数据");
DataColumn work = yushuju.Columns.Add("样本名",typeof(string));
work.AllowDBNull = false;
for(int i=0;i<zhibiao.Length;i++)
{
//MessageBox.Show(zhibiao[i]);
work = yushuju.Columns.Add(zhibiao[i],typeof(double));
work.AllowDBNull = false;
}
this.myds.Tables.Clear();
this.myds.Tables.Add(this.yushuju);
this.dataGrid1.DataSource = this.myds.Tables[0];
this.dataGrid1.Refresh();
//System.Data.DataTable mytable = new DataTable("yuanshuju");
//this.myds.ReadXml(@"E:\Mywork\经济管理模型\经济管理模型\bin\Debug\test.xml");
}
catch(DuplicateNameException)
{
MessageBox.Show("列名不能相同!");
}
catch(Exception err)
{
MessageBox.Show(err.ToString());
}
}
private void button2_Click(object sender, System.EventArgs e)
{
if(this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
this.myds.WriteXml(this.saveFileDialog1.FileName);
}
}
private void button1_Click(object sender, System.EventArgs e)
{
if(this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.InitVarValuble();
try
{
this.myds.Tables.Clear();
this.myds.ReadXml(this.openFileDialog1.FileName);
for(int i =0;i<this.myds.Tables[0].Columns.Count;i++)
{
this.myds.Tables[0].Columns[1].AllowDBNull = false;
}
this.yushuju = this.myds.Tables[0];
if(this.myds.Tables.Count>1)
{
this.biaozhun = this.myds.Tables[1];
}
if(this.myds.Tables.Count>2) this.juli = this.myds.Tables[2];
this.dataGrid1.DataSource = this.myds.Tables[0];
this.dataGrid1.Refresh();
}
catch(Exception err)
{
MessageBox.Show(err.ToString());
}
}
}
private void biaozhu_bzc()
{//标准差标准化数据
Int32 ColumnsCount = this.yushuju.Columns.Count-1;
Int32 RowsCount = this.yushuju.Rows.Count;
Int32 i,j;
double[] temp = new double[ColumnsCount];
double[] s = new double[ColumnsCount];
for(j=0;j<ColumnsCount;j++)
{//求每一列的平均值
temp[j]=0;
for(i = 0;i<RowsCount;i++)
temp[j]+=Convert.ToDouble(this.biaozhun.Rows[i][j+1]);
temp[j]/=RowsCount;
}
for(j=0;j<ColumnsCount;j++)
{//求每一列的标准差
s[j]=0;
for(i =0;i<RowsCount;i++)
s[j]+=Math.Pow(Convert.ToDouble(this.biaozhun.Rows[i][j+1])-temp[j],2);
s[j]=Math.Sqrt(s[j]/(RowsCount-1));
}
for(j=0;j<ColumnsCount;j++)//求每一个数据的标准数据
for(i=0;i<RowsCount;i++)
this.biaozhun.Rows[i][j+1]=(Convert.ToDouble(this.biaozhun.Rows[i][j+1])-temp[j])/s[j];
}
private void biaozhu_zh()
{//总和法标准化数据
Int32 ColumnsCount = this.yushuju.Columns.Count-1;
Int32 RowsCount = this.yushuju.Rows.Count;
Int32 i,j;
double[] temp = new double[ColumnsCount];
for(j=0;j<ColumnsCount;j++)
{
for(i = 0;i<RowsCount;i++)
{
temp[j]+=Convert.ToDouble(this.biaozhun.Rows[i][j+1]);
}
}
for(j=0;j<ColumnsCount;j++)
{
for(i = 0;i<RowsCount;i++)
{
this.biaozhun.Rows[i][j+1]=Convert.ToDouble(this.biaozhun.Rows[i][j+1])/temp[j];
}
}
}
private void biaozhu_jc()
{//极差标准化
Int32 ColumnsCount = this.yushuju.Columns.Count-1;
Int32 RowsCount = this.yushuju.Rows.Count;
Int32 i,j;
double xmin,xmax;
double temp;
for(j=0;j<ColumnsCount;j++)
{
xmin=xmax=Convert.ToDouble(this.biaozhun.Rows[0][j+1]);
for(i = 0;i<RowsCount;i++)
{
temp = Convert.ToDouble(this.biaozhun.Rows[i][j+1]);
if(xmin>temp) xmin = temp;
if(xmax<temp) xmax = temp;
}
for(i=0;i<RowsCount;i++)
this.biaozhun.Rows[i][j+1]=(Convert.ToDouble(this.biaozhun.Rows[i][j+1])-xmin)/(xmax-xmin);
}
}
private void button5_Click(object sender, System.EventArgs e)//标准化数据
{
if(myds.Tables.Count>1)
{
if((MessageBox.Show("要重新计算吗?","数据已经标准化",System.Windows.Forms.MessageBoxButtons.YesNo)==DialogResult.No))
return;
else
{this.myds.Tables.Clear();this.myds.Tables.Add(this.yushuju);}
}
try
{
this.biaozhun = this.myds.Tables[0].Copy();
this.biaozhun.TableName = "标准化数据";
switch(this.sta_method)
{
case 1:
this.biaozhu_bzc();//标准差
break;
case 2:
this.biaozhu_jc();//极差
break;
default :
this.biaozhu_zh();//总和
break;
}
this.myds.Tables.Add(this.biaozhun);
this.dataGrid1.DataSource = this.myds.Tables[1];
this.dataGrid1.Refresh();
}
catch(Exception err)
{
MessageBox.Show(err.ToString());
}
}
private void button6_Click(object sender, System.EventArgs e)
{
if(myds.Tables.Count>2)
{
if((MessageBox.Show("要重新计算吗?","距离已经计算",System.Windows.Forms.MessageBoxButtons.YesNo)==DialogResult.No))
return;
else
{this.myds.Tables.Clear();this.myds.Tables.Add(this.yushuju);this.myds.Tables.Add(this.biaozhun);}
}
if(myds.Tables.Count<2)
{
MessageBox.Show("没有进行标准计算!");
return;
}
//殴式距离算法
try
{
DataColumn work;
DataTable temptable = this.biaozhun;
Int32 i,j;
int k;
Int32 RowCount = this.myds.Tables[0].Rows.Count;
int ColCount = this.myds.Tables[0].Columns.Count;
double tempSumCol;
this.juli=null;
this.juli = new DataTable("距离数据");
for(i=0;i<temptable.Rows.Count;i++)
{
work = this.juli.Columns.Add(temptable.Rows[i][0].ToString(),typeof(double));
work.AllowDBNull = false;
}
for(i=0;i<RowCount;i++)
{
DataRow temprow = this.juli.NewRow();
for(j=0;j<RowCount;j++)
{
tempSumCol=0;
for(k=1;k<ColCount;k++)
tempSumCol += Math.Pow((Convert.ToDouble(temptable.Rows[i][k])-Convert.ToDouble(temptable.Rows[j][k])),2);
tempSumCol=Math.Sqrt(tempSumCol);
temprow[j]=tempSumCol;
}
this.juli.Rows.Add(temprow);
}
this.juli.TableName = "距离数据";
this.myds.Tables.Add(this.juli);
this.dataGrid1.DataSource = this.myds.Tables[2];
this.dataGrid1.Refresh();
}
catch(Exception err)
{
MessageBox.Show(err.ToString());
}
}
private void button7_Click(object sender, System.EventArgs e)
{
if(this.myds.Tables.Count<1)
{
MessageBox.Show("没有数据!");
return;
}
else
{
this.dataGrid1.DataSource = this.myds.Tables[0];
this.dataGrid1.Refresh();
}
}
private void button8_Click(object sender, System.EventArgs e)
{
if(this.myds.Tables.Count<2)
{
MessageBox.Show("没有进行标准化计算!");
return;
}
else
{
this.dataGrid1.DataSource = this.myds.Tables[1];
this.dataGrid1.Refresh();
}
}
private void button9_Click(object sender, System.EventArgs e)
{
if(this.myds.Tables.Count<3)
{
MessageBox.Show("没有距离计算!");
return;
}
else
{
this.dataGrid1.DataSource = this.myds.Tables[2];
this.dataGrid1.Refresh();
}
}
private void button4_Click(object sender, System.EventArgs e)
{
if(this.myds.Tables.Count<3)
{
MessageBox.Show("没有进行距离计算!");
return;
}
try
{
this.richTextBox1.Text="";
Int32 ClassCount = this.myds.Tables[0].Rows.Count-1;
Int32 RowCount = this.myds.Tables[0].Rows.Count;
Int32[] XY = new Int32[RowCount];
Int32 mini=0,minj=0,i,j,k;
for(i=0;i<RowCount;i++)
XY[i]=i;
double mind,tempmind=0;
this.Initjulimatrix();
this.Gramatrix = new double[RowCount,3];
this.newClassCount = 0;
while(RowCount >1)
{
mind = 56767;
mini = 0;minj=1;
for(i=1;i<RowCount;i++)//找出最小距离
for(j=0;j<i;j++)
if(mind>this.julimatrix[i,j])
{
mind=this.julimatrix[i,j];
mini=i;minj=j;
}
string tempstr;
if(tempmind == mind)//输出文字说明
tempstr="将"+XY[mini]+"类和"+XY[minj]+"类合并成"+ClassCount+"类,合并距离为:"+mind+"\n\n";
else
{
ClassCount++;
tempstr="将"+XY[mini]+"类和"+XY[minj]+"类合并成"+ClassCount+"类,合并距离为:"+mind+"\n\n";
}
tempmind=mind;
this.richTextBox1.Text +=tempstr;
this.Gramatrix[ClassCount-this.myds.Tables[0].Rows.Count,0]=XY[mini];//为画图阵增加数据
this.Gramatrix[ClassCount-this.myds.Tables[0].Rows.Count,1]=XY[minj];
this.Gramatrix[ClassCount-this.myds.Tables[0].Rows.Count,2]=mind;
XY[mini]=ClassCount;
for(k=minj;k<RowCount-1;k++)
XY[k]=XY[k+1];
for(j=0;j<RowCount;j++)
{
switch(this.julimethod)
{
case 1://重心法
break;
case 2://最长距离法
if(this.julimatrix[mini,j]<this.julimatrix[minj,j]) this.julimatrix[j,mini]=this.julimatrix[mini,j]=this.julimatrix[minj,j];
break;
case 3://中间距离法
this.julimatrix[j,mini]=Math.Sqrt(this.julimatrix[mini,j]=Math.Pow(this.julimatrix[j,mini],2)/2+Math.Pow(this.julimatrix[j,minj],2)/2-Math.Pow(this.julimatrix[mini,minj],2)/4);
if(this.julimatrix[mini,j]<this.julimatrix[minj,j]) this.julimatrix[j,mini]=this.julimatrix[mini,j]=this.julimatrix[minj,j];
break;
case 4://类平均法
break;
case 5://离差平方和
break;
default://最短距离法
if(this.julimatrix[mini,j]>this.julimatrix[minj,j]) this.julimatrix[j,mini]=this.julimatrix[mini,j]=this.julimatrix[minj,j];
//this.julimatrix[j,minj]=this.julimatrix[mini,j]=(this.julimatrix[mini,j]<this.julimatrix[minj,i]?julimatrix[mini,j]:this.julimatrix[minj,j]);
break;
}
}
for(i=minj;i<RowCount-1;i++)//删除minj行,列
{
for(j=0;j<RowCount;j++)
this.julimatrix[j,i]=this.julimatrix[i,j]=this.julimatrix[i+1,j];
}
this.newClassCount++;
RowCount--;
}
//this.InitmyDraw();
}
catch(Exception err)
{
MessageBox.Show(err.ToString());
}
MessageBox.Show("计算完成!");
}
private void jlfx2_Load(object sender, System.EventArgs e)
{
}
private void Initjulimatrix()
{
this.julimatrix = new double[this.myds.Tables[0].Rows.Count,this.myds.Tables[0].Rows.Count];
Int32 i,j;
for(i=0;i<this.myds.Tables[0].Rows.Count;i++)
for(j=0;j<this.myds.Tables[0].Rows.Count;j++)
this.julimatrix[i,j]=Convert.ToDouble(this.myds.Tables[2].Rows[i][j]);
}
private void InitmyDraw()
{
Graphics myG;//= this.panel1.CreateGraphics();
myG = Graphics.FromHwnd(this.panel1.Handle);
Pen mypen = new Pen(Color.Red,10);
mypen.DashStyle = DashStyle.Solid;
myG.DrawLine(mypen,10,10,100,100);
mypen.Dispose();
}
private void button10_Click(object sender, System.EventArgs e)
{
this.dataGrid1.AllowSorting = false;
this.dataGrid1.Refresh();
this.dataGrid1.AllowSorting = true;
}
private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)//画图
{
//this.InitVarValuble();
if(this.newClassCount<1) return;
Int32 RowCount = this.myds.Tables[0].Rows.Count;
this.Calzuobiao();
float max_yy=0;
for(Int32 i=0;i<this.newClassCount;i++)
{
max_yy=max_yy>Convert.ToSingle(this.Gramatrix[i,2])?max_yy:Convert.ToSingle(this.Gramatrix[i,2]);
}
float temp1=Convert.ToSingle(this.textBox2.Text);//左边宽
float temp2=Convert.ToSingle(this.textBox3.Text);//上边宽
float temp3=Convert.ToSingle(this.textBox4.Text);//间距倍数
float temp4=Convert.ToSingle(this.textBox5.Text);//距离倍数
Graphics myGa = e.Graphics;
Pen mypen = new Pen(Color.Blue,1);
mypen.DashStyle = DashStyle.Solid;
myGa.DrawLine(mypen,this.panel1.Location.X+temp1,this.panel1.Location.Y+temp2-5,this.panel1.Location.X+temp1,this.panel1.Location.Y+temp3*RowCount+100);//画出坐标y
myGa.DrawLine(mypen,this.panel1.Location.X+temp1,this.panel1.Location.Y+temp3*RowCount+100,this.panel1.Location.X+temp1-3,this.panel1.Location.Y+temp3*RowCount+95);
myGa.DrawLine(mypen,this.panel1.Location.X+temp1,this.panel1.Location.Y+temp3*RowCount+100,this.panel1.Location.X+temp1+3,this.panel1.Location.Y+temp3*RowCount+95);
myGa.DrawLine(mypen,this.panel1.Location.X+temp1,this.panel1.Location.Y+temp2-5,this.panel1.Location.X+temp1+temp4*max_yy+100,this.panel1.Location.Y+temp2-5);//画出坐标x
myGa.DrawLine(mypen,this.panel1.Location.X+temp1+temp4*max_yy+100,this.panel1.Location.Y+temp2-5,this.panel1.Location.X+temp1+temp4*max_yy+95,this.panel1.Location.Y+temp2-2);
myGa.DrawLine(mypen,this.panel1.Location.X+temp1+temp4*max_yy+100,this.panel1.Location.Y+temp2-5,this.panel1.Location.X+temp1+temp4*max_yy+95,this.panel1.Location.Y+temp2-8);
this.label1.Location = new Point(Convert.ToInt32(temp4*max_yy)+100,Convert.ToInt32(temp3*RowCount)+100);
mypen = new Pen(Color.Black,1);
Font aVertialFont = new Font("Comic Sans Ms",8,FontStyle.Italic,GraphicsUnit.Point);
Brush aBrush = Brushes.Black;
Brush bBrush = Brushes.Red;
float x1,y1,x2,y2,x3,y3;
Int32 t,t1;
for(int i=0;i<this.newClassCount;i++)
{
t=Convert.ToInt32(this.Gramatrix[i,0]);
t1=Convert.ToInt32(this.Gramatrix[i,1]);
x1=this.panel1.Top+temp1+temp4*this.zuobiao[t,0];
y1=this.panel1.Location.Y+temp2+temp3*this.zuobiao[t,1];
x2=this.panel1.Top+temp1+temp4*this.zuobiao[i+RowCount,0];
y2=this.panel1.Location.Y+temp2+temp3*this.zuobiao[i+RowCount,1];
x3=this.panel1.Top+temp1+temp4*this.zuobiao[t1,0];
y3=this.panel1.Location.Y+temp2+temp3*this.zuobiao[t1,1];
myGa.DrawLine(mypen,x1,y1,x2,y1);//横线
myGa.DrawLine(mypen,x2,y1,x2,y3);//竖线
myGa.DrawLine(mypen,x3,y3,x2,y3);//横线
if(t<RowCount)
myGa.DrawString(this.myds.Tables[0].Rows[t][0].ToString(),aVertialFont,aBrush,new Point(0,Convert.ToInt16(y1-5)));
if(t1<RowCount)
myGa.DrawString(this.myds.Tables[0].Rows[t1][0].ToString(),aVertialFont,aBrush,new Point(0,Convert.ToInt16(y3-5)));
myGa.DrawString(this.zuobiao[RowCount+i,0].ToString(),aVertialFont,bBrush,new Point(Convert.ToInt16(x2),Convert.ToInt16(y2)));
}
mypen.Dispose();
//aBrush.Dispose();
aVertialFont.Dispose();
}
private void Calzuobiao()//计算坐标
{
Int32 i;
Int32 count=0;
Int32 RowCount=this.myds.Tables[0].Rows.Count;
this.zuobiao=new float[RowCount+this.newClassCount,2];
for(i=0;i<this.newClassCount;i++)
{
if(this.Gramatrix[i,0]<RowCount)
{
this.zuobiao[Convert.ToInt32(this.Gramatrix[i,0]),0]=0;;
this.zuobiao[Convert.ToInt32(this.Gramatrix[i,0]),1]=count;
count++;
}
if(this.Gramatrix[i,1]<RowCount)
{
this.zuobiao[Convert.ToInt32(this.Gramatrix[i,1]),0]=0;
this.zuobiao[Convert.ToInt32(this.Gramatrix[i,1]),1]=count;
count++;
}
this.zuobiao[i+RowCount,1]=(float)(this.zuobiao[Convert.ToInt32(this.Gramatrix[i,0]),1]+this.zuobiao[Convert.ToInt32(this.Gramatrix[i,1]),1])/2;
this.zuobiao[i+RowCount,0]=(float)(this.Gramatrix[i,2]);
}
}
private void button11_Click(object sender, System.EventArgs e)
{
this.panel1.Invalidate();
}
private void InitVarValuble()
{
this.newClassCount = 0;
this.zuobiao = null;
this.richTextBox1.Text="";
//this.coordinate_x=0;
//this.coordinate_y=0;
}
private void radioButton2_CheckedChanged(object sender, System.EventArgs e)
{
if(this.radioButton2.Checked == true)
this.julimethod =1;
}
private void radioButton8_CheckedChanged(object sender, System.EventArgs e)
{
if(this.radioButton8.Checked == true)
this.sta_method = 1;
}
private void radioButton7_CheckedChanged(object sender, System.EventArgs e)
{
if(this.radioButton7.Checked == true)
this.sta_method = 0;
}
private void radioButton9_CheckedChanged(object sender, System.EventArgs e)
{
if(this.radioButton9.Checked == true)
this.sta_method = 2;
}
private void radioButton4_CheckedChanged(object sender, System.EventArgs e)
{
if(this.radioButton4.Checked == true)
this.julimethod =2;
}
private void radioButton5_CheckedChanged(object sender, System.EventArgs e)
{
if(this.radioButton5.Checked == true)
this.julimethod =3;
}
private void radioButton1_CheckedChanged(object sender, System.EventArgs e)
{
if(this.radioButton1.Checked == true)
this.julimethod =4;
}
private void radioButton6_CheckedChanged(object sender, System.EventArgs e)
{
if(this.radioButton6.Checked == true)
this.julimethod =5;
}
}
}
做的很好,可否能将所有程序源代码传一份来看看。谢谢。