Exemplo n.º 1
0
        public byte[]   MakeChart(ChartData cd, int Width, int Height)
        {
            //Initialize
            ChartControl cc = new ChartControl();

            cc.AddChartData(cd);
            cc.Width     = Width;
            cc.Height    = Height;
            cc.BackColor = Color.White;

            //Make Image
            Image image = cc.GetImage();

            //Save Image to tmp file
            string tmp = Path.GetTempFileName();

            image.Save(tmp, ImageFormat.Png);

            //Read Image from disk and fill byte[]
            byte[]       b;
            BinaryReader br;

            using (Stream s = File.OpenRead(tmp))
            {
                br = new BinaryReader(s);
                b  = br.ReadBytes((int)s.Length);
                br.Close();
            }

            //Delete tmp file
            File.Delete(tmp);

            //Cleanup image
            image.Dispose();

            return(b);
        }
Exemplo n.º 2
0
        public string[] PlotXml(string xml)
        {
            //Set Properties
            string Xml          = xml;
            string RelativePath = (string)Application["RelativePath"];
            string AbsolutePath = (string)Application["PhysicalPath"];

            if (!Directory.Exists((string)Application["PhysicalPath"]))
            {
                Directory.CreateDirectory((string)Application["PhysicalPath"]);
            }

            ArrayList sc   = new ArrayList();
            ArrayList absc = new ArrayList();

            //Make images
            ChartService cs = new ChartService();

            ChartData[]  cds = cs.GetFromXml(xml);
            ChartControl cc;
            ChartData    cd;
            int          width;
            int          height;
            Image        image;

            for (int i = 0; i < cds.Length; i++)
            {
                cd     = cds[i];
                width  = XmlTranslator.Widths[i];
                height = XmlTranslator.Heights[i];

                cc = new ChartControl();
                cc.AddChartData(cd);
                cc.Width     = width;
                cc.Height    = height;
                cc.BackColor = Color.White;

                image = cc.GetImage();
                string filename     = "/plot_" + DateTime.Now.Ticks + ".png";
                string physPath     = AbsolutePath + filename;
                string relativePath = RelativePath + filename;
                image.Save(physPath, ImageFormat.Png);

                absc.Add(physPath);
                sc.Add(relativePath);
            }
            string[] relPaths = (string[])sc.ToArray(typeof(string));

            //Get AbsolutePaths
            this.Session["LastImages"] = (string[])absc.ToArray(typeof(string));

            if (relPaths != null)
            {
                //Add http:// + MachineName to filenames;
                IPHostEntry ihe   = Dns.GetHostByName(this.Server.MachineName);
                string      local = "http://" + ihe.HostName;
                ArrayList   al    = new ArrayList();
                foreach (string s in relPaths)
                {
                    al.Add(local + s);
                }
                return((string[])al.ToArray(typeof(string)));
            }
            else
            {
                return(new string[] {});
            }
        }
Exemplo n.º 3
0
        public string   MakeInnerHtmlChart(ChartData[] cds, int Width, int Height)
        {
            //Check that we have some data
            if (cds == null)
            {
                return("An error occured, could not find any plots!");
            }

            try
            {
                //Get paths, set in global.cs
                string RelativePath = (string)Application["RelativePath"] + "/";
                string AbsolutePath = (string)Application["PhysicalPath"] + "/";

                //Generate unique filename
                string filename = DateTime.Now.Ticks.ToString();

                //Get hostinfo
                IPHostEntry ihe = Dns.GetHostByName(this.Server.MachineName);

                //Initialize variables
                string[]      imgs      = new string[cds.Length];
                StringBuilder sr        = new StringBuilder();
                string[]      datanames = new string[cds.Length];
                string        xmldata;
                string        tabdata;
                ArrayList     files = new ArrayList();

                //Run through ChartData[] and create charts
                for (int i = 0; i < cds.Length; i++)
                {
                    //Initialize chart
                    ChartControl cc = new ChartControl();
                    cc.AddChartData(cds[i]);
                    cc.BackColor = Color.White;

                    //Check X and Y values
                    if (cds[i].X == null || cds[i].Y == null)
                    {
                        continue;
                    }

                    //Check y values
                    bool ok = false;
                    foreach (double[] d in cds[i].Y)
                    {
                        if (d != null)
                        {
                            ok = true;
                        }
                    }
                    if (!ok)
                    {
                        continue;
                    }

                    //Check if the current ChartData has Width and Height set
                    try
                    {
                        if (cds[i].Width != 0)
                        {
                            cc.Width = cds[i].Width;
                        }
                        else
                        {
                            cc.Width = Width;
                        }

                        if (cds[i].Height != 0)
                        {
                            cc.Height = cds[i].Height;
                        }
                        else
                        {
                            cc.Height = Height;
                        }
                    }
                    catch {}

                    //Create Image
                    Image img = cc.GetImage();

                    if (img == null)
                    {
                        continue;
                    }

                    //Save image to disk
                    img.Save(AbsolutePath + filename + "_" + i + ".png", ImageFormat.Png);

                    //Store relative path to image
                    imgs[i] = RelativePath + filename + "_" + i + ".png";

                    //Save ChartData as XML
                    try
                    {
                        xmldata = filename + "_" + i + ".xnc";
                        using (Stream s = File.OpenWrite(AbsolutePath + xmldata))
                        {
                            cds[i].ToXML(s);
                        }
                    }
                    catch
                    {
                        xmldata = null;
                    }

                    //Save ChartData as HTML
                    try
                    {
                        tabdata = filename + "_" + i + ".html";
                        using (Stream s = File.OpenWrite(AbsolutePath + tabdata))
                        {
                            cds[i].ToHtml(s);
                        }
                    }
                    catch
                    {
                        tabdata = null;
                    }

                    //Create HTML to return to client
                    sr.Append("<table cellpadding=0 cellspacing=2><tr><td >");
                    sr.Append("<img src='" + "http://" + ihe.HostName + imgs[i] + "'>\n");

                    if (xmldata != null)
                    {
                        sr.Append("<tr><td align=right><a href='http://" + ihe.HostName + RelativePath + xmldata + "'>Xml format</a> | ");
                    }

                    if (tabdata != null)
                    {
                        sr.Append("<a href='http://" + ihe.HostName + RelativePath + tabdata + "' target='htmldata'>Html format</a></table><br>");
                    }

                    //Files to be deleted at the end of session
                    files.Add(AbsolutePath + filename + "_" + i + ".png");
                    files.Add(AbsolutePath + xmldata);
                    files.Add(AbsolutePath + tabdata);
                }

                //Add to session end delete
                AddToDelete((string[])files.ToArray(typeof(string)));

                //Return HTML
                return(sr.ToString());
            }
            catch (Exception e)
            {
                return("An unknown error occured. Please send the following message to the administrator:<br>" + e.Message + "<br>" + e.StackTrace +
                       "<br>" + cds.Length + ", " + Width + ", " + Height);
            }
        }