Ejemplo n.º 1
0
        public byte[] GetImage(Size size)
        {
            //TODO: Externalize the count
            Dictionary <string, string> data = _accountDao.GetMostActiveUsers(5);


            List <string> dataArray  = new List <string>();
            List <string> labelArray = new List <string>();

            dataArray.AddRange(data.Values);
            labelArray.AddRange(data.Keys);

            BarGraph c = new BarGraph(Color.White);

            c.VerticalTickCount = 2;
            c.ShowLegend        = false;
            c.ShowData          = false;
            c.Height            = size.Height;
            c.Width             = size.Width;
            c.TopBuffer         = 10;
            c.BottomBuffer      = 20;
            c.FontColor         = Color.Black;
            c.CollectDataPoints(labelArray.ToArray(), dataArray.ToArray());

            Bitmap image = c.Draw();
            //TODO: Externalize the temp filepath or do it in memory
            string tempFileName = Path.GetTempFileName();

            image.Save(tempFileName);

            return(File.ReadAllBytes(tempFileName));
        }
        protected void Page_Load(object sender, System.EventArgs e)
        {
            // Set return type to png image format
            Response.ContentType = "image/png";

            string xValues, yValues;

            // Get input parameters from query string
            xValues = Request.QueryString["xValues"];
            yValues = Request.QueryString["yValues"];

            if (xValues != null && yValues != null)
            {
                Bitmap       bmp;
                MemoryStream memStream = new MemoryStream();
                BarGraph     bar       = new BarGraph(Color.White);

                // Set bar color for 7 days
                for (int i = 0; i < 7; i++)
                {
                    bar.SetColor(i, Color.Sienna);
                }

                // Graph settings
                bar.VerticalTickCount = 2;
                bar.ShowLegend        = false;
                bar.ShowData          = true;
                bar.Height            = 119;
                bar.Width             = 195;
                bar.TopBuffer         = 5;
                bar.BottomBuffer      = 15;
                bar.FontColor         = Color.Gray;

                bar.CollectDataPoints(xValues.Split("|".ToCharArray()), yValues.Split("|".ToCharArray()));
                bmp = bar.Draw();

                // Render BitMap Stream Back To Client
                bmp.Save(memStream, ImageFormat.Png);
                memStream.WriteTo(Response.OutputStream);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void Page_Load(object sender, EventArgs e)
        {
            // This is a quick check for referrer server name against host server
            // For best security practice in a deployed application, an authenticating mechanism should be in placed.
            // Image should only be rendered for authenticated users only.

            // set return type to png image format
            Response.ContentType = "image/png";

            string xValues, yValues, chartType, print;
            bool   boolPrint;

            // Get input parameters from query string
            chartType = Request.QueryString["chartType"];
            xValues   = Request.QueryString["xValues"];
            yValues   = Request.QueryString["yValues"];
            print     = Request.QueryString["Print"];

            if (chartType == null)
            {
                chartType = string.Empty;
            }

            // check for printing option
            if (print == null)
            {
                boolPrint = false;
            }
            else
            {
                try
                {
                    boolPrint = Convert.ToBoolean(print);
                }
                catch
                {
                    boolPrint = false;
                }
            }

            if (xValues != null && yValues != null)
            {
                Color bgColor;

                if (boolPrint)
                {
                    bgColor = Color.White;
                }
                else
                {
                    bgColor = Color.White;
                }

                Bitmap       StockBitMap;
                MemoryStream memStream = new MemoryStream();

                switch (chartType)
                {
                case "bar":
                    BarGraph bar = new BarGraph(bgColor);

                    bar.VerticalLabel     = string.Empty;
                    bar.VerticalTickCount = 5;
                    bar.ShowLegend        = false;
                    bar.ShowData          = false;
                    bar.Height            = 400;
                    bar.Width             = 700;

                    bar.CollectDataPoints(xValues.Split("|".ToCharArray()), yValues.Split("|".ToCharArray()));
                    StockBitMap = bar.Draw();
                    break;

                default:
                    PieChart pc = new PieChart(bgColor);

                    pc.CollectDataPoints(xValues.Split("|".ToCharArray()), yValues.Split("|".ToCharArray()));

                    StockBitMap = pc.Draw();

                    break;
                }

                // Render BitMap Stream Back To Client
                StockBitMap.Save(memStream, ImageFormat.Png);
                memStream.WriteTo(Response.OutputStream);
            }
        }