Search code examples
asp.net-mvc-3graphael

graphael charts with dynamic data from database using ASP.NET MVC3


I would like to understand how to get data from a database and show that data inside a graphael chart.
I'm working in ASP.NET MVC3 and I really don't understand how to make the things working.
Any help is really appreciated.


Solution

  • great I solved the problem! I think that could be useful for someone else too:

    here it is the Controller code:

     public ActionResult ActionName()
            {
                List<String> data = new List<String>();
                data.Add("legend0_4");
                data.Add("legend1_5");
                data.Add("legend2_8");
                data.Add("legend3_12");
                data.Add("legend4_8");
    
                return Json(data, JsonRequestBehavior.AllowGet);
            }
    

    here it is the ajax side that also creates the pie chart:

    jQuery.getJSON("/ControllerName/ActionName", function (data) {

    // # Init
    var levels = [], values = [];
    
    
    for (var id in data) {
        var compo = data[id].split("_");
        levels.push("%%.%% " + compo[0]);
        values.push(parseInt(compo[1]));
    }
    
    var r = Raphael("accountCharts"),
                pie = r.piechart(320, 240, 100, values, { legend: levels, legendpos: "west", href: ["http://raphaeljs.com", "http://g.raphaeljs.com"] });
    
    r.text(320, 100, "Interactive Pie Chart").attr({ font: "20px sans-serif" });
    pie.hover(function () {
        this.sector.stop();
        this.sector.scale(1.1, 1.1, this.cx, this.cy);
    
        if (this.label) {
            this.label[0].stop();
            this.label[0].attr({ r: 7.5 });
            this.label[1].attr({ "font-weight": 800 });
        }
    }, function () {
        this.sector.animate({ transform: 's1 1 ' + this.cx + ' ' + this.cy }, 500, "bounce");
    
        if (this.label) {
            this.label[0].animate({ r: 5 }, 500, "bounce");
            this.label[1].attr({ "font-weight": 400 });
        }
    });
    

    });