1
Answer

Need Help With Gdi + asp.net + c# charting

aa

aa

16y
2.7k
1
Hi

I am having a very hard time making charts with the gdi. I have finally managed to make my charts draw and stuff. The problem is this:

Say this is my data I have:

05/03/08 - Correct = 800 Wrong = 100 AssitanceNeeded = 100
06/03/08 - Correct = 100  Wrong = 20 AssitaneNeeded = 10;

So the first problem this data gives me is that the 800 from the correct will go off the screen since my bitmap is 800 by 600(can't be bigger then this).

So I am at stump of how to scale everything so it would fit nicely on the page.

The second problem I have is that I need to make the scale on the left hand side. YOu know the one that would have the units like it would have -500 or something like that to tell the user that at this line is 500units.

So I been reading tutorials and stuff but there not much help. Like first many are written in VB and I can't read VB so I have to use a converter what sometimes fails to convert the stuff. The second problem is that they don't write them for the web its for windows forms  and the third problem is they hardcode all there values so even if both of the above where true it would be still useless to me since my stuff is coming from a database that changes.

So Here is how I want my chart to look like(this is  reason why I could not use a charting program since its a type of graph that I never seen before).

http://img217.imageshack.us/img217/9373/chartcopyxe1.jpg

and here is what my code currently makes

http://img160.imageshack.us/img160/7151/chartvh2.jpg

Basically my chart is a one column stacked chart. The reason for this is when a user does a quiz they can get help on the answer. But it still gets marked right since they are forced to write the answer in to help them remember. So basicly the total correct is minused from the AsstianceNeeded. Then the assitanceNeeded is added ontop of the remaining correct.

Here is my code.


[code]using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Drawing.Imaging;
using System.Data.SqlClient;
using System.Drawing.Drawing2D;
using System.Drawing.Text;

public partial class _Default : System.Web.UI.Page
{


    Guid providerUserKey;

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        /* dropdown with my options in */
        string option = DropDownList1.SelectedValue;
        makeChart(option);
    }
    protected void makeChart(string option)
    {
        /* stored procedure */
        SqlCommand newcommand = new SqlCommand("getChartInfo", getConnection());
        newcommand.CommandType = CommandType.StoredProcedure;

        SqlDataReader newReader;


        newcommand.Parameters.Add("@UserID", SqlDbType.UniqueIdentifier);
        newcommand.Parameters["@UserID"].Value = providerUserKey;

        newcommand.Parameters.Add("@option", SqlDbType.VarChar);
        newcommand.Parameters["@option"].Value = option;

        newcommand.Connection.Open();
        newcommand.ExecuteNonQuery();
        newReader = newcommand.ExecuteReader();


        Bitmap myImage;

        /* make my Bitmap 800 by 600 */
        myImage = new Bitmap(800, 600);

        // Get the graphics context for the bitmap.
        Graphics objGraphics = Graphics.FromImage(myImage);


        // Make sure you do not have          
        // smoothing problems
        objGraphics.SmoothingMode = SmoothingMode.HighQuality;
        objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;


        /* make sure the graph is white */
        objGraphics.Clear(Color.White);


        /* make my brushes one for each column */
        Brush blue = new SolidBrush(Color.Blue);
        Brush red = new SolidBrush(Color.Red);
        Brush green = new SolidBrush(Color.Green);

        int barNumber = 0;
        while (newReader.Read() == true)
        {
            /* go through and get the results from the SP */
            int correct = Convert.ToInt32(newReader["CORRECT"]);
            int wrong = Convert.ToInt32(newReader["Wrong"]);
            int assitance = Convert.ToInt32(newReader["AssitanceNeeded"]);
            string date = newReader["TimeDateStamp"].ToString();

            /* send it to be drawn */
            DrawBar(objGraphics, correct, wrong, assitance, date, barNumber, blue, red, green);
            barNumber++;
        }

        /* make it a jpg so it can be seen */
        Response.ContentType = "image/jpeg";

        /* saves it as Jpeg */
        myImage.Save(Response.OutputStream, ImageFormat.Jpeg);

    }
    protected SqlConnection getConnection()
    {
        /* Declares the connections for the database */
        SqlConnection conn;
        /*Grabs the connection string */
        string connectionString = ConfigurationManager.ConnectionStrings["Japanese"].ConnectionString;
        conn = new SqlConnection(connectionString);
        return conn;
    }
    private void DrawBar(Graphics objGraphics, int correct, int wrong, int assitance, string date,
        int barNumber, Brush brush1, Brush brush2, Brush brush3)
    {
        /* find out how much real correct answers they got(IE how many they did not need help on */
        int newCorrect = correct - assitance;


        /* spaceing from the left */
        int intLeft = (barNumber * 80) + 70;
        /* where the bottom is */
        int intBottom = 550;
        /* find out how high to make it */
        int intHeight = newCorrect;


        /* draw the corret column bar */
        objGraphics.FillRectangle(brush1, intLeft, intBottom - intHeight, 25, intHeight);


        /* figured out the new bottom */
        intBottom = intBottom - intHeight - assitance;

        /* set the height for the next bar */
        intHeight = assitance;

        /* draw the bar */
        objGraphics.FillRectangle(brush3, intLeft, intBottom, 25, intHeight);


        /*  set up everything for the wrong bar */
        intLeft = ((barNumber + 1) * 80) + 25;
        intBottom = 550;
        intHeight = wrong;


        /* draw the wrong bar */
        objGraphics.FillRectangle(brush2, intLeft, intBottom - intHeight, 25, intHeight);


        /* get the dates that will be drawn under the horizontal line */
        objGraphics.DrawString(date, new Font("Verdana", 8, FontStyle.Bold), Brushes.Black, this.Temp, 560);

        /* set the next dates postion */
        this.Temp = this.Temp + 80;

        /* set the veritical scale */
        objGraphics.DrawString("1000", new Font("Verdana", 8, FontStyle.Bold), Brushes.Black, 0, 480);


        // Draw the vertical line
        objGraphics.DrawLine(Pens.Brown, 40, 10, 40, 550);

        // draws the horizontal line
        objGraphics.DrawLine(Pens.Brown, 20, 550, 800, 550);    
    }

    /* get and set property */
    protected int holdKey = 90;
    protected int Temp
    {
        get
        {
            return holdKey;
        }
        set
        {
            holdKey = value;
        }
    }
   

}
[/code]
Answers (1)