I would like to create multiple charts dynamically within my controller. When I instantiate a new Chart control in my for each it renders a blank chart. I am wondering what can be done to allow me to create and populate multiple charts on one page with the code below.
public ActionResult BuildChart()
{
var chart = new System.Web.UI.DataVisualization.Charting.Chart()
{
Width = 576,
Height = 100,
BackColor = System.Drawing.Color.White,
};
GetPersonas();
GetSenarioVoting();
List<CIEToolRole.Models.ScenarioModel> scenarios = GetSenarioVotingResults();
int i = 0;
foreach (ScenarioModel scenario in scenarios)
{
chart = new System.Web.UI.DataVisualization.Charting.Chart()
{
Width = 576,
Height = 100,
BackColor = System.Drawing.Color.White,
};
chart.ID = "Chart" + i;
Series mySeries = new Series();
mySeries.ChartType = SeriesChartType.Column;
mySeries.BorderWidth = 1;
mySeries.Palette = ChartColorPalette.Chocolate;
mySeries.Name = "series" + i;
List<int> voteVal = scenario.Votes.Select(p => p.Value).ToList();
foreach (int vote in voteVal)
{
DataPoint point = new DataPoint();
point.YValues = new double[] { vote };
mySeries.Points.Add(point);
}
chart.Series.Add(mySeries);
i++;
}
ChartArea ca1 = new ChartArea("ca1");
ca1.BackColor = System.Drawing.Color.LightGray;
ca1.AxisY.LineColor = System.Drawing.Color.LightBlue;
chart.ChartAreas.Add(ca1);
var imgStream = new System.IO.MemoryStream();
chart.SaveImage(imgStream, ChartImageFormat.Png);
imgStream.Seek(0, System.IO.SeekOrigin.Begin);
return File(imgStream, "image/png");
}