Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use chart control in mvc

Tags:

asp.net-mvc

I want to use Chart in my MVC Dashboard form.

I have used following code.

@{
        var key = new Chart(width: 300, height: 300)
          .AddTitle("Employee Chart")
          .AddSeries(
              chartType: "Bubble",
              name: "Employee",
              xValue: new[] { "Peter", "Andrew", "Julie", "Dave" },
              yValues: new[] { "2", "7", "5", "3" });     
    }
    <!DOCTYPE html>
    <html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <div> 
            <div>
                @key.Write()
            </div>
        </div>
    </body>
    </html>

but @key.Write() writes chart in whole page but I want it in partial form not in whole form with other content in Dashboard page. Can anybody help me for this.

like image 562
SanketS Avatar asked Apr 26 '13 11:04

SanketS


1 Answers

Move your chart code to controller action like this:

    public ActionResult GetChartImage()
    {
        var key = new Chart(width: 300, height: 300)
            .AddTitle("Employee Chart")
            .AddSeries(
            chartType: "Bubble",
            name: "Employee",
            xValue: new[] { "Peter", "Andrew", "Julie", "Dave" },
            yValues: new[] { "2", "7", "5", "3" });

        return File(key.ToWebImage().GetBytes(), "image/jpeg");
    }

(include using System.Web.Helpers namespace) and modify your view:

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
</head>
<body>
    <div> 
        <div>
            <img src="@Url.Action("GetChartImage")" />
        </div>
    </div>
</body>
</html>
like image 123
kgm Avatar answered Sep 28 '22 02:09

kgm