3

I'm trying to use an a chart generation library (ChartDirector) in ASP MVC3.

ChartDirector is generating a bar chart and an HTML image map based on a fairly large data structure.

I am looking for a way to:

  1. Use the image directly without having to write it to a file
  2. Use the generated HTML image map

I can generate an image and display it just fine:

public ActionResult barChart() // ImageController
{
    // Code to make chart from hard-coded data
    var imageBytes = chart.makeChart2(Chart.PNG);
    var imageMap = chart.getHTMLImageMap();
    FileContentResult byteStream = new FileContentResult(image, "image/png");
    return byteStream;
}

And then reference the controller directly inside one of the views with something like:

<img src="/Image/barChart"/>

The problem is that I can't find a good way to get the necessary data from which to build the bar chart to the controller.

  • I can't have a controller add the image data to a model, because HTML requires the image is linked from some URI -- a controller.
  • None of the HTML helpers seem to allow me to send a model to a controller
  • I've looked into making an HTML helper and an ImageResult type, but it doesn't solve the problem of the image map and the only method I could find to build the URI, "BuildUrlFromExpression", doesn't seem to exist in MVC3.

One way I can accomplish all this is to put the model needed to generate the chart into TempData, then call the controller which will use TempData, and have the controller spit the HTML image map into TempData as well, which will be read by the view. That seems remarkably ugly to me.

I can probably store the data by some sort of session ID, but that doesn't seem much better.

I've looking at (among others): Can an ASP.NET MVC controller return an Image? but it doesn't apply because that controller is reading some pre-generated image.

Community
  • 1
  • 1
Charles Burns
  • 10,310
  • 7
  • 64
  • 81
  • I'm a bit confused here. To clarify: you have request for a webpage based on some model. Inside that webpage is an image that's a chart based on the data in the model. Your problem is that you can't get the necessary data to the chart generator. Is that correct? – Tridus Jul 24 '11 at 22:14
  • @Tridus: Yes, one of two questions is about passing the data to the chart generator, which is called from within a model that returns the final image. The second is that the chart generator builds not only an image, but an image map for use in the HTML. Both can be resolved using lots of data passing through TempData, but that requires the controller and view have knowledge of what the other uses as the key. It works, but I am asking if anyone has a clean (or at least better) solution. – Charles Burns Jul 24 '11 at 23:04

2 Answers2

1

If I understand your problem, you are just looking for a way to supply model data to the controller to use to generate the image?

The URL in your img tag should be able to include parameters just like any other URL. That is, you should be able to do:

<img src="/Home/ImageData/4?src=maps" />

and it will call this method:

class HomeController
{
    public ActionResult ImageData ( int id, string src )
    {
    }
}

Generating the links in the view would then just be a matter of using UrlHelper.ActionLink and supplying the correct parameters.

Michael Edenfield
  • 28,070
  • 4
  • 86
  • 117
  • Thank you for the response. An issue is that the controller hasn't enough data from an ID to generate the image. I can pre-generate it and use an ID, but I am not sure how to get the necessary data (currently a model with about 500K of numbers) to the controller. It wouldn't fit on a URI. Perhaps I can delay the model generation and pass the same form data to the image controller as to the HTML controller -- just like you showed above, but with a more complex URI. That would solve the "get data to generator" problem but would leave the "Get the map back from it" problem. Ah, web development :) – Charles Burns Jul 25 '11 at 15:39
0

To me, the issue here is that you're trying to combine too many things (which shows as the question actually has several different questions in it). The image request is a separate request from the HTML request. Look at it this way - what happens if I copy the image URL and send it to someone else because I want to show them the chart and not the rest of the page? Hot linking happens.

To deal with that, you can't generate the image when someone hits the controller for the HTML page. You need to do it when that second request is made specifically for the chart. So your image request actually has to look very similar to the request for the webpage itself: it needs to contain enough information in the URL that the controller serving the image can get the necessary model data back from the database (or wherever it's coming from) in order to generate the chart. So if the controller action that creates the webpage you want to display the chart on is this:

public ActionResult Details(int id)

Then in the same controller you need another action like this:

public ActionResult DetailsChart(int id)

The second one will go get the data, use the chart library you have to create the image, then return it. You'll reference it like this:

<img src="/SomeController/DetailsChart/6" />

So that gets you the chart on the webpage, or wherever it was requested from. Now to display it without having to read the image off disk, you need to tell your chart control to not write to disk. If it's got another MakeChart() that writes to a Stream then you're good to go because you can write it to Response.OutputStream (or to a MemoryStream if you don't want to write it to the HTTP response right away for some reason).

As for the ImageMap... that's another thing again. ImageMaps are either HTML put on the page that tells the browser where to go depending on where the user clicks, or a server one where you need a third controller action to take a click on the image and handle it. You don't send that at the same time as you're sending the image, because that stuff's not image data.

Tridus
  • 5,021
  • 1
  • 19
  • 19
  • Note I have different controllers for the HTML page and the image: The controller shown in the question returns a FileContentResult (image bytes). Hot linking the image will result in a blank image (as-is) since the controller lacks data it needs, generated in a model based on form data. An issue with separation is the image software generates the chart and map at the same time, so to separate them out would require either sharing/saving a lot of state or generating the chart/map twice. It's a _large_ chart (up to 30,000px wide) and can take a few seconds even on a fast server. – Charles Burns Jul 25 '11 at 15:28
  • Strictly speaking you don't need different controllers, you just need different methods for the two things, but that doesn't really matter for this purpose. I guess the best option here is to generate the image as part of the first method (when you generate the HTML), and either write it to disk like you are now or stick it into a server cache like HttpContext.Cache. Using that you just have your view output a key for the image, and the method that retrieves it simply pulls the image out of the cache based on the key. – Tridus Jul 25 '11 at 15:44
  • I haven't really found a satisfying solution, but I implemented both using a cache as you suggested and using TempData. Both work fine, they just don't make me smile when I look at the code. Hopefully Microsoft can later implement a more elegant way to deal with binary image generation in MVC. – Charles Burns Jul 27 '11 at 03:03