-
C# pie chart is actually a very simple thing. I posted it because I want to implement a function that cannot be achieved by any control on the network.
After searching on the Internet for nearly a day, I was dizzy, but the result was really disappointing. In a fit of anger, I drew one myself, which only took three hours. I hope it can
Bring help to friends in need and avoid detours. Knowledge lies in sharing and everyone can make progress together. Okay, no more nonsense. The code is as follows. There is no need to install anything.
You can use it directly. For C#.Net 2005 environment and XP operating system, if you need to connect to the database for dynamic display, you only need to change the DataTable passed in.
There are no remarks above. I thought it was very simple, so I ignored it. I believe that as long as you read it carefully, it should be very simple.
First create an aspx file and post the following code. Note that the red part can be changed to your own file name.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
public partial class Test_Chart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Put user code here to initialize the page
MemoryStream stream = new MemoryStream();
MemoryStream columnarStream = new MemoryStream();
Bitmap graph = null;
DataTable dt = new DataTable();
dt.Columns.Add("Ven", typeof(System.String));
dt.Columns.Add("BadQty", typeof(System.Int32));
dt.Rows.Add("Weihai, Shandong", 65);
dt.Rows.Add("Huangshan, Anhui",23);
dt.Rows.Add("Jiangsu Taihu",34);
dt.Rows.Add("Shaanxi Huashan",98);
dt.Rows.Add("Jinggang Mountain, Hunan",102);
dt.Rows.Add("Hainan Nanhai",74);
graph = GetPieGraph("Compex failure rate statistics of each group",600,500,100,30,dt);
graph.Save(stream, ImageFormat.Jpeg);
//Picture output
Response.Clear();
Response.ContentType = "image/jpeg";
Response.BinaryWrite(stream.ToArray());
}
protected Bitmap GetPieGraph(string title, int width, int height, int left, int top, DataTable gdt)
{
Bitmap objbitmap = new Bitmap(width, height);
Graphics objgraphics;
objgraphics = Graphics.FromImage(objbitmap);
objgraphics.Clear(Color.White);
StringFormat drawformat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical);
StringFormat drawformat1 = new System.Drawing.StringFormat(StringFormatFlags.DisplayFormatControl);
objgraphics.DrawString(title, new Font("宋体", 16),Brushes.Black, 150, 5, drawformat1);
PointF symbolleg = new PointF(left, height-top-45);
PointF descleg = new PointF(left + 20, height - top - 45);
//Draw a border
objgraphics.DrawRectangle(Pens.Black, 0, 0, width-1, height-1);
//Small frame within the picture
int h = gdt.Rows.Count / 4;
if (gdt.Rows.Count % 4 > 0)
{
h = h + 1;
}
objgraphics.DrawRectangle(Pens.Black, left-10, height-top-50,gdt.Rows.Count*70+10,20*h);
//Show what color represents what
for (int i = 0; i < gdt.Rows.Count; i++)
{
if (i >= 4 && (i+1)%4==1)
{
symbolleg.Y += 20;
descleg.Y += 20;
symbolleg.X = left;
descleg.X = left + 20;
}
objgraphics.FillRectangle(new SolidBrush(getcolor(i)), symbolleg.X, symbolleg.Y, 12, 10);
objgraphics.DrawRectangle(Pens.Black, symbolleg.X, symbolleg.Y, 12, 10);
objgraphics.DrawString(gdt.Rows[i][0].ToString().Trim(), new Font("宋体", 10), Brushes.Black, descleg);
symbolleg.X += 100;
descleg.X += 100;
}
float sglcurrentangle = 0;
float sgltotalangle = 0;
float sgltotalvalues = 0;
for (int i = 0; i < gdt.Rows.Count; i++)
{
sgltotalvalues += float.Parse(gdt.Rows[i][1].ToString().Trim());
}
for (int i = 0; i < gdt.Rows.Count; i++)
{
sglcurrentangle = float.Parse(gdt.Rows[i][1].ToString().Trim()) / sgltotalvalues * 360;
objgraphics.FillPie(new SolidBrush(getcolor(i)), left+50, top+30, 300, 300, sgltotalangle, sglcurrentangle);
objgraphics.DrawPie(Pens.Black, left + 50, top + 30,300, 300, sgltotalangle, sglcurrentangle);
//radius r
double r = 300 / 2;
//Circle center position:
double cX = left + 50 + r;
double cY = top + 30 + r;
//The coordinates of the point on the circle:
double dX = r * Math.Cos((360 - sgltotalangle - sglcurrentangle / 2) * 3.14 / 180);
double dY = r * Math.Sin((360 - sgltotalangle - sglcurrentangle / 2) * 3.14 / 180);
//Position on the circle:
double dcX = cX + dX;
double dcY = cY - dY;
//radius r
double r1 = 350 / 2;
//Circle center position:
double cX1 = left + 50 + r;
double cY1 = top + 30 + r;
//The coordinates of the point on the circle:
double dX1 = r1 * Math.Cos((360 - sgltotalangle - sglcurrentangle / 2) * 3.14 / 180);
double dY1 = r1 * Math.Sin((360 - sgltotalangle - sglcurrentangle / 2) * 3.14 / 180);
//Position on the circle:
double dcX1 = cX1 + dX1;
double dcY1 = cY1 - dY1;
objgraphics.DrawLine(Pens.Black,Convert.ToInt32(dcX) ,Convert.ToInt32(dcY),Convert.ToInt32(dcX1), Convert.ToInt32(dcY1));
if (dX1 >= 0 && dY1 >= 0)
{
objgraphics.DrawString(gdt.Rows[i][0].ToString().Trim(), new Font("宋体", 10), Brushes.Black, Convert.ToInt32(dcX1), Convert.ToInt32(dcY1 - 5 ));
}
if (dX1 <= 0 && dY1 >= 0)
{
objgraphics.DrawString(gdt.Rows[i][0].ToString().Trim(), new Font("宋体", 10), Brushes.Black, Convert.ToInt32(dcX1 - 25), Convert.ToInt32(dcY1 - 15));
}
if (dX1 <= 0 && dY1 <= 0)
{
objgraphics.DrawString(gdt.Rows[i][0].ToString().Trim(), new Font("宋体", 10), Brushes.Black, Convert.ToInt32(dcX1 - 30), Convert.ToInt32(dcY1 ));
}
if (dX1 >= 0 && dY1 <= 0)
{
objgraphics.DrawString(gdt.Rows[i][0].ToString().Trim(), new Font("宋体", 10), Brushes.Black, Convert.ToInt32(dcX1), Convert.ToInt32(dcY1)) ;
}
//GetLet(left + 50+150, top + 30+150,300,sgltotalangle, sglcurrentangle,arrvalues[i].ToString());
sgltotalangle += sglcurrentangle;
}
return objbitmap;
}
protected void GetLet(int startX, int startY, double dia, double totalAngle, double currentAngle, string leg)
{
double x = 0;
double y = 0;
double r = dia / 2;
x = r * Math.Cos((360 - totalAngle - currentAngle / 2) * 3.14 / 180);
y = r * Math.Sin((360 - totalAngle - currentAngle / 2) * 3.14 / 180);
}
protected Color getcolor(int itemindex)
{
Color objcolor;
if (itemindex >= 14)
{
itemindex = itemindex % 14;
}
if (itemindex == 0)
{
objcolor = Color.DarkMagenta;
}
else if (itemindex == 1)
{
objcolor = Color.MediumAquamarine;
}
else if (itemindex == 2)
{
objcolor = Color.DeepSkyBlue;
}
else if (itemindex == 3)
{
objcolor = Color.DarkRed;
}
else if (itemindex == 4)
{
objcolor = Color.Pink;
}
else if (itemindex == 5)
{
objcolor = Color.Salmon;
}
else if (itemindex == 6)
{
objcolor = Color.Khaki;
}
else if (itemindex == 7)
{
objcolor = Color.Maroon;
}
else if (itemindex == 8)
{
objcolor = Color.LawnGreen;
}
else if (itemindex == 9)
{
objcolor = Color.LightGoldenrodYellow;
}
else if (itemindex == 10)
{
objcolor = Color.Moccasin;
}
else if (itemindex == 11)
{
objcolor = Color.YellowGreen;
}
else if (itemindex == 12)
{
objcolor = Color.DarkCyan;
}
else if (itemindex == 13)
{
objcolor = Color.SteelBlue;
}
else if (itemindex == 14)
{
objcolor = Color.Tomato;
}
else
{
objcolor = Color.SlateGray;
}
return objcolor;
}
}