Asp. NET dynamically create a histogram and pie chart

Source: Internet
Author: User
Tags getcolor tostring
When doing web project development, you will inevitably encounter dynamic creation of histograms or pie charts. Now use the .net technology to create them dynamically. Since I like football more, I am also a fan of Luneng, so I used Luneng's home fans as an example. A Shandong native in Xi'an hopes that Luneng can win the championship in advance! !! Haha, far away ...

The foreground HTML code for dynamically creating histograms and pie charts is as follows:

<Body>
<Form id = "Form1" method = "post" runat = "server">
<Table width = "517" border = "0" height = "255">
Tr <tr>
<Td align = "middle"> <img src = "ChartText.aspx"> </ td>
</ Tr>
<Tr>
<Td height = "20" align = "middle"> Example of dynamically creating graphics in ASP.NET </ td>
</ Tr>
</ Table>
</ Form>
</ Body>
The effect of the program after running is as follows:

The background code is as follows:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging; // Add a reference to Imaging.
A
namespace WebApplication1
{
/// <summary>
// // Summary description of ChartText.
/// Author: Shadow
// // </ summary>
Chart Text: System.Web.UI.Page
{{
Private private void Page_Load (object sender, System.EventArgs e)
{{{{
Bitmap objBitMap = new Bitmap (400, 240);
Graphics objGraphics;
ObjGraphics = Graphics.FromImage (objBitMap);
ObjGraphics.Clear (Color.White);
Int [] arrValues = {40000,32000,24000,30000,36000,28000};
String [] arrValueNames = newstring [] {"first time", "second time", "third time", "fourth time", "fifth time", "sixth time"};
ObjGraphics.DrawString ("Shandong Luneng home fans statistics (person)",
New Font ("宋体", 16), Brushes.Blue, new PointF (5, 5));
PointF symbolLeg = new PointF (335, 20);
PointF descLeg = new PointF (360, 16);
/ / Draw the graphics of the explanatory part
(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 Font ("宋体", 10), Brushes.Black, descLeg);
__ symbolLeg.Y + = 15;
DescLeg.Y + = 15;
}}}

Float TotalValues = 0;
(Int i = 0; i <= arrValues.Length-1; i ++)
This is the case {
Total Values Total Values + = arrValues [i];
}}
Draw a rectangular diagram.
Float Rectangleheight = 0;
PointF recLeg = new PointF (12,200-arrValues [0] / TotalValues * 300);
For for (int i = 0; i <arrValues.Length; i ++)
{{}
Rectangleheight = arrValues [i] / TotalValues * 300;
ObjGraphics.FillRectangle (new SolidBrush (GetColor (i)), (i * 35) + 15, 200-Rectangleheight, 20,
Rectangleheight + 50);
ObjGraphics.DrawRectangle (Pens.Black, (i * 35) + 15, 200-Rectangleheight, 20, Rectangleheight + 50);
RecLeg.Y = 200-Rectangleheight-14;
ObjGraphics.DrawString (arrValues [i] .ToString (), new Font ("宋体", 10), Brushes.Blue, recLeg);
RecLeg.X + = 35;
}}
// // Draw a circle diagram.
Float sglCurrentAngle = 0;
Float sglTotalAngle = 0;
(Int i = 0; i <arrValues.Length; i ++)
This is the case {
SglCurrentAngle = arrValues [i] / TotalValues * 360;
ObjGraphics.FillPie (new SolidBrush (GetColor (i)), 220, 95, 100, 100, sglTotalAngle, sglCurrentAngle);
ObjGraphics.DrawPie (Pens.Black, 220, 95, 100, 100, sglTotalAngle, sglCurrentAngle);
SglTotalAngle + = sglCurrentAngle;
}}
ObjBitMap.Save (Response.OutputStream, ImageFormat.Gif);
}}
A
#Region Web Form Designer Generated Code
Protected override void OnInit (EventArgs e)
{{
... //
/ // CODEGEN: This call is required by the ASP.NET Web Form Designer.
... //
Initial InitializeComponent ();
The base.OnInit (e);
}}

^ // // <summary>
// // The method required by the designer support-do not use the code editor to modify
// // The content of this method.
/// </ summary>
Private void InitializeComponent ()
To be {n
This.Load + = new System.EventHandler (this.Page_Load);
}}
^ #Endregion
A
// Define the color.
Private Color GetColor (int itemIndex)
{{
Color objColor;
If (itemIndex == 0)
This is the case {
ObjColor = Color.Maroon; objColor = Color.Maroon;
}}
Elseif (itemIndex == 1)
{{}
ObjColor = Color.Red;
}}}
Elseif (itemIndex == 2)
{{}
ObjColor = Color.Gray; objColor = Color.Gray;
}}
Elseif (itemIndex == 3)
{{}
ObjColor = Color.Blue;
}}
The elseif (itemIndex == 4)
This is the case {
ObjColor = Color.Orange; objColor = Color.Orange;
}}}
The elseif (itemIndex == 5)
{
ObjColor = Color.Cyan; objColor = Color.Cyan;
}}}
Elseif (itemIndex == 6)
This is the case {
ObjColor = Color.Bisque;
}}}
Elseif (itemIndex == 7)
This is the case {
ObjColor = Color.Maroon; objColor = Color.Maroon;
}}}
Elseif (itemIndex == 8)
This is the case {
ObjColor = Color.Maroon; objColor = Color.Maroon;
}}}
... else
This is the case {
ObjColor = Color.Blue;
}}}
Return objColor;
}}
A
}
}
A
————————————————
Copyright statement: This article is the original article of the CSDN blogger "yichangxin", which follows the CC 4.0 BY-SA copyright agreement.
Original link: https://blog.csdn.net/yichangxin/java/article/details/1008011

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.