将图表从静态数据转换为数据库数据的图表

时间:2013-12-22 06:45:22

标签: c# asp.net .net function charts

我有这个功能

    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");
    }

这将从静态数据创建图表。

如果我的数据库包含名为Employee的表以及功能NameSatisfunction

问题:如何转换上述功能以返回包含动态数据的图表?我对C#没有任何经验以及它是如何工作的。

1 个答案:

答案 0 :(得分:1)

在行动中我有:

///Get data from db

var articles = _db.Articles
        .OrderByDescending(a => a.Visites)
        .Take(6)
        .ToList();

//create chart note xValue i m passing my articles as data to this
// xFiled witch is set to Title is a property on my article class

var chart = new Chart(520, 340, theme: ChartTheme.Blue)
            .AddTitle("Most visited articles")
            .AddSeries(name: "Default",
                xValue: articles, xField: "Title", chartType: "StackedColumn",
                yValues: articles, yFields: "Visites")
            .GetBytes("png");

        return File(chart, "image/png");

您也可以查看Displaying data in charts on asp.net