2014-09-29 30 views
0

我有我的控制器創建圖表:顯示圖表

var myChart = new Chart(width: 1000, height: 600) 
    .AddTitle("Employee's Efficiency") 
    .AddSeries(
     name: "Employee", 
     xValue: new[] { "Peter", "Andrew", "Julie", "Mary", "Dave" }, 
     yValues: new[] { "2", "6", "4", "5", "3" }) 
    .Write(); 
myChart.Save("~/Content/chart" + pid, "jpeg"); 
return base.File("~/Content/chart" + pid, "jpeg"); 

和我的Razor視圖顯示它:

<body> 
<div class="jumbotron"> 
    <h1>Contas e Despesas <span class="glyphicon glyphicon-euro"></span></h1> 
    <p class="lead">Onde gastei o meu dinheiro? Controle melhor as suas despesas com o Contas e Despesas. </p> 
</div> 
<div> 
    <img src="@Url.Action("EfficiencyChart", "Home", new { pid = @Model.Id })" /> 
</div> 

我的問題是,圖像覆蓋整個page ..我無法顯示我的jumbotron或除圖表圖像以外的任何其他內容:/

回答

0

您可以將m如下圖所示: -

@{ 
var myChart = new Chart(width: 1000, height: 600) 
     .AddTitle("Employee's Efficiency") 
     .AddSeries(
      name: "Employee", 
      xValue: new[] { "Peter", "Andrew", "Julie", "Mary", "Dave" }, 
      yValues: new[] { "2", "6", "4", "5", "3" }) 
     .Write(); 
     myChart.Save("~/Content/chart" + @Model.Id, "jpeg"); 
} 

<div class="jumbotron"> 
    <h1>Contas e Despesas <span class="glyphicon glyphicon-euro"></span></h1> 
    <p class="lead">Onde gastei o meu dinheiro? Controle melhor as suas despesas com o Contas e Despesas. </p> 
</div> 
<div> 
    <img src="@myChart" /> 
</div> 

不要忘記在視圖中添加相關的命名空間。

OR

而不是

<img src="@Url.Action("EfficiencyChart", "Home", new { pid = @Model.Id })" /> 

嘗試

@{ Html.RenderAction("EfficiencyChart", "Home", new { pid = @Model.Id }); } 

控制器(主頁): -

public ActionResult EfficiencyChart(int pid) 
{ 
    var myChart = new Chart(width: 1000, height: 600) 
     .AddTitle("Employee's Efficiency") 
     .AddSeries(
      name: "Employee", 
      xValue: new[] { "Peter", "Andrew", "Julie", "Mary", "Dave" }, 
      yValues: new[] { "2", "6", "4", "5", "3" }) 
     .Write(); 
     myChart.Save("~/Content/chart" + pid, "jpeg"); 
    return File("~/Content/chart" + pid, "image/jpeg");// <----change here 
} 

控制器(主頁): -

public ActionResult EfficiencyChart(int pid) 
{ 
    var myChart = new Chart(width: 1000, height: 600) 
     .AddTitle("Employee's Efficiency") 
     .AddSeries(
      name: "Employee", 
      xValue: new[] { "Peter", "Andrew", "Julie", "Mary", "Dave" }, 
      yValues: new[] { "2", "6", "4", "5", "3" }) 
     .GetBytes("jpeg"); 

    return File(myChart , "image/jpeg"); // <----change here 
} 

查看: -

<img src="@Url.Action("EfficiencyChart", "Home", new { pid = @Model.Id })" /> 
+0

不錯,但我需要做我的控制器使用從我的DB值這裏面的邏輯..如何我可以在控制器內部做到嗎? – tiagocarvalho92 2014-09-29 15:32:41

+0

@ user2002274 ....查看更新的答案... – 2014-09-29 15:49:40

+0

@ user2002274..and嘗試更改較小的值更改圖表的高度和寬度.. – 2014-09-29 17:58:30