Detailed examples of ASP. net mvc using chart controls

Source: Internet
Author: User

 

In. net 3.5, Microsoft provided a chart control. There are a lot of articles on using this control in vs2008 on the network. In vs2010, this control has been integrated into ASP.. Net 4.0, which can be used directly from the toolbox.

 

 

 

 

This control is easy to use in ASP. NET classic pages, but it may cause various problems when used in ASP. net mvc.

 

 

 

 

Some online users can do this by adding a method in global. asax.

 

Code [http://www.oeedu.com]

 1         protected void Session_Start()
2 {
3 string id = this.Session.SessionID;
4 }

 

 

 

The added result is as follows.

 

 

 

 

What should we do?

 

 

 

Because I didn't see the source code of the chart, from the error message, we can see that there is a problem in handler processing.

 

 

 

There are many articles on configuration on the network, such:

 

Scottgu's built-in charting controls (vs 2010 and. Net 4 Series)

 

Michael ceranski's building a dashboard using the Microsoft chart controls

 

Handling chart generated images using chart HTTP handler of Delian tchoparinov

 

 

 

However, unfortunately, it is either outdated, and most configurations are not required in vs2010, or the problem is not solved.

 

 

 

In ASP. net MVC, the processing program mechanism has changed a lot. Instead of directly generating results through the processing program, it passes through multiple transfers from the processing program to the controler to the action. It is completely different from the ASP. NET Classic mode.

 

 

 

Nic_rocheStreaming chart images as fileresult from MVC controllers provides an idea of filling in an image reference in the view, and then returning the image through an action. Unfortunately, the download fails.

 

 

 

There are no errors in the direction, but there are some small points to note.

 

 

 

Next, we will implement ASP. net mvc in vs2010.

 

 

 

1. Create an ASP. net mvc project.

 

2. Insert an image reference in the View index. aspx. The referenced address is an action.

 

 

 

 

3. Add model: staticmodel. CS to the models of the project to provide data.

 

 

 

 

 

Code [http://www.oeedu.com]

  1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Linq;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.HtmlControls;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.WebControls.WebParts;
11 using System.Xml.Linq;
12 using System.Collections.Generic;
13
14 namespace mvcChart.Models
15 {
16 public class StaticModel
17 {
18 public static List<int> createStaticData()
19 {
20 List<int> c_data = new List<int>();
21 c_data.Add(1);
22 c_data.Add(6);
23 c_data.Add(4);
24 c_data.Add(3);
25 return c_data;
26 }
27 }
28 }
29

 

4. Add an action named getchart to the home controler.

 

Note:

 

1. This action returns a fileresult, that is, a file. In this method, filestreamresult, a derived class of fileresult, is used to return an image through the stream.

 

2. In row 33, set the current position of the stream to the starting position to read the generated image data. Imagestream. Position = 0;

 

Code [http://www.oeedu.com]

  1  public FileResult GetChart()
2 {
3 List<int> data = Models.StaticModel.createStaticData();
4 System.Web.UI.DataVisualization.Charting.Chart Chart2 = new System.Web.UI.DataVisualization.Charting.Chart();
5 Chart2.Width = 412;
6 Chart2.Height = 296;
7 Chart2.RenderType = System.Web.UI.DataVisualization.Charting.RenderType.ImageTag;
8 Chart2.Palette = ChartColorPalette.BrightPastel;
9 Title t = new Title("IMG source streamed from Controller", Docking.Top, new System.Drawing.Font("Trebuchet MS", 14, System.Drawing.FontStyle.Bold), System.Drawing.Color.FromArgb(26, 59, 105));
10 Chart2.Titles.Add(t);
11 Chart2.ChartAreas.Add("Series 1");
12 // create a couple of series
13 Chart2.Series.Add("Series 1");
14 Chart2.Series.Add("Series 2");
15 // add points to series 1
16 foreach (int value in data)
17 {
18 Chart2.Series["Series 1"].Points.AddY(value);
19 }
20 // add points to series 2
21 foreach (int value in data)
22 {
23 Chart2.Series["Series 2"].Points.AddY(value 1);
24 }
25 Chart2.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
26 Chart2.BorderlineWidth = 2;
27 Chart2.BorderColor = System.Drawing.Color.Black;
28 Chart2.BorderlineDashStyle = ChartDashStyle.Solid;
29 Chart2.BorderWidth = 2;
30 Chart2.Legends.Add("Legend1");
31 MemoryStream imageStream = new MemoryStream();
32 Chart2.SaveImage(imageStream, ChartImageFormat.Png);
33 imageStream.Position = 0;
34 return new FileStreamResult(imageStream, "image/png");
35 }

 

 

 

The final result is as follows:

 

 

 

 

Source File Download

 

From: http://www.oeedu.com/contents/1237/10957.html

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.