首页 > 代码库 > C#下如何用NPlot绘制期货股票K线图(3):设计要显示的股票价格图表窗口并定义相应类的成员及函数

C#下如何用NPlot绘制期货股票K线图(3):设计要显示的股票价格图表窗口并定义相应类的成员及函数

[内容简介]

上一篇介绍了要显示K线图所需要的数据结构,及要动态显示K线图,需要动态读取数据文件必需的几个功能函数.本篇介绍要显示蜡烛图所用到的窗口界面设计及对应类定义.下面分述如下:

[窗口界面] 见下图

技术分享

 

上图中对应各周期功能按钮和标签进行相应拖放操作就是了, 不再多述 .倒是图表区为一个Nplot控件,命名:myPlot.

[窗口类定义]

  1. 界面部分自动生成的代码文件:

///<ctp1.Designer.cs>

///K线图窗口界面代码此代码由VS自动生成。

//------------------------------------------------------------------------------

 

namespace Ctp

{

partialclassFormCtpThread

{

///<summary>

/// Required designer variable.

///</summary>

private System.ComponentModel.IContainer components = null;

 

///<summary>

/// Clean up any resources being used.

///</summary>

///<param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

protectedoverridevoid Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

 

#region Windows Form Designer generated code

 

///<summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

///</summary>

privatevoid InitializeComponent()

{

this.menuStrip1 = new System.Windows.Forms.MenuStrip();

this.M5 = new System.Windows.Forms.Button();

this.myPlot = new NPlot.Windows.PlotSurface2D();

this.M1 = new System.Windows.Forms.Button();

this.M15 = new System.Windows.Forms.Button();

this.M30 = new System.Windows.Forms.Button();

this.H1 = new System.Windows.Forms.Button();

this.D1 = new System.Windows.Forms.Button();

this.W1 = new System.Windows.Forms.Button();

this.Mn1 = new System.Windows.Forms.Button();

this.label_symbol = new System.Windows.Forms.Label();

this.label1 = new System.Windows.Forms.Label();

this.textBox1 = new System.Windows.Forms.TextBox();

this.enlarge = new System.Windows.Forms.Button();

this.shrink = new System.Windows.Forms.Button();

this.labelTime = new System.Windows.Forms.Label();

this.button3 = new System.Windows.Forms.Button();

this.buttonRoll = new System.Windows.Forms.Button();

this.textBox2 = new System.Windows.Forms.TextBox();

this.buttonMove = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// menuStrip1

//

this.menuStrip1.Location = new System.Drawing.Point(0, 0);

this.menuStrip1.Name = "menuStrip1";

this.menuStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System;

this.menuStrip1.Size = new System.Drawing.Size(1049, 24);

this.menuStrip1.TabIndex = 1;

this.menuStrip1.Text = "menuStrip1";

//

// M5

//

this.M5.AutoSize = true;

this.M5.CausesValidation = false;

this.M5.Location = new System.Drawing.Point(337, 0);

this.M5.Name = "M5";

this.M5.Size = new System.Drawing.Size(39, 24);

this.M5.TabIndex = 2;

this.M5.TabStop = false;

this.M5.Text = "5";

this.M5.UseVisualStyleBackColor = true;

this.M5.Click += new System.EventHandler(this.M5_Click);

//

// myPlot

//

this.myPlot.AutoScaleAutoGeneratedAxes = false;

this.myPlot.AutoScaleTitle = false;

this.myPlot.BackColor = System.Drawing.Color.White;

this.myPlot.DateTimeToolTip = true;

this.myPlot.Dock = System.Windows.Forms.DockStyle.Fill;

this.myPlot.ForeColor = System.Drawing.Color.White;

this.myPlot.Legend = null;

this.myPlot.LegendZOrder = -1;

this.myPlot.Location = new System.Drawing.Point(0, 24);

this.myPlot.Name = "myPlot";

this.myPlot.RightMenu = null;

this.myPlot.ShowCoordinates = true;

this.myPlot.Size = new System.Drawing.Size(1049, 414);

this.myPlot.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;

this.myPlot.TabIndex = 1;

this.myPlot.Text = "myPlotSurface2D1";

this.myPlot.Title = "";

this.myPlot.TitleFont = new System.Drawing.Font("Arial", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);

this.myPlot.XAxis1 = null;

this.myPlot.XAxis2 = null;

this.myPlot.YAxis1 = null;

this.myPlot.YAxis2 = null;

//

// M1

//

this.M1.AutoSize = true;

this.M1.CausesValidation = false;

this.M1.Location = new System.Drawing.Point(301, 0);

this.M1.Name = "M1";

this.M1.Size = new System.Drawing.Size(39, 24);

this.M1.TabIndex = 4;

this.M1.TabStop = false;

this.M1.Text = "1";

this.M1.UseVisualStyleBackColor = true;

this.M1.Click += new System.EventHandler(this.M1_Click);

//

// M15

//

this.M15.AutoSize = true;

this.M15.CausesValidation = false;

this.M15.Location = new System.Drawing.Point(373, 0);

this.M15.Name = "M15";

this.M15.Size = new System.Drawing.Size(39, 24);

this.M15.TabIndex = 6;

this.M15.TabStop = false;

this.M15.Text = "15";

this.M15.UseVisualStyleBackColor = true;

this.M15.Click += new System.EventHandler(this.M15_Click);

//

// M30

//

this.M30.AutoSize = true;

this.M30.CausesValidation = false;

this.M30.Location = new System.Drawing.Point(410, 0);

this.M30.Name = "M30";

this.M30.Size = new System.Drawing.Size(39, 24);

this.M30.TabIndex = 5;

this.M30.TabStop = false;

this.M30.Text = "30";

this.M30.UseVisualStyleBackColor = true;

this.M30.Click += new System.EventHandler(this.M30_Click);

//

// H1

//

this.H1.AutoSize = true;

this.H1.CausesValidation = false;

this.H1.Location = new System.Drawing.Point(448, 0);

this.H1.Name = "H1";

this.H1.Size = new System.Drawing.Size(39, 24);

this.H1.TabIndex = 8;

this.H1.TabStop = false;

this.H1.Text = "1";

this.H1.UseVisualStyleBackColor = true;

this.H1.Click += new System.EventHandler(this.H1_Click);

//

// D1

//

this.D1.AutoSize = true;

this.D1.CausesValidation = false;

this.D1.Location = new System.Drawing.Point(486, 0);

this.D1.Name = "D1";

this.D1.Size = new System.Drawing.Size(39, 24);

this.D1.TabIndex = 7;

this.D1.TabStop = false;

this.D1.Text = "1";

this.D1.UseVisualStyleBackColor = true;

this.D1.Click += new System.EventHandler(this.D1_Click);

//

// W1

//

this.W1.AutoSize = true;

this.W1.CausesValidation = false;

this.W1.Location = new System.Drawing.Point(524, 0);

this.W1.Name = "W1";

this.W1.Size = new System.Drawing.Size(39, 24);

this.W1.TabIndex = 9;

this.W1.TabStop = false;

this.W1.Text = "1";

this.W1.UseVisualStyleBackColor = true;

this.W1.Click += new System.EventHandler(this.W1_Click);

//

// Mn1

//

this.Mn1.AutoSize = true;

this.Mn1.CausesValidation = false;

this.Mn1.Location = new System.Drawing.Point(561, 0);

this.Mn1.Name = "Mn1";

this.Mn1.Size = new System.Drawing.Size(39, 24);

this.Mn1.TabIndex = 10;

this.Mn1.TabStop = false;

this.Mn1.Text = "1";

this.Mn1.UseVisualStyleBackColor = true;

this.Mn1.Click += new System.EventHandler(this.Mn1_Click);

//

// label_symbol

//

this.label_symbol.AutoSize = true;

this.label_symbol.Location = new System.Drawing.Point(60, 6);

this.label_symbol.Name = "label_symbol";

this.label_symbol.Size = new System.Drawing.Size(41, 12);

this.label_symbol.TabIndex = 11;

this.label_symbol.Text = "label1";

//

// label1

//

this.label1.AutoSize = true;

this.label1.Location = new System.Drawing.Point(5, 6);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(29, 12);

this.label1.TabIndex = 12;

this.label1.Text = "商品";

//

// textBox1

//

this.textBox1.Location = new System.Drawing.Point(742, 2);

this.textBox1.Name = "textBox1";

this.textBox1.Size = new System.Drawing.Size(104, 21);

this.textBox1.TabIndex = 13;

//

// enlarge

//

this.enlarge.FlatStyle = System.Windows.Forms.FlatStyle.System;

this.enlarge.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.enlarge.Location = new System.Drawing.Point(601, 0);

this.enlarge.Name = "enlarge";

this.enlarge.Size = new System.Drawing.Size(24, 23);

this.enlarge.TabIndex = 14;

this.enlarge.TabStop = false;

this.enlarge.Text = "+";

this.enlarge.UseVisualStyleBackColor = true;

this.enlarge.Click += new System.EventHandler(this.enlarge_Click);

//

// shrink

//

this.shrink.FlatStyle = System.Windows.Forms.FlatStyle.System;

this.shrink.Font = new System.Drawing.Font("Arial", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.shrink.Location = new System.Drawing.Point(625, 0);

this.shrink.Name = "shrink";

this.shrink.Size = new System.Drawing.Size(24, 23);

this.shrink.TabIndex = 15;

this.shrink.TabStop = false;

this.shrink.Text = "-";

this.shrink.UseVisualStyleBackColor = true;

this.shrink.Click += new System.EventHandler(this.shrink_Click);

//

// labelTime

//

this.labelTime.AutoSize = true;

this.labelTime.Location = new System.Drawing.Point(182, 6);

this.labelTime.Name = "labelTime";

this.labelTime.Size = new System.Drawing.Size(41, 12);

this.labelTime.TabIndex = 16;

this.labelTime.Text = "label2";

//

// button3

//

this.button3.Location = new System.Drawing.Point(974, 0);

this.button3.Name = "button3";

this.button3.Size = new System.Drawing.Size(75, 23);

this.button3.TabIndex = 17;

this.button3.Text = "button3";

this.button3.UseVisualStyleBackColor = true;

this.button3.Click += new System.EventHandler(this.button3_Click);

//

// buttonRoll

//

this.buttonRoll.FlatStyle = System.Windows.Forms.FlatStyle.System;

this.buttonRoll.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

this.buttonRoll.Location = new System.Drawing.Point(689, 0);

this.buttonRoll.Name = "buttonRoll";

this.buttonRoll.Size = new System.Drawing.Size(38, 23);

this.buttonRoll.TabIndex = 19;

this.buttonRoll.TabStop = false;

this.buttonRoll.Text = "滚动";

this.buttonRoll.UseVisualStyleBackColor = true;

this.buttonRoll.Click += new System.EventHandler(this.buttonRoll_Click);

//

// textBox2

//

this.textBox2.Location = new System.Drawing.Point(862, 2);

this.textBox2.Name = "textBox2";

this.textBox2.Size = new System.Drawing.Size(100, 21);

this.textBox2.TabIndex = 20;

this.textBox2.Visible = false;

//

// buttonMove

//

this.buttonMove.Location = new System.Drawing.Point(651, 0);

this.buttonMove.Name = "buttonMove";

this.buttonMove.Size = new System.Drawing.Size(37, 23);

this.buttonMove.TabIndex = 21;

this.buttonMove.Text = "平移";

this.buttonMove.UseVisualStyleBackColor = true;

this.buttonMove.Click += new System.EventHandler(this.buttonMove_Click);

//

// FormCtpThread

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(1049, 438);

this.Controls.Add(this.buttonMove);

this.Controls.Add(this.textBox2);

this.Controls.Add(this.buttonRoll);

this.Controls.Add(this.button3);

this.Controls.Add(this.labelTime);

this.Controls.Add(this.shrink);

this.Controls.Add(this.enlarge);

this.Controls.Add(this.textBox1);

this.Controls.Add(this.label1);

this.Controls.Add(this.label_symbol);

this.Controls.Add(this.Mn1);

this.Controls.Add(this.W1);

this.Controls.Add(this.H1);

this.Controls.Add(this.D1);

this.Controls.Add(this.M15);

this.Controls.Add(this.M30);

this.Controls.Add(this.M1);

this.Controls.Add(this.myPlot);

this.Controls.Add(this.M5);

this.Controls.Add(this.menuStrip1);

this.KeyPreview = true;

this.Name = "FormCtpThread";

this.Text = "FormCtpThread";

this.Load += new System.EventHandler(this.FormCtp_Load);

this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FormCtp_KeyDown);

this.ResumeLayout(false);

this.PerformLayout();

 

}

 

#endregion

 

private System.Windows.Forms.MenuStrip menuStrip1;

private System.Windows.Forms.Button M5;

private NPlot.Windows.PlotSurface2D myPlot;

private System.Windows.Forms.Button M1;

private System.Windows.Forms.Button M15;

private System.Windows.Forms.Button M30;

private System.Windows.Forms.Button H1;

private System.Windows.Forms.Button D1;

private System.Windows.Forms.Button W1;

private System.Windows.Forms.Button Mn1;

private System.Windows.Forms.Label label_symbol;

private System.Windows.Forms.Label label1;

private System.Windows.Forms.TextBox textBox1;

private System.Windows.Forms.Button enlarge;

private System.Windows.Forms.Button shrink;

private System.Windows.Forms.Label labelTime;

private System.Windows.Forms.Button button3;

private System.Windows.Forms.Button buttonRoll;

private System.Windows.Forms.TextBox textBox2;

private System.Windows.Forms.Button buttonMove;

//private NPlot.Windows.PlotSurface2D myplot;

}

}

  1. 蜡烛图表类的显示部分代码:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Text;

