Pie Chart - Labelling

Discussion in 'ASP .Net' started by =?Utf-8?B?cmtibmFpcg==?=, Jan 21, 2005.

  1. I created a pie chart in an aspx. However, how can I display the data in the
    piechart itself in the right location? The code is as follows:
    (Please see the definition of float x and float y)


    private void CreatePieChart_a(string pStr_SQL, string
    dataColumnName,string labelColumnName,string title, int width)
    {
    string connString = Cls_DManager.GetOleDbConnection().ToString();
    OleDbConnection myConnection = new OleDbConnection(connString);
    myConnection.Open();

    //string pStr_SQL = "SELECT DISTINCT [" + dataColumnName + "], [" +
    labelColumnName + "] FROM " + tableName;
    OleDbCommand myCommand = new OleDbCommand(pStr_SQL, myConnection);

    // 3. Create/Populate the DataSet
    DataSet ds = new DataSet();
    OleDbDataAdapter myAdapter = new OleDbDataAdapter(myCommand);
    myAdapter.Fill(ds);

    // close the connection
    myConnection.Close();

    // find the total of the numeric data
    float total = 0.0F, tmp;
    int iLoop;
    for (iLoop=0; iLoop < ds.Tables[0].Rows.Count; iLoop++)
    {
    tmp = Convert.ToSingle(ds.Tables[0].Rows[iLoop][dataColumnName]);
    total += tmp;
    }


    // we need to create fonts for our legend and title
    Font fontLegend = new Font("Arial", 10),
    fontTitle = new Font("Arial", 15, FontStyle.Bold);

    // We need to create a legend and title, how big do these need to be?
    // Also, we need to resize the height for the pie chart, respective to the
    // height of the legend and title
    const int bufferSpace = 15;
    int legendHeight = fontLegend.Height * (ds.Tables[0].Rows.Count+1) +
    bufferSpace;
    int titleHeight = fontTitle.Height + bufferSpace;
    int height = width + legendHeight + titleHeight + bufferSpace;
    int pieHeight = width; // maintain a one-to-one ratio

    // Create a rectange for drawing our pie
    Rectangle pieRect = new Rectangle(0, titleHeight, width, pieHeight);

    // Create our pie chart, start by creating an ArrayList of colors
    ArrayList colors = new ArrayList();
    Random rnd = new Random();
    for (iLoop = 0; iLoop < ds.Tables[0].Rows.Count; iLoop++)
    colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255),
    rnd.Next(255))));

    float currentDegree = 0.0F;

    // Create a Bitmap instance
    Bitmap objBitmap = new Bitmap(width, height);
    Graphics objGraphics = Graphics.FromImage(objBitmap);

    SolidBrush blackBrush = new SolidBrush(Color.Black);
    int xxx=0;
    // Put a white backround in
    objGraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width,
    height);
    for (iLoop = 0; iLoop < ds.Tables[0].Rows.Count; iLoop++)
    {
    objGraphics.FillPie((SolidBrush) colors[iLoop], pieRect, currentDegree,
    Convert.ToSingle(ds.Tables[0].Rows[iLoop][dataColumnName]) / total *
    360);
    // increment the currentDegree
    currentDegree +=
    Convert.ToSingle(ds.Tables[0].Rows[iLoop][dataColumnName]) / total * 360;

    string drawString = ds.Tables[0].Rows[iLoop][labelColumnName].ToString();
    // Create font and brush.
    Font drawFont = new Font("Arial", 9);
    SolidBrush drawBrush = new SolidBrush(Color.Black);
    // Create point for upper-left corner of drawing.

    //////////////////////////////////////////////////////////////////////
    float x = currentDegree;
    float y =Convert.ToSingle(ds.Tables[0].Rows[iLoop][dataColumnName]) /
    total * 360;

    StringFormat drawFormat = new StringFormat();
    objGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
    //////////////////////////////////////////////////////////////////////

    }

    // Create the title, centered
    StringFormat stringFormat = new StringFormat();
    stringFormat.Alignment = StringAlignment.Center;
    stringFormat.LineAlignment = StringAlignment.Center;

    objGraphics.DrawString(title, fontTitle, blackBrush,
    new Rectangle(0, 0, width, titleHeight), stringFormat);

    // Create the legend
    objGraphics.DrawRectangle(new Pen(Color.Black, 2), 0, height -
    legendHeight, width, legendHeight);
    for (iLoop = 0; iLoop < ds.Tables[0].Rows.Count; iLoop++)
    {
    objGraphics.FillRectangle((SolidBrush) colors[iLoop], 5,
    height - legendHeight + fontLegend.Height * iLoop + 5, 10, 10);

    objGraphics.DrawString(((string)
    ds.Tables[0].Rows[iLoop][labelColumnName].ToString()) + " - " +
    Convert.ToString(ds.Tables[0].Rows[iLoop][dataColumnName]), fontLegend,
    blackBrush,
    20, height - legendHeight + fontLegend.Height * iLoop + 1);
    }

    // display the total
    objGraphics.DrawString("Total Inspections: " + Convert.ToString(total),
    fontLegend, blackBrush,
    5, height - fontLegend.Height - 5);

    // Since we are outputting a Jpeg, set the ContentType appropriately
    Response.ContentType = "image/jpeg";

    // Save the image to a file
    objBitmap.Save(Response.OutputStream, ImageFormat.Jpeg);

    // clean up...
    objGraphics.Dispose();
    objBitmap.Dispose();
    }



    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion
    }
    }
     
    =?Utf-8?B?cmtibmFpcg==?=, Jan 21, 2005
    #1
    1. Advertising

  2. I worked with someone who used geometry and trig calculations to get the pie
    charts with the proper labels (and clickable to boot). I would google and see
    if anyone has done this type of work in an open source project. The asp.net
    site has a report writer application for download that might have this type
    of algorithm.
    ---

    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA

    ***************************
    Think Outside the Box!
    ***************************

    "rkbnair" wrote:

    > I created a pie chart in an aspx. However, how can I display the data in the
    > piechart itself in the right location? The code is as follows:
    > (Please see the definition of float x and float y)
    >
    >
    > private void CreatePieChart_a(string pStr_SQL, string
    > dataColumnName,string labelColumnName,string title, int width)
    > {
    > string connString = Cls_DManager.GetOleDbConnection().ToString();
    > OleDbConnection myConnection = new OleDbConnection(connString);
    > myConnection.Open();
    >
    > //string pStr_SQL = "SELECT DISTINCT [" + dataColumnName + "], [" +
    > labelColumnName + "] FROM " + tableName;
    > OleDbCommand myCommand = new OleDbCommand(pStr_SQL, myConnection);
    >
    > // 3. Create/Populate the DataSet
    > DataSet ds = new DataSet();
    > OleDbDataAdapter myAdapter = new OleDbDataAdapter(myCommand);
    > myAdapter.Fill(ds);
    >
    > // close the connection
    > myConnection.Close();
    >
    > // find the total of the numeric data
    > float total = 0.0F, tmp;
    > int iLoop;
    > for (iLoop=0; iLoop < ds.Tables[0].Rows.Count; iLoop++)
    > {
    > tmp = Convert.ToSingle(ds.Tables[0].Rows[iLoop][dataColumnName]);
    > total += tmp;
    > }
    >
    >
    > // we need to create fonts for our legend and title
    > Font fontLegend = new Font("Arial", 10),
    > fontTitle = new Font("Arial", 15, FontStyle.Bold);
    >
    > // We need to create a legend and title, how big do these need to be?
    > // Also, we need to resize the height for the pie chart, respective to the
    > // height of the legend and title
    > const int bufferSpace = 15;
    > int legendHeight = fontLegend.Height * (ds.Tables[0].Rows.Count+1) +
    > bufferSpace;
    > int titleHeight = fontTitle.Height + bufferSpace;
    > int height = width + legendHeight + titleHeight + bufferSpace;
    > int pieHeight = width; // maintain a one-to-one ratio
    >
    > // Create a rectange for drawing our pie
    > Rectangle pieRect = new Rectangle(0, titleHeight, width, pieHeight);
    >
    > // Create our pie chart, start by creating an ArrayList of colors
    > ArrayList colors = new ArrayList();
    > Random rnd = new Random();
    > for (iLoop = 0; iLoop < ds.Tables[0].Rows.Count; iLoop++)
    > colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255),
    > rnd.Next(255))));
    >
    > float currentDegree = 0.0F;
    >
    > // Create a Bitmap instance
    > Bitmap objBitmap = new Bitmap(width, height);
    > Graphics objGraphics = Graphics.FromImage(objBitmap);
    >
    > SolidBrush blackBrush = new SolidBrush(Color.Black);
    > int xxx=0;
    > // Put a white backround in
    > objGraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width,
    > height);
    > for (iLoop = 0; iLoop < ds.Tables[0].Rows.Count; iLoop++)
    > {
    > objGraphics.FillPie((SolidBrush) colors[iLoop], pieRect, currentDegree,
    > Convert.ToSingle(ds.Tables[0].Rows[iLoop][dataColumnName]) / total *
    > 360);
    > // increment the currentDegree
    > currentDegree +=
    > Convert.ToSingle(ds.Tables[0].Rows[iLoop][dataColumnName]) / total * 360;
    >
    > string drawString = ds.Tables[0].Rows[iLoop][labelColumnName].ToString();
    > // Create font and brush.
    > Font drawFont = new Font("Arial", 9);
    > SolidBrush drawBrush = new SolidBrush(Color.Black);
    > // Create point for upper-left corner of drawing.
    >
    > //////////////////////////////////////////////////////////////////////
    > float x = currentDegree;
    > float y =Convert.ToSingle(ds.Tables[0].Rows[iLoop][dataColumnName]) /
    > total * 360;
    >
    > StringFormat drawFormat = new StringFormat();
    > objGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
    > //////////////////////////////////////////////////////////////////////
    >
    > }
    >
    > // Create the title, centered
    > StringFormat stringFormat = new StringFormat();
    > stringFormat.Alignment = StringAlignment.Center;
    > stringFormat.LineAlignment = StringAlignment.Center;
    >
    > objGraphics.DrawString(title, fontTitle, blackBrush,
    > new Rectangle(0, 0, width, titleHeight), stringFormat);
    >
    > // Create the legend
    > objGraphics.DrawRectangle(new Pen(Color.Black, 2), 0, height -
    > legendHeight, width, legendHeight);
    > for (iLoop = 0; iLoop < ds.Tables[0].Rows.Count; iLoop++)
    > {
    > objGraphics.FillRectangle((SolidBrush) colors[iLoop], 5,
    > height - legendHeight + fontLegend.Height * iLoop + 5, 10, 10);
    >
    > objGraphics.DrawString(((string)
    > ds.Tables[0].Rows[iLoop][labelColumnName].ToString()) + " - " +
    > Convert.ToString(ds.Tables[0].Rows[iLoop][dataColumnName]), fontLegend,
    > blackBrush,
    > 20, height - legendHeight + fontLegend.Height * iLoop + 1);
    > }
    >
    > // display the total
    > objGraphics.DrawString("Total Inspections: " + Convert.ToString(total),
    > fontLegend, blackBrush,
    > 5, height - fontLegend.Height - 5);
    >
    > // Since we are outputting a Jpeg, set the ContentType appropriately
    > Response.ContentType = "image/jpeg";
    >
    > // Save the image to a file
    > objBitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
    >
    > // clean up...
    > objGraphics.Dispose();
    > objBitmap.Dispose();
    > }
    >
    >
    >
    > #region Web Form Designer generated code
    > override protected void OnInit(EventArgs e)
    > {
    > //
    > // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    > //
    > InitializeComponent();
    > base.OnInit(e);
    > }
    >
    > /// <summary>
    > /// Required method for Designer support - do not modify
    > /// the contents of this method with the code editor.
    > /// </summary>
    > private void InitializeComponent()
    > {
    > this.Load += new System.EventHandler(this.Page_Load);
    > }
    > #endregion
    > }
    > }
    >
    >
     
    =?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN, Jan 21, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. IPaul
    Replies:
    0
    Views:
    628
    IPaul
    Apr 2, 2004
  2. Cliff Harris

    Pie Chart control

    Cliff Harris, Jun 12, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    462
    Ken Cox [Microsoft MVP]
    Jun 12, 2004
  3. =?Utf-8?B?UGF0cmljay5PLklnZQ==?=

    PIE/BAR CHART QUESTIONS?(creating them dynamically)

    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=, Oct 14, 2004, in forum: ASP .Net
    Replies:
    10
    Views:
    3,788
  4. LB

    OWC pie chart

    LB, Mar 31, 2005, in forum: ASP .Net
    Replies:
    9
    Views:
    3,623
  5. Tarek
    Replies:
    6
    Views:
    349
    Chris Hohmann
    Nov 19, 2004
Loading...

Share This Page