Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>Here's the solution I came up with:</p> <p><a href="http://blog.smirne.com/2012/09/creating-interactive-charts-with-aspnet.html">http://blog.smirne.com/2012/09/creating-interactive-charts-with-aspnet.html</a></p> <p>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.</p> <p><strong>UPDATE</strong></p> <p>Here's code as requested:</p> <p>CONTROLLER:</p> <pre><code>public ActionResult Chart() { var chart = buildChart(); StringBuilder result = new StringBuilder(); result.Append(getChartImage(chart)); result.Append(chart.GetHtmlImageMap("ImageMap")); return Content(result.ToString()); } </code></pre> <p>Utility Functions:</p> <pre><code>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 = "&lt;img src='data:image/png;base64,{0}' alt='' usemap='#ImageMap'&gt;"; chart.SaveImage(stream, ChartImageFormat.Png); string encoded = Convert.ToBase64String(stream.ToArray()); return String.Format(img, encoded); } } </code></pre> <p>VIEW:</p> <pre><code>@{ Html.RenderAction("Chart"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload