Пример #1
0
        public ChartDragInfo(FormulaChart Chart, FormulaHitInfo HitInfo)
        {
            this.HitInfo    = HitInfo;
            this.AreaHeight = new int[Chart.Areas.Count];
            FormulaAxisY axisY = HitInfo.AxisY;

            if ((axisY == null) && (HitInfo.Area != null))
            {
                axisY = HitInfo.Area.AxisY;
            }
            if (axisY != null)
            {
                this.AreaMinY = axisY.MinY;
                this.AreaMaxY = axisY.MaxY;
            }
            for (int i = 0; i < Chart.Areas.Count; i++)
            {
                FormulaArea area = Chart.Areas[i];
                this.AreaHeight[i] = area.Rect.Height;
            }
            this.StartTime = Chart.StartTime;
            this.EndTime   = Chart.EndTime;
        }
Пример #2
0
        /// <summary>
        /// Constructor of ChartDragInfo
        /// </summary>
        /// <param name="Chart">FormulaChart instance</param>
        /// <param name="HitInfo">The Hit information of the dragging start point</param>
        public ChartDragInfo(FormulaChart Chart, FormulaHitInfo HitInfo)
        {
            this.HitInfo = HitInfo;
            AreaHeight   = new int[Chart.Areas.Count];
            FormulaAxisY fay = HitInfo.AxisY;

            if (fay == null && HitInfo.Area != null)
            {
                fay = HitInfo.Area.AxisY;
            }
            if (fay != null)
            {
                AreaMinY = fay.MinY;
                AreaMaxY = fay.MaxY;
            }

            for (int i = 0; i < Chart.Areas.Count; i++)
            {
                FormulaArea fa = Chart.Areas[i];
                AreaHeight[i] = fa.Rect.Height;
            }
            StartTime = Chart.StartTime;
            EndTime   = Chart.EndTime;
        }
Пример #3
0
        //Random Rnd = new Random();
        private void BindChart()
        {
            string Quote1 = tbQuote1.Text;
            string Quote2 = tbQuote2.Text;

            FormulaChart fc = new FormulaChart();

            // Compare two closing data
            fc.AddArea("CmpIndi(" + tbQuote2.Text + ",CLOSE)", 2);
            // Compare two RSI(14) of two stocks
            fc.AddArea("CmpIndi(" + tbQuote2.Text + ",RSI(14))");
            fc.AddArea("SlowSTO(14,3,3)", tbQuote1.Text, 1);
            fc.AddArea("SlowSTO(14,3,3)", tbQuote2.Text, 1);

            // Set skin
            fc.SetSkin(FormulaSkin.RedWhite);

            DataManagerBase dmb = Utils.GetDataManager(Config.DefaultDataManager);

            //MemoryStream ms = new MemoryStream();
            try
            {
                // Bind data
                fc.DataProvider = dmb[Quote1];

                // Add another AxisY to the main panel
                FormulaAxisY fay = fc[0].AddNewAxisY(AxisPos.Left, true, 1);

                // Bind another stock to the second AxisY
                //FormulaData fd = fc[0][1];
                //fd.Transform = Transform.Normal;
                //fd.AxisYIndex = 1;

                fc.ShowCursorLabel = true;

                DataTable dt = new DataTable();
                dt.Columns.Add("Symbol");
                dt.Columns.Add("Name");
                dt.Columns.Add("Last");
                dt.Columns.Add("Date");
                dt.Columns.Add("Open");
                dt.Columns.Add("High");
                dt.Columns.Add("Low");
                dt.Columns.Add("Close");
                dt.Columns.Add("Volume");
                dt.Columns.Add("Change");

                tblChart.Visible = true;
                int Width = (int)ibChart.Width.Value;
                if (Width <= 0)
                {
                    Width = 780;
                }
                int Height = (int)ibChart.Height.Value;
                if (Height <= 0)
                {
                    Height = 850;
                }

                ibChart.ImageUrl = "~/ImageFromCache.aspx?CacheId=" +
                                   fc.SaveToImageStream(Width, Height, ImageFormat.Png,
                                                        Tools.ToIntDef(Request.Form[ibChart.ID + ".x"], 0),
                                                        Tools.ToIntDef(Request.Form[ibChart.ID + ".y"], 0));

                AddDataToTable(dt, fc.DataProvider, fc.CursorPos);
                IDataProvider idp2 = dmb[Quote2];
                idp2.BaseDataProvider = fc.DataProvider;
                AddDataToTable(dt, idp2, fc.CursorPos);

                dgStockData.DataSource = dt;
                dgStockData.DataBind();
            }
            catch (Exception ex)
            {
                lMsg.Text = ex.Message;
            }
        }
