以数据为参数的 MVC3 图表助手

发布于 2024-12-05 23:51:58 字数 1137 浏览 1 评论 0原文

我正在处理 mvc 框架的图表助手,我想知道是否有一种方法可以将动态数据添加到图表中。 所有教程(例如 http://www.asp. net/webmatrix/tutorials/7-displaying-data-in-a-chart)使用硬编码数据。

如果我想传递数据(例如股票列表)并在图表中显示数据该怎么办?

我将图表包含在内:

 <img src="@Url.Action("GenerateChart", new {data = stocks})" alt="stocks" />

操作方法的可能代码:

 public FileContentResult GenerateChartImage(IList<StockProgressionModel> data)
    {
        Chart chart = new Chart(800, 600);

        foreach (var stock in data {

            int[] xArray = Enumerable.Range(0, stock.Entries.Count).ToArray();
            var yArray = stock.Entries.Select(e => e.Value).ToArray();

            chart.AddSeries(ap.FieldOfActivityName, chartType: "FastLine",
                            xValue: xArray,
                            yValues: yArray);
            chart.SetXAxis("Month", 0);
            chart.SetYAxis("Value", 0);
        }

        return File(chart.GetBytes(), "image/png");
    }

这意味着 url 的链接始终相同,并且我无法将数据作为参数传递。 有更好的方法吗?

I'm dealing with the chart helper of mvc framework and I wonder if there is a way to add dynamic data into the chart.
all the tutorials (e.g. http://www.asp.net/webmatrix/tutorials/7-displaying-data-in-a-chart) use hard coded data.

What if I want to pass the data, for example a list of stocks and display the data in the charts.

I'm including the chart with:

 <img src="@Url.Action("GenerateChart", new {data = stocks})" alt="stocks" />

The possible code of the action methode:

 public FileContentResult GenerateChartImage(IList<StockProgressionModel> data)
    {
        Chart chart = new Chart(800, 600);

        foreach (var stock in data {

            int[] xArray = Enumerable.Range(0, stock.Entries.Count).ToArray();
            var yArray = stock.Entries.Select(e => e.Value).ToArray();

            chart.AddSeries(ap.FieldOfActivityName, chartType: "FastLine",
                            xValue: xArray,
                            yValues: yArray);
            chart.SetXAxis("Month", 0);
            chart.SetYAxis("Value", 0);
        }

        return File(chart.GetBytes(), "image/png");
    }

This means that the link of the url is always the same and I cannot pass the data as parameter.
Is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜吻♂芭芘 2024-12-12 23:51:58

我找到了一个解决方案:

视图:

@{
    string key = Guid.NewGuid().ToString();
    @Html.Action("GenerateChart", new { @cacheKey = key, data = stocks })
    string url = Url.Action("ShowChart", new { @cacheKey = key });
    <img src="@url" alt="Chart" />
}

控制器:

public void GenerateChartImage(IList<StockProgressionModel> data)
{
    Chart chart = new Chart(800, 600);

    foreach (var stock in data {

        int[] xArray = Enumerable.Range(0, stock.Entries.Count).ToArray();
        var yArray = stock.Entries.Select(e => e.Value).ToArray();

        chart.AddSeries(ap.FieldOfActivityName, chartType: "FastLine",
                        xValue: xArray,
                        yValues: yArray);
        chart.SetXAxis("Month", 0);
        chart.SetYAxis("Value", 0);
    }
    chart.SaveToCache(cacheKey, 1);
}

public void ShowChart(string cacheKey)
{
    Chart chart = Chart.GetFromCache(cacheKey);
    chart.Write("jpeg");
}

如果有人有更好的解决方案,我会感兴趣。

I found a solution:

View:

@{
    string key = Guid.NewGuid().ToString();
    @Html.Action("GenerateChart", new { @cacheKey = key, data = stocks })
    string url = Url.Action("ShowChart", new { @cacheKey = key });
    <img src="@url" alt="Chart" />
}

Controller:

public void GenerateChartImage(IList<StockProgressionModel> data)
{
    Chart chart = new Chart(800, 600);

    foreach (var stock in data {

        int[] xArray = Enumerable.Range(0, stock.Entries.Count).ToArray();
        var yArray = stock.Entries.Select(e => e.Value).ToArray();

        chart.AddSeries(ap.FieldOfActivityName, chartType: "FastLine",
                        xValue: xArray,
                        yValues: yArray);
        chart.SetXAxis("Month", 0);
        chart.SetYAxis("Value", 0);
    }
    chart.SaveToCache(cacheKey, 1);
}

public void ShowChart(string cacheKey)
{
    Chart chart = Chart.GetFromCache(cacheKey);
    chart.Write("jpeg");
}

If someone had a better solution, i would be interested.

倥絔 2024-12-12 23:51:58

您可以查询控制器中的数据并将其传递给视图

    public ActionResult Index()
    {
        string cs = System.Configuration.ConfigurationManager.ConnectionStrings["DevelopmentConnectionString"].ConnectionString;
        System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(cs);
        var currentYear = DateTime.Now.Year;
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand($"SELECT top 5 product, itemUnitPrice FROM [Development].[dbo].[Orderslist] WHERE timeStamp LIKE '%{currentYear}%' GROUP BY itemUnitPrice, product order by COUNT(product) desc", con);
            con.Open();
            System.Data.SqlClient.SqlDataReader rdr = cmd.ExecuteReader();
            ViewBag.data = rdr;


        return View();
    }

然后您可以使用

.DataBindTable(ViewBag.data, xField: "product")
.Write()

You can query the data in your controller and pass it to view

    public ActionResult Index()
    {
        string cs = System.Configuration.ConfigurationManager.ConnectionStrings["DevelopmentConnectionString"].ConnectionString;
        System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(cs);
        var currentYear = DateTime.Now.Year;
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand($"SELECT top 5 product, itemUnitPrice FROM [Development].[dbo].[Orderslist] WHERE timeStamp LIKE '%{currentYear}%' GROUP BY itemUnitPrice, product order by COUNT(product) desc", con);
            con.Open();
            System.Data.SqlClient.SqlDataReader rdr = cmd.ExecuteReader();
            ViewBag.data = rdr;


        return View();
    }

And then you can use

.DataBindTable(ViewBag.data, xField: "product")
.Write()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文