Asp.Net实例:C# 绘制统计图(二) ——折线统计图的绘制

简介:
折线统计图的绘制
效果 :
 
折线图的完整代码 :
折线图的完整代码
private void CreateImage()
{
int height = 480, width = 700;
Bitmap image = new Bitmap(width, height);
Graphics g = Graphics.FromImage(image);
try
{
//
清空图片背景色
g.Clear(Color.White);
Font font = new System.Drawing.Font("Arial", 9, FontStyle.Regular);
Font font1 = new System.Drawing.Font("
宋体 ", 20, FontStyle.Regular);
Font font2 = new System.Drawing.Font("Arial", 8, FontStyle.Regular);
LinearGradientBrush brush = new LinearGradientBrush(
new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Blue, 1.2f, true);
g.FillRectangle(Brushes.AliceBlue, 0, 0, width, height);
Brush brush1 = new SolidBrush(Color.Blue);
Brush brush2 = new SolidBrush(Color.SaddleBrown);
g.DrawString(this.ddlTaget.SelectedItem.Text + " " + this.ddlYear.SelectedItem.Text + 
成绩统计折线图 ", font1, brush1, new PointF(85, 30));
//
画图片的边框线
g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);
Pen mypen = new Pen(brush, 1);
Pen mypen2 = new Pen(Color.Red, 2);
//
绘制线条
//
绘制纵向线条
int x = 60;
for (int i = 0; i < 8; i++)
{
g.DrawLine(mypen, x, 80, x, 340);
x = x + 80;
}
Pen mypen1 = new Pen(Color.Blue, 3);
x = 60;
g.DrawLine(mypen1, x, 82, x, 340);
// 绘制横向线条
int y = 106;
for (int i = 0; i < 10; i++)
{
g.DrawLine(mypen, 60, y, 620, y);
y = y + 26;
}
// y = 106;
g.DrawLine(mypen1, 60, y - 26, 620, y - 26);
//x
String[] n = { "
第一期 ", " 第二期 ", " 第三期 ", " 第四期 ", " 上半年 ", " 下半年 ", " 全年统计 " };
x = 45;
for (int i = 0; i < 7; i++)
{
g.DrawString(n[i].ToString(), font, Brushes.Red, x, 348); //
设置文字内容及输出位置
x = x + 77;
}
//y
String[] m = { "220
", " 200 ", " 175 ", "150 ", " 125 ", " 100 ", " 75 ", " 50 ",
" 25
"};
y = 100;
for (int i = 0; i < 9; i++)
{
g.DrawString(m[i].ToString(), font, Brushes.Red, 10, y); //
设置文字内容及输出位置
y = y + 26;
}
int[] Count1 = new int[7];
int[] Count2 = new int[7];
SqlConnection Con = new SqlConnection("Server=(Local);Database=committeeTraining;Uid=sa;Pwd=eesoft");
Con.Open();
string cmdtxt2 = "SELECT * FROM ##Count where Company='" + this.ddlTaget.SelectedItem.Text.Trim() + "'";
SqlDataAdapter da = new SqlDataAdapter(cmdtxt2, Con);
DataSet ds = new DataSet();
da.Fill(ds);
// 报名人数
Count1[0] = Convert.ToInt32(ds.Tables[0].Rows[0]["count1"].ToString());
Count1[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count3"].ToString());
Count1[2] = Convert.ToInt32(ds.Tables[0].Rows[0]["count5"].ToString());
Count1[3] = Convert.ToInt32(ds.Tables[0].Rows[0]["count7"].ToString());
Count1[6] = Convert.ToInt32(ds.Tables[0].Rows[0]["count9"].ToString()); // 全年
Count1[4] = Count1[0] + Count1[1];
Count1[5] = Count1[2] + Count1[3];

Count2[0] = Convert.ToInt32(ds.Tables[0].Rows[0]["count2"].ToString());
Count2[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count4"].ToString());
Count2[2] = Convert.ToInt32(ds.Tables[0].Rows[0]["count6"].ToString());
Count2[3] = Convert.ToInt32(ds.Tables[0].Rows[0]["count8"].ToString());
Count2[6] = Convert.ToInt32(ds.Tables[0].Rows[0]["count10"].ToString()); // 全年
Count2[4] = Count2[0] + Count2[1];
Count2[5] = Count2[2] + Count2[3];

//
显示折线效果
Font font3 = new System.Drawing.Font("Arial", 10, FontStyle.Bold);
SolidBrush mybrush = new SolidBrush(Color.Red);
Point[] points1 = new Point[7];
points1[0].X = 60; points1[0].Y = 340 - Count1[0]; //
106 纵坐标开始 (0, 0) 坐标时
points1[1].X = 140; points1[1].Y = 340 - Count1[1];
points1[2].X = 220; points1[2].Y = 340 - Count1[2];
points1[3].X = 300; points1[3].Y = 340 - Count1[3];
points1[4].X = 380; points1[4].Y = 340 - Count1[4];
points1[5].X = 460; points1[5].Y = 340 - Count1[5];
points1[6].X = 540; points1[6].Y = 340 - Count1[6];
g.DrawLines(mypen2, points1); //
绘制折线
// 绘制数字
g.DrawString(Count1[0].ToString(), font3, Brushes.Red, 58, points1[0].Y - 20);
g.DrawString(Count1[1].ToString(), font3, Brushes.Red, 138, points1[1].Y - 20);
g.DrawString(Count1[2].ToString(), font3, Brushes.Red, 218, points1[2].Y - 20);
g.DrawString(Count1[3].ToString(), font3, Brushes.Red, 298, points1[3].Y - 20);
g.DrawString(Count1[4].ToString(), font3, Brushes.Red, 378, points1[4].Y - 20);
g.DrawString(Count1[5].ToString(), font3, Brushes.Red, 458, points1[5].Y - 20);
g.DrawString(Count1[6].ToString(), font3, Brushes.Red, 538, points1[6].Y - 20);
Pen mypen3 = new Pen(Color.Green, 2);
Point[] points2 = new Point[7];
points2[0].X = 60; points2[0].Y = 340 - Count2[0];
points2[1].X = 140; points2[1].Y = 340 - Count2[1];
points2[2].X = 220; points2[2].Y = 340 - Count2[2];
points2[3].X = 300; points2[3].Y = 340 - Count2[3];
points2[4].X = 380; points2[4].Y = 340 - Count2[4];
points2[5].X = 460; points2[5].Y = 340 - Count2[5];
points2[6].X = 540; points2[6].Y = 340 - Count2[6];
g.DrawLines(mypen3, points2); //
绘制折线
// 绘制通过人数
g.DrawString(Count2[0].ToString(), font3, Brushes.Green, 61, points2[0].Y - 15);
g.DrawString(Count2[1].ToString(), font3, Brushes.Green, 131, points2[1].Y - 15);
g.DrawString(Count2[2].ToString(), font3, Brushes.Green, 221, points2[2].Y - 15);
g.DrawString(Count2[3].ToString(), font3, Brushes.Green, 301, points2[3].Y - 15);
g.DrawString(Count2[4].ToString(), font3, Brushes.Green, 381, points2[4].Y - 15);
g.DrawString(Count2[5].ToString(), font3, Brushes.Green, 461, points2[5].Y - 15);
g.DrawString(Count2[6].ToString(), font3, Brushes.Green, 541, points2[6].Y - 15);
// 绘制标识
g.DrawRectangle(new Pen(Brushes.Red), 180, 390, 250, 50); //
绘制范围框
g.FillRectangle(Brushes.Red, 270, 402, 20, 10); //
绘制小矩形
g.DrawString("
报名人数 ", font2, Brushes.Red, 292, 400);
g.FillRectangle(Brushes.Green, 270, 422, 20, 10);
g.DrawString("
通过人数 ", font2, Brushes.Green, 292, 420);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
 
折线图的完整代码 :
折线图的完整代码
private void CreateImage()
{
int height = 480, width = 700;
Bitmap image = new Bitmap(width, height);
Graphics g = Graphics.FromImage(image);
try
{
//
清空图片背景色
g.Clear(Color.White);
Font font = new System.Drawing.Font("Arial", 9, FontStyle.Regular);
Font font1 = new System.Drawing.Font("
宋体 ", 20, FontStyle.Regular);
Font font2 = new System.Drawing.Font("Arial", 8, FontStyle.Regular);
LinearGradientBrush brush = new LinearGradientBrush(
new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Blue, 1.2f, true);
g.FillRectangle(Brushes.AliceBlue, 0, 0, width, height);
Brush brush1 = new SolidBrush(Color.Blue);
Brush brush2 = new SolidBrush(Color.SaddleBrown);
g.DrawString(this.ddlTaget.SelectedItem.Text + " " + this.ddlYear.SelectedItem.Text + 
成绩统计折线图 ", font1, brush1, new PointF(85, 30));
//
画图片的边框线
g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);
Pen mypen = new Pen(brush, 1);
Pen mypen2 = new Pen(Color.Red, 2);
//
绘制线条
//
绘制纵向线条
int x = 60;
for (int i = 0; i < 8; i++)
{
g.DrawLine(mypen, x, 80, x, 340);
x = x + 80;
}
Pen mypen1 = new Pen(Color.Blue, 3);
x = 60;
g.DrawLine(mypen1, x, 82, x, 340);
// 绘制横向线条
int y = 106;
for (int i = 0; i < 10; i++)
{
g.DrawLine(mypen, 60, y, 620, y);
y = y + 26;
}
// y = 106;
g.DrawLine(mypen1, 60, y - 26, 620, y - 26);
//x
String[] n = { "
第一期 ", " 第二期 ", " 第三期 ", " 第四期 ", " 上半年 ", " 下半年 ", " 全年统计 " };
x = 45;
for (int i = 0; i < 7; i++)
{
g.DrawString(n[i].ToString(), font, Brushes.Red, x, 348); //
设置文字内容及输出位置
x = x + 77;
}
//y
String[] m = { "220
", " 200 ", " 175 ", "150 ", " 125 ", " 100 ", " 75 ", " 50 ",
" 25
"};
y = 100;
for (int i = 0; i < 9; i++)
{
g.DrawString(m[i].ToString(), font, Brushes.Red, 10, y); //
设置文字内容及输出位置
y = y + 26;
}
int[] Count1 = new int[7];
int[] Count2 = new int[7];
SqlConnection Con = new SqlConnection("Server=(Local);Database=committeeTraining;Uid=sa;Pwd=eesoft");
Con.Open();
string cmdtxt2 = "SELECT * FROM ##Count where Company='" + this.ddlTaget.SelectedItem.Text.Trim() + "'";
SqlDataAdapter da = new SqlDataAdapter(cmdtxt2, Con);
DataSet ds = new DataSet();
da.Fill(ds);
// 报名人数
Count1[0] = Convert.ToInt32(ds.Tables[0].Rows[0]["count1"].ToString());
Count1[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count3"].ToString());
Count1[2] = Convert.ToInt32(ds.Tables[0].Rows[0]["count5"].ToString());
Count1[3] = Convert.ToInt32(ds.Tables[0].Rows[0]["count7"].ToString());
Count1[6] = Convert.ToInt32(ds.Tables[0].Rows[0]["count9"].ToString()); // 全年
Count1[4] = Count1[0] + Count1[1];
Count1[5] = Count1[2] + Count1[3];

Count2[0] = Convert.ToInt32(ds.Tables[0].Rows[0]["count2"].ToString());
Count2[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count4"].ToString());
Count2[2] = Convert.ToInt32(ds.Tables[0].Rows[0]["count6"].ToString());
Count2[3] = Convert.ToInt32(ds.Tables[0].Rows[0]["count8"].ToString());
Count2[6] = Convert.ToInt32(ds.Tables[0].Rows[0]["count10"].ToString()); // 全年
Count2[4] = Count2[0] + Count2[1];
Count2[5] = Count2[2] + Count2[3];

//
显示折线效果
Font font3 = new System.Drawing.Font("Arial", 10, FontStyle.Bold);
SolidBrush mybrush = new SolidBrush(Color.Red);
Point[] points1 = new Point[7];
points1[0].X = 60; points1[0].Y = 340 - Count1[0]; //
106 纵坐标开始 (0, 0) 坐标时
points1[1].X = 140; points1[1].Y = 340 - Count1[1];
points1[2].X = 220; points1[2].Y = 340 - Count1[2];
points1[3].X = 300; points1[3].Y = 340 - Count1[3];
points1[4].X = 380; points1[4].Y = 340 - Count1[4];
points1[5].X = 460; points1[5].Y = 340 - Count1[5];
points1[6].X = 540; points1[6].Y = 340 - Count1[6];
g.DrawLines(mypen2, points1); //
绘制折线
// 绘制数字
g.DrawString(Count1[0].ToString(), font3, Brushes.Red, 58, points1[0].Y - 20);
g.DrawString(Count1[1].ToString(), font3, Brushes.Red, 138, points1[1].Y - 20);
g.DrawString(Count1[2].ToString(), font3, Brushes.Red, 218, points1[2].Y - 20);
g.DrawString(Count1[3].ToString(), font3, Brushes.Red, 298, points1[3].Y - 20);
g.DrawString(Count1[4].ToString(), font3, Brushes.Red, 378, points1[4].Y - 20);
g.DrawString(Count1[5].ToString(), font3, Brushes.Red, 458, points1[5].Y - 20);
g.DrawString(Count1[6].ToString(), font3, Brushes.Red, 538, points1[6].Y - 20);
Pen mypen3 = new Pen(Color.Green, 2);
Point[] points2 = new Point[7];
points2[0].X = 60; points2[0].Y = 340 - Count2[0];
points2[1].X = 140; points2[1].Y = 340 - Count2[1];
points2[2].X = 220; points2[2].Y = 340 - Count2[2];
points2[3].X = 300; points2[3].Y = 340 - Count2[3];
points2[4].X = 380; points2[4].Y = 340 - Count2[4];
points2[5].X = 460; points2[5].Y = 340 - Count2[5];
points2[6].X = 540; points2[6].Y = 340 - Count2[6];
g.DrawLines(mypen3, points2); //
绘制折线
// 绘制通过人数
g.DrawString(Count2[0].ToString(), font3, Brushes.Green, 61, points2[0].Y - 15);
g.DrawString(Count2[1].ToString(), font3, Brushes.Green, 131, points2[1].Y - 15);
g.DrawString(Count2[2].ToString(), font3, Brushes.Green, 221, points2[2].Y - 15);
g.DrawString(Count2[3].ToString(), font3, Brushes.Green, 301, points2[3].Y - 15);
g.DrawString(Count2[4].ToString(), font3, Brushes.Green, 381, points2[4].Y - 15);
g.DrawString(Count2[5].ToString(), font3, Brushes.Green, 461, points2[5].Y - 15);
g.DrawString(Count2[6].ToString(), font3, Brushes.Green, 541, points2[6].Y - 15);
// 绘制标识
g.DrawRectangle(new Pen(Brushes.Red), 180, 390, 250, 50); //
绘制范围框
g.FillRectangle(Brushes.Red, 270, 402, 20, 10); //
绘制小矩形
g.DrawString("
报名人数 ", font2, Brushes.Red, 292, 400);
g.FillRectangle(Brushes.Green, 270, 422, 20, 10);
g.DrawString("
通过人数 ", font2, Brushes.Green, 292, 420);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}




本文转自 qianshao 51CTO博客,原文链接:http://blog.51cto.com/qianshao/202143,如需转载请自行联系原作者

目录
相关文章
|
10天前
|
开发框架 .NET C#
【Azure Developer】C# / .NET 静态函数中this关键字的作用
在C#中,`this`关键字用于扩展方法,允许向已有类型添加功能而不修改其源代码。扩展方法必须在静态类中定义,且第一个参数使用`this`修饰,如`public static XElement AcquireElement(this XContainer container, string name, bool addFirst = false)`。这种方式增强了代码的可读性和类型的安全性,尤其在处理第三方库时。
|
1月前
|
开发框架 前端开发 .NET
LIMS(实验室)信息管理系统源码、有哪些应用领域?采用C# ASP.NET dotnet 3.5 开发的一套实验室信息系统源码
集成于VS 2019,EXT.NET前端和ASP.NET后端,搭配MSSQL 2018数据库。系统覆盖样品管理、数据分析、报表和项目管理等实验室全流程。应用广泛,包括生产质检(如石化、制药)、环保监测、试验研究等领域。随着技术发展,现代LIMS还融合了临床、电子实验室笔记本和SaaS等功能,以满足复杂多样的实验室管理需求。
43 3
LIMS(实验室)信息管理系统源码、有哪些应用领域?采用C# ASP.NET dotnet 3.5 开发的一套实验室信息系统源码
|
1月前
|
Java C# 数据安全/隐私保护
|
1月前
|
Cloud Native API C#
C#的现代化:.NET Core引领的技术革命
【6月更文挑战第9天】`.NET Core引领C#现代化,实现跨平台革命,提升性能并支持云原生应用。异步编程模型优化体验,统一API简化开发流程。C#应用场景扩展,开发效率提高,技术创新加速,预示其未来在技术领域将持续发挥关键作用。`
38 10
|
21天前
|
人工智能 开发框架 调度
C#/.NET这些实用的技巧和知识点你都知道吗?
C#/.NET这些实用的技巧和知识点你都知道吗?
|
1月前
|
存储 编解码 算法
C#.NET逃逸时间算法生成分形图像的毕业设计完成!晒晒功能
该文介绍了一个使用C#.NET Visual Studio 2008开发的程序,包含错误修复的Julia、Mandelbrot和优化过的Newton三种算法,生成色彩丰富的分形图像。作者改进了原始算法的效率,将内层循环的画点操作移至外部,提升性能。程序提供五种图形模式,支持放大缩小及颜色更新,并允许用户自定义画布大小以调整精度。还具备保存为高质JPG的功能。附有四张示例图片展示生成的分形效果。
419 3
|
1月前
|
XML 开发框架 .NET
【.NET Core】常见C#代码约定
【.NET Core】常见C#代码约定
25 5
|
1月前
|
前端开发 Java C#
GitHub突破5k Star!这件事情我坚持了3年,努力打造C#/.NET/.NET Core全面的学习、工作、面试指南知识库
GitHub突破5k Star!这件事情我坚持了3年,努力打造C#/.NET/.NET Core全面的学习、工作、面试指南知识库
|
28天前
|
开发框架 .NET Nacos
使用 Nacos 在 C# (.NET Core) 应用程序中实现高效配置管理和服务发现
使用 Nacos 在 C# (.NET Core) 应用程序中实现高效配置管理和服务发现
70 0
|
29天前
|
存储 IDE C#
C#入门:在JetBrains Rider中创建.Net Framework控制台应用程序,输出“Hello, World!”
C#入门:在JetBrains Rider中创建.Net Framework控制台应用程序,输出“Hello, World!”
74 0