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:
- Use the image directly without having to write it to a file
- 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.