c#画图之折线图

public JsonResult DrawLineChart()
        {
            // 预置颜色
            List<Color> colors = new List<Color>()
            {
                Color.FromArgb(255,182,193),
                Color.FromArgb(238,130,238),
                Color.FromArgb(220,20,60),
                Color.FromArgb(153,50,204),
                Color.FromArgb(30,144,255),
                Color.FromArgb(60,179,113),
                Color.FromArgb(255,215,0),
                Color.FromArgb(255,140,0),
                Color.FromArgb(105,105,105)
            };

            #region 允许配置项

            //定义宽高
            int height = 500, width = 700;

            //边缘位置留白
            int margin_top = 20;
            int margin_right = 40;
            int margin_bottom = 60;
            int margin_left = 60;

            //辅助线距离顶部的距离
            int xsubline = 20;

            //文字大小,单位:px
            int fontsize = 12;

            // 折线名称预留的位置  颜色框20,与文字间隙5,文字80,距离折线图10,需要包含边缘留白
            int lineNameWidth = 120 - margin_right;

            #endregion

            #region 数据

            //最大数量/总数量--生成y轴时,显示数字需要
            int maxCount = 0;

            //x轴底部显示的名称
            string[] bottomData = new string[] { "第一个", "第二个", "第三个", "第四个", "第五个" };

            //折线名称
            string[] lineName = new string[] { "折线1", "折线2" };

            //折线数据
            List<List<int>> lineData = new List<List<int>> {
                new List<int>(){ 12,23,15,44,32 },
                new List<int>(){ 9,33,6,21,22 }
            };

            //maxCount = xCount.Max();
            for (int i = 0; i < lineData.Count; i++)
            {
                int tempMaxCount = lineData[i].Max();

                if (tempMaxCount > maxCount)
                {
                    maxCount = tempMaxCount;
                }
            }

            maxCount = maxCount == 0 ? 5 : maxCount;

            #endregion

            //单位转换对象
            Spire.Pdf.Graphics.PdfUnitConvertor unitCvtr = new Spire.Pdf.Graphics.PdfUnitConvertor();

            //生成图像对象
            Bitmap image = new Bitmap(width + margin_left + margin_right + lineNameWidth, height + margin_top + margin_bottom);

            //创建画布
            Graphics g = Graphics.FromImage(image);
            //消除锯齿
            g.SmoothingMode = SmoothingMode.AntiAlias;
            //质量
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighQuality;

            //黑色画笔--主轴颜色
            Brush blackBrush = new SolidBrush(Color.FromArgb(255, 102, 102, 102));
            Pen blackPen = new Pen(blackBrush, 1);

            //灰色画笔--辅助线条颜色
            Brush grayBrush = new SolidBrush(Color.FromArgb(255, 224, 224, 224));
            Pen grayPen = new Pen(grayBrush, 1);

            //填充区域内容
            g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width + margin_left + margin_right + lineNameWidth, height + margin_top + margin_bottom);

            //y轴
            g.DrawLine(blackPen, margin_left, margin_top, margin_left, (height + margin_top));

            //x轴
            g.DrawLine(blackPen, margin_left, (height + margin_top), (width + margin_left), (height + margin_top));

            Font font = new Font("宋体", unitCvtr.ConvertUnits(fontsize, Spire.Pdf.Graphics.PdfGraphicsUnit.Pixel, Spire.Pdf.Graphics.PdfGraphicsUnit.Point));

            //x轴--辅助线

            //画5条辅助线,不管数字大小
            int avgCount = Convert.ToInt32(Math.Ceiling(maxCount / 5.0));

            // 为了适应后面的计算
            maxCount = avgCount * 5;

            int lineHeight = (height - xsubline) / 5;

            //画辅助线与文字
            for (int i = 0; i <= 5; i++)
            {
                //辅助线
                if (i > 0)
                {
                    g.DrawLine(grayPen, margin_left, (height + margin_top - lineHeight * i), (width + margin_left), (height + margin_top - lineHeight * i));
                }

                //指向文字的线
                g.DrawLine(blackPen, (margin_left - 5), (height + margin_top - lineHeight * i), margin_left, (height + margin_top - lineHeight * i));
                //文字
                int text = avgCount * i;

                RectangleF rec = new RectangleF(10, (height + margin_top - lineHeight * i - fontsize / 2), margin_left - 20, 20);
                StringFormat format = new StringFormat(StringFormatFlags.DirectionRightToLeft);
                g.DrawString(text.ToString(), font, blackBrush, rec, format);
            }

            //底部文字
            int singleWidth = width / (bottomData.Length - 1);

            for (int i = 0; i < bottomData.Length; i++)
            {
                StringFormat format = new StringFormat();
                format.Alignment = StringAlignment.Center; //居中

                //x轴下的文字
                //指向线
                g.DrawLine(blackPen, margin_left + i * singleWidth, (height + margin_top), margin_left + i * singleWidth, (height + margin_top + 5));
                //文字
                RectangleF rec = new RectangleF(margin_left + (i * singleWidth) - singleWidth / 2, (height + margin_top + 15), singleWidth, (margin_bottom - 20));
                g.DrawString(bottomData[i].ToString(), font, blackBrush, rec, format);
            }

            //预定颜色
            for (int i = 0; i < lineName.Length; i++)
            {
                //随机颜色
                Color tempColor = colors[i];//GetRandomColor();

                //文字内容
                StringFormat format = new StringFormat(StringFormatFlags.DirectionVertical);
                //format.Alignment = StringAlignment.Center; //居中

                //画笔
                SolidBrush brush = new SolidBrush(tempColor);
                Pen pen = new Pen(brush, 1);

                // 折线名称处理
                // 颜色块
                Rectangle rectangle = new Rectangle(margin_left + width + 10, margin_top + i * 25, 20, 20);
                g.FillRectangle(brush, rectangle);

                // 文字
                RectangleF rec = new RectangleF(margin_left + width + 10 + 25, margin_top + i * 25, 80, 20);
                g.DrawString(bottomData[i].ToString(), font, blackBrush, rec, format);

                //这里要开始画折线了
                Point[] points = new Point[lineData[i].Count];

                for (int j = 0; j < lineData[i].Count; j++)
                {
                    int x = j * singleWidth + margin_left;
                    int y = height + margin_top - Convert.ToInt32(lineData[i][j] / Convert.ToDouble(maxCount) * (height - xsubline));

                    Point point = new Point(x,y);

                    points[j] = point;
                }

                g.DrawLines(pen, points);
            }

            string relativePath = @"draw-image" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg";
            string path = Server.MapPath(relativePath);
            image.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);

            //return relativePath;
            return Json(relativePath, JsonRequestBehavior.AllowGet);
        }

样式

原文地址:https://www.cnblogs.com/zhoushangwu/p/11726420.html