views:

366

answers:

1

Using the answer Generating the Image from a Controller from this post, I created a controller action to return a chart image as seen below (the X and Y values are just there as test data):

    public FileContentResult HistoryChart()
    {
        Chart chart = new Chart();
        string[] currencies = { "ZAR", "USD", "GBP", "JPY" };

        foreach (string currency in currencies)
        {
            Series series = new Series(currency);
            series.ChartType = SeriesChartType.FastLine;
            for (int x = 0; x <= 30; x++)
                series.Points.AddXY(x, (x * 5));
            chart.Series.Add(series);
        }

        using (MemoryStream ms = new MemoryStream())
        {
            chart.SaveImage(ms, ChartImageFormat.Png);
            ms.Seek(0, SeekOrigin.Begin);

            return File(ms.ToArray(), "image/png", "mychart.png");
        }
    }

The problem is, the image that the controller returns is blank (although it DOES return an image)

Im hoping its something simple that I have left out! Any input would be appreciated, thanks.

+3  A: 

Hope this helps.....

I've had the same problem:

It's all to do with colors, I added some code to yours after having used another example from this blog and deduced the issue from that - so 'Thanks' to everyone....

    public FileContentResult HistoryChart()
    {
        Chart chart = new Chart();
        **chart.BackColor = Color.Transparent;**

        string[] currencies = { "ZAR", "USD", "GBP", "JPY" };

        foreach (string currency in currencies)
        {
            Series series = new Series(currency);
            series.ChartType = SeriesChartType.FastLine;
            for (int x = 0; x <= 30; x++)
                series.Points.AddXY(x, (x * 5));
            chart.Series.Add(series);
        }

        **ChartArea ca1 = new ChartArea("ca1");
        ca1.BackColor = Color.Cyan;
        chart.ChartAreas.Add(ca1);**

        using (MemoryStream ms = new MemoryStream())
        {
            chart.SaveImage(ms, ChartImageFormat.Png);
            ms.Seek(0, SeekOrigin.Begin);

            return File(ms.ToArray(), "image/png", "mychart.png");
        }
    } 

Also, you will need to ensure that your controller has:

using System.Drawing; using System.Web.UI.WebControls;

Cheers to all...

JK.

Jeff
Legend - thank you.
Jimbo