zoukankan      html  css  js  c++  java
  • C# 绘制统计图(柱状图, 折线图, 扇形图)

    统计图形种类繁多, 有柱状图, 折线图, 扇形图等等, 而统计图形的绘制方法也有很多, 有Flash制作的统计图形, 有水晶报表生成统计图形, 有专门制图软件制作, 也有编程语言自己制作的;这里我们用就C# 制作三款最经典的统计图: 柱状图, 折线图和扇形图;既然是统计, 当然需要数据, 这里演示的数据存于Sql Server2000中, 三款统计图形都是动态生成. 其中柱状图我会附上制作步骤, 其他两款统计图直接附源码.



    说明: 需求不一样, 统计图形绘制后的显示效果也不一样, 比如这里柱状图的主要需求是为了比较每一期报名人数与通过人数的差, 因此会把两根柱子放在一起会使比较结果一目了然. 因此大家可以根据需要灵活绘制.

     

    一. 柱状图的绘制.

    绘制步骤如下:

    1. 定义绘图用到的类.

    定义绘图类
    1. int height = 500, width = 700;
    2. Bitmap image = new Bitmap(width, height);
    3. Graphics g = Graphics.FromImage(image);
    4. Pen mypen = new Pen(brush, 1);
    复制代码
    2. 绘制图框.

    绘制图框
    1. g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);
    复制代码
    3. 绘制横向坐标线

    绘制横向坐标线
    1. for (int i = 0; i < 14; i++)
    2. {
    3. g.DrawLine(mypen, x, 80, x, 340);
    4. x = x + 40;
    5. }
    复制代码
    4. 绘制纵向坐标线

    绘制纵向坐标线
    1. for (int i = 0; i < 9; i++)
    2. {
    3. g.DrawLine(mypen, 60, y, 620, y);
    4. y = y + 26;
    5. }
    复制代码
    5. 绘制横坐标值

    绘制横坐标值
    1. String[] n = { "第一期", "第二期", "第三期", "第四期", "全年" };
    2. for (int i = 0; i < 7; i++)
    3. {
    4. g.DrawString(n.ToString(), font, Brushes.Blue, x, 348);
    5. x = x + 78;
    6. }
    复制代码
    6. 绘制纵坐标值

    绘制纵坐标
    1. String[] m = {"250","225", "200", "175", "150", "125", "100“};
    2. for (int i = 0; i < 10; i++)
    3. {
    4. g.DrawString(m.ToString(), font, Brushes.Blue, 25, y);
    5. y = y + 26;
    6. }
    复制代码
    7. 定义数组存储数据库中统计的数据

    定义存储统计数据的数组
    1. int[] Count1 = new int[7]; //存储从数据库读取的报名人数
    2. int[] Count2 = new int[7]; //存储从数据库读取的通过人数
    复制代码
    8. 从数据库中读取报名人数与通过人数

    读取数据
    1. SqlConnection Con = new SqlConnection(
    2. "Server=(Local);Database=committeeTraining;");
    3. Con.Open();
    4. string cmdtxt2 = "SELECT * FROM ##Count
    5. where Company='" + ****+ "'";
    6. SqlDataAdapter da = new SqlDataAdapter(cmdtxt2, Con);
    7. DataSet ds = new DataSet();
    8. da.Fill(ds);
    复制代码
    9. 将读取的数据存储到数组中

    将数据存储到数组中
    1. Count1[0] = Convert.ToInt32(ds.Tables[0].Rows[0][“count1”].ToString());
    2. Count1[1] = Convert.ToInt32(ds.Tables[0].Rows[0][“count3”].ToString());
    3. Count2[0] = Convert.ToInt32(ds.Tables[0].Rows[0][“count2”].ToString());
    4. Count2[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count4"].ToString());
    复制代码
    10.定义画笔和画刷准备绘图

    准备绘制柱状图
    1. x = 80;
    2. Font font2 = new System.Drawing.Font(
    3. "Arial", 10, FontStyle.Bold);
    4. SolidBrush mybrush = new SolidBrush(Color.Red);
    5. SolidBrush mybrush2 = new SolidBrush(Color.Green);
    复制代码
    11. 根据数组中的值绘制柱状图



    绘制柱状图

    (1)第一期报名人数
    1. g.FillRectangle(mybrush, x, 340 - Count1[0], 20, Count1[0]);
    2. g.DrawString(Count1[0].ToString(), font2,
    3. Brushes.Red, x, 340 - Count1[0] - 15);
    复制代码
    (2) 第一期通过人数
    1. x = x + 20;
    2. g.FillRectangle(mybrush2, x, 340 - Count2[0], 20, Count2[0]);
    3. g.DrawString(Count2[0].ToString(), font2,
    4. Brushes.Green, x, 340 - Count2[0] - 15);
    复制代码
    12. 将图形输出到页面.

    将页面输出到页中
    1. System.IO.MemoryStream ms = new
    2. System.IO.MemoryStream();
    3. image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    4. Response.ClearContent();
    5. Response.ContentType = "image/Jpeg";
    6. Response.BinaryWrite(ms.ToArray());
    复制代码
    最终柱状图的效果图:

    附件: histogram.png



    柱状图的完整代码:

    绘制柱状统计图的完整代码
    1. private void CreateImage()
    2. {
    3. int height = 500, width = 700;
    4. Bitmap image = new Bitmap(width, height);
    5. //创建Graphics类对象
    6. Graphics g = Graphics.FromImage(image);

    7. try
    8. {
    9. //清空图片背景色
    10. g.Clear(Color.White);

    11. Font font = new Font("Arial", 10, FontStyle.Regular);
    12. Font font1 = new Font("宋体", 20, FontStyle.Bold);

    13. LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
    14. Color.Blue, Color.BlueViolet, 1.2f, true);
    15. g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);
    16. // Brush brush1 = new SolidBrush(Color.Blue);

    17. g.DrawString(this.ddlTaget.SelectedItem.Text + " " + this.ddlYear.SelectedItem.Text +
    18. " 成绩统计柱状图", font1, brush, new PointF(70, 30));
    19. //画图片的边框线
    20. g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);


    21. Pen mypen = new Pen(brush, 1);
    22. //绘制线条
    23. //绘制横向线条
    24. int x = 100;
    25. for (int i = 0; i < 14; i++)
    26. {
    27. g.DrawLine(mypen, x, 80, x, 340);
    28. x = x + 40;
    29. }
    30. Pen mypen1 = new Pen(Color.Blue, 2);
    31. x = 60;
    32. g.DrawLine(mypen1, x, 80, x, 340);

    33. //绘制纵向线条
    34. int y = 106;
    35. for (int i = 0; i < 9; i++)
    36. {
    37. g.DrawLine(mypen, 60, y, 620, y);
    38. y = y + 26;
    39. }
    40. g.DrawLine(mypen1, 60, y, 620, y);

    41. //x轴
    42. String[] n = { "第一期", "第二期", "第三期", "第四期", "上半年", "下半年", "全年统计" };
    43. x = 78;
    44. for (int i = 0; i < 7; i++)
    45. {
    46. g.DrawString(n.ToString(), font, Brushes.Blue, x, 348); //设置文字内容及输出位置
    47. x = x + 78;
    48. }

    49. //y轴
    50. String[] m = {"250","225", "200", "175", "150", "125", "100", " 75",
    51. " 50", " 25", " 0"};
    52. y = 72;
    53. for (int i = 0; i < 10; i++)
    54. {
    55. g.DrawString(m.ToString(), font, Brushes.Blue, 25, y); //设置文字内容及输出位置
    56. y = y + 26;
    57. }

    58. int[] Count1 = new int[7];
    59. int[] Count2 = new int[7];

    60. SqlConnection Con = new SqlConnection("Server=(Local);Database=committeeTraining;Uid=sa;Pwd=**");
    61. Con.Open();
    62. string cmdtxt2 = "SELECT * FROM ##Count where Company='" + this.ddlTaget.SelectedItem.Text.Trim() + "'";
    63. SqlDataAdapter da = new SqlDataAdapter(cmdtxt2, Con);
    64. DataSet ds = new DataSet();
    65. da.Fill(ds);

    66. Count1[0] = Convert.ToInt32(ds.Tables[0].Rows[0]["count1"].ToString());
    67. Count1[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count3"].ToString());
    68. Count1[2] = Convert.ToInt32(ds.Tables[0].Rows[0]["count5"].ToString());
    69. Count1[3] = Convert.ToInt32(ds.Tables[0].Rows[0]["count7"].ToString());

    70. Count1[4] = Count1[0] + Count1[1];
    71. Count1[5] = Count1[2] + Count1[3];

    72. Count1[6] = Convert.ToInt32(ds.Tables[0].Rows[0]["count9"].ToString());


    73. Count2[0] = Convert.ToInt32(ds.Tables[0].Rows[0]["count2"].ToString());
    74. Count2[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count4"].ToString());
    75. Count2[2] = Convert.ToInt32(ds.Tables[0].Rows[0]["count6"].ToString());
    76. Count2[3] = Convert.ToInt32(ds.Tables[0].Rows[0]["count8"].ToString());

    77. Count2[4] = Count2[0] + Count2[1];
    78. Count2[5] = Count2[2] + Count2[3];

    79. Count2[6] = Convert.ToInt32(ds.Tables[0].Rows[0]["count10"].ToString());

    80. //绘制柱状图.
    81. x = 80;
    82. Font font2 = new System.Drawing.Font("Arial", 10, FontStyle.Bold);
    83. SolidBrush mybrush = new SolidBrush(Color.Red);
    84. SolidBrush mybrush2 = new SolidBrush(Color.Green);

    85. //第一期
    86. g.FillRectangle(mybrush, x, 340 - Count1[0], 20, Count1[0]);
    87. g.DrawString(Count1[0].ToString(), font2, Brushes.Red, x, 340 - Count1[0] - 15);

    88. x = x + 20;
    89. g.FillRectangle(mybrush2, x, 340 - Count2[0], 20, Count2[0]);
    90. g.DrawString(Count2[0].ToString(), font2, Brushes.Green, x, 340 - Count2[0] - 15);


    91. //第二期
    92. x = x + 60;
    93. g.FillRectangle(mybrush, x, 340 - Count1[1], 20, Count1[1]);
    94. g.DrawString(Count1[1].ToString(), font2, Brushes.Red, x, 340 - Count1[1] - 15);


    95. x = x + 20;
    96. g.FillRectangle(mybrush2, x, 340 - Count2[1], 20, Count2[1]);
    97. g.DrawString(Count2[1].ToString(), font2, Brushes.Green, x, 340 - Count2[1] - 15);


    98. //第三期
    99. x = x + 60;
    100. g.FillRectangle(mybrush, x, 340 - Count1[2], 20, Count1[2]);
    101. g.DrawString(Count1[2].ToString(), font2, Brushes.Red, x, 340 - Count1[2] - 15);

    102. x = x + 20;
    103. g.FillRectangle(mybrush2, x, 340 - Count2[2], 20, Count2[2]);
    104. g.DrawString(Count2[2].ToString(), font2, Brushes.Green, x, 340 - Count2[2] - 15);

    105. //第四期
    106. x = x + 60;
    107. g.FillRectangle(mybrush, x, 340 - Count1[3], 20, Count1[3]);
    108. g.DrawString(Count1[3].ToString(), font2, Brushes.Red, x, 340 - Count1[3] - 15);

    109. x = x + 20;
    110. g.FillRectangle(mybrush2, x, 340 - Count2[3], 20, Count2[3]);
    111. g.DrawString(Count2[3].ToString(), font2, Brushes.Green, x, 340 - Count2[3] - 15);

    112. //上半年
    113. x = x + 60;
    114. g.FillRectangle(mybrush, x, 340 - Count1[4], 20, Count1[4]);
    115. g.DrawString(Count1[4].ToString(), font2, Brushes.Red, x, 340 - Count1[4] - 15);

    116. x = x + 20;
    117. g.FillRectangle(mybrush2, x, 340 - Count2[4], 20, Count2[4]);
    118. g.DrawString(Count2[4].ToString(), font2, Brushes.Green, x, 340 - Count2[4] - 15);

    119. //下半年
    120. x = x + 60;
    121. g.FillRectangle(mybrush, x, 340 - Count1[5], 20, Count1[5]);
    122. g.DrawString(Count1[5].ToString(), font2, Brushes.Red, x, 340 - Count1[5] - 15);

    123. x = x + 20;
    124. g.FillRectangle(mybrush2, x, 340 - Count2[5], 20, Count2[5]);
    125. g.DrawString(Count2[5].ToString(), font2, Brushes.Green, x, 340 - Count2[5] - 15);

    126. //全年
    127. x = x + 60;
    128. g.FillRectangle(mybrush, x, 340 - Count1[6], 20, Count1[6]);
    129. g.DrawString(Count1[6].ToString(), font2, Brushes.Red, x, 340 - Count1[6] - 15);


    130. x = x + 20;
    131. g.FillRectangle(mybrush2, x, 340 - Count2[6], 20, Count2[6]);
    132. g.DrawString(Count2[6].ToString(), font2, Brushes.Green, x, 340 - Count2[6] - 15);


    133. //绘制标识
    134. Font font3 = new System.Drawing.Font("Arial", 10, FontStyle.Regular);
    135. g.DrawRectangle(new Pen(Brushes.Blue), 170, 400, 250, 50); //绘制范围框
    136. g.FillRectangle(Brushes.Red, 270, 410, 20, 10); //绘制小矩形
    137. g.DrawString("报名人数", font3, Brushes.Red, 292, 408);

    138. g.FillRectangle(Brushes.Green, 270, 430, 20, 10);
    139. g.DrawString("通过人数", font3, Brushes.Green, 292, 428);

    140. System.IO.MemoryStream ms = new System.IO.MemoryStream();
    141. image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    142. Response.ClearContent();
    143. Response.ContentType = "image/Jpeg";
    144. Response.BinaryWrite(ms.ToArray());
    145. }
    146. finally
    147. {
    148. g.Dispose();
    149. image.Dispose();
    150. }
    151. }
    复制代码
    二. 折线统计图的绘制

    效果:

    附件: line chart.png


    折线图的完整代码:

    折线图的完整代码
    1. private void CreateImage()
    2. {
    3. int height = 480, width = 700;
    4. Bitmap image = new Bitmap(width, height);
    5. Graphics g = Graphics.FromImage(image);

    6. try
    7. {
    8. //清空图片背景色
    9. g.Clear(Color.White);

    10. Font font = new System.Drawing.Font("Arial", 9, FontStyle.Regular);
    11. Font font1 = new System.Drawing.Font("宋体", 20, FontStyle.Regular);
    12. Font font2 = new System.Drawing.Font("Arial", 8, FontStyle.Regular);
    13. LinearGradientBrush brush = new LinearGradientBrush(
    14. new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Blue, 1.2f, true);
    15. g.FillRectangle(Brushes.AliceBlue, 0, 0, width, height);
    16. Brush brush1 = new SolidBrush(Color.Blue);
    17. Brush brush2 = new SolidBrush(Color.SaddleBrown);

    18. g.DrawString(this.ddlTaget.SelectedItem.Text + " " + this.ddlYear.SelectedItem.Text +
    19. " 成绩统计折线图", font1, brush1, new PointF(85, 30));
    20. //画图片的边框线
    21. g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);

    22. Pen mypen = new Pen(brush, 1);
    23. Pen mypen2 = new Pen(Color.Red, 2);
    24. //绘制线条
    25. //绘制纵向线条
    26. int x = 60;
    27. for (int i = 0; i < 8; i++)
    28. {
    29. g.DrawLine(mypen, x, 80, x, 340);
    30. x = x + 80;
    31. }
    32. Pen mypen1 = new Pen(Color.Blue, 3);
    33. x = 60;
    34. g.DrawLine(mypen1, x, 82, x, 340);

    35. //绘制横向线条
    36. int y = 106;
    37. for (int i = 0; i < 10; i++)
    38. {
    39. g.DrawLine(mypen, 60, y, 620, y);
    40. y = y + 26;
    41. }
    42. // y = 106;
    43. g.DrawLine(mypen1, 60, y - 26, 620, y - 26);

    44. //x轴
    45. String[] n = { "第一期", "第二期", "第三期", "第四期", "上半年", "下半年", "全年统计" };
    46. x = 45;
    47. for (int i = 0; i < 7; i++)
    48. {
    49. g.DrawString(n.ToString(), font, Brushes.Red, x, 348); //设置文字内容及输出位置
    50. x = x + 77;
    51. }

    52. //y轴
    53. String[] m = { "220人", " 200人", " 175人", "150人", " 125人", " 100人", " 75人", " 50人",
    54. " 25人"};
    55. y = 100;
    56. for (int i = 0; i < 9; i++)
    57. {
    58. g.DrawString(m.ToString(), font, Brushes.Red, 10, y); //设置文字内容及输出位置
    59. y = y + 26;
    60. }

    61. int[] Count1 = new int[7];
    62. int[] Count2 = new int[7];

    63. SqlConnection Con = new SqlConnection("Server=(Local);Database=committeeTraining;Uid=sa;Pwd=eesoft");
    64. Con.Open();
    65. string cmdtxt2 = "SELECT * FROM ##Count where Company='" + this.ddlTaget.SelectedItem.Text.Trim() + "'";
    66. SqlDataAdapter da = new SqlDataAdapter(cmdtxt2, Con);
    67. DataSet ds = new DataSet();
    68. da.Fill(ds);

    69. //报名人数
    70. Count1[0] = Convert.ToInt32(ds.Tables[0].Rows[0]["count1"].ToString());
    71. Count1[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count3"].ToString());
    72. Count1[2] = Convert.ToInt32(ds.Tables[0].Rows[0]["count5"].ToString());
    73. Count1[3] = Convert.ToInt32(ds.Tables[0].Rows[0]["count7"].ToString());

    74. Count1[6] = Convert.ToInt32(ds.Tables[0].Rows[0]["count9"].ToString()); //全年

    75. Count1[4] = Count1[0] + Count1[1];
    76. Count1[5] = Count1[2] + Count1[3];


    77. Count2[0] = Convert.ToInt32(ds.Tables[0].Rows[0]["count2"].ToString());
    78. Count2[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count4"].ToString());
    79. Count2[2] = Convert.ToInt32(ds.Tables[0].Rows[0]["count6"].ToString());
    80. Count2[3] = Convert.ToInt32(ds.Tables[0].Rows[0]["count8"].ToString());

    81. Count2[6] = Convert.ToInt32(ds.Tables[0].Rows[0]["count10"].ToString()); //全年

    82. Count2[4] = Count2[0] + Count2[1];
    83. Count2[5] = Count2[2] + Count2[3];


    84. //显示折线效果
    85. Font font3 = new System.Drawing.Font("Arial", 10, FontStyle.Bold);
    86. SolidBrush mybrush = new SolidBrush(Color.Red);
    87. Point[] points1 = new Point[7];
    88. points1[0].X = 60; points1[0].Y = 340 - Count1[0]; //从106纵坐标开始, 到(0, 0)坐标时
    89. points1[1].X = 140; points1[1].Y = 340 - Count1[1];
    90. points1[2].X = 220; points1[2].Y = 340 - Count1[2];
    91. points1[3].X = 300; points1[3].Y = 340 - Count1[3];

    92. points1[4].X = 380; points1[4].Y = 340 - Count1[4];
    93. points1[5].X = 460; points1[5].Y = 340 - Count1[5];

    94. points1[6].X = 540; points1[6].Y = 340 - Count1[6];
    95. g.DrawLines(mypen2, points1); //绘制折线

    96. //绘制数字
    97. g.DrawString(Count1[0].ToString(), font3, Brushes.Red, 58, points1[0].Y - 20);
    98. g.DrawString(Count1[1].ToString(), font3, Brushes.Red, 138, points1[1].Y - 20);
    99. g.DrawString(Count1[2].ToString(), font3, Brushes.Red, 218, points1[2].Y - 20);
    100. g.DrawString(Count1[3].ToString(), font3, Brushes.Red, 298, points1[3].Y - 20);

    101. g.DrawString(Count1[4].ToString(), font3, Brushes.Red, 378, points1[4].Y - 20);
    102. g.DrawString(Count1[5].ToString(), font3, Brushes.Red, 458, points1[5].Y - 20);

    103. g.DrawString(Count1[6].ToString(), font3, Brushes.Red, 538, points1[6].Y - 20);

    104. Pen mypen3 = new Pen(Color.Green, 2);
    105. Point[] points2 = new Point[7];
    106. points2[0].X = 60; points2[0].Y = 340 - Count2[0];
    107. points2[1].X = 140; points2[1].Y = 340 - Count2[1];
    108. points2[2].X = 220; points2[2].Y = 340 - Count2[2];
    109. points2[3].X = 300; points2[3].Y = 340 - Count2[3];

    110. points2[4].X = 380; points2[4].Y = 340 - Count2[4];
    111. points2[5].X = 460; points2[5].Y = 340 - Count2[5];

    112. points2[6].X = 540; points2[6].Y = 340 - Count2[6];
    113. g.DrawLines(mypen3, points2); //绘制折线

    114. //绘制通过人数
    115. g.DrawString(Count2[0].ToString(), font3, Brushes.Green, 61, points2[0].Y - 15);
    116. g.DrawString(Count2[1].ToString(), font3, Brushes.Green, 131, points2[1].Y - 15);
    117. g.DrawString(Count2[2].ToString(), font3, Brushes.Green, 221, points2[2].Y - 15);
    118. g.DrawString(Count2[3].ToString(), font3, Brushes.Green, 301, points2[3].Y - 15);

    119. g.DrawString(Count2[4].ToString(), font3, Brushes.Green, 381, points2[4].Y - 15);
    120. g.DrawString(Count2[5].ToString(), font3, Brushes.Green, 461, points2[5].Y - 15);

    121. g.DrawString(Count2[6].ToString(), font3, Brushes.Green, 541, points2[6].Y - 15);

    122. //绘制标识
    123. g.DrawRectangle(new Pen(Brushes.Red), 180, 390, 250, 50); //绘制范围框
    124. g.FillRectangle(Brushes.Red, 270, 402, 20, 10); //绘制小矩形
    125. g.DrawString("报名人数", font2, Brushes.Red, 292, 400);

    126. g.FillRectangle(Brushes.Green, 270, 422, 20, 10);
    127. g.DrawString("通过人数", font2, Brushes.Green, 292, 420);

    128. System.IO.MemoryStream ms = new System.IO.MemoryStream();
    129. image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    130. Response.ClearContent();
    131. Response.ContentType = "image/Jpeg";
    132. Response.BinaryWrite(ms.ToArray());
    133. }
    134. finally
    135. {
    136. g.Dispose();
    137. image.Dispose();
    138. }
    139. }
    复制代码
    三. 扇形统计图的绘制

    效果图:

    附件: fan chart.gif





    完整代码:

    扇形统计图的绘制
    1. private void CreateImage()
    2. {
    3. //把连接字串指定为一个常量
    4. SqlConnection Con = new SqlConnection("Server=(Local);
    5. Database=committeeTraining;Uid=sa;Pwd=**");
    6. Con.Open();
    7. string cmdtxt = selectString; // "select * from ##Count"; //
    8. //SqlCommand Com = new SqlCommand(cmdtxt, Con);
    9. DataSet ds = new DataSet();
    10. SqlDataAdapter Da = new SqlDataAdapter(cmdtxt, Con);
    11. Da.Fill(ds);
    12. Con.Close();
    13. float Total = 0.0f, Tmp;

    14. //转换成单精度。也可写成Convert.ToInt32
    15. Total = Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]]);

    16. // Total=Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]]);
    17. //设置字体,fonttitle为主标题的字体
    18. Font fontlegend = new Font("verdana", 9);
    19. Font fonttitle = new Font("verdana", 10, FontStyle.Bold);

    20. //背景宽
    21. int width = 350;
    22. int bufferspace = 15;
    23. int legendheight = fontlegend.Height * 10 + bufferspace; //高度
    24. int titleheight = fonttitle.Height + bufferspace;
    25. int height = width + legendheight + titleheight + bufferspace;//白色背景高
    26. int pieheight = width;
    27. Rectangle pierect = new Rectangle(0, titleheight, width, pieheight);

    28. //加上各种随机色
    29. ArrayList colors = new ArrayList();
    30. Random rnd = new Random();
    31. for (int i = 0; i < 2; i++)
    32. colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255))));

    33. //创建一个bitmap实例
    34. Bitmap objbitmap = new Bitmap(width, height);
    35. Graphics objgraphics = Graphics.FromImage(objbitmap);

    36. //画一个白色背景
    37. objgraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);

    38. //画一个亮黄色背景
    39. objgraphics.FillRectangle(new SolidBrush(Color.Beige), pierect);

    40. //以下为画饼图(有几行row画几个)
    41. float currentdegree = 0.0f;

    42. //画通过人数
    43. objgraphics.FillPie((SolidBrush)colors[1], pierect, currentdegree,
    44. Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]) / Total * 360);
    45. currentdegree += Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]) / Total * 360;

    46. //未通过人数饼状图
    47. objgraphics.FillPie((SolidBrush)colors[0], pierect, currentdegree,
    48. ((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]]))-(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]))) / Total * 360);
    49. currentdegree += ((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])) -
    50. (Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]))) / Total * 360;


    51. //以下为生成主标题
    52. SolidBrush blackbrush = new SolidBrush(Color.Black);
    53. SolidBrush bluebrush = new SolidBrush(Color.Blue);
    54. string title = " 机关单位成绩统计饼状图: "
    55. + "\n \n\n";
    56. StringFormat stringFormat = new StringFormat();
    57. stringFormat.Alignment = StringAlignment.Center;
    58. stringFormat.LineAlignment = StringAlignment.Center;

    59. objgraphics.DrawString(title, fonttitle, blackbrush,
    60. new Rectangle(0, 0, width, titleheight), stringFormat);

    61. //列出各字段与得数目
    62. objgraphics.DrawRectangle(new Pen(Color.Red, 2), 0, height + 10 - legendheight, width, legendheight + 50);

    63. objgraphics.DrawString("----------------统计信息------------------",
    64. fontlegend, bluebrush, 20, height - legendheight + fontlegend.Height * 1 + 1);
    65. objgraphics.DrawString("统计单位: " + this.ddlTaget.SelectedItem.Text,
    66. fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 3 + 1);
    67. objgraphics.DrawString("统计年份: " + this.ddlYear.SelectedItem.Text,
    68. fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 4 + 1);
    69. objgraphics.DrawString("统计期数: " + this.ddlSpan.SelectedItem.Text,
    70. fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 5 + 1);

    71. objgraphics.FillRectangle((SolidBrush)colors[1], 5,height - legendheight + fontlegend.Height * 8 + 1, 10, 10);
    72. objgraphics.DrawString("报名总人数: " + Convert.ToString(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])),
    73. fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 7 + 1);
    74. objgraphics.FillRectangle((SolidBrush)colors[0], 5, height - legendheight + fontlegend.Height * 9 + 1, 10, 10);
    75. objgraphics.DrawString("通过总人数: " + Convert.ToString(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]])),
    76. fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 8 + 1);
    77. objgraphics.DrawString("未通过人数: " + ((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])) -
    78. (Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]))), fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 9 + 1);

    79. objgraphics.DrawString("通过率: " + Convert.ToString((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]) /
    80. Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])) * 100)+ " %", fontlegend,
    81. blackbrush, 20, height - legendheight + fontlegend.Height * 10 + 1);

    82. Response.ContentType = "image/Jpeg";
    83. objbitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    84. objgraphics.Dispose();
    85. objbitmap.Dispose();

    86. }
    复制代码
    (文/ziyiFly  出处/博客园)

    来自:http://www.pin5i.com/showtopic-20231.html

  • 相关阅读:
    (转)位置无关码、位置有关码
    【转载】PM为什么会存在?怎么存在?
    战略是什么?3-互联网产品行业以及竞争对手分析
    【转载】2014,要格外留意这7大投资机会——哈继铭 {key:人口红利、房价、社会结构、贸易逆差}
    2014年书单-30本
    产品经理要树立战略意识——战略是什么2(谈国家限制支付宝的战略分析)
    产品经理要树立战略意识——战略是什么1
    【转载】产品经理入门案例分析——锤子手机策略
    javascript设计模式-工厂模式
    javascript设计模式-单例模式
  • 原文地址:https://www.cnblogs.com/gisoracle/p/2019025.html
Copyright © 2011-2022 走看看