First, we need to add reference to the graph namespace, System. Web. DataVisualization
# Region obtain the registered user Curve
Public FileResult GetTongjiChart (string sTime, string eTime, string adType, string keyWord)
{
// You can skip this part.
ViewData ["Select"] = "Extend ";
ViewData ["onpage"] = "Tongji ";
DateTime startTime;
If (string. IsNullOrEmpty (sTime) |! DateTime. TryParse (sTime, out startTime ))
{
StartTime = Convert. ToDateTime (DateTime. Now. tow.datestring (). AddDays (-30 );
}
DateTime endTime;
If (string. IsNullOrEmpty (eTime) |! DateTime. TryParse (eTime, out endTime ))
{
EndTime = Convert. ToDateTime (DateTime. Now. tow.datestring ());
}
List <Tongji> tongjiList = new List <Tongji> ();
If (string. IsNullOrEmpty (adType) & string. IsNullOrEmpty (keyWord ))
{
TongjiList = tongjiCore. GetModels ();
}
Else if (string. IsNullOrEmpty (adType )&&! String. IsNullOrEmpty (keyWord ))
{
// Search by keyword
TongjiList = tongjiCore. GetModelsByKeyWord (keyWord );
}
Else if (! String. IsNullOrEmpty (adType )&&! String. IsNullOrEmpty (keyWord ))
{
// Based on keywords and promotion methods
TongjiList = tongjiCore. GetModelsBySearch (adType, keyWord );
}
Else
{
// Based on the Promotion Method
TongjiList = tongjiCore. GetModelsByAdType (adType );
}
// Filter by Time
Var newList = from c in tongjiList
Where c. CreateTime> = startTime & c. CreateTime <= endTime. AddDays (1)
Select c;
// Group by Time
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 ()
};
// We get an object set. The class in the SET has two members: count and time.
// Indicates the number of people on the day of time. Here is the number of registrants on this day.
// Create a graph object
System. Web. UI. DataVisualization. Charting. Chart Chart2 = new System. Web. UI. DataVisualization. Charting. Chart ();
// Set the width and height
Chart2.Width = 958;
Chart2.Height = 400;
// Output as an image.
Chart2.RenderType = System. Web. UI. DataVisualization. Charting. RenderType. ImageTag;
// Set the title object and add the title object to the graph.
Title title Title = new Title (startTime. ToString ("yyyy-MM-dd") + "~ "+ EndTime. ToString (" yyyy-MM-dd ") +" time period user profile ");
Chart2.Titles. Add (title );
// Drawing Area
Chart2.ChartAreas. Add ("series ");
Series series1 = new Series ();
Series1.Name = "Number of registrants ";
// Data display method Line: Line Spline: Curve
Series1.ChartType = System. Web. UI. DataVisualization. Charting. SeriesChartType. Spline;
// Line color
Series1.Color = Color. Green;
// Line width
Series1.BorderWidth = 2;
// Shadow width
Series1.ShadowOffset = 1;
If (list. Count () <= 200)
{
Series1.IsVisibleInLegend = true; // indicates whether line data is displayed.
Series1.IsValueShownAsLabel = true; // whether the specified data is on the line
Series1.MarkerStyle = MarkerStyle. Circle; // data point Flag type on the line
Series1.MarkerSize = 8; // flag size
Series1.LabelForeColor = Color. Red;
}
// Memory stream
MemoryStream imageStream = new MemoryStream ();
Double totalNum = 0;
Foreach (TongjiUser model in list)
{
// Add data to X and Y axes respectively (multiple data types can be added)
Series1.Points. AddXY (model. Time. ToString ("yyyy-MM-dd"), model. Count );
TotalNum + = model. Count;
}
String strSarr = "Total:" + totalNum + "average:" + Math. round (totalNum/(endTime-startTime ). days + 1), 2) + "registration rate:" + 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 = "Number of registrants ";
Chart2.ChartAreas [0]. AxisX. Title = strSarr;
/// Set the color of the X and Y lines in the middle
// 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