Monday, 13 May 2019
MVC - Charting - Create simple chart
Watch this example on YouTube
Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.DataVisualization.Charting;
using System.IO;
using System.Text;
using System.Drawing;
namespace MVCCharting.Controllers
{
public class HomeController : Controller
{
public ActionResult Chrt1()
{
var data = new Dictionary<string, float>
{
{"test1", 10.1f },
{"test2", 1.1f },
{"test3", 22.3f },
{"test4", 30.1f },
{"test5", 13.1f },
{"test6", 82.3f }
};
var chart = new Chart();
var area = new ChartArea();
chart.ChartAreas.Add(area);
var series = new Series();
foreach (var item in data)
{
series.Points.AddXY(item.Key, item.Value);
}
series.Label = "Some Label";
series.Font = new Font("Arial", 10.0f, FontStyle.Bold);
series.ChartType = SeriesChartType.Column;
series["PieLabelStyle"] = "Outside";
chart.Series.Add(series);
var returnStream = new MemoryStream();
chart.ImageType = ChartImageType.Png;
chart.SaveImage(returnStream);
returnStream.Position = 0;
return new FileStreamResult(returnStream, "image/png");
}
View
@{
ViewBag.Title = "Chrt1";
}
<h2>Chrt1</h2>
<img src="@Url.Action("Chrt1")" />
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment