C#画K线实现画K线和截图保存

本文介绍了如何使用C#语言在Visual Studio Code中实现K线图的绘制,包括设置K线图的尺寸、坐标以及颜色,并通过查询数据库获取数据,最后展示如何根据日期筛选K线数据并进行屏幕截图保存。代码示例中涉及到数据库查询、图形绘制以及时间日期处理等技术。
摘要由CSDN通过智能技术生成

在这里插入图片描述
string begindate = null;
string curdate = null;
string overdate = null;
int half = 0;
string gpname = null;
int width = 1050;//K线图矩形宽
int height = 400;//K线图矩形高
int leftX = 40;//k线图矩形原始坐标X
int topY = 80;//k线图矩形原始坐标Y
//按钮加载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 Kwidth = width / dt.Rows.Count - 10;
        g.DrawRectangle(p, leftX, topY, 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 + leftX - changdu * 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值