简体   繁体   English

ASP MVC5-使用Url.Action函数的向下钻取图表

[英]ASP MVC5 - Drilldown chart using Url.Action function

I am working in ASP MVC5 to create a chart with drill down capabilities. 我正在ASP MVC5中创建具有向下钻取功能的图表。 From this page , I was able to get the following code: 该页面 ,我可以得到以下代码:

Here is the code to create the image map and cache the chart: 这是创建图像地图并缓存图表的代码:

public ActionResult ChartMap(int? id, string name)
{
    Chart chart = new Chart();
    // Get data for id and add chart area, series, points, etc. to chart
    // Make sure to use Url.Action for any drill-down URLs
    MemoryStream ms = new MemoryStream();
    chart.SaveImage(ms, ChartImageFormat.Png);
    Session["Chart"] = ms.ToArray();
    return Content(chart.GetHtmlImageMap(name));
}

Then the code to render the cached chart becomes as follows: 然后,呈现缓存图表的代码如下:

public ActionResult Chart(int? id)
{
    byte[] data = Session["Chart"] as byte[];
    return File(data, "img/png");
}

Then this is the code for your view: 这是您的视图的代码:

<img src="<%= Url.Action("Chart", new { id = Model.Id }) %>" usemap="#MyMap" />
<% Html.RenderAction("ChartMap", new { id = Model.Id, name = "MyMap" }); %>

This works fine for displaying the chart itself once I add in my data. 一旦添加数据,该方法就可以很好地显示图表本身。 However, I am having a hard time figuring out how to use Url.Action as it says to create the drilldown functionality. 但是,我很难确定如何使用Url.Action来创建向下钻取功能。 For example, suppose underneath the comment block in the above code I add data to the chart to be displayed as a bar graph as well as chart formatting as follows: 例如,假设在上面的代码中的注释块下面,我将数据添加到要显示为条形图的图表以及如下所示的图表格式:

        chart.Width = 500;
        chart.Height = 500;
        chart.Titles.Add("Test Title");
        chart.ChartAreas.Add(new ChartArea());

        Series series = new Series();
        series.Points.AddXY("ValueA", 3);
        series.Points.AddXY("ValueB", 7);
        series.Points.AddXY("ValueC", 5);
        series.Points.AddXY("ValueD", 4);
        series.Points.AddXY("ValueE", 2);

Suppose when a bar on the graph is clicked, it should take the user to /Home/MyTestPage?value=x, where x is the name given for that column (eg ValueA). 假设单击图形上的条形图,它将把用户带到/ Home / MyTestPage?value = x,其中x是为该列指定的名称(例如ValueA)。 While this code properly displays as a graph, I cannot figure out where or how to had calls to Url.Action related to this so that clicking the chart routes to the proper page. 尽管此代码正确显示为图形,但我无法弄清楚与此相关的Url.Action的调用位置或调用方式,因此单击图表将转到正确的页面。 If anyone has any suggestions it would be greatly appreciated. 如果有人有任何建议,将不胜感激。 Thanks. 谢谢。

For future references for others, I found some changes to the above code here that solve the problem. 为了将来为其他人提供参考,我在这里发现了上述代码的一些更改,以解决该问题。 The major changes are: 主要变化是:

1) When setting values for chart , add the line chart.IsMapEnabled = true; 1)在为chart设置值时,添加折线chart chart.IsMapEnabled = true;

2) Create series points as follows: 2)创建序列点,如下所示:

series.Points.Add(new DataPoint {
AxisLabel = "A",
YValues = new double[] {1},
Url = "MyTestPage?value=A");

3) Replace the return statement in ChartMap with the following: 3)用以下内容替换ChartMap中的return语句:

return Content("<img src='" + Url.Action("Chart").ToString() + "' usemap='#MyMap'/>" + chart.GetHtmlImageMap("MyMap"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM