GDI+柱状图

在网上搜寻了一下柱状图的例子,看到这一篇http://www.cnblogs.com/ziyifly/archive/2008/09/24/1297841.html

修改一下后发出来,大家指正.

  1ExpandedBlockStart.gifContractedBlock.gif        /**//// <summary>
  2InBlock.gif        /// 生成柱状图(有比较)
  3ExpandedBlockEnd.gif        /// </summary>

  4None.gif        public static Bitmap CreateZhuZhuangTu(IList<PReportParam> paraList, IList<PReportParam> paraList2, string name1, string name2)
  5ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
  6InBlock.gif            int height = 500, width = 780;
  7InBlock.gif            Bitmap image = new Bitmap(width, height);
  8InBlock.gif            //创建Graphics类对象
  9InBlock.gif            Graphics g = Graphics.FromImage(image);
 10InBlock.gif            bool hasPara2 = false//是否有比较参数
 11InBlock.gif            if (paraList2 != null && paraList2.Count > 0)
 12ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 13InBlock.gif                hasPara2 = true;
 14ExpandedSubBlockEnd.gif            }

 15InBlock.gif
 16InBlock.gif            try
 17ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 18InBlock.gif                //清空图片背景色
 19InBlock.gif                g.Clear(Color.White);
 20InBlock.gif
 21InBlock.gif                Font font = new Font("Arial"10, FontStyle.Regular);
 22InBlock.gif                Font font1 = new Font("宋体"12, FontStyle.Bold);
 23InBlock.gif
 24InBlock.gif                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(00, image.Width, image.Height), Color.Blue, Color.BlueViolet, 1.2ftrue);
 25InBlock.gif                g.FillRectangle(Brushes.WhiteSmoke, 00, width, height);
 26InBlock.gif                // Brush brush1 = new SolidBrush(Color.Blue);
 27InBlock.gif                StringFormat sf = new StringFormat();
 28InBlock.gif                sf.FormatFlags = StringFormatFlags.DirectionVertical;
 29InBlock.gif
 30InBlock.gif                g.DrawString("月收支分类统计柱状图", font1, brush, new PointF(2020), sf);
 31InBlock.gif                //画图片的边框线
 32InBlock.gif                g.DrawRectangle(new Pen(Color.Blue), 00, image.Width - 1, image.Height - 1);
 33InBlock.gif
 34InBlock.gif                Pen mypen = new Pen(brush, 1);
 35InBlock.gif                Pen mypen1 = new Pen(Color.Blue, 2);
 36InBlock.gif
 37InBlock.gif                int counts = paraList.Count();//显示的项数
 38InBlock.gif                int zx = 100;  //坐标轴原点x坐标
 39InBlock.gif                int zy = 420;  //坐标轴原点y坐标
 40InBlock.gif                int zwidth = 580;  //坐标轴宽
 41InBlock.gif                int zheight = 400//坐标轴高
 42InBlock.gif
 43InBlock.gif                decimal maxNum = 0.00m;
 44InBlock.gif
 45InBlock.gif                List<PReportParam> p1 = paraList.OrderBy<PReportParam, decimal>(tmp => tmp.Num).ToList<PReportParam>();
 46InBlock.gif                decimal maxNum1 = p1[p1.Count - 1].Num;  //最大的一个数字
 47InBlock.gif                if (hasPara2)
 48ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 49InBlock.gif                    List<PReportParam> p2 = paraList2.OrderBy<PReportParam, decimal>(tmp => tmp.Num).ToList<PReportParam>();
 50InBlock.gif                    decimal maxNum2 = p2[p2.Count - 1].Num;
 51InBlock.gif                    if (maxNum1 > maxNum2)
 52ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 53InBlock.gif                        maxNum = maxNum1;
 54ExpandedSubBlockEnd.gif                    }

 55InBlock.gif                    else
 56ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 57InBlock.gif                        maxNum = maxNum2;
 58ExpandedSubBlockEnd.gif                    }

 59ExpandedSubBlockEnd.gif                }

 60InBlock.gif                else
 61ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 62InBlock.gif                    maxNum = maxNum1;
 63ExpandedSubBlockEnd.gif                }

 64InBlock.gif                decimal peNum = zheight / maxNum;  //每单位代表多少像素
 65InBlock.gif
 66InBlock.gif                //绘制线条
 67InBlock.gif                //绘制竖向线条
 68InBlock.gif                int x = zx;
 69InBlock.gif                g.DrawLine(mypen1, zx, zy, zx, zy - zheight);
 70InBlock.gif
 71InBlock.gif                //绘制横向线条
 72InBlock.gif                int y = zy;
 73InBlock.gif                g.DrawLine(mypen1, zx, zy, zx + zwidth, zy);
 74InBlock.gif
 75InBlock.gif                Font font2 = new System.Drawing.Font("Arial"8);
 76InBlock.gif
 77InBlock.gif                for (int i = 0; i < paraList.Count; i++)
 78ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 79InBlock.gif                    string sc = paraList[i].Color;
 80InBlock.gif                    if (sc == null || sc == "")
 81ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 82InBlock.gif                        sc = "ff000000";
 83ExpandedSubBlockEnd.gif                    }

 84InBlock.gif                    int ca = 0, cr = 0, cg = 0, cb = 0;
 85InBlock.gif                    ColorARGBToParam(sc, ref ca, ref cr, ref cg, ref cb);
 86InBlock.gif                    SolidBrush sb = new SolidBrush(Color.FromArgb(ca, cr, cg, cb));
 87InBlock.gif
 88InBlock.gif                    //画柱状图
 89InBlock.gif                    x = x + 40;
 90InBlock.gif                    y = Convert.ToInt32(paraList[i].Num * peNum);
 91InBlock.gif                    g.FillRectangle(sb, x, zy - y - 118, y);
 92InBlock.gif                    //写字
 93InBlock.gif                    string num = DecimalTrim(paraList[i].Num);
 94InBlock.gif                    g.DrawString(num, font2, Brushes.Black, x, zy - y - 15);
 95InBlock.gif                    g.DrawString(paraList[i].Name, font2, Brushes.Black, x, zy + 1, sf);
 96InBlock.gif
 97InBlock.gif                    if (hasPara2)
 98ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 99InBlock.gif                        //画竖线
100InBlock.gif                        g.DrawLine(mypen, x + 18, zy - 1, x + 18, zy - zheight - 1);
101InBlock.gif                        //画柱状图
102InBlock.gif                        y = Convert.ToInt32(paraList2[i].Num * peNum);
103InBlock.gif                        g.FillRectangle(sb, x + 19, zy - y - 118, y);
104InBlock.gif                        //写字
105InBlock.gif                        num = DecimalTrim(paraList2[i].Num);
106InBlock.gif                        g.DrawString(num, font2, Brushes.Black, x + 20, zy - y - 15);
107ExpandedSubBlockEnd.gif                    }

108ExpandedSubBlockEnd.gif                }

109InBlock.gif
110InBlock.gif                g.DrawString("左边:" + name1, font2, Brushes.Red, 20300, sf);
111InBlock.gif                g.DrawString("右边:" + name2, font2, Brushes.Green, 40300, sf);
112ExpandedSubBlockEnd.gif            }

113InBlock.gif            catch
114ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{ }
115InBlock.gif
116InBlock.gif            return image;
117ExpandedBlockEnd.gif        }

 

转载于:https://www.cnblogs.com/tonywang711/archive/2009/11/04/zhuzhuangtu.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值