.NET统计生成曲线图
首先我们需要对曲线图的命名空间添加引用,System.Web.DataVisualization
#region 获取注册用户曲线图
public FileResult GetTongjiChart(string sTime, string eTime, string adType, string keyWord)
{
//这部分可以不看。
ViewData["Select"] = "Extend";
ViewData["onpage"] = "Tongji";
DateTime startTime;
if (string.IsNullOrEmpty(sTime) || !DateTime.TryParse(sTime, out startTime))
{
startTime = Convert.ToDateTime(DateTime.Now.ToShortDateString()).AddDays(-30);
}
DateTime endTime;
if (string.IsNullOrEmpty(eTime) || !DateTime.TryParse(eTime, out endTime))
{
endTime = Convert.ToDateTime(DateTime.Now.ToShortDateString());
}
List<Tongji> tongjiList = new List<Tongji>();
if (string.IsNullOrEmpty(adType) && string.IsNullOrEmpty(keyWord))
{
tongjiList = tongjiCore.GetModels();
}
else if (string.IsNullOrEmpty(adType) && !string.IsNullOrEmpty(keyWord))
{
//根据关键字搜索
tongjiList = tongjiCore.GetModelsByKeyWord(keyWord);
}
else if (!string.IsNullOrEmpty(adType) && !string.IsNullOrEmpty(keyWord))
{
//根据关键字和推广方式
tongjiList = tongjiCore.GetModelsBySearch(adType, keyWord);
}
else
{
//根据推广方式
tongjiList = tongjiCore.GetModelsByAdType(adType);
}
//通过时间筛选
var newList = from c in tongjiList
where c.CreateTime >= startTime && c.CreateTime <= endTime.AddDays(1)
select c;
//按照时间分组
var list = from c in newList
where c.UserId>0
group c by c.CreateTime.ToString("yyyy-MM-dd") into g
orderby g.Key
select new TongjiUser
{
Time = Convert.ToDateTime(g.Key),
Count = g.Count()
};
//前面我们得到了一个对象集合。集合里面的类有2个成员,一个是count,一个是time。
//表示在time这一天的人数。我这里是表示这一天的注册人数。
//下面创建曲线图对象
System.Web.UI.DataVisualization.Charting.Chart Chart2 = new System.Web.UI.DataVisualization.Charting.Chart();
//设置宽高
Chart2.Width = 958;
Chart2.Height = 400;
//以图片形式输出。
Chart2.RenderType = System.Web.UI.DataVisualization.Charting.RenderType.ImageTag;
//设置标题对象,将标题对象添加到曲线图中。
Title title = new Title(startTime.ToString("yyyy-MM-dd") + "~" + endTime.ToString("yyyy-MM-dd") + "时间段的推广注册用户曲线图");
Chart2.Titles.Add(title);
//绘图区域
Chart2.ChartAreas.Add("series");
Series series1 = new Series();
series1.Name = "注册人数曲线";
//数据显示方式 Line:为折线 Spline:曲线
series1.ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Spline;
//线条颜色
series1.Color = Color.Green;
//线条宽度
series1.BorderWidth = 2;
//阴影宽度
series1.ShadowOffset = 1;
if (list.Count() <= 200)
{
series1.IsVisibleInLegend = true; //是否显示线条数据说明
series1.IsValueShownAsLabel = true; //线条上是否指定的数据
series1.MarkerStyle = MarkerStyle.Circle; //线条上的数据点标志类型
series1.MarkerSize = 8; // 标志的大小
series1.LabelForeColor = Color.Red;
}
//内存流
MemoryStream imageStream = new MemoryStream();
double totalNum = 0;
foreach (TongjiUser model in list)
{
//分别往X,Y轴添加数据(可以为多种类型) (有多中添加方式)
series1.Points.AddXY(model.Time.ToString("yyyy-MM-dd"), model.Count);
totalNum += model.Count;
}
string strSarr = " 总数:" + totalNum + " 平均值:" + Math.Round(totalNum /((endTime-startTime).Days+1), 2) + "注册率:" + Math.Round(totalNum / newList.Count(), 4) * 100 + "%";
series1.BorderColor = System.Drawing.Color.Gray;
series1.ChartArea = "series";
Chart2.Series.Add(series1);
Chart2.ChartAreas[0].AxisY.Title = "注册人数曲线";
Chart2.ChartAreas[0].AxisX.Title = strSarr;
////中间X,Y线条的颜色设置
//Chart2.ChartAreas[0].AxisX.LineDashStyle = ChartDashStyle.DashDotDot;
//Chart2.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Blue;
Chart2.Legends.Add("Legend1");
Chart2.SaveImage(imageStream, ChartImageFormat.Png);
imageStream.Position = 0;
return new FileStreamResult(imageStream, "image/png");
}
#endregion