实现效果图:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing.Imaging;
using System.Drawing;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//string[] month = new string[12] { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" };
//float[] d = new float[12] { 20.5f, 60, 10.8f, 15.6f, 30, 70.9f, 50.3f, 30.7f, 70, 50.4f, 30.8f, 20 }; //图中的点的数据
string[] month = new string[15] { "2", "4", "6", "8", "10", "12", "14", "16", "18", "20", "22", "24", "26", "28", "30" };
float[] d = new float[15] { 0.03f, 0.025f, 0.04f, 0.02f, 0.045f, 0.04f, 0.04f, 0.05f, 0.06f, 0.03f, 0.04f, 0.03f, 0.01f, 0.02f, 0.04f };
//画图初始化
Bitmap bmap = new Bitmap(800, 800);
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 + 105, cpt.Y), new PointF(cpt.Y+90, cpt.Y - 8), new PointF(cpt.Y+90, cpt.Y + 8) };
//y轴端点三角形
PointF[] ypt = new PointF[3] { new PointF(cpt.X, cpt.X - 15), new PointF(cpt.X - 8, cpt.X), new PointF(cpt.X + 8, 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+90, cpt.Y);
gph.DrawPolygon(Pens.Black, xpt);
gph.FillPolygon(new SolidBrush(Color.Black), xpt);
//画x轴标题
gph.DrawString("日期(三月份)", new Font("宋体", 12), Brushes.Black, new PointF(cpt.Y + 100, 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);
//画y轴标题
gph.DrawString("发电量(单位:千瓦时)", new Font("宋体", 12), Brushes.Black, new PointF(0, 7));
for ( int i = 1; i <=15; i++)
{
//画y轴刻度
if (i < 7)
{
gph.DrawString((i * 0.01).ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X - 30, cpt.Y - i * 30 - 6));
gph.DrawLine(Pens.Black, cpt.X - 3, cpt.Y - i * 30, cpt.X, cpt.Y - i * 30);
}
//画x轴项目
gph.DrawString(month[i - 1], new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + i * 30 - 5, cpt.Y + 5));
//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));
// if (month[i - 1].Length > 2) gph.DrawString(month[i - 1].Substring(2, 1), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + i * 30 - 5, cpt.Y + 35));
//画点
gph.DrawEllipse(Pens.Black, cpt.X + i * 30 - 1.5f, cpt.Y - d[i - 1] * 3000 - 1.5f, 3, 3);
gph.FillEllipse(new SolidBrush(Color.Black), cpt.X + i * 30 - 1.5f, cpt.Y - d[i - 1] * 3000 - 1.5f, 3, 3);
//画数值
gph.DrawString(d[i - 1].ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + i * 30, cpt.Y - d[i - 1] * 3000));
//画折线
if (i > 1) gph.DrawLine(Pens.Red, cpt.X + (i - 1) * 30, cpt.Y - d[i - 2] * 3000, cpt.X + i * 30, cpt.Y - d[i - 1] *3000);
}
//保存输出图片
//bmap.Save(Response.OutputStream, ImageFormat.Gif);
// PictureBox1.Image = bmap;
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
gph.Dispose();
bmap.Dispose();
}
}
本文主要参考:
http://blog.csdn.net/yiyiwyy326/article/details/1702979