//按钮加载K线
private void button1_Click(object sender, EventArgs e)
{
if (this.comboBox1.Text == “热点”)
{
gpname = “zhy” + this.textBox1.Text.Trim();
}
else
{
gpname =this.textBox1.Text.Trim();
}
if (gpname != “zhy”)
{
int count = 10;
half = count;
//string jintian = DateTime.Now.ToShortDateString().ToString();
string jintian = this.dateTimePicker1.Text;
curdate = jintian;
begindate = jintian;
this.Refresh();
string sql = “select top " + count + " begintime,dayopen,dayclose,dayhigh,daylow,day5,day10,day20,day60 from " + gpname + " where begintime<=’” + jintian + “’ order by begintime desc”;
huatu(sql, count);
label1.Text = curdate;
this.textBox1.Text = “”;
}
}
//画K线方法
private void huatu(string sql,int num)
{
Graphics g = CreateGraphics();
Pen p = new Pen(Color.Red, 1);//上涨
Pen p1 = new Pen(Color.Green, 1);//下跌
Pen p2 = new Pen(Color.Black, 1);
Pen p3 = new Pen(Color.Brown, 1);
Pen p4 = new Pen(Color.Purple, 1);
Pen p5 = new Pen(Color.Blue, 1);
Point[] pointList5;//用来绘制曲线图的关键点,依次将这些点连接起来即得到曲线图(Ma5)
Point[] pointList10;//(Ma10)
Point[] pointList20;//(Ma20)
Point[] pointList60;//(Ma60)
DataSet ds = null;
DataTable dt = null;
ds = db.getds(sql, “mminfo”);
dt = ds.Tables[0];
pointList5 = new Point[dt.Rows.Count];//初始化坐标数组
pointList10 = new Point[dt.Rows.Count];//初始化坐标数组
pointList20 = new Point[dt.Rows.Count];//初始化坐标数组
pointList60 = new Point[dt.Rows.Count];//初始化坐标数组
int width = 1050;
int height = 400;
int Kwidth = width / dt.Rows.Count - 10;
g.DrawRectangle(p, 80, 80, width, height);
//k线开始
Font myFont = new Font(“微软雅黑”, 8);
string begindate1 = null;
string overdate1=dt.Rows[dt.Rows.Count-1][“begintime”].ToString();
overdate =overdate1.Substring(0,overdate1.IndexOf(" “));
string begindate2 = dt.Rows[0][“begintime”].ToString();
begindate = begindate2.Substring(0, begindate2.IndexOf(” "));
decimal high = (decimal)dt.Compute(“Max(dayhigh)”, null);//选择的K线组合中最高价
decimal low = (decimal)dt.Compute(“Min(daylow)”, null);//选择的K线组合中最低价
decimal hhigh = Math.Floor(high);
decimal llow = Math.Floor(low);
float llow1 = float.Parse(llow.ToString());
int changdu = width / dt.Rows.Count;//横轴每个日期的长度
decimal hlow = hhigh - llow;//纵轴总共差价
decimal oneprice = hlow / 10;
float onepoint = float.Parse((height / hlow).ToString());//单个价格所占的纵轴坐标
for (int i = 0; i < dt.Rows.Count; i++)//横轴时间
{
g.DrawLine(p, width + 80 - changdu * (i + 1) + Kwidth / 2, height + 80, width + 80 - changdu * (i + 1) + Kwidth / 2, height + 80 + 10);//横线N等分(20整体向右移动20单位,600为矩形长度,80为矩形离窗体左边为80)
}
for (int i = 0; i < 9; i++)//纵轴价格
{
g.DrawLine(p, width + 80, 480 - 40 * (i + 1), width + 90, 480 - 40 * (i + 1));//纵线10等分
g.DrawString(((i + 1) * oneprice + llow).ToString(), myFont, Brushes.Red, width + 95, height + 73 - height / 10 * (i + 1));
}
for (int i = 0; i < dt.Rows.Count; i++)
{
int len1 = dt.Rows[i]["begintime"].ToString().IndexOf(" ");
int len = dt.Rows[i]["begintime"].ToString().LastIndexOf('/') + 1;
int len2 = dt.Rows[i]["begintime"].ToString().IndexOf('/') + 1;
begindate1 = dt.Rows[i]["begintime"].ToString().Substring(len2, len1 - len2);
float o = float.Parse(dt.Rows[i]["dayopen"].ToString());
float c = float.Parse(dt.Rows[i]["dayclose"].ToString());
float h = float.Parse(dt.Rows[i]["dayhigh"].ToString());
float l = float.Parse(dt.Rows[i]["daylow"].ToString());
float day5=float.Parse(dt.Rows[i]["day5"].ToString());
float day10 = float.Parse(dt.Rows[i]["day10"].ToString());
float day20 = float.Parse(dt.Rows[i]["day20"].ToString());
float day60 = float.Parse(dt.Rows[i]["day60"].ToString());
int px5 = int.Parse((width + 80 - changdu * (i + 1) + Kwidth / 2).ToString());
int py5 = int.Parse(Math.Floor(height - (day5 - llow1) * onepoint+80).ToString());
int py10 = int.Parse(Math.Floor(height - (day10 - llow1) * onepoint + 80).ToString());
int py20 = int.Parse(Math.Floor(height - (day20 - llow1) * onepoint + 80).ToString());
int py60 = int.Parse(Math.Floor(height - (day60 - llow1) * onepoint + 80).ToString());
if (begindate1 == "10/30")
{
label17.Text = px5.ToString();
label16.Text = py5.ToString();
}
pointList5[i] = new Point(px5, py5);
pointList10[i] = new Point(px5, py10);
pointList20[i] = new Point(px5, py20);
pointList60[i] = new Point(px5, py60);
g.DrawString(begindate1, myFont, Brushes.Red, new PointF(width + 80 - changdu * (i + 1) + Kwidth / 2-5, height + 100));
if (c > o)
{
g.DrawLine(p, width + 80 - changdu * (i + 1) + Kwidth / 2, height - (c - llow1) * onepoint + 80, width + 80 - changdu * (i + 1) + Kwidth / 2, height - (h - llow1) * onepoint + 80);//上影(600为矩形长度,80为矩形到窗体左边的距离,60为长度10等分,10为纵柱最高价)
g.DrawRectangle(p, width + 80 - 20 - changdu * (i + 1) + 20, height - (c - llow1) * onepoint + 80, Kwidth, (c - o) * onepoint);//实体(Kwidth为K线宽度)(20,40为调节K线大小)
g.DrawLine(p, width + 80 - changdu * (i + 1) + Kwidth / 2, height - (o - llow1) * onepoint + 80, width + 80 - changdu * (i + 1) + Kwidth / 2, height - (l - llow1) * onepoint + 80);//下影 (20为整体向左平移20个单位)
}
else if (c < o)
{
g.DrawLine(p1, width + 80 - changdu * (i + 1) + Kwidth / 2, height - (o - llow1) * onepoint + 80, width + 80 - changdu * (i + 1) + Kwidth / 2, height - (h - llow1) * onepoint + 80);//上影
g.DrawRectangle(p1, width + 80 - 20 - changdu * (i + 1) + 20, height - (o - llow1) * onepoint + 80, Kwidth, (o - c) * onepoint);//实体
g.DrawLine(p1, width + 80 - changdu * (i + 1) + Kwidth / 2, height - (c - llow1) * onepoint + 80, width + 80 - changdu * (i + 1) + Kwidth / 2, height - (l - llow1) * onepoint + 80);//下影
}
else if (c == o)
{
g.DrawLine(p, width + 80 - changdu * (i + 1) + Kwidth / 2, height - (o - llow1) * onepoint + 80, width + 80 - changdu * (i + 1) + Kwidth / 2, height - (h - llow1) * onepoint + 80);//上影
g.DrawLine(p1, width + 80 - 20 - changdu * (i + 1) + 20, height - (o - llow1) * onepoint + 80, width + 80 - 20 - changdu * (i + 1) + 20 + Kwidth, height - (o - llow1) * onepoint + 80);//实体
g.DrawLine(p, width + 80 - changdu * (i + 1) + Kwidth / 2, height - (c - llow1) * onepoint + 80, width + 80 - changdu * (i + 1) + Kwidth / 2, height - (l - llow1) * onepoint + 80);//下影
}
}
g.DrawCurve(p2, pointList5);//画5均线
g.DrawCurve(p3, pointList10);//画10均线
g.DrawCurve(p4, pointList20);//画20均线
g.DrawCurve(p5, pointList60);//画60均线
p.Dispose();
g.Dispose();
}
///
/// 重写ProcessDialogKey,来允许监听方向键
///
///
///
protected override bool ProcessDialogKey(Keys keycode)
{
switch (keycode)
{
case Keys.Left:
case Keys.Up:
case Keys.Right:
case Keys.Down:
case Keys.D0:
case Keys.D1:
case Keys.D2:
case Keys.D3:
case Keys.D4:
case Keys.D5:
case Keys.D6:
case Keys.D7:
case Keys.D8:
case Keys.D9:
case Keys.NumPad0:
case Keys.NumPad1:
case Keys.NumPad2:
case Keys.NumPad3:
case Keys.NumPad4:
case Keys.NumPad5:
case Keys.NumPad6:
case Keys.NumPad7:
case Keys.NumPad8:
case Keys.NumPad9:
return false;
}
return true;
}
///
/// 监听方向键的KeyDown事件
///
///
///
private void Kxian_KeyDown(object sender, KeyEventArgs e)
{
DataSet ds;
string curdate1 = null;
string sql = null;
string bdate = null;
string odate = null;
//int count = int.Parse(this.comboBox1.Text.Trim());
//int count = 10;
switch (e.KeyCode)
{
case Keys.Left:
sql = "select top 1 begintime,dayopen,dayclose,dayhigh,daylow,day5,day10,day20,day60 from "+gpname+" where begintime<'" + curdate + "' order by begintime desc";
ds = db.getds(sql, "previous");
if (ds.Tables[0].Rows.Count > 0)
{
curdate1 = ds.Tables[0].Rows[0]["begintime"].ToString();
curdate = curdate1.Substring(0, curdate1.IndexOf(" "));
label1.Text = curdate;
label7.Text = ds.Tables[0].Rows[0]["dayopen"].ToString();
label6.Text = ds.Tables[0].Rows[0]["dayclose"].ToString();
label9.Text = ds.Tables[0].Rows[0]["dayhigh"].ToString();
label8.Text = ds.Tables[0].Rows[0]["daylow"].ToString();
label12.Text = ds.Tables[0].Rows[0]["day5"].ToString();
label11.Text = ds.Tables[0].Rows[0]["day10"].ToString();
label22.Text = ds.Tables[0].Rows[0]["day20"].ToString();
label21.Text = ds.Tables[0].Rows[0]["day60"].ToString();
if (CompareDate(curdate, overdate) < 0)
{
this.Refresh();
sql = "select * from(select top " + half + " begintime,dayopen,dayclose,dayhigh,daylow,day5,day10,day20,day60 from "+gpname+" where begintime>='" + curdate + "' order by begintime asc) a order by begintime desc";
huatu(sql, half);
overdate = curdate;
}
}
break;
case Keys.Up:
if (half >=10)
{
this.Refresh();
half = half / 2;
bdate = DateTime.Parse(curdate).AddDays(-half).ToShortDateString();
odate = DateTime.Parse(curdate).AddDays(half).ToShortDateString();
sql = "select begintime,dayopen,dayopen,dayclose,dayhigh,daylow,day5,day10,day20,day60 from "+gpname+" where begintime between '" + bdate + "' and '" + odate + "' order by begintime desc ";
huatu(sql, half);
label2.Text = half.ToString();
}
break;
case Keys.Right:
sql = "select top 1 begintime,dayopen,dayclose,dayhigh,daylow,day5,day10,day20,day60 from "+gpname+" where begintime>'" + curdate + "' order by begintime asc";
ds = db.getds(sql, "next");
if (ds.Tables[0].Rows.Count > 0)
{
curdate1 = ds.Tables[0].Rows[0]["begintime"].ToString();
curdate = curdate1.Substring(0, curdate1.IndexOf(" "));
label1.Text = curdate;
label7.Text = ds.Tables[0].Rows[0]["dayopen"].ToString();
label6.Text = ds.Tables[0].Rows[0]["dayclose"].ToString();
label9.Text = ds.Tables[0].Rows[0]["dayhigh"].ToString();
label8.Text = ds.Tables[0].Rows[0]["daylow"].ToString();
label12.Text = ds.Tables[0].Rows[0]["day5"].ToString();
label11.Text = ds.Tables[0].Rows[0]["day10"].ToString();
label22.Text = ds.Tables[0].Rows[0]["day20"].ToString();
label21.Text = ds.Tables[0].Rows[0]["day60"].ToString();
if (CompareDate(curdate, begindate) > 0)
{
this.Refresh();
sql = "select top " + half + " begintime,dayopen,dayclose,dayhigh,daylow,day5,day10,day20,day60 from "+gpname+" where begintime<='" + curdate + "' order by begintime desc";
huatu(sql, half);
overdate = curdate;
}
}
break;
case Keys.Down:
if (half < 356)
{
half = half * 2;
this.Refresh();
bdate = DateTime.Parse(curdate).AddDays(-half).ToShortDateString();
odate = DateTime.Parse(curdate).AddDays(half).ToShortDateString();
sql = "select begintime,dayopen,dayopen,dayclose,dayhigh,daylow,day5,day10,day20,day60 from "+gpname+" where begintime between '" + bdate + "' and '" + odate + "' order by begintime desc ";
huatu(sql, half);
label2.Text = half.ToString();
}
break;
}
}