Practical tips for generating pie-like and bar-chart examples in asp.net

Source: Internet
Author: User
Tags getcolor static class

This paper illustrates the method of generating pie and histogram in asp.net. Share to everyone for your reference. The specific methods are as follows:

A common method of generating graphics:

Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Text;
//
Using System.Data;
Using System.Web.UI.WebControls;
//
Using System.Drawing;
Using System.Drawing.Imaging;

Namespace Tools
{
public static Class Owcimagehelp
{
<summary>
Dynamic generation of histogram and pie-like graphs
</summary>
<param name= "arrValueNames" > Line coordinates the fields to display </param>
<param name= "arrvalues" > ordinate the number to display </param>
<param name= "title" > title </param>
public static void Getzbimage (string[] arrvaluenames, int[] arrvalues, string title)
{
Bitmap Objbitmap = new Bitmap (650, 300);
Graphics Objgraphics;
Objgraphics = Graphics.fromimage (Objbitmap);
Objgraphics.clear (Color.White);
Int[] arrvalues = {40000, 32000, 24000, 30000, 36000, 28000};
string[] arrValueNames = new string[] {"First", "second", "third", "Fourth Time", "Fifth Time", "sixth"};
Objgraphics.drawstring (title, New System.Drawing.Font ("XXFarEastFont-Arial"), Brushes.blue, New PointF (5, 5));
PointF Symbolleg = new PointF (335, 20);
PointF Descleg = new PointF (360, 16);
Draw a graphic of the description section
for (int i = 0; i < arrvaluenames.length; i++)
{
Objgraphics.fillrectangle (New SolidBrush (GetColor (i)), symbolleg.x, Symbolleg.y, 20, 10);
Objgraphics.drawrectangle (Pens.black, symbolleg.x, Symbolleg.y, 20, 10);
Objgraphics.drawstring (Arrvaluenames[i]. ToString (), New System.Drawing.Font ("XXFarEastFont-Arial",), Brushes.black, Descleg);
Symbolleg.y + 15;
Descleg.y + 15;
}
float totalvalues = 0;
for (int i = 0; I <= arrvalues.length-1; i++)
{
Totalvalues + = Arrvalues[i];
}
Draw a rectangular chart.
float rectangleheight = 0;
PointF Recleg = new PointF (A, 200-arrvalues[0]/totalvalues * 300);
for (int i = 0; i < arrvalues.length; i++)
{
Rectangleheight = arrvalues[i]/totalvalues * 300;
Objgraphics.fillrectangle (New SolidBrush (GetColor (i)), (I *) +, 200-rectangleheight, Rectangleheight + 50);
Objgraphics.drawrectangle (Pens.black, (i *) +, 200-rectangleheight, Rectangleheight + 50);
Recleg.y = 200-rectangleheight-14;
Objgraphics.drawstring (Arrvalues[i]. ToString (), New System.Drawing.Font ("XXFarEastFont-Arial",), Brushes.blue, Recleg);
Recleg.x + 35;
}
Draw a circular chart.
float Sglcurrentangle = 0;
float Sgltotalangle = 0;
for (int i = 0; i < arrvalues.length; i++)
{
Sglcurrentangle = arrvalues[i]/totalvalues * 360;
Objgraphics.fillpie (New SolidBrush (GetColor (i)), sglcurrentangle);
Objgraphics.drawpie (Pens.black, Sgltotalangle, Sglcurrentangle);
Sgltotalangle + = Sglcurrentangle;
}
Objbitmap.save (System.Web.HttpContext.Current.Response.OutputStream, imageformat.gif);
}
Defines a color.
private static Color getcolor (int itemindex)
{
Color Objcolor;
if (itemindex = 0)
{
Objcolor = Color.maroon;
}
else if (itemindex = 1)
{
Objcolor = color.red;
}
else if (ItemIndex = 2)
{
Objcolor = Color.gray;
}
else if (ItemIndex = 3)
{
Objcolor = Color.Blue;
}
else if (ItemIndex = 4)
{
Objcolor = Color.orange;
}
else if (ItemIndex = 5)
{
Objcolor = Color.cyan;
}
else if (ItemIndex = 6)
{
Objcolor = Color.bisque;
}
else if (ItemIndex = 7)
{
Objcolor = Color.maroon;
}
else if (ItemIndex = 8)
{
Objcolor = Color.maroon;
}
Else
{
Objcolor = Color.Blue;
}
return objcolor;
}
}
}

Second, create a pie-like Column chart page bzimage.aspx:
Background:
Copy Code code as follows:
Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using BLL;
Using Models;
public partial class GridViewDemo_BZImage:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
Getbziamge ();
}
<summary>
Generate a pie-shaped bar chart
</summary>
public void Getbziamge ()
{
DataTable dt = BLL. Studentbll.selallstudent ();
string[] rows = new String[dt. Rows.Count];
int[] columns = new INT[DT. Rows.Count];
for (int i = 0; i < dt. Rows.Count; i++)
{
Rows[i] = dt. rows[i]["Student name". ToString ();
Columns[i] = convert.toint32 (dt. rows[i]["Salary"]. ToString ());
}
Tools.OWCImageHelp.GetZBImage (Rows, columns, "Student salary query");
}
}

three, the page that shows the pie-like chart:
Front desk:
Copy Code code as follows:
<table style= "width:600px" onmouseover= "Over ()" onmouseout= "Out ()" >
<tr>
<TD style= "HEIGHT:21PX; width:35px "align=" Center ">

</td>
</tr>
</table>

I hope this article will help you with the ASP.net program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.