locked
Graph Tabulation in Visual Web Developer RRS feed

  • Question

  • User-719110449 posted
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.Drawing.Text;
    
    public partial class TemperatureGraph : System.Web.UI.Page
    {
        Graphics gfx;
        Bitmap bmp; 
        protected void Page_Load(object sender, EventArgs e)
        {
            bmp = new Bitmap(900, 800);
            gfx = Graphics.FromImage(bmp);
            gfx.Clear(Color.White);
            gfx.SmoothingMode = SmoothingMode.AntiAlias;
            string text = System.IO.File.ReadAllText(@"C:\Documents and Settings\student\Desktop\FYP (Tan Shirlene Chris 071033F)\Project\Website_GreenICT\Temperature.txt");
            string text1 = text.Replace("\r\n", "-");
            string[] str1 = text1.Split('-');
            // Define Points 
            int[] p = new int[str1.Length];
            for (int i = 0; i < p.Length; i++)
            {
                p[i] = (int)Math.Round(double.Parse(str1[i]));
            }
    
            string text2 = System.IO.File.ReadAllText(@"C:\Documents and Settings\student\Desktop\FYP (Tan Shirlene Chris 071033F)\Project\Website_GreenICT\DateTime.txt");
            string text3 = text.Replace("\r\n", "-");
            string[] str2 = text3.Split('-');
            // Define Points 
            int[] k = new int[str2.Length];
            for (int i = 0; i < k.Length; i++)
            {
                k[i] = (int)Math.Round(double.Parse(str2[i]));
            }
            //int[] k = new int[p.Length];
    
            Array.Copy(p, k,p.Length);
            Array.Sort(k);
    
            DrawChart(p, k[k.Length - 1], k[0]);
        }
        private void DrawChart(int[] points, int maxValue, int minValue)
        {
            //Offset (Margin) Values 
            int bottomOffset = 50;
            int topOffset = 30;
            int leftOffset = 60;
            int rightOffset = 10;
    
            // Taking care of some bookwork (declaring/initializing variables) 
            int maxDataPoints = points.Length;
            int chartHeight = bmp.Height - bottomOffset;
            int chartWidth = bmp.Width - rightOffset;
    
            // Adjustable Values 
            double adjustedMax = maxValue * .10 + maxValue;
            double adjustedMin = minValue - .50 * minValue;
            double adjustVerticalRatio = (chartHeight - topOffset) / adjustedMax;
            double adjustHorizontalRatio = ((chartWidth - leftOffset) / (maxDataPoints - 1));
    
            Pen chartPen = new Pen(Color.Orange, 3);
            Pen gridLine = new Pen(Color.LightGray, 1);
    
            int minYpos = chartHeight - topOffset;
            int maxYpos = 10;
    
            // Drawing the Lines 
            for (int i = 0; i < maxDataPoints - 1; i++)
            {
                int xPos = Convert.ToInt32(i * adjustHorizontalRatio) + leftOffset;
                int xPos2 = Convert.ToInt32((i + 1) * adjustHorizontalRatio) + leftOffset;
    
                int yPos = Convert.ToInt32(chartHeight - adjustVerticalRatio * points[i]);
                int yPos2 = Convert.ToInt32(chartHeight - adjustVerticalRatio * points[i + 1]);
    
                if (points[i] == minValue)
                {
                    minYpos = yPos;
                }
    
                if (points[i] == maxValue)
                {
                    maxYpos = yPos;
                }
    
                gfx.DrawLine(gridLine, new Point(xPos2, topOffset), new Point(xPos2, chartHeight));
                gfx.DrawLine(chartPen, new Point(xPos, yPos), new Point(xPos2, yPos2));
    
                gfx.DrawString(i.ToString(), new Font("Arial", 8), new SolidBrush(Color.Gray), new Point(xPos - 4, chartHeight + 10));
            }
    
            //Draw Border Lines 
            Pen borderLine = new Pen(Color.DarkGray, 2);
    
            //Left Border 
            gfx.DrawLine(borderLine, new Point(leftOffset, chartHeight), new Point(leftOffset, topOffset));
    
            //Bottom Border 
            gfx.DrawLine(borderLine, new Point(leftOffset, chartHeight), new Point(chartWidth, chartHeight));
    
            //Right Border 
            gfx.DrawLine(borderLine, new Point(chartWidth, chartHeight), new Point(chartWidth, topOffset));
    
            //Top Border 
            gfx.DrawLine(borderLine, new Point(leftOffset, topOffset), new Point(chartWidth, topOffset));
    
            //Drawing Vertical Min/Max Values 
            gfx.DrawString(maxValue.ToString(), new Font("Arial", 8), new SolidBrush(Color.Gray), new Point(leftOffset - 25, maxYpos));
            gfx.DrawString(minValue.ToString(), new Font("Arial", 8), new SolidBrush(Color.Gray), new Point(leftOffset - 25, minYpos));
            gfx.DrawLine(gridLine, new Point(leftOffset - 25, minYpos), new Point(chartWidth, minYpos));
            gfx.DrawLine(gridLine, new Point(leftOffset - 25, maxYpos), new Point(chartWidth, maxYpos));
    
            // Title 
            gfx.DrawString("[ Temperature Values ]", new Font("Arial", 10, FontStyle.Bold), new SolidBrush(Color.FromArgb(0, 102, 204)), new Point(leftOffset + 60, topOffset - 30));
    
            //Finalizing and Cleaning Up 
            Response.ContentType = "image/jpeg";
            bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
            bmp.Dispose();
            gfx.Dispose();
            Response.End();
        }
    }
    

    Above are my exisiting codes to tabulate Readings.

    X-axis showing the Temperature readings itself.(from the textfile also)

    Y-axis showing the Number of readings inside the textfile that is adding on every min when the page refresh itself.

    This particular textfiles only has the readings.

    I have created a new textfiles containing readings together with the date and time.

    eg.

    22.8 11/11/2009 2:11:34 PM

    22.3 11/11/2009 3:34:13 AM

    ------------------------------------------------

    so on so forth in the textfiles.

    instead of printing the index of the readings i would like to print the corresponding datetime instead plotted on the graph.

    where should i edit the codes on?

     


      

    Friday, November 13, 2009 4:19 AM

Answers

  • User187056398 posted

    I see you have a Linq reference which means you are using .Net 3.5.

    There is a free Microsoft Chart component you could use that would make things easier.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 13, 2009 11:07 AM