コピペ: OmegaChart 日付固定表示 ChartCanvas.cs 追加修正ポイントには//☆FixedDisplayYMD

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

このコピペの URL

JavaScript での埋め込み

iframe での埋め込み

元のテキスト