Beispiel #1
0
        private void listView_Click(object sender, SelectionChangedEventArgs e)
        {
            // Companies id = (Companies)e.AddedItems[0];
            // string name = id.Symbol;
            if (lvUsers.SelectedIndex != -1)
            {
                string date;
                string fileName = @"myplot2.png";

                Companies c = (Companies)lvUsers.SelectedItem;

                try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(String.Format("http://ichart.finance.yahoo.com/table.csv?s={0}&ignore=.csv", c.Symbol));
                    request.Method = WebRequestMethods.Http.Head;
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    response.Close();
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        MessageBox.Show("שגיאה: מידע פננסי עבור החברה שנבחרה לא קיים.");
                        this.Graph.Source = null;
                        return;
                    }
                }

                catch
                {
                    MessageBox.Show("שגיאה: מידע פננסי עבור החברה שנבחרה לא קיים.");
                    this.Graph.Source = null;
                    return;
                }

                //REngine engine;
                REngine.SetEnvironmentVariables();
                // engine = REngine.GetInstance();
                engine.Initialize();

                List <string> dates  = new List <string>();
                List <float>  p_list = new List <float>();

                for (int i = 14; i > 0; i--)
                {
                    if (DateTime.Now.Subtract(new TimeSpan(i, 0, 0, 0)).DayOfWeek.ToString() != "Sunday" && DateTime.Now.Subtract(new TimeSpan(i, 0, 0, 0)).DayOfWeek.ToString() != "Saturday")
                    {
                        date = "" + DateTime.Now.Subtract(new TimeSpan(i, 0, 0, 0)).Year.ToString() + "-" + DateTime.Now.Subtract(new TimeSpan(i, 0, 0, 0)).Month.ToString() + "-" + DateTime.Now.Subtract(new TimeSpan(i, 0, 0, 0)).Day.ToString();
                        dates.Add(date);
                    }
                }


                string temp = "getYahooStockUrl <- function(symbol, start, end, type = \"d\") {\n start <- as.Date(start)\n end <- as.Date(end)\n url <-\"http://ichart.finance.yahoo.com/table.csv?s=%s&a=%d&b=%s&c=%s&d=%d&e=%s&f=%s&g=%s&ignore=.csv\"\n sprintf(url,\ntoupper(symbol),\nas.integer(format(start, \"%m\")) - 1,\nformat(start, \"%d\"),\nformat(start, \"%Y\"),\nas.integer(format(end, \"%m\")) - 1, \nformat(end, \"%d\"),\nformat(end, \"%Y\"),\ntype)} \ndata<- read.csv(getYahooStockUrl(\"" + c.Symbol + "\", \"" + dates.First().ToString() + "\", \"" + dates.Last().ToString() + "\"),\nstringsAsFactors = FALSE)";

                DataFrame obj = engine.Evaluate(temp).AsDataFrame();


                //engine.Evaluate("plot(data$Close,type=\"l\",col=\"red\")");

                if (File.Exists(fileName))
                {
                    this.Graph.Source = null;
                    File.Delete(fileName);
                }

                //CharacterVector uVector = engine.CreateCharacterVector(u_date_list_h);
                engine.SetSymbol("date", obj["Date"]);

                //CharacterVector pVector = engine.CreateCharacterVector(help);
                engine.SetSymbol("value", obj["Close"]);

                //engine.SetSymbol("obj", obj);



                CharacterVector fileNameVector = engine.CreateCharacterVector(new[] { fileName });
                engine.SetSymbol("fileName", fileNameVector);


                engine.Evaluate("png(filename=fileName, width=6, height=6, units='in', res=100)");
                engine.Evaluate("date <- as.Date(date)");
                engine.Evaluate("family <- as.factor(data[,4])");
                engine.Evaluate("par(bg = \"transparent\")");
                engine.Evaluate("plot(value~date,pch=16, col=family, ann=FALSE)");
                obj.Close();
                engine.Evaluate("dev.off()");



                //engine.Dispose();
                var bitmap = new BitmapImage();
                var stream = File.OpenRead(fileName);
                bitmap.BeginInit();
                bitmap.CacheOption  = BitmapCacheOption.OnLoad;
                bitmap.StreamSource = stream;
                bitmap.EndInit();
                stream.Close();
                stream.Dispose();
                this.Graph.Source = bitmap;


                //MessageBox.Show(Convert.ToString(listView.SelectedIndices[0]));
            }
        }