Pastebin: OmegaChart Elliot波動 ChartCanvas.cs 追加修正ポイントには//☆Elliott

Format
Plain text
Post date
2017-12-22 07:32
Publication Period
Unlimited
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. using System.Diagnostics;
  7. using Zanetti.Indicators;
  8. using Zanetti.Data;
  9. using Zanetti.Forms;
  10. namespace Zanetti.UI
  11. {
  12. /// <summary>
  13. /// </summary>
  14. internal class ChartCanvas : System.Windows.Forms.UserControl
  15. {
  16. private AbstractBrand _brand;
  17. private ChartDrawing _drawing;
  18. private ChartTitle _title;
  19. private System.Windows.Forms.HScrollBar _scrollBar;
  20. private Label _complementaryLabel;
  21. private int _diffX = 0;
  22. private int _diffY = 0;
  23. private int _destX = 0;
  24. private int _pivotX = 0;
  25. private int _destY = 0;
  26. private int _pivotY = 0;
  27. /// <summary>
  28. /// 必要なデザイナ変数です。
  29. /// </summary>
  30. private System.ComponentModel.Container components = null;
  31. public ChartCanvas()
  32. {
  33. // この呼び出しは、Windows.Forms フォーム デザイナで必要です。
  34. InitializeComponent();
  35. // TODO: InitializeComponent 呼び出しの後に初期化処理を追加します。
  36. this.BackColor = Env.Preference.BackBrush.Color;
  37. this.SetStyle(ControlStyles.AllPaintingInWmPaint|ControlStyles.DoubleBuffer, true);
  38. _drawing = new ChartDrawing(this);
  39. }
  40. /// <summary>
  41. /// 使用されているリソースに後処理を実行します。
  42. /// </summary>
  43. protected override void Dispose( bool disposing )
  44. {
  45. if( disposing )
  46. {
  47. if(components != null)
  48. {
  49. components.Dispose();
  50. }
  51. }
  52. base.Dispose( disposing );
  53. }
  54. #region コンポーネント デザイナで生成されたコード
  55. /// <summary>
  56. /// デザイナ サポートに必要なメソッドです。このメソッドの内容を
  57. /// コード エディタで変更しないでください。
  58. /// </summary>
  59. private void InitializeComponent()
  60. {
  61. this._scrollBar = new System.Windows.Forms.HScrollBar();
  62. _title = new ChartTitle();
  63. this.SuspendLayout();
  64. //
  65. // _scrollBar
  66. //
  67. this._scrollBar.Dock = System.Windows.Forms.DockStyle.Bottom;
  68. this._scrollBar.Enabled = false;
  69. this._scrollBar.LargeChange = 2;
  70. this._scrollBar.Location = new System.Drawing.Point(0, 83);
  71. this._scrollBar.Maximum = 4;
  72. this._scrollBar.Name = "_scrollBar";
  73. this._scrollBar.Size = new System.Drawing.Size(100, 17);
  74. this._scrollBar.TabIndex = 0;
  75. this._scrollBar.ValueChanged += new System.EventHandler(this.OnScrollBarValueChanged);
  76. _title.Location = new Point(0, 0);
  77. _title.Size = new Size(this.Width, Env.Layout.HeaderHeight);
  78. //
  79. // ChartCanvas
  80. //
  81. this.Controls.Add(this._scrollBar);
  82. this.Controls.Add(this._title);
  83. this.MouseUp += new MouseEventHandler(OnMouseUp);
  84. this.Name = "ChartCanvas";
  85. this.Size = new System.Drawing.Size(100, 100);
  86. this.ResumeLayout(false);
  87. _title.BringToFront();
  88. }
  89. #endregion
  90. public Label ComplementaryLabel {
  91. get {
  92. if(_complementaryLabel==null) {
  93. _complementaryLabel = new Label();
  94. _complementaryLabel.Visible = false;
  95. _complementaryLabel.Font = Env.Preference.DefaultFont;
  96. _complementaryLabel.TextAlign = ContentAlignment.MiddleLeft;
  97. _complementaryLabel.BorderStyle = BorderStyle.FixedSingle;
  98. _complementaryLabel.BackColor = Color.FromKnownColor(KnownColor.Info);
  99. _complementaryLabel.ForeColor = Color.FromKnownColor(KnownColor.InfoText);
  100. this.Controls.Add(_complementaryLabel);
  101. }
  102. return _complementaryLabel;
  103. }
  104. }
  105. public ChartTitle ChartTitle {
  106. get {
  107. return _title;
  108. }
  109. }
  110. public ChartDrawing DrawingEngine {
  111. get {
  112. return _drawing;
  113. }
  114. }
  115. public int BodyHeight {
  116. get {
  117. return this.Height - _scrollBar.Height;
  118. }
  119. }
  120. public void LoadBrand(AbstractBrand br, bool preserve_date) {
  121. int date = -1;
  122. if(preserve_date && _brand!=null) {
  123. DataFarm f = _brand.ReserveFarm();
  124. if(!f.IsEmpty) {
  125. Debug.Assert(_drawing.FirstDateIndex>=0 && _drawing.FirstDateIndex<f.TotalLength);
  126. if(_drawing.FirstDateIndex + Env.Layout.DisplayColumnCount >= f.TotalLength)
  127. date = -1; //最終データが見えているときは「最後が見えている」状態を維持
  128. else
  129. date = f.GetByIndex(_drawing.FirstDateIndex).Date;
  130. }
  131. }
  132. _brand = br;
  133. AdjustScrollBar();
  134. if(_scrollBar.Enabled) {
  135. DataFarm f = br.ReserveFarm();
  136. int v = date==-1? GetTotalDataLength() - Env.Layout.DisplayColumnCount : f.DateToIndex(date);
  137. if(v<0) v = 0;
  138. if(v>_scrollBar.Maximum-_scrollBar.LargeChange+1) v = _scrollBar.Maximum-_scrollBar.LargeChange+1;
  139. _scrollBar.Value = v;
  140. }
  141. _drawing.SetBrand(br);
  142. //_title.InitUI();
  143. }
  144. public AbstractBrand GetBrand() {
  145. return _brand;
  146. }
  147. public int FirstDateIndex {
  148. get {
  149. return _drawing.FirstDateIndex;
  150. }
  151. }
  152. public void MoveToLatest() {
  153. AdjustScrollBar();
  154. int v = 0;
  155. if(_scrollBar.Enabled) v = GetTotalDataLength() - Env.Layout.DisplayColumnCount;
  156. if(v<0) v = 0;
  157. _scrollBar.Value = v;
  158. Invalidate(false);
  159. }
  160. public void SetDateIndex(int first, int cursor) {
  161. _scrollBar.Value = first;
  162. DataFarm f = _brand.ReserveFarm();
  163. if(f.IsEmpty)
  164. _drawing.UpdateDateLineIndex(-1);
  165. else
  166. _drawing.UpdateDateLineIndex(Math.Min(cursor, f.FilledLength-1));
  167. Invalidate(true);
  168. }
  169. public void ForceVisibleDate(int date, bool cut) {
  170. DataFarm f = _brand.ReserveFarm();
  171. if(f.IsEmpty) return;
  172. int index = f.DateToIndex(date);
  173. //スクロールしないと見えない
  174. if(_drawing.FirstDateIndex>index || index>=_drawing.FirstDateIndex+Env.Layout.DisplayColumnCount || cut) {
  175. int nv = index - Env.Layout.DisplayColumnCount/2; //大体真ん中に表示
  176. if(nv<0) nv = 0;
  177. if(nv>=f.TotalLength) nv = f.TotalLength-1;
  178. _scrollBar.Value = nv;
  179. }
  180. _drawing.UpdateDateLineIndex(index);
  181. Invalidate(false);
  182. }
  183. public void ReloadFromPreference() {
  184. Preference pref = Env.Preference;
  185. Color bc = pref.BackBrush.Color;
  186. Color fc = pref.TextColor;
  187. this.BackColor = bc;
  188. this.ForeColor = fc;
  189. if(_complementaryLabel!=null) _complementaryLabel.Font = pref.DefaultFont;
  190. //InitOscillatorGroupUI();
  191. _drawing.ClearScale();
  192. }
  193. public void ResetLayout() {
  194. //_oscillatorGroup.Visible = Env.Options.ShowOscillator;
  195. AdjustScrollBar(); //これは価格帯出来高の表示設定が変化したときだけでもよい
  196. _drawing.ClearScale();
  197. _title.InitUI();
  198. }
  199. //メッセージ処理がらみ
  200. protected override void OnLoad(EventArgs e) {
  201. base.OnLoad (e);
  202. Preference pref = Env.Preference;
  203. }
  204. protected override void OnPaint(PaintEventArgs e) {
  205. base.OnPaint (e);
  206. if(this.DesignMode || _brand==null) return;
  207. _drawing.PaintMain(e.Graphics, e.ClipRectangle);
  208. if(_currentFreeLine!=null && _currentFreeLine.PivotHasEnoughDistanceTo(this.PointToClient(Control.MousePosition))) {
  209. IntPtr hdc = e.Graphics.GetHdc();
  210. _currentFreeLine.Draw(Env.Layout.ChartBodyRect, hdc);
  211. e.Graphics.ReleaseHdc(hdc);
  212. }
  213. //☆Elliott
  214. if (_currentElliott != null && _currentElliott.PivotHasEnoughDistanceTo(this.PointToClient(Control.MousePosition)))
  215. {
  216. IntPtr hdc = e.Graphics.GetHdc();
  217. _currentElliott.Draw(Env.Layout.ChartBodyRect, hdc);
  218. e.Graphics.ReleaseHdc(hdc);
  219. }
  220. }
  221. protected override void OnResize(EventArgs e) {
  222. base.OnResize (e);
  223. if(!this.DesignMode && _drawing!=null && _brand!=null && Env.Frame.WindowState!=FormWindowState.Minimized) {
  224. bool last_is_visible = !_scrollBar.Enabled || _scrollBar.Value+_scrollBar.LargeChange>=_scrollBar.Maximum;
  225. _drawing.ClearScale();
  226. AdjustScrollBar();
  227. this.SuspendLayout();
  228. Preference pref = Env.Preference;
  229. //_oscillatorGroup.Top = this.BodyHeight - _oscillatorGroup.Height - 8;
  230. //_oscillatorGroup.Left = this.Width - Env.Layout.RemarkAreaWidth+5;
  231. if(!_brand.ReserveFarm().IsEmpty && last_is_visible) { //最新データ見えているときのリサイズはその状態を維持
  232. int v = _brand.ReserveFarm().TotalLength - Env.Layout.DisplayColumnCount;
  233. if(v<0) v = 0;
  234. _scrollBar.Value = v; //このままValueChangedイベントも誘発
  235. }
  236. Invalidate(false);
  237. this.ResumeLayout();
  238. }
  239. }
  240. protected override void OnMouseMove(MouseEventArgs ev) {
  241. base.OnMouseMove (ev);
  242. if(_drawing==null) return;
  243. Preference pref = Env.Preference;
  244. //隠れてしまうIndicatorテキストの表示・非表示
  245. Point p = new Point(ev.X, ev.Y);//this.PointToClient(Control.MousePosition);
  246. if(_complementaryLabel!=null && _complementaryLabel.Visible) {
  247. if(!new Rectangle(_complementaryLabel.Location, _complementaryLabel.Size).Contains(p))
  248. _complementaryLabel.Visible = false;
  249. }
  250. else {
  251. ArrayList ar = _drawing.TipEntries;
  252. if(ar!=null) {
  253. foreach(ComplementaryTextEntry e in ar) {
  254. if(e.rect.Contains(p)) {
  255. Label l = this.ComplementaryLabel;
  256. l.Location = new Point(e.rect.X-2, e.rect.Y-1);
  257. l.Size = new Size(e.rect.Size.Width+5, e.rect.Size.Height+2);
  258. l.Text = e.text;
  259. l.Visible = true;
  260. l.BringToFront();
  261. l.Invalidate();
  262. break;
  263. }
  264. }
  265. }
  266. }
  267. int offset = -1;
  268. if(p.X >= 0 && p.X < Env.Layout.ChartAreaWidth) {
  269. offset = (p.X-Env.Layout.CandleMiddleOffset) / Env.Layout.DatePitch;
  270. DataFarm farm = _brand.ReserveFarm();
  271. if(!farm.IsEmpty && _drawing.FirstDateIndex+offset>=farm.TotalLength) offset = -1;
  272. }
  273. //横線
  274. if(Env.Preference.MouseTrackingLineMode==MouseTrackingLineMode.Full) {
  275. int my = _drawing.NormalizeByYobine(ev.Y);
  276. if(my!=_drawing.PriceLine._lastDrawn) {
  277. int h = Env.Layout.DefaultTextHeight;
  278. Rectangle yr = new Rectangle(0, _drawing.PriceLine._lastDrawn-h/2, this.Width, h);
  279. Invalidate(yr, false);
  280. yr = new Rectangle(0, my-h/2, this.Width, h);
  281. Invalidate(yr, false);
  282. }
  283. _drawing.PriceLine._nextToBeDrawn = my;
  284. }
  285. //必要なエリアのInvalidate
  286. int ld = _drawing.DateLine._lastDrawn;
  287. if(offset==-1) {
  288. if(ld!=-1) {
  289. Rectangle r = Env.Layout.CurrentValueRect;
  290. Invalidate(r, false);
  291. int t = ld - _drawing.FirstDateIndex;
  292. r = new Rectangle((t-1)*Env.Layout.DatePitch, 0, Env.Layout.DatePitch*2, this.Height);
  293. Invalidate(r, false);
  294. }
  295. _drawing.UpdateDateLineIndex(-1);
  296. }
  297. else {
  298. if(_drawing.FirstDateIndex+offset!=ld) {
  299. Rectangle r = Env.Layout.CurrentValueRect;
  300. //r.X += ChartDrawing.VALUEWINDOW_HEADER_WIDTH;
  301. Invalidate(r, false);
  302. //価格帯出来高
  303. if(Env.Preference.ShowAccumulativeVolume)
  304. Invalidate(Env.Layout.AccumulativeVolumeRect, false);
  305. //節の数値のこともあるので広めにInvalidateする
  306. if(ld!=-1) {
  307. int t = ld - _drawing.FirstDateIndex;
  308. //Debug.WriteLine(String.Format("prev offset={0}", t));
  309. r = new Rectangle((t-1)*Env.Layout.DatePitch, 0, Env.Layout.DatePitch*2, this.Height);
  310. Invalidate(r, false);
  311. }
  312. r = new Rectangle((offset-1)*Env.Layout.DatePitch, 0, Env.Layout.DatePitch*2, this.Height);
  313. Invalidate(r, false);
  314. }
  315. _drawing.UpdateDateLineIndex(_drawing.FirstDateIndex + offset);
  316. }
  317. //今引いている直線の再描画
  318. if(_currentFreeLine!=null) {
  319. Invalidate(_currentFreeLine.GetInclusion(this.ClientRectangle), false);
  320. _currentFreeLine.Destination = p;
  321. }
  322. //左クリックしながら、マウスを動かした場合、コピー中と見なす。
  323. if (_diffX != 0 && _diffY != 0 && ev.Button == MouseButtons.Left && Win32.GetAsyncKeyState(System.Windows.Forms.Keys.ShiftKey) < 0 & 0x8000 != 0)
  324. {
  325. _currentFreeLine = new FreeLine(new Point(ev.X, ev.Y), new Point(ev.X - _diffX, ev.Y - _diffY));
  326. }
  327. //Controlキーを押しながら、マウスで左クリックしている時は、ピボット・コピー中と見なす。
  328. if (_diffX != 0 && _diffY != 0 && ev.Button == MouseButtons.Left && Win32.GetAsyncKeyState(System.Windows.Forms.Keys.ControlKey) < 0 & 0x8000 != 0)
  329. {
  330. int x, y = 0;
  331. if (_pivotX < _destX)//画面左から右へと引いたフリーライン
  332. {
  333. if ((_pivotX + _destX) / 2 > ev.X)//軸となる点は旧_dest
  334. {
  335. x = _destX;
  336. y = _destY;
  337. }
  338. else//軸となる点は旧_pivot
  339. {
  340. x = _pivotX;
  341. y = _pivotY;
  342. }
  343. }
  344. else//画面右から左へと引いたフリーライン
  345. {
  346. if ((_pivotX + _destX) / 2 > ev.X)//軸となる点は旧_pivot
  347. {
  348. x = _pivotX;
  349. y = _pivotY;
  350. }
  351. else//軸となる点は旧_dest
  352. {
  353. x = _destX;
  354. y = _destY;
  355. }
  356. }
  357. _currentFreeLine = new FreeLine(new Point(x, y), new Point(ev.X, ev.Y));
  358. }
  359. //接近した線があればそれをクリア
  360. bool near_line_found = false;
  361. foreach(FreeLine line in _drawing.FreeLines) {
  362. double d = line.GetDistance(p);
  363. FreeLine.LineDrawMode m = d<3? FreeLine.LineDrawMode.Hilight : FreeLine.LineDrawMode.Normal;
  364. if(line.DrawMode!=m) {
  365. line.DrawMode = m;
  366. Invalidate(line.GetInclusion(this.ClientRectangle), false);
  367. }
  368. if(!near_line_found) near_line_found = m==FreeLine.LineDrawMode.Hilight;
  369. }
  370. this.Cursor = near_line_found? Cursors.Hand : Cursors.Default;
  371. }
  372. private void OnMouseUp(object sender, MouseEventArgs args) {
  373. if(args.Button==MouseButtons.Right) {
  374. ContextMenu m = Env.Frame.CreateContextMenu();
  375. m.Show(this, new Point(args.X, args.Y));
  376. }
  377. else if(args.Button==MouseButtons.Left) {
  378. if(_currentFreeLine!=null) {
  379. if(_drawing.FreeLineCount==10) {
  380. Util.Warning(Env.Frame, "線は1銘柄につき10本までしか引けません");
  381. Invalidate();
  382. }
  383. else if(Env.FreeLines.Count==1000) {
  384. Util.Warning(Env.Frame, "線は全部で1000本までしか引けません");
  385. Invalidate();
  386. }
  387. else if(_currentFreeLine.PivotHasEnoughDistanceTo(_currentFreeLine.Destination)) {
  388. _drawing.FixFreeLine(_currentFreeLine);
  389. }
  390. _currentFreeLine = null;
  391. }
  392. if(_drawing.RemoveHighlitedFreeLines()) Invalidate(); //削除されたやつがあればInvalidate
  393. }
  394. }
  395. protected override bool IsInputChar(char charCode) {
  396. return false;
  397. }
  398. private void AdjustScrollBar() {
  399. if(_brand==null || _brand.ReserveFarm().IsEmpty) {
  400. _scrollBar.Enabled = false;
  401. }
  402. else {
  403. Preference pref = Env.Preference;
  404. int total = GetTotalDataLength();
  405. int display = Env.Layout.DisplayColumnCount;
  406. if(display>=total) {
  407. _scrollBar.Enabled = false;
  408. _scrollBar.Value = 0;
  409. _drawing.FirstDateIndex = 0;
  410. }
  411. else {
  412. int v = _scrollBar.Value;
  413. _scrollBar.LargeChange = display;
  414. _scrollBar.Maximum = total-1;
  415. //Debug.WriteLine(String.Format("Adjusted disp={0} all={1} v={2}", display, all, v));
  416. if(v > _scrollBar.Maximum - display) _scrollBar.Value = _scrollBar.Maximum - display + 1;
  417. _scrollBar.Enabled = true;
  418. }
  419. }
  420. }
  421. private int GetTotalDataLength() {
  422. return _brand.ReserveFarm().TotalLength;
  423. }
  424. private void OnScrollBarValueChanged(object sender, EventArgs args) {
  425. if(this.DesignMode) return;
  426. int v = _scrollBar.Value;
  427. _drawing.FirstDateIndex = v;
  428. //Debug.WriteLine(String.Format("ValueChange max={0} lc={1} v={2}", _scrollBar.Maximum, _scrollBar.LargeChange, v));
  429. _drawing.ClearScale();
  430. Invalidate();
  431. }
  432. /*
  433. private void OnOscillatorGroupCheckedChanged(object sender, EventArgs args) {
  434. int i = _oscillatorGroupBoxes.IndexOf(sender);
  435. Debug.Assert(i!=-1);
  436. _drawing.OscillatorGroup = Env.CurrentIndicators.GetOscillatorGroupAt(i);
  437. Invalidate();
  438. }
  439. */
  440. //FreeLine関係
  441. private FreeLine _currentFreeLine;
  442. private FreeLine _currentElliott;//☆Elliott
  443. protected override void OnMouseDown(MouseEventArgs e) {
  444. base.OnMouseDown (e);
  445. if(e.Button==MouseButtons.Left){
  446. if(_brand.ReserveFarm().IsEmpty) return;
  447. foreach (FreeLine line in _drawing.FreeLines)
  448. {
  449. if (line.DrawMode == FreeLine.LineDrawMode.Hilight && e.Button == MouseButtons.Left)
  450. {
  451. _diffX = line.Destination.X - line.Pivot.X;
  452. _diffY = line.Destination.Y - line.Pivot.Y;
  453. _destX = line.Destination.X;
  454. _destY = line.Destination.Y;
  455. _pivotX = line.Pivot.X;
  456. _pivotY = line.Pivot.Y;
  457. }
  458. }
  459. _currentFreeLine = new FreeLine(new Point(e.X, e.Y));
  460. }
  461. }
  462. protected override void OnDoubleClick(EventArgs e)
  463. {
  464. base.OnDoubleClick(e);
  465. var ev = (MouseEventArgs)e;
  466. _currentFreeLine = new FreeLine(new Point(0, ev.Y), new Point(ev.X, ev.Y));
  467. }
  468. public void ClearCurrentFreeLine() {
  469. if(_currentFreeLine!=null) {
  470. Invalidate(_currentFreeLine.GetInclusion(Env.Layout.ChartBodyRect), false);
  471. _currentFreeLine = null;
  472. }
  473. }
  474. //DOJIMA用半日足修正
  475. #if DOJIMA
  476. protected override void OnDoubleClick(EventArgs e) {
  477. base.OnDoubleClick (e);
  478. if(Env.CurrentIndicators.Format!=ChartFormat.HalfDaily) return;
  479. int index = _drawing.DateLine._lastDrawn;
  480. DailyDataFarm f = _brand.ReserveFarm() as DailyDataFarm;
  481. if(index<0 || index>=f.FilledLength) return;
  482. Dojima.ModifyHalfDayData dlg = new Dojima.ModifyHalfDayData();
  483. dlg.InitUI(f.GetByIndex(index), Dojima.DojimaUtil.HalfDailyDataFarmCache.Get(f), index);
  484. if(dlg.ShowDialog(Env.Frame)==DialogResult.OK) {
  485. Invalidate(true); //全画面再描画
  486. }
  487. }
  488. #endif
  489. }
  490. }
Download Printable view

URL of this paste

Embed with JavaScript

Embed with iframe

Raw text