Saturday 25 May 2019

MVC - Create simple Chart


Watch this example on YouTube


 
1. Home Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Helpers;

namespace WebApplication7.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult GetChartImage()
        {
            var key = new Chart(width: 300, height: 300)
                .AddTitle("Employee Chart")
                .AddSeries(
                chartType: "Bubble",
                name: "Employee",
                xValue: new[] {"Frank", "John", "Bob", "Ann"},
                yValues: new[] {"33", "22", "55", "44"}
                );
            return File(key.ToWebImage().GetBytes(), "image/jpeg");
        }

        public ActionResult Index()
        {
            return View();
        }


2. View

@{
    ViewBag.Title = "Home Page";
}


<img src="@Url.Action("GetChartImage")" />

No comments:

Post a Comment