C# 绘制柱形图 图形高度和Y轴不一致

头发凌乱 2013-07-29 01:55:47
private void CreateImage() {

string[] month = new string[5] { "第一季度", "第二季度", "第三季度", "第四季度", "全年"};
// float[] d = new float[12] { 20,30,10,50,25,36,14,70,50.4F,28,31,10};
Bitmap bMap = new Bitmap(700,700);
Graphics gph = Graphics.FromImage(bMap);
gph.Clear(Color.White);

PointF cpt = new PointF(40, 420);
//X轴三角形
PointF[] xpt = new PointF[3] { new PointF(cpt.Y+8, cpt.Y), new PointF(cpt.Y, cpt.Y - 4), new PointF(cpt.Y, cpt.Y + 4) };
//Y轴三角形
PointF[] ypt = new PointF[3] { new PointF(cpt.X, cpt.X - 8), new PointF(cpt.X -4, cpt.X), new PointF(cpt.X + 4, cpt.X) };
gph.DrawString("呼叫统计图", new Font("宋体", 14), Brushes.Black, new PointF(cpt.X + 60, cpt.X));

//画X轴
gph.DrawLine(Pens.Black, cpt.X, cpt.Y, cpt.Y, cpt.Y);
gph.DrawPolygon(Pens.Black, xpt);
gph.FillPolygon(new SolidBrush(Color.Black), xpt);
gph.DrawString("月份",new Font("宋体",12),Brushes.Black,new PointF(cpt.Y+10,cpt.Y+10));
//画Y轴
gph.DrawLine(Pens.Black, cpt.X, cpt.Y, cpt.X, cpt.X);
gph.DrawPolygon(Pens.Black, ypt);
gph.FillPolygon(new SolidBrush(Color.Black), ypt);
gph.DrawString("单位(万)", new Font("宋体", 12), Brushes.Black, new PointF(0,7));

for (int i = 1; i <= 12;i++ )
{
//画Y轴刻度
if(i<12){
gph.DrawString((i*25).ToString(),new Font("宋体",11),Brushes.Black,new PointF(15,cpt.Y-i*30-6));
gph.DrawLine(Pens.Black,cpt.X+3,cpt.Y-i*30,cpt.X,cpt.Y-i*30);
}
if(i<6){
gph.DrawString(month[i - 1].Substring(0, 1), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + i * 30 - 5, cpt.Y + 5));
gph.DrawString(month[i - 1].Substring(1, 1), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + i * 30 - 5, cpt.Y + 20));
}

}


int[] count=new int[5];
DBHelper.connection.Open();
SqlCommand cmd = new SqlCommand("select count(*) from History", DBHelper.connection);
int sum =(int)cmd.ExecuteScalar();
DBHelper.connection.Close();

try
{
for (int n = 1; n <= 10; n = n + 3)
{
DBHelper.connection.Open();
string sql = string.Format("select count(*) from History where month(time) between '" + n + "'and '" + (n + 2) + "'and datediff(year, time,getdate())=0");
SqlDataAdapter da = new SqlDataAdapter(sql, DBHelper.connection);
DataSet ds = new DataSet();
int num = da.Fill(ds, "Histrory");
count[n / 3] = (int)ds.Tables[0].Rows[0][0];
DBHelper.connection.Close();
}
count[4] = count[0] + count[1] + count[2] + count[3];

float x = cpt.X;
Font font2 = new System.Drawing.Font("Arial", 10, FontStyle.Bold);
Brush brush1 = new SolidBrush(Color.Red);

for(int m=0;m<5;m++){

x = x + 29;
gph.FillRectangle(brush1, x, 420 - count[m], 20, count[m] );
gph.DrawString(count[m].ToString(), font2, Brushes.Green, x, 420 - count[m]-15);
}
pictureBox1.Image = new Bitmap(bMap);
}
catch (Exception)
{

throw;
}
}


这是运行之后的图片:
...全文
103 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
bdmh 2013-07-29
  • 打赏
  • 举报
回复
懒得看,不一致,肯定是你坐标转换不对,仔细查查吧
rtdb 2013-07-29
  • 打赏
  • 举报
回复
这就是个简单的比例问题: gph.DrawLine(Pens.Black,cpt.X+3,cpt.Y-i*30,cpt.X,cpt.Y-i*30); 你每刻度数值是25,但却差了30个像素点,比例就不对了。 你把30改为25就好了。
头发凌乱 2013-07-29
  • 打赏
  • 举报
回复

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