using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Xml ;
namespace ChartControl
{
///
/// ChartControl 1.0 的摘要说明。此控件是画图的,根据xml文档的数据生成图表。
/// 目前能够画2维图和3维图 柱形图 和 折线图 饼形图。韩永矗5Apr2005制作。
/// 柱形图请将xml文件中的“add key="DiagramType"”修改为histogram
/// 折线图请将xml文件中的“add key="DiagramType"”修改为flexdiagram
/// 饼形图请将xml文件中的“add key="DiagramType"”修改为cakydiagram
/// xml文件不能修改名字,需要和你的程序执行文件在同一个目录。
/// public class ChartControl : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
///
/// 必需的设计器变量。
/// private System.ComponentModel.Container components = null;
public ChartControl()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitComponent 调用后添加任何初始化
}
///
/// 清理所有正在使用的资源。
/// protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
#region 组件设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// 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
#region 读写xml文件的几个小函数,2005 4 2 by hyc
public void SetXmlFileValue(string xmlPath,string AppKey,string AppValue)//写xmlPath是文件路径+文件名,AppKey是 Key Name,AppValue是Value
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(xmlPath);
XmlNode xNode;
XmlElement xElem1;
XmlElement xElem2;
xNode = xDoc.SelectSingleNode("//appSettings");
xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
if ( xElem1 != null )
{
xElem1.SetAttribute("value",AppValue);
}
else
{
xElem2 = xDoc.CreateElement("add");
xElem2.SetAttribute("key",AppKey);
xElem2.SetAttribute("value",AppValue);
xNode.AppendChild(xElem2);
}
xDoc.Save(xmlPath);
}
public void GetXmlFileValue(string xmlPath,string AppKey,ref string AppValue)//读xmlPath是文件路径+文件名,AppKey是 Key Name,AppValue是Value
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(xmlPath);
XmlNode xNode;
XmlElement xElem1;
xNode = xDoc.SelectSingleNode("//appSettings");
xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
if ( xElem1 != null )
{
AppValue=xElem1.GetAttribute ("value");
}
else
{
// MessageBox.Show ("There is not any information!");
}
}
#endregion
#region 变量声明,2005 4 4 by hyc
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 自定义函数,2005 4 4 by hyc
public void AllBegain()//All 初始化
{
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=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]);
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=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 );
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;
}
}
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 =(label3.Left -(label2.Left +label2.Width ))/Num*i-(label2.Left +label2.Width )-TypeValueLabel[j,i].Width /2;
this.Controls .Add (TypeValueLabel[j,i]);
}
}
}
public void CakyDiagramBegain()//饼形图初始化
{
for(int i=1;i<=Num;i++)
{
GetXmlFileValue(PathFileName,"ID"+i.ToString (),ref TypeName[i]);
GetXmlFileValue(PathFileName,"ID1Num"+i.ToString (),ref TypeValue[1,i]);
}
for(int i=1;i<=Num;i++)
{
GetXmlFileValue(PathFileName,"ID1Num"+i.ToString (),ref appvalue);
SweepAngl[i]=Int32.Parse (appvalue);
}
AllSweep=0;
for(int i=1;i<=Num;i++)
{
AllSweep=AllSweep+SweepAngl[i];
}
for(int i=1;i<=Num;i++)
{
SweepAngl[i]=(SweepAngl[i]*360)/AllSweep;
}
//去掉白边
int tempSw;
for(int j=1;j<360;j++)
{
tempSw=0;
for(int i=1;i<=Num;i++)
{
tempSw=tempSw+SweepAngl[i];
}
if(tempSw<360)
{
for(int k=1;k<=360-tempSw;k++)
{
if(k {
SweepAngl[k]++;
}
else
{
break;
}
}
}
if(tempSw>=360)
{
break;
}
}
StartAngl[1]=0;
int tempAllSweep;
for(int i=2;i<=Num;i++)
{
tempAllSweep=0;
for(int j=1;j<=i-1;j++)
{
tempAllSweep=tempAllSweep+SweepAngl[j];
}
StartAngl[i]=tempAllSweep;
}
if(FormWidth>=FormHeight)
{
KuanDu=FormHeight*2/3;
}
else
{
KuanDu=FormWidth*2/3;
}
double Angle;
TypeNameLabel = new System.Windows .Forms.Label[100];
for(int i=1;i<=Num;i++)
{
Angle=(360-StartAngl[i]+90-SweepAngl[i]/2)*3.141592653579/180;
TypeNameLabel[i]=new Label ();
TypeNameLabel[i].Top =(int)((KuanDu+60)/2* Math.Cos (Angle))+(FormHeight/6+KuanDu/2);
TypeNameLabel[i].Text =TypeName[i];
TypeNameLabel[i].AutoSize =true;
TypeNameLabel[i].Left =(int)((KuanDu+60)/2* Math.Sin (Angle))+(FormWidth/6+KuanDu/2);
this.Controls .Add (TypeNameLabel[i]);
}
TypeValueLabel = new System.Windows .Forms.Label[100,100];
string tempstr1;
for(int i=1;i<=Num;i++)
{
tempstr1=(Int32.Parse (TypeValue[1,i])*1.0/AllSweep*100.0).ToString()+" ";
TypeValueLabel[1,i]=new Label ();
TypeValueLabel[1,i].Top =TypeNameLabel[i].Top ;
TypeValueLabel[1,i].Text =tempstr1.Substring(0,5)+"%";
TypeValueLabel[1,i].AutoSize =true;
TypeValueLabel[1,i].Left =TypeNameLabel[i].Left+TypeNameLabel[i].Width +2;
this.Controls .Add (TypeValueLabel[1,i]);
}
}
#endregion
private void ChartControl_Load(object sender, System.EventArgs e)
{
AllBegain();
}
#region Draw Diagram函数,2005 4 4 by hyc
protected override void OnPaint(PaintEventArgs paintEvent)
{
Graphics g = paintEvent.Graphics;
SolidBrush brush=new SolidBrush (Color.Blue );
Pen pen=new Pen (Color.Black);
Pen pen2=new Pen (Color.DarkGray );
SolidBrush[] bb;
bb=new SolidBrush [100];
bb[1]=new SolidBrush (Color.DarkRed);
bb[2]=new SolidBrush (Color.DarkSlateBlue);
bb[3]=new SolidBrush (Color.DarkGray);
bb[4]=new SolidBrush (Color.YellowGreen);
bb[5]=new SolidBrush (Color.DarkSalmon);
bb[6]=new SolidBrush (Color.DarkOrange);
bb[7]=new SolidBrush (Color.LightYellow);
bb[8]=new SolidBrush (Color.SlateBlue);
bb[9]=new SolidBrush (Color.Black);
bb[10]=new SolidBrush (Color.YellowGreen);
bb[11]=new SolidBrush (Color.DarkSeaGreen);
bb[12]=new SolidBrush (Color.DarkSlateGray);
bb[13]=new SolidBrush (Color.DarkTurquoise);
bb[14]=new SolidBrush (Color.DarkViolet);
bb[15]=new SolidBrush (Color.DeepPink);
Pen[] pp;
pp=new Pen [100];
pp[1]=new Pen (Color.DarkRed);
pp[2]=new Pen (Color.DarkSlateBlue);
pp[3]=new Pen (Color.DarkGray);
pp[4]=new Pen (Color.YellowGreen);
pp[5]=new Pen (Color.DarkSalmon);
pp[6]=new Pen (Color.DarkOrange);
pp[7]=new Pen (Color.LightYellow);
pp[8]=new Pen (Color.SlateBlue);
pp[9]=new Pen (Color.Black);
pp[10]=new Pen (Color.YellowGreen);
pp[11]=new Pen (Color.DarkSeaGreen);
pp[12]=new Pen (Color.DarkSlateGray);
pp[13]=new Pen (Color.DarkTurquoise);
pp[14]=new Pen (Color.DarkViolet);
pp[15]=new Pen (Color.DeepPink);
GetXmlFileValue(PathFileName,"DiagramType",ref appvalue);
switch(appvalue)
{
case "histogram"://柱形图
HistogramBegain();
g.DrawLine (pen,label2.Left +label2.Width ,label3.Top-5,label3.Left+label3.Width,label3.Top-5);
g.DrawLine (pen,label2.Left +label2.Width ,label3.Top-5,label2.Left +label2.Width ,label2.Top-5);
for(int i=1;i<=Num;i++)
{
for(int j=1;j<=JNum;j++)
{
g.FillRectangle(bb[j],X[j,i],Y[j,i],polebold,YY[j,i]);
}
}
break;
case"flexdiagram"://折线图
FlexDiagramBegain();
g.DrawLine (pen,label2.Left +label2.Width ,label3.Top-5,label3.Left+label3.Width,label3.Top-5);
g.DrawLine (pen,label2.Left +label2.Width ,label3.Top-5,label2.Left +label2.Width ,label2.Top-5);
for(int i=1;i {
for(int j=1;j<=JNum;j++)
{
g.DrawLine (pp[j],X[j,i],Y[j,i],X[j,i+1],Y[j,i+1]);
}
}
for(int i=1;i<=Num;i++)
{
for(int j=1;j<=JNum;j++)
{
g.DrawLine(pen2,X[j,i],Y[j,i],X[j,i],label3.Top-5);
}
}
break;
case"cakydiagram"://饼形图
CakyDiagramBegain();
g.DrawLine (pen,label2.Left +label2.Width ,label3.Top-5,label3.Left+label3.Width,label3.Top-5);
g.DrawLine (pen,label2.Left +label2.Width ,label3.Top-5,label2.Left +label2.Width ,label2.Top-5);
for(int i=1;i<=Num;i++)
{
g.FillPie (bb[i],FormWidth/6,FormHeight/6,KuanDu,KuanDu,StartAngl[i],SweepAngl[i]);
}
break;
default:
break;
}
}
#endregion
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//XML文件内容,此文件必须放在应用程序目录,文件名XMLChart.xml