| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598 |
- namespace IndexFormula.Finance
- {
- using IndexFormula.Finance.DataProvider;
- using System;
- using System.ComponentModel;
- using System.Drawing;
- using System.Globalization;
- using System.IO;
- using System.Runtime.InteropServices;
- public class Layout
- {
- private int Bar = 0;
- public Rectangle ChartRect;
- private double[] CLOSE = null;
- private Color[] ColorTextColor = new Color[] { Color.DarkGreen, Color.Black, Color.Maroon };
- public string CompanyName;
- private double[] DATE = null;
- public LayoutLabelCollection Labels = new LayoutLabelCollection();
- private TextAlign NowAlign;
- private Rectangle NowBack = Rectangle.Empty;
- private Color NowBackColor = Color.Empty;
- private Color NowColor = Color.Black;
- private Font NowColorFont = new Font("Verdana", 8f);
- private Font NowFont = new Font("Verdana", 8f);
- private Rectangle NowFrame = Rectangle.Empty;
- private Color NowFrameColor = Color.Empty;
- private string NowIcon = "";
- private Point NowPos = Point.Empty;
- private string NowText = "";
- private bool NowUseColor = false;
- public long StartTick;
- private TypeConverter tcColor = null;
- private TypeConverter tcFont = null;
- public string URL;
- private void AddLabel()
- {
- LayoutLabel ll = new LayoutLabel();
- ll.Back = this.NowBack;
- ll.BackColor = this.NowBackColor;
- ll.Frame = this.NowFrame;
- ll.FrameColor = this.NowFrameColor;
- ll.Pos = this.NowPos;
- ll.Text = this.NowText;
- ll.TextColor = this.NowColor;
- ll.UseColor = this.NowUseColor;
- ll.Icon = this.NowIcon;
- ll.Align = this.NowAlign;
- if (this.NowUseColor)
- {
- ll.TextFont = this.NowColorFont;
- }
- else
- {
- ll.TextFont = this.NowFont;
- }
- this.Labels.Add(ll);
- }
- private void AdjustNowPos(LayoutLabel ll, SizeF sf, ref Rectangle R, ref Point NowPos)
- {
- if (ll.Pos != Point.Empty)
- {
- if (ll.Pos.X < 0)
- {
- if (ll.Align == TextAlign.Right)
- {
- NowPos.X = R.Right + ll.Pos.X;
- }
- else
- {
- NowPos.X = (R.Right + ll.Pos.X) - ((int) sf.Width);
- }
- }
- else
- {
- NowPos.X = R.X + ll.Pos.X;
- }
- if (ll.Pos.Y < 0)
- {
- NowPos.Y = (R.Bottom + ll.Pos.Y) - ((int) sf.Height);
- }
- else
- {
- NowPos.Y = R.Y + ll.Pos.Y;
- }
- }
- }
- public int GetColorIndex(double d1, double d2)
- {
- if (d1 < d2)
- {
- return 0;
- }
- if (d1 == d2)
- {
- return 1;
- }
- return 2;
- }
- public double GetLast()
- {
- if ((this.Bar > 0) && (this.Bar < (this.CLOSE.Length + 1)))
- {
- return this.CLOSE[this.Bar - 1];
- }
- return 0.0;
- }
- private void GetNameValue(string s, out string Name, out string Value)
- {
- int index = s.IndexOf('=');
- if (index > 0)
- {
- Name = s.Substring(0, index);
- Value = s.Substring(index + 1);
- if (Value.StartsWith("("))
- {
- Value = Value.Substring(1);
- }
- if (Value.EndsWith(")"))
- {
- Value = Value.Substring(0, Value.Length - 1);
- }
- }
- else
- {
- Name = s;
- Value = "";
- }
- }
- private string GetOneTag(string s, out string Name, out string Value)
- {
- int num = 0;
- for (int i = 0; i < s.Length; i++)
- {
- if (s[i] == '(')
- {
- num++;
- }
- if (s[i] == ')')
- {
- num--;
- }
- if ((s[i] == ';') && (num == 0))
- {
- this.GetNameValue(s.Substring(0, i), out Name, out Value);
- return s.Substring(i + 1);
- }
- }
- this.GetNameValue(s, out Name, out Value);
- return "";
- }
- private void InternalParse(string s, Rectangle Rect)
- {
- while (s != "")
- {
- string str;
- string str2;
- int num;
- int num2;
- s = this.GetOneTag(s, out str, out str2);
- switch (str)
- {
- case "Font":
- case "ColorFont":
- {
- if (this.tcFont == null)
- {
- this.tcFont = TypeDescriptor.GetConverter(typeof(Font));
- }
- Font font = (Font) this.tcFont.ConvertFromString(null, CultureInfo.InvariantCulture, str2);
- if (str == "Font")
- {
- this.NowFont = font;
- }
- else
- {
- this.NowColorFont = font;
- }
- continue;
- }
- default:
- {
- string str3 = str;
- if (str3 == null)
- {
- continue;
- }
- str3 = string.IsInterned(str3);
- if (str3 == "ChartRect")
- {
- this.ChartRect = this.ParseRect(Rect, str2);
- continue;
- }
- if (str3 == "Align")
- {
- if (str2 == "Right")
- {
- this.NowAlign = TextAlign.Right;
- }
- else
- {
- this.NowAlign = TextAlign.Left;
- }
- continue;
- }
- if (str3 == "Color")
- {
- if (this.tcColor == null)
- {
- this.tcColor = TypeDescriptor.GetConverter(typeof(Color));
- }
- this.NowColor = (Color) this.tcColor.ConvertFromString(null, CultureInfo.InvariantCulture, str2);
- continue;
- }
- if (str3 == "Back")
- {
- this.NowBack = this.ParseRect(Rect, str2);
- this.NowText = "";
- this.NowBackColor = this.NowColor;
- this.AddLabel();
- this.NowBack = Rectangle.Empty;
- continue;
- }
- if (str3 == "Frame")
- {
- this.NowFrame = this.ParseRect(Rect, str2);
- this.NowText = "";
- this.NowFrameColor = this.NowColor;
- this.AddLabel();
- this.NowFrame = Rectangle.Empty;
- continue;
- }
- if (str3 == "Pos")
- {
- this.NowPos = this.ParsePoint(Rect, str2);
- continue;
- }
- if (str3 == "Text")
- {
- this.NowText = str2;
- this.NowUseColor = false;
- this.AddLabel();
- this.NowPos = Point.Empty;
- this.NowText = "";
- continue;
- }
- if (str3 == "Icon")
- {
- this.NowIcon = str2;
- this.AddLabel();
- this.NowPos = Point.Empty;
- this.NowIcon = "";
- continue;
- }
- if (str3 != "ColorText")
- {
- continue;
- }
- num = 0;
- break;
- }
- }
- Label_028C:
- num2 = str2.IndexOf("{", num);
- if (num2 >= 0)
- {
- int index = str2.IndexOf("}", num2);
- if (index > 0)
- {
- this.NowUseColor = false;
- this.NowText = str2.Substring(num, num2 - num);
- this.AddLabel();
- this.NowPos = Point.Empty;
- this.NowUseColor = true;
- this.NowText = str2.Substring(num2, (index - num2) + 1);
- this.AddLabel();
- num = index + 1;
- goto Label_028C;
- }
- }
- }
- }
- public void Merge(Layout L)
- {
- foreach (LayoutLabel label in L.Labels)
- {
- this.Labels.Add(label);
- }
- }
- private Point ParsePoint(Rectangle R, string s)
- {
- string[] strArray = s.Split(new char[] { ',' });
- try
- {
- return new Point(R.X + int.Parse(strArray[0]), R.Y + int.Parse(strArray[1]));
- }
- catch
- {
- return Point.Empty;
- }
- }
- private Rectangle ParseRect(Rectangle R, string s)
- {
- string[] strArray = s.Split(new char[] { ',' });
- try
- {
- if (strArray.Length == 4)
- {
- int num = int.Parse(strArray[0]);
- if (num >= 0)
- {
- R.X += num;
- }
- else
- {
- R.X = (R.Right + num) + 1;
- }
- int num2 = int.Parse(strArray[1]);
- if (num2 >= 0)
- {
- R.Y += num2;
- }
- else
- {
- R.Y = (R.Bottom + num2) + 1;
- }
- int num3 = int.Parse(strArray[2]);
- if (num3 > 0)
- {
- R.Width = num3 - R.Left;
- }
- else
- {
- R.Width = ((R.Width + num3) + 1) - R.Left;
- }
- int num4 = int.Parse(strArray[3]);
- if (num4 > 0)
- {
- R.Height = num4 - R.Top;
- }
- else
- {
- R.Height = ((R.Height + num4) + 1) - R.Top;
- }
- R.Width = Math.Abs(R.Width);
- R.Height = Math.Abs(R.Height);
- return R;
- }
- return R;
- }
- catch
- {
- return R;
- }
- }
- public static Layout ParseString(string s, Rectangle Rect)
- {
- Layout layout = new Layout();
- layout.InternalParse(s, Rect);
- return layout;
- }
- public void Render(Graphics g, Rectangle R, FormulaChart fc)
- {
- this.Render(g, R, fc, Point.Empty);
- }
- public Point Render(Graphics g, Rectangle R, FormulaChart fc, Point NowPos)
- {
- return this.Render(g, R, fc, NowPos, -1);
- }
- public Point Render(Graphics g, Rectangle R, FormulaChart fc, Point NowPos, int Bar)
- {
- this.Bar = Bar;
- foreach (LayoutLabel label in this.Labels)
- {
- if (label.Frame != Rectangle.Empty)
- {
- g.DrawRectangle(new Pen(label.FrameColor), label.Frame);
- }
- if (label.Back != Rectangle.Empty)
- {
- g.FillRectangle(new SolidBrush(label.BackColor), label.Back);
- }
- int colorIndex = 0;
- string text = this.ReplaceText(fc, label.Text, out colorIndex);
- SizeF empty = SizeF.Empty;
- if ((label.Text != null) && (label.Text != ""))
- {
- Color textColor;
- empty = g.MeasureString(text, label.TextFont);
- this.AdjustNowPos(label, empty, ref R, ref NowPos);
- if (label.UseColor)
- {
- textColor = this.ColorTextColor[colorIndex];
- }
- else
- {
- textColor = label.TextColor;
- }
- g.DrawString(text, label.TextFont, new SolidBrush(textColor), (PointF) NowPos);
- }
- if ((label.Icon != null) && (label.Icon != ""))
- {
- string imageFile = FormulaHelper.GetImageFile(label.Icon);
- if (File.Exists(imageFile))
- {
- Image image = Image.FromFile(imageFile);
- empty = new SizeF((float) image.Width, (float) image.Height);
- this.AdjustNowPos(label, empty, ref R, ref NowPos);
- g.DrawImage(image, NowPos);
- }
- }
- if (label.Pos.X >= 0)
- {
- NowPos.X += (int) empty.Width;
- }
- }
- return NowPos;
- }
- private string ReplaceText(FormulaChart fc, string s, out int ColorIndex)
- {
- ColorIndex = 0;
- while (true)
- {
- int index = s.IndexOf('{');
- int num2 = s.IndexOf('}');
- if (num2 <= index)
- {
- return s;
- }
- string str = s.Substring(index + 1, (num2 - index) - 1);
- int length = str.IndexOf(':');
- string format = "";
- string strA = str;
- if (length > 0)
- {
- strA = str.Substring(0, length);
- format = str.Substring(length + 1);
- }
- if (strA == "Company")
- {
- strA = this.CompanyName;
- }
- else if (strA == "URL")
- {
- strA = this.URL;
- }
- else if (strA == "Time")
- {
- strA = (((DateTime.Now.Ticks - this.StartTick) / 0x2710L)).ToString() + "ms";
- }
- else
- {
- IDataProvider dataProvider = fc.DataProvider;
- FormulaArea mainArea = fc.MainArea;
- this.DATE = dataProvider["DATE"];
- this.CLOSE = dataProvider["CLOSE"];
- if ((this.CLOSE == null) || (this.CLOSE.Length == 0))
- {
- return s;
- }
- if (this.Bar < 0)
- {
- for (int i = this.CLOSE.Length - 1; i >= 0; i--)
- {
- if (!double.IsNaN(this.CLOSE[i]))
- {
- this.Bar = i;
- break;
- }
- }
- }
- if (this.Bar < 0)
- {
- this.Bar = 0;
- }
- if (string.Compare(strA, "D") == 0)
- {
- if (format == "")
- {
- format = "dd-MMM-yyyy dddd";
- }
- string stringData = dataProvider.GetStringData("LastTradeTime");
- DateTime def = fc.IndexToDate(this.Bar);
- if ((stringData != null) && (stringData != ""))
- {
- def = FormulaHelper.ToDateDef(stringData, def);
- }
- strA = def.ToString(format);
- }
- else
- {
- string str5 = dataProvider.GetStringData(strA);
- if (str5 != null)
- {
- strA = str5;
- }
- else
- {
- try
- {
- double last = 0.0;
- format = mainArea.AxisY.Format;
- if (string.Compare(strA, "Change", true) == 0)
- {
- last = this.GetLast();
- if (this.Bar < this.CLOSE.Length)
- {
- double num6 = this.CLOSE[this.Bar];
- ColorIndex = this.GetColorIndex(last, num6);
- string str6 = ((num6 - last) / last).ToString("p2");
- if (!str6.StartsWith("-"))
- {
- str6 = "+" + str6;
- }
- if (format == "")
- {
- format = "f" + FormulaHelper.TestBestFormat(num6, 2);
- }
- strA = ((num6 - last)).ToString(format) + "(" + str6 + ")";
- }
- }
- else
- {
- double naN = 0.0;
- if (strA == "LC")
- {
- naN = this.GetLast();
- }
- else
- {
- FormulaData objA = dataProvider[strA];
- double[] data = null;
- if (!object.Equals(objA, null))
- {
- data = objA.Data;
- }
- if ((data != null) && (this.Bar < data.Length))
- {
- last = this.GetLast();
- naN = data[this.Bar];
- if (strA == "VOLUME")
- {
- format = "f0";
- ColorIndex = this.GetColorIndex(last, this.CLOSE[this.Bar]);
- }
- else
- {
- ColorIndex = this.GetColorIndex(last, naN);
- }
- }
- else
- {
- naN = double.NaN;
- }
- }
- if (format == "")
- {
- format = "f" + FormulaHelper.TestBestFormat(naN, 2);
- }
- strA = FormulaHelper.FormatDouble(naN, format);
- }
- }
- catch
- {
- strA = "";
- }
- }
- }
- }
- s = s.Substring(0, index) + strA + s.Substring(num2 + 1);
- }
- }
- public void SetFont(Font F)
- {
- foreach (LayoutLabel label in this.Labels)
- {
- label.TextFont = F;
- }
- }
- }
- }
|