Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft Charting, MVC 3 and Razor

Related to This topic I wonder if anyone has made the Microsoft Charting library working with Asp MVC 3 and Razor.

I know about the new chart helper introduced, but since that is very limited that is not really an option.

To create an action method that returns an image is also easy enough, but since all interactivity breaks down (even just simple tooltips for the bars in a bar chart) this method has several limitations.

This example is probably the most helpful article I have found, but I still cant get a single easy chart working even though it does work when rendering the image only in an action method. Also I have got the samples working fine under .net 4, but obviously those arent MVC samples.

SO - has anyone got Microsoft charting working fully in Asp MVC 3 with Razor and could post a link to a complete solution?

like image 558
Victor Avatar asked Jan 19 '11 14:01

Victor


People also ask

Can you use MVC with razor pages?

You can add support for Pages to any ASP.NET Core MVC app by simply adding a Pages folder and adding Razor Pages files to this folder. Razor Pages use the folder structure as a convention for routing requests.

Should I use razor pages or MVC?

From the docs, "Razor Pages can make coding page-focused scenarios easier and more productive than using controllers and views." If your ASP.NET MVC app makes heavy use of views, you may want to consider migrating from actions and views to Razor Pages.

What is the difference between MVC and Razor?

A Razor Page is almost the same as ASP.NET MVC's view component. It has basically the syntax and functionality same as MVC. The basic difference between Razor pages and MVC is that the model and controller code is also added within the Razor Page itself. You do not need to add code separately.

What is MVC Razor view?

Razor is a templating engine and ASP.NET MVC has implemented a view engine which allows us to use Razor inside of an MVC application to produce HTML. However, Razor does not have any ties with ASP.NET MVC. Now, Razor Syntax is compact which minimizes the characters to be used, however it is also easy to learn.


2 Answers

If it is tool tip and drill down you are looking for then here is a sample. I tried and worked as a charm for me. You need to have ImageMap linked with your image to have interactivity.

MVC Charts with Interactivity

like image 109
Sarath Avatar answered Sep 22 '22 06:09

Sarath


I spent a few days looking for a solution that creates interactive charts in MVC, but all the examples I've seen were way more complicated than they have to be.

The answer from Sarath is almost perfect, but it saves the image twice which is not very efficient. With the function Html.RenderAction() we can get everything done in one pass and make it as efficient as it can be.

Here's the solution I came up with:

http://blog.smirne.com/2012/09/creating-interactive-charts-with-aspnet.html

I haven't seen any solution that can do everything in one pass. The best part of this is that it doesn't need any modifications to the web.config file. It also doesn't try to use an ASP.net control in MVC. It's pure MVC technology.

UPDATE

Here's code as requested:

CONTROLLER:

public ActionResult Chart() {     var chart = buildChart();     StringBuilder result = new StringBuilder();     result.Append(getChartImage(chart));     result.Append(chart.GetHtmlImageMap("ImageMap"));     return Content(result.ToString()); } 

Utility Functions:

private Chart buildChart() {     // Build Chart     var chart = new Chart();      // Create chart here     chart.Titles.Add(CreateTitle());     chart.Legends.Add(CreateLegend());     chart.ChartAreas.Add(CreateChartArea());     chart.Series.Add(CreateSeries());      return chart; }  private string getChartImage(Chart chart) {     using (var stream = new MemoryStream())     {        string img = "<img src='data:image/png;base64,{0}' alt='' usemap='#ImageMap'>";        chart.SaveImage(stream, ChartImageFormat.Png);        string encoded = Convert.ToBase64String(stream.ToArray());        return String.Format(img, encoded);     } } 

VIEW:

@{ Html.RenderAction("Chart"); } 
like image 43
smirne Avatar answered Sep 23 '22 06:09

smirne