Rob
Rob

Reputation: 1065

Display Chart in .cshtml

I've been using Stack Overflow pretty heavily to implement the Chart Control using C#

public class ASPChartPartialViewController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        private void CreateChart()
        {
            Chart myChart = new Chart();

            //Create a new series for the chart
            var series = new Series
            {
                Name = "Test Series",
                Color = System.Drawing.Color.Green,
                IsVisibleInLegend = true,
                IsXValueIndexed = true,
                ChartType = SeriesChartType.Line
            };

            //Bind the data to the new series
            series.Points.DataBindXY(new[] { 2001, 2002, 2003, 2004 }, new[] { 100, 200, 90, 150 });

            //Add the series to the chart
            myChart.Series.Add(series);
        }
    }

This is the controller class I'm using to start. The question is, how do I display this in the .cshtml view? MSDN documentation isn't really giving me much help with this.

Upvotes: 0

Views: 2060

Answers (1)

COLD TOLD
COLD TOLD

Reputation: 13579

If you are using ASP.net chart controll you have to make sure that in your controller you return the chart as the FileContentResult so the implementation would be something like

   public ActionResult Index()
        {
            return View(GetChart().FileContents);
        }
  public FileContentResult GetChart()
    {
     Chart myChart = new Chart();
    ///when you setup your chart you can 
    myChart.ToWebImage(format: "png");
    return new FileContentResult(myChart.GetBytes(), myChart.ImageFormat);

    }

in the view part you can try

@model  byte[]

<img src="data:image/jpg;base64,@(Html.Raw(Convert.ToBase64String((byte[])Model)))" alt="" />

Upvotes: 2

Related Questions