我一直在使用Stack Overflow来实现使用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);
}
}
这是我用来启动的控制器类。问题是,如何在.cshtml视图中显示它? MSDN文档并没有给我很多帮助。
答案 0 :(得分:1)
如果您正在使用ASP.net图表控件,您必须确保在控制器中将图表作为FileContentResult返回,因此实现将类似于
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);
}
在视图部分中,您可以尝试
@model byte[]
<img src="data:image/jpg;base64,@(Html.Raw(Convert.ToBase64String((byte[])Model)))" alt="" />