Пример #4
0
        protected void Page_Load(object sender, System.EventArgs e)
        {
            int    Height = 800;
            int    Width  = 600;
            Bitmap B;

            try
            {
                long Start = DateTime.Now.Ticks;

                ExchangeIntraday ei        = null;
                DateTime         StartTime = DateTime.Today.AddMonths(-6);
                DateTime         EndTime   = DateTime.Today;

                string Param = Request.QueryString["Span"];
                if (Param != null && Param != "")
                {
                    DataCycle dc;
                    if (Param.Length > 2)
                    {
                        dc = DataCycle.Parse(Param);
                    }
                    else
                    {
                        dc = new DataCycle(DataCycleBase.MONTH, Tools.ToIntDef(Param, 6));
                    }
                    StartTime = EndTime - dc;
                    //StartTime = EndTime.AddMonths(-Tools.ToIntDef(Param,6));
                }

                //Create financial chart instance
                FormulaChart fc = new FormulaChart();

                string Main = Request.QueryString["Main"];
                if (Main == null || Main == "")
                {
                    fc.AddArea("MAIN", 3);
                }
                else
                {
                    fc.AddArea(Main, 3);
                }

                string QuoteCode = Request.QueryString["Code"];
                if (QuoteCode == null)
                {
                    QuoteCode = "";
                }

                DataManagerBase dmb;
                Param = Request.QueryString["Provider"];
                if (Param == null)
                {
                    Param = "";
                }
                if (string.Compare(Param, "DB", true) == 0)
                {
                    //Create DBDataManager , Get stock data from sql server.
                    dmb = new DBDataManager();
                    ((DBDataManager)dmb).AutoYahooToDB =
                        Request.QueryString["His"] != "0" && Config.AutoYahooToDB == 1;
                    if (Request.QueryString["RT"] == "1")
                    {
                        ((DataManagerBase)dmb).DownloadRealTimeQuote = true;
                    }
                }
                else if (Param == "" || string.Compare(Param, "Yahoo", true) == 0)
                {
                    //Create YahooDataManager , Get stock data from yahoo.
                    dmb = new YahooDataManager();
                    ((YahooDataManager)dmb).CacheRoot = HttpRuntime.AppDomainAppPath + "Cache\\";
                    if (Config.IncludeTodayQuote == 1)
                    {
                        ((DataManagerBase)dmb).DownloadRealTimeQuote = true;
                    }
                }
                else
                {
                    string ManagerName = "WebDemos." + Param + "DataManager";
                    Type   t           = Type.GetType(ManagerName);
                    if (t == null)
                    {
                        throw new Exception(ManagerName + " not found!");
                    }
                    dmb = (DataManagerBase)Activator.CreateInstance(t);
                    if (dmb is IntraDataManagerBase)
                    {
                        ei = ExchangeIntraday.GetExchangeIntraday(Utils.GetExchange(QuoteCode));
                        ((CacheDataManagerBase)dmb).CacheTimeSpan = TimeSpan.FromMilliseconds(Config.IntradayCacheTime * 1000);
                        //((CacheDataManagerBase)dmb).EnableFileCache = false;

                        StartTime = ei.GetCurrentTradingDay();
                        EndTime   = StartTime.AddSeconds(3600 * 24 - 1);
                        QuoteCode = Utils.GetPart1(QuoteCode);
                    }
                    dmb.EndTime   = Utils.ToDateDef(Request.QueryString["End"], EndTime);
                    dmb.StartTime = Utils.ToDateDef(Request.QueryString["Start"], StartTime);
                }

                int MainIndex = 0;

                if (Config.SymbolCase == "Upper")
                {
                    QuoteCode = QuoteCode.ToUpper();
                }
                else if (Config.SymbolCase == "Lower")
                {
                    QuoteCode = QuoteCode.ToLower();
                }

                string[] QuoteCodes = QuoteCode.Split(',');
                string   Over       = "";
                QuoteCode = QuoteCodes[0];
                for (int i = 1; i < QuoteCodes.Length; i++)
                {
                    if (Over != "")
                    {
                        Over += ";";
                    }
                    Over += "Compare(" + QuoteCodes[i] + ")";
                }

                Param = Request.QueryString["IND"];
                string[] Inds = null;
                if (Param != null && Param != "")
                {
                    Inds = Param.Split(';');
                }

                //Set stock chart size
                Param = Request.QueryString["Size"];
                try
                {
                    if (Param != null && Param != "")
                    {
                        int i = Param.IndexOf('*');
                        if (i > 0)
                        {
                            Width  = int.Parse(Param.Substring(0, i));
                            Height = int.Parse(Param.Substring(i + 1));
                        }
                        else
                        {
                            Width  = int.Parse(Param);
                            Height = Width * 3 / 4;
                            if (Inds != null && Inds.Length > 1)
                            {
                                Height += Height / 5 * (Inds.Length - 2);
                            }
                            Height += 36;
                        }
                    }
                }
                catch
                {
                }

                CommonDataProvider DataProvider = (CommonDataProvider)dmb[QuoteCode];
                if (DataProvider == null)
                {
                    throw new Exception(QuoteCode + " not found!");
                }
                if (dmb is IntraDataManagerBase)
                {
                    fc.FixedTime = object.Equals(Request.QueryString["Fix"], "1");
                    if (fc.FixedTime)
                    {
                        DataProvider.IntradayInfo = ei;
                    }
                }

                if (DataProvider.Count == 0)
                {
                    throw new Exception("No data found!");
                }
                fc[MainIndex].Formulas[0].Name = QuoteCode;

                //Add MA lines to main stock view
                Param = Request.QueryString["MA"];
                if (Param != null && Param != "")
                {
                    foreach (string s in Param.Split(';'))
                    {
                        if (s != "")
                        {
                            fc[MainIndex].AddFormula("FML.MA(" + s + ")");
                        }
                    }
                }

                //Add EMA lines to main stock view
                Param = Request.QueryString["EMA"];
                if (Param != null && Param != "")
                {
                    foreach (string s in Param.Split(';'))
                    {
                        if (s != "")
                        {
                            fc[MainIndex].AddFormula("FML.EMA(" + s + ")");
                        }
                    }
                }

                //Add override lines to main stock view
                Param = Request.QueryString["OVER"];
                if (Param != null && Param != "")
                {
                    if (Over != "")
                    {
                        Param = Param + ";" + Over;
                    }
                }
                else
                {
                    Param = Over;
                }

                if (dmb is IntraDataManagerBase)
                {
                    string s = DataProvider.GetStringData("LastPrice");
                    if (s != null && s != "")
                    {
                        double d = double.Parse(s);
                        s = "DotLine(" + d.ToString("f2") + ")";
                        if (Param != null && Param != "")
                        {
                            Param += ";" + s;
                        }
                        else
                        {
                            Param = s;
                        }
                    }
                }

                if (Param != null && Param != "")
                {
                    //foreach(string s in Param.Split(';'))
                    string[] ss = Param.Split(';');
                    for (int i = 0; i < ss.Length; i++)
                    {
                        string s = ss[i].ToUpper();
                        if (s != "")
                        {
                            if (s == "AreaBB")
                            {
                                fc[MainIndex].InsertFormula(0, "FML." + s);
                            }
                            else
                            {
                                fc[MainIndex].AddFormula("FML." + s);
                            }
                        }
                    }
                }

                //Add indicators to stock chart
                if (Inds != null)
                {
                    foreach (string s in Inds)
                    {
                        if (s != "")
                        {
                            int k = s.IndexOf("{U}");
                            if (k > 0)
                            {
                                fc.InsertArea(MainIndex, "FML." + s.Substring(0, k));
                                MainIndex++;
                            }
                            else
                            {
                                fc.AddArea("FML." + s);
                            }
                        }
                    }
                }

                //Apply build-in stock chart skin
                Param = Request.QueryString["Skin"];
                if (Param == null || Param == "")
                {
                    Param = Config.DefaultSkin;
                }
                FormulaSkin fs = FormulaSkin.GetSkinByName(Param);
                if (fs != null)
                {
                    if (Config.YAxisFormat != "")
                    {
                        fs.AxisY.Format = Config.YAxisFormat;
                    }

                    if (Config.ShowXAxisInLastArea)
                    {
                        fs.ShowXAxisInLastArea = true;
                    }

                    fc.SetSkin(fs);
                }

                try
                {
                    fc.LatestValueType = (LatestValueType)Enum.Parse(typeof(LatestValueType), Config.LatestValueType, true);
                }
                catch
                {
                }

                //Add compare line to other stocks
                Param = Request.QueryString["COMP"];
                if (Param != null && Param != "")
                {
                    foreach (string s in Param.Split(';', ','))
                    {
                        if (s != "")
                        {
                            fc[MainIndex].AddFormula("COMPARE(" + s + ")");
                        }
                    }
                    fc[MainIndex].AxisY.ShowAsPercent = true;
                    fc[MainIndex].AxisY.Format        = "p1";
                    fc.LatestValueType = LatestValueType.None;
                }

                //Set stock chart time period
                fc.EndTime             = Utils.ToDateDef(Request.QueryString["End"], EndTime);
                fc.StartTime           = Utils.ToDateDef(Request.QueryString["Start"], StartTime);
                DataProvider.DataCycle = DataCycle.Parse(Request.QueryString["Cycle"]);

                //Set X-Axis format
                string XFormat = Request.QueryString["XFormat"];
                if (XFormat != null && XFormat != "")
                {
                    fc.AxisXFormat = XFormat;
                    fc.AllXFormats = null;
                }

                //Set Y-Axis format
                string YFormat = Request.QueryString["YFormat"];
                if (YFormat != null && YFormat != "")
                {
                    fc.AxisYFormat = YFormat;
                }

                //Set X-Axis cycle
                string XCycle = Request.QueryString["XCycle"];
                if (XCycle != null && XCycle != "")
                {
                    fc.DataCycle = DataCycle.Parse(XCycle);
                }

                //Set render type : Bar , Candle or Line
                Param = Request.QueryString["Type"];
                if (Param != null && Param != "")
                {
                    StockRenderType srt;
                    if (Param.Length == 1)
                    {
                        srt = (StockRenderType)int.Parse(Param);
                    }
                    else
                    {
                        srt = (StockRenderType)Enum.Parse(typeof(StockRenderType), Param, true);
                    }
                    fc[MainIndex].StockRenderType = srt;
                }

                //Set Scale type : Normal or Log
                Param = Request.QueryString["Scale"];
                if (Param != null && Param != "")
                {
                    ScaleType st;
                    if (Param.Length == 1)
                    {
                        st = (ScaleType)int.Parse(Param);
                    }
                    else
                    {
                        st = (ScaleType)Enum.Parse(typeof(ScaleType), Param, true);
                    }
                    fc[MainIndex].AxisY.Scale = st;
                }

                //Bottom margin
                Param = Request.QueryString["BMargin"];
                if (Param != null && Param != "")
                {
                    fc[MainIndex].BottomMargin = Tools.ToDoubleDef(Param, 0);
                }

                //Bind stock data
                fc.DataProvider = DataProvider;

                for (int i = 1; i < fc[MainIndex].FormulaDataArray.Count; i++)
                {
                    if (fc[MainIndex][i].ParentFormula.FormulaName.StartsWith("COMPARE"))
                    {
                        FormulaAxisY fay = fc[MainIndex].AddNewAxisY(AxisPos.Left);
                        FormulaData  fd  = fc[MainIndex][i];
                        fd.Transform  = Transform.Normal;
                        fd.AxisYIndex = 1;
                        break;
                    }
                }

                fc[MainIndex].AxisY.MajorTick.ShowLine = Config.ShowMainAreaLineY;
                fc[MainIndex].AxisX.MajorTick.ShowLine = Config.ShowMainAreaLineX;

                //Set indicator line width
                Param = Request.QueryString["Width"];
                if (Param != null && Param != "")
                {
                    float LineWidth = float.Parse(Param);
                    for (int i = 0; i < fc.Areas.Count; i++)
                    {
                        fc[i].LinePen.Width = LineWidth;
                    }
                }
                fc[MainIndex].AxisY.AutoMultiply = false;

                if (Request.QueryString["SV"] == "0")
                {
                    fc.ShowValueLabel = false;
                }
                fc.StickRenderType = Config.StickRenderType;

                Rectangle Rect = new Rectangle(0, 0, Width, Height);
                Param = Request.QueryString["Layout"];
                if (Param == null || Param == "")
                {
                    Param = "Default";
                }
                string[] Layouts = Param.Split(';', ',');
                Layout   L       = Layout.ParseString(Config.Read(Layouts[0] + "Layout", "ChartRect=(0,0,0,0)"), Rect);    //,DataProvider);
                for (int j = 1; j < Layouts.Length; j++)
                {
                    L.Merge(Layout.ParseString(Config.Read(Layouts[j] + "Layout", "ChartRect=(0,0,0,0)"), Rect));                //,DataProvider));
                }
                L.CompanyName = Config.CompanyName;
                L.URL         = Config.URL;
                L.StartTick   = Start;
                fc.Rect       = L.ChartRect;
                B             = fc.GetBitmap(Width, Height, fc.Rect);
                Graphics g = Graphics.FromImage(B);
                L.Render(g, Rect, fc);


                int X = Tools.ToIntDef(Request.QueryString["X"], 0);
                int Y = Tools.ToIntDef(Request.QueryString["Y"], 0);
                if (X > 0 && Y > 0)
                {
                    fc.Rect            = new Rectangle(0, 0, Width, Height);
                    fc.ShowCursorLabel = true;
                    fc.DrawCursor(g, X, Y);
                }
            }
            catch (Exception ex)
            {
                B = new Bitmap(Width, Height);
                Graphics g = Graphics.FromImage(B);

                g.Clear(Color.White);

                StringFormat sf = new StringFormat();
                sf.Alignment     = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;
                g.DrawString(ex.Message, new Font("Verdana", 12),
                             new SolidBrush(Color.FromArgb(196, Color.Black)), new Rectangle(0, 0, Width, Height), sf);
            }

            //Create stock image to Bitmap
            Response.ContentType = "Image/" + Config.ImageFormat;

            //Create chart stream
            MemoryStream ms = new MemoryStream();

            if (Config.ImageFormat == "Gif")
            {
                Type tQuantization = (Type)Application["Quantization"];
                if (tQuantization == null)
                {
                    Assembly[] ass = AppDomain.CurrentDomain.GetAssemblies();
                    foreach (Assembly a in ass)
                    {
                        if (a.FullName.StartsWith("ImageQuantization"))
                        {
                            tQuantization = a.GetType("ImageQuantization.OctreeQuantizer");
                        }
                    }
                    Application["Quantization"] = tQuantization;
                }

                if (tQuantization != null)
                {
                    object[] os;
                    if (Config.TransparentColor.IsEmpty)
                    {
                        os = new object[] { B, ms, Config.GifColors }
                    }
                    ;
                    else
                    {
                        os = new object[] { B, ms, Config.GifColors, Config.TransparentColor }
                    };

                    tQuantization.InvokeMember("Save",
                                               BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod, null, null, os);
                }

                if (ms.Length == 0)
                {
                    B.Save(ms, ImageFormat.Png);
                }
            }
            else if (Config.ImageFormat == "Png")
            {
                B.Save(ms, ImageFormat.Png);
            }
            else if (Config.ImageFormat == "Jpeg")
            {
                B.Save(ms, ImageFormat.Jpeg);
            }

            //Output the chart stream to web browser
            ms.WriteTo(Response.OutputStream);
        }