Stacked Column Chart C# in Controller class

So, my problem is this. I have to insert a Stacked column chart into a web app. It won't be interacted with, it only needs to be displayed and refreshed occasionally.

At its most basic, this was the code I had in the controller class to produce a simple bar chart.

public ActionResult CharterHelp(int _completed, int _remaining)
{
    var workChart = new System.Web.Helpers.Chart(300, 500).AddTitle("Work Items");
    workChart .AddSeries(chartType: "column",
               xValue: new[] { "Completed", "Remaining" },
               yValues: new[] { _completed, _remaining })
               .Write("png");

    return null;
}

This prior code was working. However, I need to present it as a stacked column and... have been finding it difficult to get a clear answer or tutorial on how to make even a basic one without them being for proprietary charting software.

All the ones I have found for just straightforward C# and ASP.net have helped me to a point, but I haven't found one that gives me the equivalent code to ".write(png)".

I know that I need to use the SaveImage method, but this is being run out on a server where I can't just store the file in a static location. I need to create it on the fly.

This is about as far as I've gotten with the revamped code and would appreciate any help on this subject.

public ActionResult CharterHelp(int _completed, int _remaining)
{
    System.Web.UI.DataVisualization.Charting.Chart workChart = new System.Web.UI.DataVisualization.Charting.Chart();

    var seriesCompleted = new System.Web.UI.DataVisualization.Charting.Series
    {
            Name = "Completed",
            Color = System.Drawing.Color.Green,
            IsVisibleInLegend = false,
            IsXValueIndexed = true,
            ChartType = SeriesChartType.StackedColumn
    };
    var seriesRemaining = new System.Web.UI.DataVisualization.Charting.Series
    {
            Name = "Remaining",
            Color = System.Drawing.Color.Red,
            IsVisibleInLegend = false,
            IsXValueIndexed = true,
            ChartType = SeriesChartType.StackedColumn
    };

    workChart.Series.Add(seriesCompleted);
    workChart.Series.Add(seriesRemaining);

    seriesRemaining.Points.AddY(_completed);
    seriesCompleted.Points.AddY(_remaining);

    var chartArea1 = new System.Web.UI.DataVisualization.Charting.ChartArea
    {
            Name = "ChartArea1"
    };

    workChart.ChartAreas.Add(chartArea1);
    workChart.Width = 300;
    workChart.Height = 500;

    System.IO.MemoryStream imageStream = new System.IO.MemoryStream();
    workChart.SaveImage(imageStream, ChartImageFormat.Png);
    return null;
}

Upvotes: 1

Views: 418

Answers (1)

I found my answer after a lot of trial, error and searching... I never found a straightforward example of what I needed to do, but I was able to piece together a solution from the parts I found.

To do what I was trying to do, I needed to have the following code at the end of the actionresult method in the controller class:

        System.IO.MemoryStream chartStream = new System.IO.MemoryStream();
        workChart.SaveImage(chartStream, ChartImageFormat.Png);
        return File(chartStream.ToArray(), ("chart/png"));

Upvotes: 1

Related Questions