柱状图/ 折线图 总结

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace ChartBuild
{
    public class ChartBuild
    {
        public Bitmap GetChat()
        {

            /**/
            /*自写编码*/
            int ImgWidth = 600;         //1.[总宽度]              ***图宽度  
            int ImgHeight = 300;        //2.[总高度]              ***图高度       
            int ItemNum = 1;            //3.[项目数量]            ***图表划分的块           
            int ChildNum = 7;           //4.[块数]                ***大块中划分的子项的数量
            float ChildRate = 0.3f;     //5.[各块总占空间比率]
            //int ChildSpace=15;        //6.[各块间的间距]
            int ChartLeft = 80;         //7.[图表左边距]          ***图表距图的左边距离
            int ChartRight = 50;        //8.[图表右边距]          ***图表距图的右边距离
            int ChartTop = 50;          //9.[图表顶边距]          ***图表距图顶边距离
            int ChartBottom = 50;       //10.[图表底边距]         ***图表距图底边距离
            //int YMaxValue = 5000;       //11.[纵坐标标尺最大值]   ***纵坐标标尺的最大值
            int YItemNum = 6;           //12.[纵坐标标尺段数]     ***纵坐标标尺的段数
            int YTop = 15;              //13.[距纵轴顶端间隔]
            int YStrStart = 35;         //14.[纵坐标标尺文字起始X坐标]
            int XRight = 15;            //15.[距横轴右端间隔]
            int XStrStart = 20;         //16.[横坐标标尺文字起始Y坐标]


            //[图表总宽度]=[总宽度]-[图表左边距]-[图表右边距]-[距横轴右端间隔]
            int chartwidth = ImgWidth - ChartLeft - ChartRight - XRight;
            //[项目宽度]=[图表总宽度]/[项目数量]
            int itemwidth = chartwidth / ItemNum;
            //[各块总占空间比率的实际宽度]=[项目宽度]*[各块总占空间比率]
            int factwidth = Convert.ToInt32(Math.Floor(itemwidth * ChildRate));
            //[各块矩形宽度]=[各块总占空间比率的实际宽度]/[块数]
            int rectanglewidth = factwidth / ChildNum;
            //[各块间的间距]=([项目宽度]-[各块总占空间比率的实际宽度])/([块数]+1)
            int childspace = Convert.ToInt32(Math.Floor((Double)((itemwidth - factwidth) / (ChildNum + 1))));

            Graphics objGps;//建立画板对象
            Bitmap objBitMap = new Bitmap(ImgWidth, ImgHeight);//建立位图对象
            objGps = Graphics.FromImage(objBitMap);//根据位图对象建立画板对象
            objGps.Clear(Color.Silver);//设置画板对象的背景色

            //在绘画图面的指定位置,以指定的字体、指定的颜色绘制指定的字符串。即为图表标题
            objGps.DrawString(" 问题统计图", new Font("黑体", 10), Brushes.Black, new Point(1, 1));

            //***以下代码是是实现图右上部
            Point myRec = new Point(500, 30);
            Point myDec = new Point(515, 30);

            //以上是在图01中为下面绘制定位
            //objGps.DrawString("单位:千栋", new Font("宋体", 9), Brushes.Black, new Point(500, 12));

            string[] sBiaoshi ={ "未解决问题总数", "新增问题总数" };

            for (int i = 0; i < 2; i++)
            {
                objGps.DrawRectangle(Pens.Black, myRec.X, myRec.Y, 10, 10);
                //绘制小方块

                objGps.FillRectangle(new SolidBrush(GetColor(i)), myRec.X, myRec.Y, 10, 10);
                //填充小方块

                objGps.DrawString(sBiaoshi[i].ToString(), new Font("宋体", 9), Brushes.Black, myDec);
                //绘制小方块右边的文字

                myRec.Y += 15;
                myDec.Y += 15;
            }


            //数据数组
            int[] arrValues ={ 0, 0, 0, 0, 0, 0 };

            //处理显示数据,进行图表数值对应
            arrValues[0] = Convert.ToInt32(Math.Floor((Double)((ImgHeight - ChartBottom - ChartTop - YTop) / YItemNum * 2)));
            arrValues[1] = Convert.ToInt32(Math.Floor((Double)((ImgHeight - ChartBottom - ChartTop - YTop) / YItemNum * 1.5)));
            arrValues[2] = Convert.ToInt32(Math.Floor((Double)((ImgHeight - ChartBottom - ChartTop - YTop) / YItemNum * 2.5)));
            arrValues[3] = Convert.ToInt32(Math.Floor((Double)((ImgHeight - ChartBottom - ChartTop - YTop) / YItemNum * 2)));
            arrValues[4] = Convert.ToInt32(Math.Floor((Double)((ImgHeight - ChartBottom - ChartTop - YTop) / YItemNum * 3.5)));
            arrValues[5] = Convert.ToInt32(Math.Floor((Double)((ImgHeight - ChartBottom - ChartTop - YTop) / YItemNum * 2)));
            int[] arrValuesNow ={ 20, 28, 30, 20, 40, 10 };

            //纵坐标标码
            string[] arrValueNames = { "2", "4", "6", "8", "10", "12" };


            string[] arrItemNames ={ "注约", "移行A", "移行B", "GLADOFFICE", "AAA", "DDDD" };


            //得出矩形宽度,和画图X轴位置

            //[项目宽度]=[总宽度]/[项目数量]
            //======[各块总占空间比率]=([各块矩形宽度]+[各块间的间距])/[项目宽度]
            //[各块总占空间比率的实际宽度]=[项目宽度]*[各块总占空间比率]
            //[各块矩形宽度]=([各块总占空间比率的实际宽度]-[各块间的间距]*([块数]))/[块数]
            //[一边空余空间宽度]=([项目宽度]-[各块所占空间比率的总宽度])/2 

            System.Drawing.Point[] pi = new Point[arrValues.Length];    //定义折线点的对象数组
            System.Drawing.Point[] pit = new Point[3];    //定义坐标三角点的对象数组
            System.Drawing.Pen pe = new Pen(new SolidBrush(GetColor(7)), 1f);    //定义画直线的对象
            //画纵轴
            objGps.DrawLine(pe, new Point(ChartLeft, ImgHeight - ChartBottom), new Point(ChartLeft, ChartTop + YTop));
            objGps.DrawLine(pe, new Point(ImgWidth - ChartRight, ImgHeight - ChartBottom), new Point(ImgWidth - ChartRight, ChartTop + YTop));

            画纵轴终点箭头
            //pit[0].X = ImgWidth - ChartRight;    //确定三角形三点的位置
            //pit[0].Y = ImgHeight - ChartBottom - 4;
            //pit[1].X = ImgWidth - ChartRight;
            //pit[1].Y = ImgHeight - ChartBottom + 4;
            //pit[2].X = ImgWidth - ChartRight + 10;
            //pit[2].Y = ImgHeight - ChartBottom;
            //objGps.FillPolygon(new SolidBrush(GetColor(7)), pit);

            //画纵轴标尺和标尺描述
            for (int i = 1; i <= YItemNum; i++)
            {
                //画标尺
                //objGps.DrawLine(pe, new PointF(ChartLeft, ImgHeight - ChartBottom - (ImgHeight - ChartBottom - ChartTop - YTop) / YItemNum * i), new PointF(ChartLeft - 5, ImgHeight - ChartBottom - (ImgHeight - ChartBottom - ChartTop - YTop) / YItemNum * i));
                objGps.DrawLine(pe, new Point(ChartLeft, ImgHeight - ChartBottom - (ImgHeight - ChartBottom - ChartTop - YTop) / YItemNum * i), new Point(ImgWidth - ChartRight, ImgHeight - ChartBottom - (ImgHeight - ChartBottom - ChartTop - YTop) / YItemNum * i));
                //画描述
                objGps.DrawString(arrValueNames[i - 1].ToString(), new Font("宋体", 10), Brushes.Black, new Point(YStrStart, ImgHeight - ChartBottom - (ImgHeight - ChartBottom - ChartTop - YTop) / YItemNum * i - 5));
            }
            //画横轴
            objGps.DrawLine(pe, new Point(ChartLeft, ImgHeight - ChartBottom), new Point(ImgWidth - ChartRight, ImgHeight - ChartBottom));

            画横轴终点箭头
            //pit[0].X = ChartLeft - 4;    //确定三角形三点的位置
            //pit[0].Y = ChartTop;
            //pit[1].X = ChartLeft + 4;
            //pit[1].Y = ChartTop;
            //pit[2].X = ChartLeft;
            //pit[2].Y = ChartTop - 10;
            //objGps.FillPolygon(new SolidBrush(GetColor(7)), pit);

            //画横轴标尺和标尺描述
            for (int i = 1; i <= arrValues.Length; i++)
            {
                //objGps.DrawLine(pe, new PointF(ChartLeft + itemwidth * i, ImgHeight - ChartBottom), new PointF(ChartLeft + itemwidth * i, ImgHeight - ChartBottom + 5));

                objGps.DrawString((arrItemNames[i - 1].ToString()), (new Font("宋体", 9)), (Brushes.Black), new Point(ChartLeft + childspace + itemwidth * (i - 1) / 7, ImgHeight - ChartBottom + XStrStart));
            }

            for (int j = 0; j < arrValues.Length; j++)//画矩形图
            {
                objGps.FillRectangle(new SolidBrush(GetColor(4)), (j * (childspace + rectanglewidth)) + childspace + ChartLeft, ImgHeight - ChartBottom - arrValues[j], rectanglewidth, arrValues[j]);
                objGps.FillRectangle(new SolidBrush(GetColor(3)), (j * (childspace + rectanglewidth)) + childspace + ChartLeft + rectanglewidth, ImgHeight - ChartBottom - arrValuesNow[j], rectanglewidth, arrValuesNow[j]);
                objGps.DrawRectangle(Pens.Black, (j * (childspace + rectanglewidth)) + childspace + ChartLeft, ImgHeight - ChartBottom - arrValues[j], rectanglewidth, arrValues[j]);
                objGps.DrawRectangle(Pens.Black, (j * (childspace + rectanglewidth)) + childspace + ChartLeft + rectanglewidth, ImgHeight - ChartBottom - arrValuesNow[j], rectanglewidth, arrValuesNow[j]);
                折线图
                //pi[j].X = (j * (childspace + rectanglewidth)) + childspace + ChartLeft;
                //pi[j].Y = ImgHeight - ChartBottom - arrValues[j];
                //pe.SetLineCap(System.Drawing.Drawing2D.LineCap.RoundAnchor, System.Drawing.Drawing2D.LineCap.RoundAnchor, System.Drawing.Drawing2D.DashCap.Round);
                //if (j > 0)
                //{
                //    objGps.DrawLine(pe, pi[j - 1], pi[j]);
                //}
            }
            return  objBitMap;//该位图对象以"GIF"格式输出
        }

        /**/
        /// <param name="itemIndex">系统定义的颜色,有效值0到7,分别为(Blue,Yellow,Red,Orange,Purple,Brown,Pink,Black)</param>
        /// <returns></returns>
        public static Color GetColor(int itemIndex)
        {
            Color objColor = new Color();
            switch (itemIndex)
            {
                case 0:
                    objColor = Color.Blue;
                    break;
                case 1:
                    objColor = Color.Red;
                    break;
                case 2:
                    objColor = Color.Yellow;
                    break;
                case 3:
                    objColor = Color.Orange;
                    break;
                case 4:
                    objColor = Color.Purple;
                    break;
                case 5:
                    objColor = Color.Brown;
                    break;
                case 6:
                    objColor = Color.Pink;
                    break;
                default:
                    objColor = Color.Black;
                    break;
            }

            return objColor;
        }

        /**/
        /// <param name="red">自定义颜色红色分量值,有效值0到255</param>
        /// <param name="green">自定义颜色绿色分量值,有效值0到255</param>
        /// <param name="blue">自定义颜色蓝色分量值,有效值0到255</param>
        /// <returns></returns>
        public static Color GetColor(int red, int green, int blue)
        {
            Color objColor = new Color();
            objColor = Color.FromArgb(red, green, blue);
            return objColor;
        }
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值