using System.IO;

using System.Data;

using System.Drawing;

using System.Globalization;

using System.Drawing.Drawing2D;

using System.Linq;

using System.Collections;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Runtime.Remoting;

using StockAnalyse;

using INIRead;

using NPlot;

using System.Runtime.InteropServices;

using System.Threading;

 

/////////////////////////////////////////////////////////////////////////

///20160806        觉海QQ:806935610创建该文件

///@brief 蜡烛图显示类的定义文件: ///@ctp.cs

///类定义名称:class FormCtpThread

///20160806

///此版本已经可以自动更新价格,但不能滚动K线

///20160904

///已经实现K价格图表自动更新

/////////////////////////////////////////////////////////////////////////

namespace Ctp

{

publicpartialclassFormCtpThread : Form

{

 

//使用多线程计时器

private System.Timers.Timer timer = new System.Timers.Timer(1000);

///<summary>

/// /////////////////////////////成员

///</summary>

publicList<QuotesInfo> DataSymbol { get; set; }

publicList<StockInfo> DataStock { get; set; }

publicList<StockInfo> DataStockReverse { get; set; }

String period;

StockAnalyse.TimeFrame timeFrame = StockAnalyse.TimeFrame.D1;

String stockName;

double xMin, xMax, yMin, yMax;

int xInitShowBars = 300;

int xShowBars = 300;//k线最大数

CandlePlot cp = newCandlePlot();

 

string pathCtp { get; set; }

string file { get; set; }

public FormCtpThread()

{

InitializeComponent();

//MessageBox.Show("PK");

}

public FormCtpThread(string _file)

{

InitializeComponent();

file = _file;

//this.label_symbol.Text = file;

//MessageBox.Show("PK");

}

 

 

 

privatevoid FormCtp_Load(object sender, EventArgs e)

{

 

OpenChartFile(file);

 

this.MouseWheel += newMouseEventHandler(FormCtp_MouseWheel);

timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);

timer.Enabled = true;

timer.AutoReset = true; //是否不断重复定时器操作

 

 

}

void FormCtp_MouseWheel(object sender, MouseEventArgs e)

{

if (e.Delta > 0)

{

// this.Text = "正在向上滚动滑轮";

ChangeSize(1);

}

elseif (e.Delta < 0)

{

// this.Text = "正在向下滚动滑轮";

ChangeSize(-1);

}

}

void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

{

//模拟的做一些耗时的操作

//System.Threading.Thread.Sleep(2000);

labelTime.Text = DateTime.Now.ToString();//调用内容,并用lable1显示出来。。。

OpenChartFileLastBar(file);

if (!isMove)

KMove(ref xBegin, ref xEnd, ref xShowBars, 0);

 

}

}

C#下如何用NPlot绘制期货股票K线图(3):设计要显示的股票价格图表窗口并定义相应类的成员及函数