1 /* 2 * NPlot - A charting library for .NET 3 * 4 * FinancialDemo.cs 5 * Copyright (C) 2003-2006 Matt Howlett and others. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without modification, 9 * are permitted provided that the following conditions are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright notice, this 12 * list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright notice, 14 * this list of conditions and the following disclaimer in the documentation 15 * and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 26 * OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 30 31 using System; 32 using System.Drawing; 33 using System.Collections; 34 using System.ComponentModel; 35 using System.Windows.Forms; 36 using System.Data; 37 using System.Reflection; 38 39 using NPlot; 40 41 42 namespace NPlotDemo 43 { 44 /// <summary> 45 /// Summary description for BasicDemo. 46 /// </summary> 47 public class FinancialDemo : System.Windows.Forms.Form 48 { 49 private NPlot.Windows.PlotSurface2D volumePS; 50 private NPlot.Windows.PlotSurface2D costPS; 51 private System.Windows.Forms.Button closeButton; 52 53 54 /// <summary> 55 /// Right context menu with non-default functionality. Just take out some functionality. Could also have added some. 56 /// </summary> 57 public class ReducedContextMenu : NPlot.Windows.PlotSurface2D.PlotContextMenu 58 { 59 60 /// <summary> 61 /// Constructor 62 /// </summary> ReducedContextMenu()63 public ReducedContextMenu() 64 : base() 65 { 66 ArrayList menuItems = this.MenuItems; 67 68 // remove show coordinates, and print functionality 69 menuItems.RemoveAt(4); 70 menuItems.RemoveAt(3); 71 menuItems.RemoveAt(1); 72 73 this.SetMenuItems(menuItems); 74 75 } 76 77 } 78 79 FinancialDemo()80 public FinancialDemo() 81 { 82 // 83 // Required for Windows Form Designer support 84 // 85 InitializeComponent(); 86 87 costPS.Clear(); 88 costPS.DateTimeToolTip = true; 89 90 // obtain stock information from xml file 91 DataSet ds = new DataSet(); 92 System.IO.Stream file = 93 Assembly.GetExecutingAssembly().GetManifestResourceStream("NPlotDemo.resources.asx_jbh.xml"); 94 ds.ReadXml(file, System.Data.XmlReadMode.ReadSchema); 95 DataTable dt = ds.Tables[0]; 96 DataView dv = new DataView(dt); 97 98 // create CandlePlot. 99 CandlePlot cp = new CandlePlot(); 100 cp.DataSource = dt; 101 cp.AbscissaData = "Date"; 102 cp.OpenData = "Open"; 103 cp.LowData = "Low"; 104 cp.HighData = "High"; 105 cp.CloseData = "Close"; 106 cp.BearishColor = Color.Red; 107 cp.BullishColor = Color.Green; 108 cp.Style = CandlePlot.Styles.Filled; 109 costPS.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 110 costPS.Add(new Grid()); 111 costPS.Add(cp); 112 costPS.Title = "AU:JBH"; 113 costPS.YAxis1.Label = "Price [$]"; 114 costPS.YAxis1.LabelOffset = 40; 115 costPS.YAxis1.LabelOffsetAbsolute = true; 116 costPS.XAxis1.HideTickText = true; 117 costPS.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.HorizontalDrag()); 118 costPS.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.VerticalDrag()); 119 costPS.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.AxisDrag(false)); 120 costPS.InteractionOccured += new NPlot.Windows.PlotSurface2D.InteractionHandler(costPS_InteractionOccured); 121 costPS.AddAxesConstraint(new AxesConstraint.AxisPosition(PlotSurface2D.YAxisPosition.Left, 60)); 122 123 PointPlot pp = new PointPlot(); 124 pp.Marker = new Marker(Marker.MarkerType.Square, 0); 125 pp.Marker.Pen = new Pen(Color.Red, 5.0f); 126 pp.Marker.DropLine = true; 127 pp.DataSource = dt; 128 pp.AbscissaData = "Date"; 129 pp.OrdinateData = "Volume"; 130 volumePS.Add(pp); 131 volumePS.YAxis1.Label = "Volume"; 132 volumePS.YAxis1.LabelOffsetAbsolute = true; 133 volumePS.YAxis1.LabelOffset = 40; 134 volumePS.AddAxesConstraint(new AxesConstraint.AxisPosition(PlotSurface2D.YAxisPosition.Left, 60)); 135 volumePS.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.AxisDrag(false)); 136 volumePS.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.HorizontalDrag()); 137 volumePS.InteractionOccured += new NPlot.Windows.PlotSurface2D.InteractionHandler(volumePS_InteractionOccured); 138 volumePS.PreRefresh += new NPlot.Windows.PlotSurface2D.PreRefreshHandler(volumePS_PreRefresh); 139 140 this.costPS.RightMenu = new ReducedContextMenu(); 141 142 } 143 OnResize(EventArgs e)144 protected override void OnResize(EventArgs e) 145 { 146 this.costPS.Height = (int)(0.5 * (this.Height)); 147 this.costPS.Width = (int)(this.Width - 35); 148 149 this.volumePS.Top = costPS.Height + 12; 150 this.volumePS.Height = this.Height - (costPS.Height + 25) - 80; 151 this.volumePS.Width = (int)(this.Width - 35); 152 153 base.OnResize(e); 154 } 155 156 #region Windows Form Designer generated code 157 /// <summary> 158 /// Required method for Designer support - do not modify 159 /// the contents of this method with the code editor. 160 /// </summary> InitializeComponent()161 private void InitializeComponent() 162 { 163 this.closeButton = new System.Windows.Forms.Button(); 164 this.volumePS = new NPlot.Windows.PlotSurface2D(); 165 this.costPS = new NPlot.Windows.PlotSurface2D(); 166 this.SuspendLayout(); 167 // 168 // closeButton 169 // 170 this.closeButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); 171 this.closeButton.Location = new System.Drawing.Point(8, 421); 172 this.closeButton.Name = "closeButton"; 173 this.closeButton.TabIndex = 1; 174 this.closeButton.Text = "Close"; 175 this.closeButton.Click += new System.EventHandler(this.closeButton_Click); 176 // 177 // volumePS 178 // 179 this.volumePS.AutoScaleAutoGeneratedAxes = false; 180 this.volumePS.AutoScaleTitle = false; 181 this.volumePS.BackColor = System.Drawing.SystemColors.ControlLightLight; 182 this.volumePS.Legend = null; 183 this.volumePS.Location = new System.Drawing.Point(13, 305); 184 this.volumePS.Name = "volumePS"; 185 this.volumePS.RightMenu = null; 186 this.volumePS.ShowCoordinates = false; 187 this.volumePS.Size = new System.Drawing.Size(606, 109); 188 this.volumePS.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None; 189 this.volumePS.TabIndex = 3; 190 this.volumePS.Title = ""; 191 this.volumePS.TitleFont = new System.Drawing.Font("Arial", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel); 192 this.volumePS.XAxis1 = null; 193 this.volumePS.XAxis2 = null; 194 this.volumePS.YAxis1 = null; 195 this.volumePS.YAxis2 = null; 196 // 197 // costPS 198 // 199 this.costPS.AutoScaleAutoGeneratedAxes = false; 200 this.costPS.AutoScaleTitle = false; 201 this.costPS.BackColor = System.Drawing.SystemColors.ControlLightLight; 202 this.costPS.Legend = null; 203 this.costPS.Location = new System.Drawing.Point(13, 13); 204 this.costPS.Name = "costPS"; 205 this.costPS.RightMenu = null; 206 this.costPS.ShowCoordinates = false; 207 this.costPS.Size = new System.Drawing.Size(606, 285); 208 this.costPS.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None; 209 this.costPS.TabIndex = 2; 210 this.costPS.Title = ""; 211 this.costPS.TitleFont = new System.Drawing.Font("Arial", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel); 212 this.costPS.XAxis1 = null; 213 this.costPS.XAxis2 = null; 214 this.costPS.YAxis1 = null; 215 this.costPS.YAxis2 = null; 216 // 217 // FinancialDemo 218 // 219 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 220 this.ClientSize = new System.Drawing.Size(631, 450); 221 this.Controls.Add(this.volumePS); 222 this.Controls.Add(this.costPS); 223 this.Controls.Add(this.closeButton); 224 this.Name = "FinancialDemo"; 225 this.Text = "BasicDemo"; 226 this.ResumeLayout(false); 227 228 } 229 #endregion 230 231 232 /// <summary> 233 /// Callback for close button. 234 /// </summary> closeButton_Click(object sender, System.EventArgs e)235 private void closeButton_Click(object sender, System.EventArgs e) 236 { 237 this.Close(); 238 } 239 240 241 /// <summary> 242 /// When the costPS chart has changed, this is called which updates the volumePS chart. 243 /// </summary> 244 /// <param name="sender"></param> costPS_InteractionOccured(object sender)245 private void costPS_InteractionOccured(object sender) 246 { 247 DateTimeAxis axis = new DateTimeAxis(costPS.XAxis1); 248 axis.Label = "Date / Time"; 249 axis.HideTickText = false; 250 this.volumePS.XAxis1 = axis; 251 this.volumePS.Refresh(); 252 } 253 254 255 /// <summary> 256 /// When the volumePS chart has changed, this is called which updates hte costPS chart. 257 /// </summary> volumePS_InteractionOccured(object sender)258 private void volumePS_InteractionOccured(object sender) 259 { 260 DateTimeAxis axis = new DateTimeAxis(volumePS.XAxis1); 261 axis.Label = ""; 262 axis.HideTickText = true; 263 this.costPS.XAxis1 = axis; 264 this.costPS.Refresh(); 265 } 266 267 268 /// <summary> 269 /// This is called prior to volumePS refresh to enforce the WorldMin is 0. 270 /// This may have been changed by the axisdrag interaction. 271 /// </summary> 272 /// <param name="sender"></param> volumePS_PreRefresh(object sender)273 private void volumePS_PreRefresh(object sender) 274 { 275 volumePS.YAxis1.WorldMin = 0.0; 276 } 277 278 } 279 } 280